<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Imsfiscal_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('invoice_model');
        $this->load->model('business_model');
        $this->load->model('imsfiscal_model');

        if ($this->session->userdata('role') == '') {
            redirect('logout');
        }
    }

    
    public function imsfiscal() {
        exit();
        $url = "http://localhost:8524/api/IMSFiscal/GetInvoiceNumberByModel";
        $invoice = new stdClass();
        $invoice->InvoiceNumber = "";
        $invoice->POSID = 943046;
        $invoice->USIN = "B14C9276";
        $invoice->DateTime = "2020-01-01 12:00:00";
        $invoice->BuyerPNTN = "1234567-8";
        $invoice->BuyerCNIC = "12345-1234567-8";
        $invoice->BuyerName = "Depilex";
        $invoice->BuyerPhoneNumber = "0000-0000000";
        $invoice->TotalBillAmount = 0.0;
        $invoice->TotalQuantity = 0.0;
        $invoice->TotalSaleValue = 0.0;
        $invoice->TotalTaxCharged = 0.0;
        $invoice->Discount = 0.0;
        $invoice->FurtherTax = 0.0;
        $invoice->PaymentMode = 1;
        $invoice->RefUSIN = null;
        $invoice->InvoiceType = 1;

        $item1 = new stdClass();
        $item1->ItemCode = "IT_1011";
        $item1->ItemName = "Test Item";
        $item1->Quantity = 1.0;
        $item1->PCTCode = "00000000";
        $item1->TaxRate = 0.0;
        $item1->SaleValue = 0.0;
        $item1->TotalAmount = 0.0;
        $item1->TaxCharged = 0.0;
        $item1->Discount = 0.0;
        $item1->FurtherTax = 0.0;
        $item1->InvoiceType = 1;
        $item1->RefUSIN = null;

        $item2 = new stdClass();
        $item2->ItemCode = "IT_1011";
        $item2->ItemName = "Test Item";
        $item2->Quantity = 1.0;
        $item2->PCTCode = "00000000";
        $item2->TaxRate = 0.0;
        $item2->SaleValue = 0.0;
        $item2->TotalAmount = 0.0;
        $item2->TaxCharged = 0.0;
        $item2->Discount = 0.0;
        $item2->FurtherTax = 0.0;
        $item2->InvoiceType = 1;
        $item2->RefUSIN = null;

        $item3 = new stdClass();
        $item3->ItemCode = "IT_1011";
        $item3->ItemName = "Test Item";
        $item3->Quantity = 1.0;
        $item3->PCTCode = "00000000";
        $item3->TaxRate = 0.0;
        $item3->SaleValue = 0.0;
        $item3->TotalAmount = 0.0;
        $item3->TaxCharged = 0.0;
        $item3->Discount = 0.0;
        $item3->FurtherTax = 0.0;
        $item3->InvoiceType = 1;
        $item3->RefUSIN = null;

        $invoice->Items = [$item1, $item2, $item3];

        $myJSON = json_encode($invoice);

        //echo $myJSON;
        //exit;
        //php api rest

        $curl = curl_init();
        //curl_setopt($curl, CURLOPT_POST, 1);

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $myJSON);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($curl);

        curl_close($curl);

        print_r($result);
        exit;
    }
    
    function getimsdetails(){
        $type=$this->input->post('type');
        
        $imsfiscal = $this->imsfiscal_model->get_imsfiscal_details($type);
        echo json_encode($imsfiscal);
    }
    
    function getimsjson(){
        $invoiceid = $this->input->post('invoiceid');
        $invoicetype = $this->input->post('invoicetype');
        $business=$this->business_model->getbusinessdetails();
        $mJson='{}';
        if(isset($business[0]['imsfiscal'])){
            $this->load->model('imsfiscal_model');
            if($business[0]['imsfiscal']=='Yes'){
                $ims_details = $this->imsfiscal_model->get_imsfiscal_details('service');
                if($ims_details->posid !== null && $ims_details->posid !== '' && $ims_details->ims_url!==null && $ims_details->ims_url!==''){
                    $mJson = $this->imsfiscal_model->imsfiscal_invoice_json($ims_details->posid, $invoiceid, $ims_details->ims_url, $ims_details->ims_token, $invoicetype, $business[0]['tax_mode']);
                    
                }
            }        
        }        
        $data = array('mJson'=>$mJson , 'imsdetails'=>$ims_details);
        
        echo json_encode($data);
    }
    
    function getimsjson_r(){
        $invoiceid = $this->input->post('invoiceid');
        $invoicetype = $this->input->post('invoicetype');
        $business=$this->business_model->getbusinessdetails();
        $mJson='{}';
        if(isset($business[0]['imsfiscal'])){
            $this->load->model('imsfiscal_model');
            if($business[0]['imsfiscal']=='Yes'){
                $ims_details = $this->imsfiscal_model->get_imsfiscal_details('sale');
                if($ims_details->posid !== null && $ims_details->posid !== '' && $ims_details->ims_url!==null && $ims_details->ims_url!==''){
                    $mJson = $this->imsfiscal_model->imsfiscal_invoice_r_json($ims_details->posid, $invoiceid, $ims_details->ims_url, $ims_details->ims_token, $invoicetype, $business[0]['tax_mode']);
                    
                }
            }        
        }        
        
       $data = array('mJson'=>$mJson , 'imsdetails'=>$ims_details);
        
        echo json_encode($data);
    }
    
    
    function imsfiscal_update_log(){
        $result = $this->imsfiscal_model->imsfiscal_update_log();       
        echo $result;
    }

    function srbfiscal_update_log(){
        $result = $this->imsfiscal_model->srbfiscal_update_log();
       
        echo $result;
    }
    
    
    function ims_unposted_invoices(){
        
       if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            checkroles(1,0,0);

            $data['nav'] = 'procurement';
            $data['subnav'] = 'return_notes';
            
            $startdate= $this->input->post('startdate');
            $enddate= $this->input->post('enddate');

            if($startdate==null){$startdate=date('Y-m-d 00:00');}
            if($enddate==null){$enddate=date('Y-m-d 23:59');}
            
            $invoice_type='service';
            if(null != $this->input->post('invoice_type')){
                $invoice_type=$this->input->post('invoice_type');
            }
                        
            $data['business'] = $this->business_model->getbusinessdetails();
            $data['invoices'] = $this->imsfiscal_model->get_ims_unposted_invoices($startdate, $enddate, $invoice_type);
            
            $data['startdate']=$startdate; 
            $data['enddate']=$enddate; 
            $data['invoice_type']=$invoice_type; 
            
            $this->load->view('includes/header', $data);
            $this->load->view('setting/ims_unposted_invoices');
            $this->load->view('includes/footer');
        }
        
    }
    
    function get_ims_unposted_invoices(){
        
        $this->load->model('imsfiscal_model');
        $data['invoices'] = $this->ims_model->get_ims_unposted_invoices();

        echo json_encode($data);
    }


    function getsrbjson(){
        $invoiceid = $this->input->post('invoiceid');
        $invoicetype = $this->input->post('invoicetype');
        $business=$this->business_model->getbusinessdetails();
        $mJson='{}';
        if(isset($business[0]['srb_fiscal_service'])){
            $this->load->model('imsfiscal_model');
            if($business[0]['srb_fiscal_service']=='Yes'){
                $ims_details = $this->imsfiscal_model->get_imsfiscal_details('service');
                if($ims_details->posid !== null && $ims_details->posid !== '' && $ims_details->ims_url!==null && $ims_details->ims_url!==''){
                    $mJson = $this->imsfiscal_model->srb_invoice_json($ims_details->posid, $ims_details->pos_user, $ims_details->pos_pass, $invoiceid, $ims_details->ims_url, $ims_details->ntn, $invoicetype, $business[0]['tax_mode']);
                 //$posid, $pos_user, $pos_pass, $invoiceid, $url, $ntn, $invoicetype, $taxmode   
                }
            }        
        }        
        $data = array('mJson'=>$mJson , 'imsdetails'=>$ims_details);
        
        echo json_encode($data);
    }
    
}
