<?php

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

class Franchise_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('franchise_model');
        $this->load->model('business_model');
       $this->load->model('accounting_model');
       $this->load->model('product_model');
    }
    
    public function franchises(){
        
        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'] = 'business';
            $data['subnav'] = 'accounting';

            $data['business'] = $this->business_model->get_business_details();
    
            $data['franchises'] = $this->franchise_model->get_all_franchises();


            $this->load->view('includes/header', $data);
            $this->load->view('setting/franchise_list_view');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function addnewfranchise(){
        
        
        $result = $this->franchise_model->add_new_franchise();
        echo "success|" . $result;

        
    }
    
    public function edit_franchise(){
        $franchise_id= $this->input->post('id_franchises');
        
        
        $data = $this->franchise_model->get_franchise($franchise_id);
        echo (json_encode($data));
        
        
    }
    
    public function update_franchise(){
        
        $result = $this->franchise_model->update_franchise($this->input->post('id_franchises'));
        echo('success|' . $result);
        
    }
    
    public function searchnameforfranchise(){
        
        $franchisename = $this->input->get("franchisename", TRUE);
        $data = $this->franchise_model->searchnameforfranchise($franchisename);

        echo(json_encode($data));
        
        
    }
    
    
    public function franchise_orders(){
        
        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);

            $franchiseid = $this->input->post('selected_franchise');
            $status = $this->input->post('order_status');
            $dtype=$this->input->post('dtype');
            
            if(null==$franchiseid){$franchiseid='all';}
            
            $data['nav'] = 'business';
            $data['subnav'] = 'accounting';

            $startdate=date('Y-m-01');
            $enddate=date("Y-m-d");
            
            if(null !== $this->input->post('startdate')){
                $startdate = $this->input->post('startdate');
            }
            
            if(null !== $this->input->post('enddate')){
                $enddate = $this->input->post('enddate');
            }
            
            $data['orders'] = $this->franchise_model->get_all_franchise_orders($franchiseid, $startdate, $enddate, $status, $dtype);
            
            $data['business'] = $this->business_model->get_business_details();
    
            $data['franchises'] = $this->franchise_model->get_all_franchises();
            
            
            
            $x =0;
            if(isset($data['orders'])){
                $account_head = $this->accounting_model->get_event_account(37, 'total_receivable');
                foreach($data['orders'] as $order){
                    $sql = "select sum(debit)-sum(credit) as balance from account_voucher_detail 
                    join account_vouchers on account_vouchers.id_account_vouchers=account_voucher_detail.account_voucher_id
                    where account_vouchers.voucher_status='Active' and account_vouchers.franchise_order_id = ".$order['id_franchise_orders']." and account_voucher_detail.account_head_id =".$account_head->account_head_id;                    
                    $query = $this->db->query($sql);
                    $receivings = $query->row();
                    
                    $balance=0;
                    $balance = $receivings->balance;
                    array_push($data['orders'][$x], ['balance'=>$balance]);
                    $x++;

                }
            }
//           /print_r($data['orders']); exit();
            $data['selected_franchise'] = $this->input->post('selected_franchise');
            $data['selected_status'] = $this->input->post('order_status');
            $data['selected_dtype'] = $this->input->post('dtype');
            $data['startdate'] = $startdate;
            $data['enddate'] = $enddate;
            

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/franchise_orders');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function new_order(){
        
        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'] = 'business';
            $data['subnav'] = 'accounting';

            
            $franchiseid = $this->input->post('selected_franchise');
            $orderid=$this->input->post('order_id');
            
            if(null==$franchiseid){$franchiseid='all';}
            if(null==$orderid){$orderid=0;}
            
            $data['business'] = $this->business_model->get_business_details();
    
            $data['franchises'] = $this->franchise_model->get_all_franchises();
            
            //echo $franchiseid;
            $data['franchise'] = $this->franchise_model->get_franchise($franchiseid);
            
            $data['selected_franchise'] = $franchiseid;
            
            $data['months'] = $this->franchise_model->get_months();
                        
            $data['order']=[];
            $data['orderid']=$orderid;
           
            if($orderid>0){
                $data['order'] = $this->franchise_model->get_franchise_order($orderid);
            }
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/franchise_order');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function add_order(){
               
        $result = $this->franchise_model->add_new_order();
        echo "success|" . $result;
    }
    
    public function update_order(){
        
        $franchiseorderid=$this->input->post('orderid');
        $result = $this->franchise_model->update_order($franchiseorderid);
        echo $result;
    }
    
    public function cancel_order(){
        
        $franchiseorderid=$this->input->post('order_id');
       
        $result = $this->franchise_model->cancel_order($franchiseorderid);
        
        redirect('franchise_orders');
        
    }
    
    public function new_order_invoice(){
        
        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'] = 'business';
            $data['subnav'] = 'accounting';

            
            $franchiseid = $this->input->post('selected_franchise');
            $orderid=$this->input->post('order_id');
            
            $data['business'] = $this->business_model->get_business_details();
            $data['franchise'] = $this->franchise_model->get_franchise($franchiseid);
            $data['franchiseinvoicenumber']= $this->franchise_model->get_customer_invoice_number($franchiseid);
                    
            $data['arrears'] = $this->franchise_model->get_receivable_amount($franchiseid, $orderid);
            
            $data['order']=[];
            $data['orderid']=$orderid;
           
            if($orderid>0){
                $data['order'] = $this->franchise_model->get_franchise_order($orderid);
            }
            
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/franchise_invoice_view');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function updateorderinvoice(){
        $franchiseorderid=$this->input->post('orderid');
        $result = $this->franchise_model->update_invoice($franchiseorderid);
        echo $result;
        
    }
    
    
    public function cancelorderinvoice(){
        
        $franchiseorderid=$this->input->post('order_id');
        
        $franchise_status = $this->franchise_model->checkorderstatus($franchiseorderid);
        
        if($franchise_status=='Invoiced'){        
            $result = $this->franchise_model->cancel_invoice($franchiseorderid);            
            
        } else {
            redirect('franchise_orders');
        }  
        
                
        $data['nav'] = 'business';
        $data['subnav'] = 'accounting';

        $franchiseid = $this->input->post('selected_franchise');
        $orderid=$this->input->post('order_id');

        $data['business'] = $this->business_model->get_business_details();
        $data['franchise'] = $this->franchise_model->get_franchise($franchiseid);
        $data['franchiseinvoicenumber']= $this->franchise_model->get_customer_invoice_number($franchiseid);
        
        $data['order']=[];
        $data['orderid']=$orderid;

        if($orderid>0){
            $data['order'] = $this->franchise_model->get_franchise_order($orderid);
        }

        $this->load->view('includes/header', $data);
        $this->load->view('accounting/franchise/franchise_invoice_view');
        $this->load->view('includes/footer');
           
            
              
        
    }
    
    
    public function receiving_note(){
        
         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'] = 'business';
            $data['subnav'] = 'accounting';

            
            $data['bank_accounts'] = $this->accounting_model->get_account_heads(3,'all',$this->session->userdata('businessid'));
            $data['cash_accounts'] = $this->accounting_model->get_account_heads(2,'all',$this->session->userdata('businessid'));
            $data['card_accounts'] = $this->accounting_model->get_account_heads(8,'all',$this->session->userdata('businessid'));
            
            
            $franchiseid = $this->input->post('selected_franchise');
            $orderid=$this->input->post('order_id');
            
            $data['business'] = $this->business_model->get_business_details();
            $data['franchise'] = $this->franchise_model->get_franchise($franchiseid);
            
            $data['order']=[];
            $data['orderid']=$orderid;
           
            if($orderid>0){
                $data['order'] = $this->franchise_model->get_franchise_order($orderid);
            }
            $data['cost_center_types'] = $this->accounting_model->get_cost_center_types();
            $data['receiving'] = $this->accounting_model->get_sale_order_receivings($orderid);
            
            // $data['receivings']=0;
            // $debit=0; $credit=0;
            // foreach($data['receiving'] as $receiving){
            //     $debit=$debit+$receiving['debit'];
            //     $credit=$credit+$receiving['credit'];
            // }
            // $data['receivings']=$debit-$credit;
            
            $account_head = $this->accounting_model->get_event_account(37, 'total_receivable');

            $sql = "select sum(debit)-sum(credit) as balance from account_voucher_detail 
            join account_vouchers on account_vouchers.id_account_vouchers=account_voucher_detail.account_voucher_id
            where account_vouchers.voucher_status='Active' and account_vouchers.franchise_order_id = ".$orderid." and account_voucher_detail.account_head_id =".$account_head->account_head_id;
            
            // echo $sql; exit();
            $query = $this->db->query($sql);
            $balance = $query->row();
        
            
            $data['receivings'] =$balance->balance;
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/franchise_payment_view');
            $this->load->view('includes/footer');
        }
        
    }
    
     public function updatereceiving_note(){
        
        $franchiseorderid=$this->input->post('orderid');
        $result = $this->franchise_model->update_receiving_note($franchiseorderid);
        
        $r=explode('|',$result);
                
        
        if($r[0]=='success'){
            redirect('franchise_orders');
        }else{
            echo $r[1];
        }
        
        
    }
    
    public function print_receiving($orderid=null){
    
        
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            checkroles(1, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
            
              $data['menu'] = 'hidden';
              
            if(null==$orderid){$orderid=$this->input->post('order_id');}  
              
            $data['business'] = $this->business_model->get_business_details();
            
            $data['voucher']=$this->accounting_model->get_sale_order_receivings($orderid);
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/franchise_print_voucher');
            $this->load->view('includes/footer');
        }        
        
    }
    
    public function franchise_aging_report(){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            checkroles(1, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
           
            $data['orders']=$this->franchise_model->franchise_aging_report();
            $data['business'] = $this->business_model->get_business_details();
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/franchise_aging_report');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function franchise_product_return(){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            checkroles(1, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
           
            $franchiseid = $this->input->post('selected_franchise');
            $orderid=$this->input->post('order_id');
            
            $data['business'] = $this->business_model->get_business_details();
            $data['franchise'] = $this->franchise_model->get_franchise($franchiseid);
            $data['franchiseinvoicenumber']= $this->franchise_model->get_customer_invoice_number($franchiseid);
                    
            $data['order']=[];
            $data['orderid']=$orderid;
           
            if($orderid>0){
                $data['order'] = $this->franchise_model->get_franchise_order($orderid);
            }
            
            $data['received_amount'] = $this->franchise_model->get_paid_amount($orderid);
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/franchise_product_return');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function product_wise_sale(){
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            checkroles(1, 0, 0);

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

            if(null == $startdate){
                $startdate=date('Y-m-d');
                $enddate=date('Y-m-d');
            }

            $sql = "SELECT * FROM franchise_orders
                    join franchise_order_products 
                    on franchise_orders.id_franchise_orders = franchise_order_products.franchise_order_id
                    where franchise_orders.order_status !='Cancelled'
                   
                    and franchise_orders.business_id = ". $this->session->userdata('businessid')." 
                    and date(franchise_orders.invoice_date) >= '".$startdate."'
                    and date(franchise_orders.invoice_date) <= '".$enddate."'";
            $query = $this->db->query($sql);

            $data['orders']=$query->result_array();
            $data['startdate']=$startdate;
            $data['enddate']=$enddate;
             
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/franchise/product_sale_report');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function get_product_wise_invoiced(){
        
        $startdate= $this->input->post('start');
        $enddate= $this->input->post('end');
        $products= $this->input->post('products');
        
        
        
    }
    
}