<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class ReceiptsAPI extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
       
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }
    
    public function receiptsjson_service(){

        $sql="select tax_mode from business where id_business = ". $this->session->userdata('businessid');
        $query=$this->db->query($sql);
        $tx = $query->row();
        $taxmode =$tx->tax_mode;

        $invoiceid = $this->input->post('invoiceid');

        $this->db->select("invoice.*, invoice_details.*, invoice.payment_mode as mode, business.id_business, business.business_name, business.business_address, SUBSTRING(business_address, position('BR>' IN business_address)+3, length(business_address)) as business_taxcode, business.business_phone1, customers.customer_email, case when instrument_number like 'Online%' then 'Online' ELSE 'Check' END as payment_mode, invoice.discount as invoice_discount, invoice_details.invoice_discount as invoice_discount_item, round(100*taxes/discounted_price ,2) as beforerate ", false);
        $this->db->join('customers','customers.id_customers=invoice.customer_id');
        $this->db->join('business','invoice.business_id=business.id_business');
        $this->db->join('invoice_details','invoice_details.invoice_id = invoice.id_invoice');
        $this->db->where('id_invoice', $invoiceid);
        $this->db->group_by('id_invoice_details');
        $query = $this->db->get('invoice');
        
        $invoice_data = $query->result_array();

        if($query->num_rows() > 0){
           
            $invoice = new stdClass();
            $invoice->invoice_number = $invoice_data[0]['invoice_number'];
            $invoice->date = $invoice_data[0]['invoice_date'];
            $invoice->business_id = "2";
            $invoice->branch_id = "2"; //$invoice_data[0]['business_id'];
            
            $invoice->taxation_code = $invoice_data[0]['business_taxcode'];
            //$invoice->ims_code = $invoice_data[0]['imsfiscal_invoice_number'];
            $invoice->ims_code ="804236DF6N39554356*test*";
            
            $invoice->customer_name = $invoice_data[0]['customer_name'];
            $invoice->customer_cell = $invoice_data[0]['customer_cell'];
            $invoice->customer_cnic= '';
            $invoice->customer_email = $invoice_data[0]['customer_email'];

            $invoice->invoice_status = "Active";
            $invoice->payment_mode = $invoice_data[0]['mode'];
            $invoice->instrument_number = $invoice_data[0]['instrument_number'];
            $invoice->cashier = $invoice_data[0]['created_by'];

            $invoice->subtotal = $invoice_data[0]['sub_total'];
            $invoice->other_charges = $invoice_data[0]['other_charges'];

            $invoice->discount = 0;// $invoice_data[0]['invoice_discount'];

            if($taxmode=='after'){
                $invoice->total_before_tax = $invoice_data[0]['gross_wo_tax'];
            }elseif($taxmode=='before'){
                $invoice->total_before_tax = $invoice_data[0]['sub_total'];
            }
            $invoice->total_tax = $invoice_data[0]['tax_total'];           
                        
            $invoice->total_after_tax = $invoice_data[0]['total_payable'];
            
            $invoice->paid_amount = $invoice_data[0]['paid_amount'];
            $invoice->remaining_amount = $invoice_data[0]['balance'];
            $myItems = new stdClass();
            $x=1;
            foreach($invoice_data as $d){
                $detail = new stdClass();

                $detail->deal_name = $d['service_flag'] == 'packagetype' ?  $d['s_type'] :  '';
                $detail->product_name = $d['service_name'];
                $detail->barcode = $d['service_id'];
                $detail->unit_price =  round($d['price'] + $d['service_addition'],2);
                $detail->qty =  1.0;
                $detail->discount_percentage =  ceil((($d['discount']+$d['invoice_discount_item'])*100)/($d['price'] + $d['service_addition']));                
                $detail->discount_amount = round($d['invoice_discount_item'] + $d['discount'],2);                

                $detail->tax_percentage = ceil(($d['taxes']*100)/($d['final_price']));
                $detail->tax_amount =  round($d['taxes'],2);

                $detail->total_price =  round($d['final_price'],2);
                
                $invoice->detail[]= $detail;            
                $x++;
            }
        }       

        //print_r($invoice); exit();
        $myJSON = json_encode($invoice);
        
        echo $myJSON;
        
    }


    public function receiptsjson_sale(){

        $sql="select tax_mode from business where id_business = ". $this->session->userdata('businessid');
        $query=$this->db->query($sql);
        $tx = $query->row();
        $taxmode =$tx->tax_mode;

        $invoiceid = $this->input->post('invoiceid');

        $this->db->select("invoice.*, invoice_details.*, business.id_business, business.business_name, business.business_address, SUBSTRING(business_address, position('BR>' IN business_address)+3, length(business_address)) as business_taxcode, business.business_phone1, customers.customer_email, case when instrument_number like 'Online%' then 'Online' ELSE 'Check' END as payment_mode, invoice.discount as invoice_discount, invoice_details.invoice_discount as invoice_discount_item, round(100*taxes/discounted_price ,2) as beforerate ", false);
        $this->db->join('customers','customers.id_customers=invoice.customer_id');
        $this->db->join('business','invoice.business_id=business.id_business');
        $this->db->join('invoice_details','invoice_details.invoice_id = invoice.id_invoice');
        $this->db->where('id_invoice', $invoiceid);
        $query = $this->db->get('invoice');
        
        $invoice_data = $query->result_array();

        if($query->num_rows() > 0){

            $invoice = new stdClass();
            $invoice->invoice_number = "123";
            $invoice->date = $invoice_data[0]['invoice_date'];
            $invoice->business_id = "1";
            $invoice->branch_id = $invoice_data[0]['business_id'];
            
            $invoice->taxation_code = $invoice_data[0]['business_taxcode'];
            $invoice->ims_code = $invoice_data[0]['imsfiscal_invoice_number'];
            
            
            $invoice->customer_name = $invoice_data[0]['customer_name'];
            $invoice->customer_cell = $invoice_data[0]['customer_cell'];
            $invoice->customer_cnic= '';
            $invoice->customer_email = $invoice_data[0]['customer_email'];

            $invoice->invoice_status = "Active";
            $invoice->payment_mode = $invoice_data[0]['payment_mode'];
            $invoice->instrument_number = $invoice_data[0]['instrument_number'];
            $invoice->cashier = $invoice_data[0]['created_by'];

            $invoice->subtotal = $invoice_data[0]['subtotal'];
            $invoice->other_charges = $invoice_data[0]['other_charges'];

            if($taxmode=='after'){
                $invoice->discount = 0.0;
            } elseif($taxmode=='before'){
                $invoice->discount = $invoice_data[0]['invoice_discount'];
            }


            if($taxmode=='after'){
                $invoice->total_before_tax = $invoice_data[0]['gross_wo_tax'];
            }elseif($taxmode=='before'){
                $invoice->total_before_tax = $invoice_data[0]['sub_total'];
            }
            $invoice->total_tax = $invoice_data[0]['tax_total'];           
                        
            $invoice->total_after_tax = $invoice_data[0]['total_payable'];
            
            $invoice->paid_amount = $invoice_data[0]['paid_amount'];
            $invoice->remaining_amount = $invoice_data[0]['balance'];

            $myItems = new stdClass();
            $x=1;
            foreach($invoice_data as $d){
                $detail = new stdClass();
                
                $detail->deal_name = '';
                $detail->product_name = $d['service_name'];
                $detail->barcode = $d['service_id'];
                $detail->unit_price =  $d['price']+$d['service_addition'];
                $detail->qty =  1.0;
                $detail->discount_percentage =  ceil(($d['discount']*100)/($d['price']+$d['service_addition']));                
                $detail->tax_percentage = ceil(($d['taxes']*100)/($d['price']+$d['service_addition']));
                $detail->tax_amount =  round($d['taxes'],2);
                if($taxmode=='after'){
                    $detail->discount_amount = 0.0;
                } elseif($taxmode=='before'){
                    $detail->discount_amount = $d['invoice_discount_item'];                
                }
                $detail->total_price =  $d['final_price']+$d['taxes'];
                
                $invoice->detail[]= $detail;            
                $x++;
            }
        }       

        //print_r($invoice); exit();
        $myJSON = json_encode($invoice);
        
        echo $myJSON;
        
    }
    
}

