<?php

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

class Accounting_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('accounting_model');
        $this->load->model('business_model');
        $this->load->model('supplier_model');
        $this->load->model('staff_model');
        $this->load->model('franchise_model');
        $this->load->model('purchaseorder_model');
        $this->load->model('report_model');
       
    }

    /////Main Accounts/////
    public function mainaccounts(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

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

          //  var_dump($data);exit();
            
            $data['account_main'] = $this->accounting_model->get_account_main();


            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_main');
            $this->load->view('includes/footer');
        }
    }
    
    public function mainaccountadd(){
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_main_add');
            $this->load->view('includes/footer');
            
        }
    }
    
    public function insert_account_main(){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $account_main_id = $this->accounting_model->account_main_insert();
            if($account_main_id && $account_main_id>0){
                $this->session->set_flashdata('Success', 'Main Account successfully added!');
            } else {
                $this->session->set_flashdata('Error', 'Main Account not added!');
            }

            $data['business'] = $this->business_model->get_business_details();
            $data['account_main']=$this->accounting_model->get_account_main($account_main_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_main_edit');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function mainaccountedit(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
            
            $account_main_id= $this->input->post('id_account_main');
            
            $data['account_main']=$this->accounting_model->get_account_main($account_main_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_main_edit');
            $this->load->view('includes/footer');
        }
    }
    
    public function update_account_main(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $result = $this->accounting_model->account_main_update();
            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Record successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Record not updated!');
            }
            $account_main_id=$this->input->post('id_account_main');
           
            $data['business'] = $this->business_model->get_business_details();
            $data['account_main']=$this->accounting_model->get_account_main($account_main_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_main_edit');
            $this->load->view('includes/footer');
        }
    }
    
    public function main_account_delete(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
            
            $account_main_id= $this->input->post('id_account_main');
            
            $data['account_control']=$this->accounting_model->get_account_control($account_main_id);

            
            //echo sizeof($data['account_control']); exit();
            if(sizeof($data['account_control']) >= 1){
                
                $this->session->set_flashdata('Error', 'There are Control Accounts In This Main Account Head!');
            } else {
                
                $this->accounting_model->delete_account_main($account_main_id);
                $this->session->set_flashdata('Success', 'Main Account Deleted!');
            }
            
            redirect('coa');

        }
        
        
    }
    ////Main Account End/////
    
    /////Control Accounts/////
    public function controlaccounts($mainid){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

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

            $data['account_main'] = $this->accounting_model->get_account_main($mainid);
            $data['account_control'] = $this->accounting_model->get_account_control($mainid);


            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_control');
            $this->load->view('includes/footer');
        }
    }
    
    public function controlaccountadd(){
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $data['id_account_main']=$this->input->post('id_account_main');
            $data['account_main']=$this->input->post('account_main');
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_control_add');
            $this->load->view('includes/footer');
        }
    }


    public function insert_account_control(){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            
            $account_main_id=$this->input->post('id_accoun_main');
            
            $account_control_id = $this->accounting_model->account_control_insert($account_main_id);
            if($account_control_id && $account_control_id>0){
                $this->session->set_flashdata('Success', 'Control Account successfully added!');
            } else {
                $this->session->set_flashdata('Error', 'Control Account not added!');
            }

            $data['business'] = $this->business_model->get_business_details();
            $data['account_control']=$this->accounting_model->get_account_control('all',$account_control_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_control_edit');
            $this->load->view('includes/footer');
        }
    }
    
    public function controlaccountedit(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  

        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            
            $account_control_id= $this->input->post('id_account_control');

            $data['id_account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->accounting_model->get_account_control('all',$account_control_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_control_edit');
            $this->load->view('includes/footer');
        }
    }
        
   public function update_account_control(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $result = $this->accounting_model->account_control_update();
            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Record successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Record not updated!');
            }
            $account_control_id=$this->input->post('id_account_control');
           
            $data['business'] = $this->business_model->get_business_details();
            $data['id_account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->accounting_model->get_account_control('all', $account_control_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_control_edit');
            $this->load->view('includes/footer');
        }
    }
    
    public function control_account_delete(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
            
            $account_control_id= $this->input->post('id_account_control');
            
            $data['account_sub_control']=$this->accounting_model->get_account_sub_control($account_control_id);

            
            //echo sizeof($data['account_sub_control']); exit();
            if(sizeof($data['account_sub_control']) >= 1){
                
                $this->session->set_flashdata('Error', 'There are Sub Control Accounts In This Control Account Head!');
            } else {
                
                $this->accounting_model->delete_account_control($account_control_id);
                $this->session->set_flashdata('Success', 'Control Account Deleted!');
            }
            
            redirect('control_accounts/'.$this->input->post('id_account_main'));

        }
    }

    /////Control Accounts End////////
    
    
    
    /////Sub Control Accounts/////
    public function subcontrolaccounts($mainid, $controlid){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

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

            $data['account_main'] = $mainid;
            $data['account_control'] = $this->accounting_model->get_account_control('all', $controlid);
            $data['account_sub_control'] = $this->accounting_model->get_account_sub_control($controlid);


            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_sub_control');
            $this->load->view('includes/footer');
        }
    }
    
    public function subcontrolaccountadd(){
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $data['account_main'] = $this->input->post('id_account_main');;
            $data['id_account_control']=$this->input->post('id_account_control');
            $data['account_control']=$this->input->post('account_control');
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_sub_control_add');
            $this->load->view('includes/footer');
        }
    }


    public function insert_account_sub_control(){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            
            $account_main_id=$this->input->post('id_account_main');
            $account_control_id=$this->input->post('id_account_control');
            
            $account_sub_control_id = $this->accounting_model->account_sub_control_insert($account_control_id);
            if($account_sub_control_id && $account_sub_control_id>0){
                $this->session->set_flashdata('Success', 'Sub Control Account successfully added!');
            } else {
                $this->session->set_flashdata('Error', 'Sub Control Account not added!');
            }

            $data['business'] = $this->business_model->get_business_details();
            
            $data['account_main']=$account_main_id;
            $data['account_control']=$account_control_id;
            $data['account_sub_control']=$this->accounting_model->get_account_sub_control('all',$account_sub_control_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_sub_control_edit');
            $this->load->view('includes/footer');
        }
    }
   
    public function subcontrolaccountedit(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  

        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

                   
            $account_sub_control_id= $this->input->post('id_account_sub_control');

            $data['account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->input->post('id_account_control');
            $data['account_sub_control']=$this->accounting_model->get_account_sub_control('all',$account_sub_control_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_sub_control_edit');
            $this->load->view('includes/footer');
        }
    }
        
   public function update_account_sub_control(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $result = $this->accounting_model->account_sub_control_update();
            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Record successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Record not updated!');
            }
            $account_sub_control_id=$this->input->post('id_account_sub_control');
           
            $data['business'] = $this->business_model->get_business_details();
            $data['account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->input->post('id_account_control');
            $data['account_sub_control']=$this->accounting_model->get_account_sub_control('all', $account_sub_control_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_sub_control_edit');
            $this->load->view('includes/footer');
        }
    }
    
    public function sub_control_account_delete(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
            
            
            
            $account_main = $this->input->post('id_account_main');
            $account_control = $this->input->post('id_account_control');
            $account_sub_control_id= $this->input->post('id_account_sub_control');
            
            $data['account_heads']=$this->accounting_model->get_account_heads($account_sub_control_id);

            
            //echo sizeof($data['account_sub_control']); exit();
            if(sizeof($data['account_heads']) >= 1){
                
                $this->session->set_flashdata('Error', 'There are Existing Account Heads In This Sub Control Account!');
            } else {
                
                $this->accounting_model->delete_account_sub_control($account_sub_control_id);
                
                $this->session->set_flashdata('Success', 'Sub Control Account Deleted!');
               
            }
            
            redirect('sub_control_accounts/'.$account_main."/".$account_control);

        }
    }
    //////////End Sub Control Accounts///////////////
    
    
    ////////// Account Heads ////////////////////
    
    public function account_heads($mainid, $controlid, $subcontrolid){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

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

            $data['account_main'] = $mainid;
            $data['account_control'] = $controlid;
            $data['account_sub_control'] = $this->accounting_model->get_account_sub_control('all', $subcontrolid);
            $data['account_heads'] = $this->accounting_model->get_account_heads($subcontrolid,'all', $this->session->userdata('businessid'));


            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_heads');
            $this->load->view('includes/footer');
        }
    }
    
    public function accountheadadd(){
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $data['account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->input->post('id_account_control');
            $data['id_account_sub_control']=$this->input->post('id_account_sub_control');
            $data['account_sub_control']=$this->input->post('account_sub_control');
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_head_add');
            $this->load->view('includes/footer');
        }
    }


    public function insert_account_head(){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            
            $account_sub_control_id=$this->input->post('id_account_sub_control');
            
            $account_head_id = $this->accounting_model->account_head_insert($account_sub_control_id);
            if($account_head_id && $account_head_id>0){
                $this->session->set_flashdata('Success', 'Sub Control Account successfully added!');
            } else {
                $this->session->set_flashdata('Error', 'Sub Control Account not added!');
            }

            $data['business'] = $this->business_model->get_business_details();
            
            $data['account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->input->post('id_account_control');
            $data['id_account_sub_control']=$this->input->post('id_account_sub_control');
            $data['account_head']=$this->accounting_model->get_account_heads('all',$account_head_id);

            $data['sub_control_accounts']=$this->accounting_model->get_account_sub_control();
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_head_edit');
            $this->load->view('includes/footer');
        }
    }
    
     public function accountheadedit(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  

        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $account_head_id= $this->input->post('id_account_heads');

            $data['account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->input->post('id_account_control');
            $data['id_account_sub_control']=$this->input->post('id_account_sub_control');
            $data['account_head']=$this->accounting_model->get_account_heads('all',$account_head_id);
            
            $data['sub_control_accounts']=$this->accounting_model->get_account_sub_control();

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_head_edit');
            $this->load->view('includes/footer');
        }
    }
        
   public function update_account_head(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $result = $this->accounting_model->account_head_update();
            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Record successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Record not updated!');
            }
            $account_head_id=$this->input->post('id_account_heads');
           
            $data['business'] = $this->business_model->get_business_details();
            $data['account_main']=$this->input->post('id_account_main');
            $data['account_control']=$this->input->post('id_account_control');
            $data['account_sub_control']=$this->input->post('id_account_sub_control');
            $data['account_head']=$this->accounting_model->get_account_heads('all', $account_head_id);

            $data['sub_control_accounts']=$this->accounting_model->get_account_sub_control();
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa/account_head_edit');
            $this->load->view('includes/footer');
        }
    }
    
    public function account_head_delete(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
            
            $account_main=$this->input->post('id_account_main');
            $account_control=$this->input->post('id_account_control');
            $id_account_sub_control=$this->input->post('id_account_sub_control');
            $account_head_id= $this->input->post('id_account_heads');
            
            $account_vouchers = $this->accounting_model->get_account_vouchers('all','all','all',$account_head_id);
            
            if(sizeof($account_vouchers) >= 1){
                $this->session->set_flashdata('Error', 'Account Head has vouchers created and cannot be deleted!');
          
            } else {
                $a = $this->accounting_model->delete_account_head($account_head_id);
                if($a>0){
                    $this->session->set_flashdata('Success', 'Account Head Deleted!');
                }else {
                    $this->session->set_flashdata('Error', 'This Account Head is mapped to system events and cannot be deleted!');
                }
            }
            redirect('account_heads/'.$account_main."/".$account_control."/".$this->input->post('id_account_sub_control'));

        }
    }
    
    ////////// End Account Heads ///////////////////
    
    
    /////////// Print Vouchers ////////////////////
    
    function print_voucher($voucher_id){
    
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
           // checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';
            
              $data['menu'] = 'hidden';
              
            $data['business'] = $this->business_model->get_business_details();
            
            $data['voucher']=$this->accounting_model->get_any_voucher($voucher_id);
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/print_voucher_view');
            $this->load->view('includes/footer');
        }        
        
    }
    
    //////////////////Print Vouchers End//////////////
    
    
    
    //////////////////////System Generated Vochers /////////////////////////////////////
    
    
    public function systemeventaccounts($systemevent){
        
        
        if($systemevent==null){
            
            return 'No Account Heads Mapped For This Event!';
            
        } else {
            
            $systemevents=  $this->accounting_model->get_system_events($systemevent);
            
            
            
        }
        
        
    }
    
    
    
    
    //////////////////End System Generated Vouchers/////////////////////////////////////
    
    
    
    public function coa() {
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

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

            $data['account_heads'] = $this->accounting_model->get_all_account_heads();


            $this->load->view('includes/header', $data);
            $this->load->view('accounting/coa_view');
            $this->load->view('includes/footer');
        }
    }

    function accountheadedit_old(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $account_head_id=$this->input->post('id_account_head');

            $data['account_sub_types']=$this->accounting_model->get_account_subtypes();
            $data['account_head']=$this->accounting_model->get_account_head($account_head_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/account_head_edit');
            $this->load->view('includes/footer');
        }
    }
    
    function update_account_head_old(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $result = $this->accounting_model->account_head_update();
            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Record successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Record not updated!');
            }
            $account_head_id=$this->input->post('id_account_heads');
            $data['account_sub_types']=$this->accounting_model->get_account_subtypes();
            $data['account_head']=$this->accounting_model->get_account_head($account_head_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/account_head_edit');
            $this->load->view('includes/footer');
        }
    }
    
    function accountheadadd_old(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $data['account_sub_types']=$this->accounting_model->get_account_subtypes();

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/account_head_add');
            $this->load->view('includes/footer');
        }
    }
    
    function insert_account_head_old(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $account_head_id = $this->accounting_model->account_head_insert();
            if($account_head_id && $account_head_id>0){
                $this->session->set_flashdata('Success', 'Account Head successfully added!');
            } else {
                $this->session->set_flashdata('Error', 'Account Head not added!');
            }

            $data['account_sub_types']=$this->accounting_model->get_account_subtypes();
            $data['account_head']=$this->accounting_model->get_account_head($account_head_id);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/account_head_edit');
            $this->load->view('includes/footer');
        }
    }
    
    

    public function account_vouchers($startdate=null, $enddate=null) {

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

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            
            if(null !== $this->input->post('startdate')){
                $data['startdate'] = $this->input->post('startdate'); 
            }else if(null!=$startdate){
                $data['startdate'] = $startdate; 
            }else {$data['startdate'] ='';}
            
            if(null !==  $this->input->post('enddate')){
                $data['enddate'] = $this->input->post('enddate');
            } else if(null !== $enddate){
                $data['enddate'] = $enddate;
            }else {$data['enddate'] ='';}
            
            if(null !==  $this->input->post('voucher_type')){
                $data['voucher_type'] = $this->input->post('voucher_type');
            } else {$data['voucher_type'] ='';}
            
            if(null !==  $this->input->post('business_id')){
                $data['businessselected'] = $this->input->post('business_id');
            } else {$data['businessselected'] ='';}
            
            $data['auto_voucher']=$this->input->post('auto_voucher');
            
            
            $data['business'] = $this->business_model->get_business_details();
            $data['businesses'] = $this->business_model->get_all_businesses();
            $data['voucher_types'] = $this->accounting_model->get_voucher_types();
            $data['account_vouchers'] = $this->accounting_model->get_all_account_vouchers(null, $data['startdate'], $data['enddate']);

            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/account_vouchers_list');
            $this->load->view('includes/footer');
        }
        
    }
    
    
    
    public function account_vouchers_ref() {

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

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $data['business'] = $this->business_model->get_business_details();
            $data['businesses'] = $this->business_model->get_all_businesses();
            $data['voucher_types'] = $this->accounting_model->get_voucher_types();
            $data['account_vouchers'] = $this->accounting_model->get_ref_account_vouchers();

            if(null !== $this->input->post('startdate')){
                $data['startdate'] = $this->input->post('startdate'); 
            }else {$data['startdate'] ='';}
            if(null !==  $this->input->post('enddate')){
                $data['enddate'] = $this->input->post('enddate');
            }else {$data['enddate'] ='';}
            if(null !==  $this->input->post('voucher_type')){
                $data['voucher_type'] = $this->input->post('voucher_type');
            } else {$data['voucher_type'] ='';}
            if(null !==  $this->input->post('business_id')){
                $data['businessselected'] = $this->input->post('business_id');
            } else {$data['businessselected'] ='';}
            
            $data['auto_voucher']=$this->input->post('auto_voucher');
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/account_vouchers_list');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function general_ledger(){
        //echo $this->input->post('business_partner_id'); exit();
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
             checkroles(0, 0, 0);

             $data['nav'] = 'accounting';
             $data['subnav'] = 'accounting';

            // echo $this->input->post('business_partner_id');exit();
             
             if(null !== $this->input->post('startdate')){
                 $data['startdate'] = $this->input->post('startdate'); 
             }else {$data['startdate'] ='';}
             
             if(null !==  $this->input->post('enddate')){
                 $data['enddate'] = $this->input->post('enddate');
             }else {$data['enddate'] ='';}
             
             if(null !== $this->input->post('from_accounts')){
                 $data['from_account'] = $this->input->post('from_accounts'); 
             }else {$data['fromaccount'] ='';}

             if(null !== $this->input->post('to_accounts')){
                 $data['to_account'] = $this->input->post('to_accounts'); 
             }else {$data['to_account'] ='';}

             if(null !== $this->input->post('selected_account_sub_control')){
                 $data['selected_account_sub_control'] = $this->input->post('selected_account_sub_control'); 
             }else {$data['selected_account_sub_control'] ='';}
             
             $data['businesses'] = $this->business_model->get_all_businesses();
             $data['businessselected']=$this->input->post('business_id');
             $data['business'] = $this->business_model->get_business_details();
             $data['account_heads'] = $this->accounting_model->get_account_heads();
             $data['account_sub_control'] = $this->accounting_model->get_account_sub_control();
             
             if(null !== $this->input->post('startdate') && null !== $this->input->post('enddate')){
                $data['account_vouchers'] = $this->accounting_model->get_all_account_vouchers_opening_balance();
             } else {
                $data['account_vouchers'] = [];
             }
//             var_dump($data['account_vouchers']);
//             exit();
            if(null !== $this->input->post('bptype')){             
                $data['business_partner_selected']=$this->input->post('bptype');
                $data['business_partner_id_selected']=$this->input->post('business_partner_id');
            }else {$data['business_partner_selected']='';$data['business_partner_id_selected']='';}

            if(null !== $this->input->post('cost_center_type')){
                $data['cost_center_type_selected']=$this->input->post('cost_center_type');
                $data['cost_center_name_selected']=$this->input->post('cost_center_name');
            } else {
                $data['cost_center_type_selected']='';$data['cost_center_name_selected']='';
            }
            
            $data['bptypes'] = $this->accounting_model->get_account_business_partners();            
            $data['cost_center_types'] = $this->accounting_model->get_cost_center_types();
            
            $data['suppliers'] = $this->supplier_model->get_suppliers();
            $data['staffs'] = $this->staff_model->all_staff();
            $data['franchises'] = $this->franchise_model->get_all_franchises();
            $data['branches'] = $this->business_model->get_all_branches();
            
             $this->load->view('includes/header', $data);
             $this->load->view('accounting/general_ledger');
             $this->load->view('includes/footer');
        }
        
    }
    
    public function trial_balance(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
              checkroles(0, 0, 0);

              $startdate = $this->input->post('startdate'); 
              $enddate = $this->input->post('enddate');
              $voucher_type = $this->input->post('voucher_type');

              $from_account = $this->input->post('from_accounts');
              $to_account = $this->input->post('to_accounts');

              if($startdate==""){
                  $startdate= date('Y-m-d');
              }
              if($enddate==""){
                  $enddate= date('Y-m-d');
              }
             
             
              $data['nav'] = 'accounting';
              $data['subnav'] = 'accounting';

              $data['business'] = $this->business_model->get_business_details();
             $data['businesses'] = $this->business_model->get_all_businesses();
             $data['businessselected']=$this->input->post('business_id');
             
             
             $data['trial_balance'] = $this->accounting_model->get_account_heads($from_account,'all');
             
              $data['account_heads'] = $this->accounting_model->get_account_heads('all','all');
              $data['account_sub_control'] = $this->accounting_model->get_account_sub_control();
              $data['get_accounts'] = $this->accounting_model->get_all_account_heads_params($from_account,$to_account);
              $data['trial_data'] = $this->accounting_model->trial_balance_new($startdate,$enddate);
              
              $data['trial_type']='Balance';    
              if(null!=$this->input->post('trial_type')){                          
                  $data['trial_type']=$this->input->post('trial_type');                  
              }

            

              if(null !== $this->input->post('startdate')){
                  $data['startdate'] = $this->input->post('startdate'); 
              }else {$data['startdate'] ='';}
              if(null !==  $this->input->post('enddate')){
                  $data['enddate'] = $this->input->post('enddate');
              }else {$data['enddate'] ='';}
              if(null !==  $this->input->post('voucher_type')){
                  $data['voucher_type'] = $this->input->post('voucher_type');
              } else {$data['voucher_type'] ='';}
                if(null !== $this->input->post('from_accounts')){
                  $data['from_account'] = $this->input->post('from_accounts'); 
              }else {$data['fromaccount'] ='';}

              if(null !== $this->input->post('to_accounts')){
                  $data['to_account'] = $this->input->post('to_accounts'); 
              }else {$data['to_account'] ='';}
              
              
              if(null !== $this->input->post('bptype')){             
                $data['business_partner_selected']=$this->input->post('bptype');
                $data['business_partner_id_selected']=$this->input->post('business_partner_id');
                }else {$data['business_partner_selected']='';$data['business_partner_id_selected']='';}

                if(null !== $this->input->post('cost_center_type')){
                    $data['cost_center_type_selected']=$this->input->post('cost_center_type');
                    $data['cost_center_name_selected']=$this->input->post('cost_center_name');
                } else {
                    $data['cost_center_type_selected']='';$data['cost_center_name_selected']='';
                }
                if(null !== $this->input->post('selected_account_sub_control')){
                     $data['selected_account_sub_control'] = $this->input->post('selected_account_sub_control'); 
                 }else {$data['selected_account_sub_control'] ='';}
             
            $data['bptypes'] = $this->accounting_model->get_account_business_partners();            
            $data['cost_center_types'] = $this->accounting_model->get_cost_center_types();
            
            $data['suppliers'] = $this->supplier_model->get_suppliers();
            $data['staffs'] = $this->staff_model->all_staff();
            $data['franchises'] = $this->franchise_model->get_all_franchises();
            $data['branches'] = $this->business_model->get_all_branches();  

              
            
              $this->load->view('includes/header', $data);
              $this->load->view('accounting/trial_balance');
              $this->load->view('includes/footer');
        }
        
    }
    
    
    public function trial_movement(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
              checkroles(0, 0, 0);

              $startdate = $this->input->post('startdate'); 
              $enddate = $this->input->post('enddate');
              $voucher_type = $this->input->post('voucher_type');

              $from_account = $this->input->post('from_accounts');
              $to_account = $this->input->post('to_accounts');

              if($startdate==""){
                  $startdate= date('Y-m-d');
              }
              if($enddate==""){
                  $enddate= date('Y-m-d');
              }
             
             
              $data['nav'] = 'accounting';
              $data['subnav'] = 'accounting';

              $data['business'] = $this->business_model->get_business_details();
              $data['businesses'] = $this->business_model->get_all_businesses();
              $data['businessselected']=$this->input->post('business_id');
             
              $data['account_heads'] = $this->accounting_model->get_account_heads('all','all');
              $data['account_sub_control'] = $this->accounting_model->get_account_sub_control();
              $data['get_accounts'] = $this->accounting_model->get_all_account_heads_params($from_account,$to_account);
              $data['trial_data'] = $this->accounting_model->trial_balance($startdate,$enddate);
              $data['trial_type']='Movement';    
              if(null!=$this->input->post('trial_type')){                          
                  $data['trial_type']=$this->input->post('trial_type');                  
              }

             
              if(null !== $this->input->post('startdate')){
                  $data['startdate'] = $this->input->post('startdate'); 
              }else {$data['startdate'] ='';}
              if(null !==  $this->input->post('enddate')){
                  $data['enddate'] = $this->input->post('enddate');
              }else {$data['enddate'] ='';}
              if(null !==  $this->input->post('voucher_type')){
                  $data['voucher_type'] = $this->input->post('voucher_type');
              } else {$data['voucher_type'] ='';}
                if(null !== $this->input->post('from_accounts')){
                  $data['from_account'] = $this->input->post('from_accounts'); 
              }else {$data['fromaccount'] ='';}

              if(null !== $this->input->post('to_accounts')){
                  $data['to_account'] = $this->input->post('to_accounts'); 
              }else {$data['to_account'] ='';}
              
              
              if(null !== $this->input->post('bptype')){             
                $data['business_partner_selected']=$this->input->post('bptype');
                $data['business_partner_id_selected']=$this->input->post('business_partner_id');
                }else {$data['business_partner_selected']='';$data['business_partner_id_selected']='';}

                if(null !== $this->input->post('cost_center_type')){
                    $data['cost_center_type_selected']=$this->input->post('cost_center_type');
                    $data['cost_center_name_selected']=$this->input->post('cost_center_name');
                } else {
                    $data['cost_center_type_selected']='';$data['cost_center_name_selected']='';
                }
                if(null !== $this->input->post('selected_account_sub_control')){
                     $data['selected_account_sub_control'] = $this->input->post('selected_account_sub_control'); 
                 }else {$data['selected_account_sub_control'] ='';}
             
            $data['bptypes'] = $this->accounting_model->get_account_business_partners();            
            $data['cost_center_types'] = $this->accounting_model->get_cost_center_types();
            
            $data['suppliers'] = $this->supplier_model->get_suppliers();
            $data['staffs'] = $this->staff_model->all_staff();
            $data['franchises'] = $this->franchise_model->get_all_franchises();
            $data['branches'] = $this->business_model->get_all_branches();  

//              echo '<pre>';
//              print_r($data['trial_data']);
//              echo '</pre>';
//              exit();
            
              $this->load->view('includes/header', $data);
              $this->load->view('accounting/trial_balance');
              $this->load->view('includes/footer');
        }
        
    }
    
    public function balacne_sheet(){
       
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

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

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

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $data['business'] = $this->business_model->get_business_details();            
            $data['businesses'] = $this->business_model->get_all_businesses();             
            
            $businessid=$this->session->userdata('businessid');
            if(null == $this->input->post('business_id')){ 
                
                $data['businessselected']=$this->session->userdata('businessid');
            }else{
                $businessid=$this->input->post('business_id');
                $data['businessselected']=$this->input->post('business_id');
            }
             
           // $data['voucher_types'] = $this->accounting_model->get_voucher_types();
            //$data['account_heads'] = $this->accounting_model->get_account_heads();
            
            $data['account_main'] = $this->accounting_model->get_account_main();
            

            foreach($data['account_main'] as $main){
                $data['account_control'] = $this->accounting_model->get_account_control($main['id_account_main'],'all');
                
                foreach($data['account_control'] as $ac){
                    
                    $data['account_sub_control'] = $this->accounting_model->get_account_sub_control($ac['id_account_control'],'all');
                    
                    foreach($data['account_sub_control'] as $row){                        
                        
                        $data['accounts'] = $this->accounting_model->get_account_heads($row['id_account_sub_control'],'all',$businessid);
                       
                            foreach($data['accounts'] as $acc){
//                                echo '<pre>';
//                                print_r($acc['id_account_heads']. ' '.$acc['business_name']);
//                                echo '</pre>';
                                $data['blanace_sheet_assets'] = $this->accounting_model->balanceSheet($startdate,$acc['id_account_heads'],$main['account_main']);
                                $data['balanceSheet'][$main['account_main']][$ac['account_control']][$row['account_sub_control']][$acc['account_head'].'/'.$acc['business_name']] = $data['blanace_sheet_assets'];
                               // var_dump($data['blanace_sheet_assets']); exit();
                            }
                    }
                }
            }
            
          //  if(null!== $this->input->post('business_id')  && $this->input->post('business_id') !== ''){
                $data['retainedincome'] = $this->accounting_model->get_retained_income($startdate, $businessid); 
//            } else{
//                foreach($data['businesses'] as $b){
//                    $data['retainedincome'][$b['business_name']] = $this->accounting_model->get_retained_income($startdate, $b['id_business']); 
//                }
//            }
            
            if(null !== $this->input->post('startdate')){
                $data['startdate'] = $this->input->post('startdate'); 
            }else {$data['startdate'] ='';}
            
            

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/balance_sheet');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function profit_loss(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $startdate = $this->input->post('startdate'); 
            $enddate = $this->input->post('enddate');
            $voucher_type = $this->input->post('voucher_type');

            $from_account = $this->input->post('from_accounts');
            $to_account = $this->input->post('to_accounts');

            if($from_account == ""){
                $from_account = 1;
            }
            if($to_account == ""){
                $to_account = 1;
            }

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

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $data['business'] = $this->business_model->get_business_details();
            
             $data['businesses'] = $this->business_model->get_all_businesses();
             $data['businessselected']=$this->input->post('business_id');
             
            
            $data['voucher_types'] = $this->accounting_model->get_voucher_types();
            $data['account_heads'] = $this->accounting_model->get_all_account_heads();
            $data['account_main'] = $this->accounting_model->get_account_main();
            $data['invoice'] = $this->accounting_model->get_cash_info($startdate,$enddate);
            $data['voucher'] = $this->accounting_model->get_cash_voucher($startdate,$enddate);
            $data['revenue_service'] = $this->accounting_model->revenue_breakup($startdate,$enddate, 'service');
            $data['revenue_sale'] = $this->accounting_model->revenue_breakup($startdate,$enddate, 'sale');
//            echo "<pre>";
//            print_r($data['revenue_service']);
//            exit();

            foreach($data['account_main'] as $ma){
                $data['account_control'] = $this->accounting_model->get_account_control($ma['id_account_main']);
                foreach($data['account_control'] as $ac){
                    $id = intval($ac['id_account_control']);
                    $data['account_sub_control'] = $this->accounting_model->get_account_sub_control($id);
                    foreach($data['account_sub_control'] as $asc){
                        $id = intval($asc['id_account_sub_control']);
                        $data['accounts'] = $this->accounting_model->get_account_heads($id);
                       
                        foreach($data['accounts'] as $acc){
                            //print_r($ma['account_main']);
                            $data['pl'] = $this->accounting_model->profit_and_loss($startdate,$enddate,$acc['id_account_heads'],$ma['account_main'],$ma['account_main']);
                            $data['profit_loss'][$ma['account_main']][$asc['account_sub_control']][$acc['account_head'].' - '.$acc['business_name']] = $data['pl'];
//                            if($ma['account_main']=='Expenses'){
//                                 print_r($data['profit_loss']['Expenses']);
//                            }
                        }
                    }
                    
                }
                
            }
            // exit();
            //var_dump($data['profit_loss']); exit();
            if(null !== $this->input->post('startdate')){
                $data['startdate'] = $this->input->post('startdate'); 
            }else {$data['startdate'] ='';}
            if(null !==  $this->input->post('enddate')){
                $data['enddate'] = $this->input->post('enddate');
            }else {$data['enddate'] ='';}
            if(null !==  $this->input->post('voucher_type')){
                $data['voucher_type'] = $this->input->post('voucher_type');
            } else {$data['voucher_type'] ='';}
              if(null !== $this->input->post('from_accounts')){
                $data['from_account'] = $this->input->post('from_accounts'); 
            }else {$data['fromaccount'] ='';}

            if(null !== $this->input->post('to_accounts')){
                $data['to_account'] = $this->input->post('to_accounts'); 
            }else {$data['to_account'] ='';}

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/profit_loss');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function get_vouchers(){
  
            //ajax
            $data = $this->accounting_model->get_all_account_vouchers();
            echo (json_encode($data));
    
        
    }
    public function get_vouchers_created(){
        
            //ajax
            $data = $this->accounting_model->get_all_vouchers_created();
            echo (json_encode($data));
        
    }
    
    public function accountvoucheradd(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //open add page   
           checkroles(0, 0, 0);

           $data['nav'] = 'accounting';
           $data['subnav'] = 'accounting';

           $data['business'] = $this->business_model->get_business_details();
           $data['account_voucher_types'] = $this->accounting_model->get_account_voucher_type();
           $data['account_business_partners'] = $this->accounting_model->get_account_business_partners();
           $data['bank_accounts'] = $this->accounting_model->get_account_heads(3,'all',$this->session->userdata('businessid'));
           $data['card_accounts'] = $this->accounting_model->get_account_heads(8,'all',$this->session->userdata('businessid'));
           $data['account_heads'] = $this->accounting_model->get_account_heads('all','all',$this->session->userdata('businessid'));
           $data['cost_center_types'] = $this->accounting_model->get_cost_center_types();
           $cih = $this->accounting_model->get_cash_in_hand();
           if(null!==$cih){
                $data['cash_in_hand'] = $cih->id_account_heads;
           } else { $data['cash_in_hand'] = 0;}

           
            $data['suppliers'] = $this->supplier_model->get_suppliers();
            $data['staffs'] = $this->staff_model->all_staff();
            $data['franchises'] = $this->franchise_model->get_all_franchises();
            $data['branches'] = $this->business_model->get_all_branches();
           
           
           $this->load->view('includes/header', $data);
           $this->load->view('accounting/account_voucher_add');
           $this->load->view('includes/footer');  
        }
        
    }
    
    public function accountvoucheredit($id_account_vouchers=null){
       
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //open add page   
           checkroles(0, 0, 0);

           $data['nav'] = 'accounting';
           $data['subnav'] = 'accounting';

           if(null == $id_account_vouchers){
                $voucherid = $this->input->post('id_account_vouchers');
           } else {
               $voucherid = $id_account_vouchers;
           }
           
           $data['voucher_details']= $this->accounting_model->get_account_voucher($voucherid);
                     
           $data['business'] = $this->business_model->get_business_details();
           $data['account_voucher_types'] = $this->accounting_model->get_account_voucher_type();
           $data['account_business_partners'] = $this->accounting_model->get_account_business_partners();
           $data['bank_accounts'] = $this->accounting_model->get_account_heads(3,'all',$this->session->userdata('businessid'));
           $data['card_accounts'] = $this->accounting_model->get_account_heads(8,'all',$this->session->userdata('businessid'));
           $data['account_heads'] = $this->accounting_model->get_account_heads('all','all',$this->session->userdata('businessid'));
           $data['cost_center_types'] = $this->accounting_model->get_cost_center_types();
           $cih = $this->accounting_model->get_cash_in_hand();
           if(null!==$cih){
                $data['cash_in_hand'] = $cih->id_account_heads;
           } else { $data['cash_in_hand'] = 0;}

           $this->load->view('includes/header', $data);
           $this->load->view('accounting/account_voucher_edit');
           $this->load->view('includes/footer');  
        }
        
    }
    
    function insert_multiple_vouchers(){

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

        //$tabledata = json_decode($tableData);
        $bank_name='';
        foreach($tableData as $row){

            $data = array( 
                'business_id' => $this->session->userdata('businessid'),
                'description' => $row['description'],
                'voucher_date' => $row['voucher_date'],
                'voucher_amount' => $row['voucher_amount'],
                'voucher_status' => 'Active',
                'created_by' => $this->session->userdata('username'),
                'cost_center'=> $row['cost_center'],
                'cost_center_name' => $row['cost_center'],
                'business_partner' => $row['business_partner'],
                'business_partner_id' => $row['business_partner_id'],
                'business_partner_name'=>$row['business_partner_name'],
                'voucher_type'=>$row['voucher_type'],
                'payment_mode'=>$row['payment_mode'],
                'bank_name'=>$bank_name

            );
            $debit_accounts=$row['debit_accounts'];
            $credit_accounts=$row['credit_accounts'];
            //echo '<pre>';
            //print_r($data);
            $result = $this->accounting_model->add_account_voucher($data, $debit_accounts, $credit_accounts);
        }
        echo "success|";

    }

    function insert_account_voucher(){
      
            $debit_accounts=$this->input->post('debit_accounts', TRUE);
            $credit_accounts=$this->input->post('credit_accounts', TRUE);
            $bank_name='';
            if(null!==$this->input->post('bank_accounts')){$bank_name=$this->input->post('bank_accounts');}
            $data = array( 
                'business_id' => $this->session->userdata('businessid'),
                'description' => $this->input->post('description', TRUE),
                'voucher_date' => $this->input->post('voucher_date', TRUE),
                'voucher_amount' => $this->input->post('voucher_amount', TRUE),
                'voucher_status' => 'Active',
                'created_by' => $this->session->userdata('username'),
                'cost_center'=>$this->input->post('cost_center', TRUE),
                'cost_center_name'=>$this->input->post('cost_center_name', TRUE),
                'business_partner'=>$this->input->post('business_partner', TRUE),
                'business_partner_id'=>$this->input->post('business_partner_id', TRUE),
                'business_partner_name'=>$this->input->post('business_partner_name', TRUE),
                'voucher_type'=>$this->input->post('voucher_type', TRUE),
                'payment_mode'=>$this->input->post('payment_mode', TRUE),
                'bank_name'=>$bank_name

            );

            $result = $this->accounting_model->add_account_voucher($data, $debit_accounts, $credit_accounts);
            if($result && $result>0){
                echo "success|" . $result;
            } else {
                echo "error|" . $result;
            }
        
    }
    
    function cancel_account_voucher(){
        
        $voucherid= $this->input->post('id_account_vouchers');
        
         $result = $this->accounting_model->cancel_account_voucher($voucherid);
//            if($result && $result>0){
//                echo "success|" . $result;
//            } else {
//                echo "error|" . $result;
//            }
        redirect('account_vouchers');
    }
    
    function create_account_voucher(){

        $result = $this->accounting_model->create_account_voucher();
        if($result && $result>0){
            echo "success|" . $result;
        } else {
            echo "error|" . $result;
        }
        
        
    }
    
    function edit_account_voucher(){
       // exit();
        $voucher_id=$this->input->post('voucher_id', TRUE);
        $result = $this->accounting_model->edit_account_voucher();
        
        if($result>0){
            
            // $sql="select id_account_voucher_detail from account_voucher_detail where account_voucher_id=".$voucher_id;
            // $query = $this->db->query($sql);
            // $total_records = $query->num_rows();
            
            // if($total_records > sizeof($result)){
            
            //     foreach($result as $old_detail_id){
            //         $this->db->where('id_account_voucher_detail', $old_detail_id['id_account_voucher_detail']);
            //         $this->db->delete('account_voucher_detail');
            //     }
            //     echo "success|" . $voucher_id;
            // } else {
            //     echo "success|" . $voucher_id;
            // }
            echo "success|" . $voucher_id;
        } else {
            echo "error|Please check the remarks data and ensure you are not using slashes or back slashes";
        }
        
    }
    
    function payments(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {

            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'reception';
            $data['subnav'] = 'reception';

            $data['business'] = $this->business_model->get_business_details();
            $data['suppliers'] = $this->supplier_model->get_suppliers();
            $data['payments'] = $this->accounting_model->get_payments();

            if(null !== $this->input->post('startdate')){
                $data['startdate'] = $this->input->post('startdate'); 
            }else {$data['startdate'] ='';}
            if(null !==  $this->input->post('enddate')){
                $data['enddate'] = $this->input->post('enddate');
            }else {$data['enddate'] ='';}
            if(null !==  $this->input->post('voucher_type')){
                $data['voucher_type'] = $this->input->post('voucher_type');
            } else {$data['voucher_type'] ='';}
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/payments');
            $this->load->view('includes/footer');
        }
    }
    
    function statement(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
    
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'accounting';
            $data['subnav'] = 'accounting';

            $startdate=$this->input->post('startdate', TRUE);
            $enddate=$this->input->post('enddate', TRUE);

            if(!isset($startdate) || empty($startdate)){
                 $startdate = date('Y-m-01');
            }

            if(!isset($enddate) || empty($enddate)){
                 $enddate = date('Y-m-d');
            }

            //get the posted values
            $data['startdatetxt'] = date("d-M-Y", strtotime($startdate));
            $data['enddatetxt'] = date("d-M-Y", strtotime($enddate));
            $data['startdate'] = $startdate;
            $data['enddate'] = $enddate;


             $this->load->model('staff_model');

            $data['business'] = $this->business_model->get_business_details();
            $data['serviceincome'] = $this->accounting_model->get_month_services($startdate, $enddate);
            $data['retailincome'] = $this->accounting_model->get_month_sales($startdate, $enddate);

            $data['advancereceived'] = $this->accounting_model->get_month_advance($startdate, $enddate);

            $data['paymentvouchers'] = $this->accounting_model->getstatementpayments($startdate, $enddate,1);
            $data['receivevouchers'] = $this->accounting_model->getstatementpayments($startdate, $enddate,2);

            $data['salaries'] = $this->staff_model->get_staff_payments('Salary', $startdate, $enddate);
            $data['commissions'] = $this->staff_model->get_staff_payments('Commission',$startdate, $enddate);
            $data['otherstaffpayments'] = $this->staff_model->get_staff_payments('Other',$startdate, $enddate);

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/statement');
            $this->load->view('includes/footer');
        }
        
    }
    
    function supplier_payment($po_id){
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $data['business'] = $this->business_model->get_business_details();


            $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'));
            
            $eventmapping = $this->accounting_model->get_event_mappings(24);
            
            foreach ($eventmapping as $em){
                if($em['entity_name']=='purchase_salestax' ){$data['salestaxaccount']= $em['account_head_id'];}
                if($em['entity_name']=='purchase_withholdingtax')  {$data['withhodlingtaxaccount']= $em['account_head_id'];}
                if($em['entity_name']=='purchase_discount')  {$data['purchasediscountaccount']= $em['account_head_id'];}
            }
            
            $data['cost_center_types'] = $this->accounting_model->get_cost_center_types();
            $data['purchase_order'] = $this->purchaseorder_model->get_purchase_order_by_id($po_id);
            $data['payments'] = $this->accounting_model->get_purchase_order_payments($po_id);
            $data['payable']=0;
            $debit=0; $credit=0;
            foreach($data['payments'] as $payments){
                $debit=$debit+$payments['debit'];
                $credit=$credit+$payments['credit'];
            }
            $data['payable']=$credit-$debit;
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/supplier_payment');
            $this->load->view('includes/footer');
        }
        
    }
    function insert_po_payment(){
        
        
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $result=$this->accounting_model->insert_po_payment();

            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Record successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Record not updated! Please ask admin to map the accounts first.');
            }

            redirect('grn_list/'.$this->input->post('txtpurchaseorderid'));
        }
    }
    
    function cancel_voucher(){
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            $result = $this->accounting_model->cancel_voucher($this->input->post('id_account_voucher'));

            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Record successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Record not updated!');
            }

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

            $data['return_path']=$this->input->post('return_path');

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/result_view');
            $this->load->view('includes/footer');
        }
        
    }
    
    function transferretained($customerid){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            
            $data['invoice_retained']=$this->accounting_model->invoice_retained($customerid);
            $data['voucher_retained']=$this->accounting_model->voucher_retained($customerid);
            
            $data['businesses'] = $this->business_model->get_all_businesses();
            
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/transferretained');
            $this->load->view('includes/footer');
        }
        
    }
    
    function retainedtransfervoucher(){
        $frombranch=$this->input->post('frombranch');
        $tobranch=$this->input->post('tobranch');
        
        //exit();
        $this->accounting_model->decreasetheretained($frombranch, $tobranch);
        $this->accounting_model->increasetheretained($frombranch, $tobranch);
        
        redirect('transferretained/'.$this->input->post('customer_id'));
    }
    
    function cashregister_listing(){
         if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
    //       $startdate = $this->input->post('startdate', TRUE);
    //       $enddate = $this->input->post('enddate');

   //        if(null!==$this->input->post('startdate')){
   //            $startdate=$this->input->post('startdate');
   //        } else {$startdate =date('Y-m-01');}

          // echo $this->input->post('enddate');exit();
            
            $enddate = date('Y-m-d');
           if(null!==$this->input->post('enddate') && $this->input->post('enddate') !=='' ){
               $enddate=$this->input->post('enddate');
           } 
           
           $date = DateTime::createFromFormat('Y-m-d',$enddate);

           $startdate = $date->modify("-7 days");
       //    echo $startdate->format("Y-m-d");
       //    exit();
           //storemanager,hr,reception...user serial
           checkroles(0, 0, 0);

           $data['nav'] = 'accounting';
           $data['subnav'] = 'accounting';

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


           $transactions=[];
           $denominations=[];
           $expenses=[];
           $vouchers=[];
           $yesterdaytill=[];


           $today=$startdate->format('Y-m-d');
          // echo $today;exit();
           while($today <= $enddate){

              $tomorrow=$this->report_model->gettomorrowfromserver($today);

              array_push($transactions, $this->report_model->cash_register($today, $tomorrow));
              array_push($denominations, $this->report_model->get_cash_register($today, $tomorrow));
              array_push($expenses, $this->report_model->get_today_expenses($today, $tomorrow));
              array_push($vouchers, $this->report_model->get_cash_voucher($today, $tomorrow));
              array_push($yesterdaytill, $this->report_model->get_yesterday_till_amount($today, $tomorrow));


              $today=$tomorrow;
           }
           
           //echo sizeof($denominations); echo sizeof($transactions); exit();
           
           $data['transactions']=$transactions;
           $data['denominations']=$denominations;
           $data['expenses']=$expenses;
           $data['vouchers']=$vouchers;
           $data['yesterdaytill']=$yesterdaytill;
           if(null!==$this->input->post('cashregister_status') && $this->input->post('cashregister_status')!==''){
               $data['selectedstatus']=$this->input->post('cashregister_status');
           }else{
               $data['selectedstatus']="open";
           }
           
           

           $data['cashregisters'] = $this->report_model->cash_register($today, $tomorrow);        

           $this->load->view('includes/header', $data);
           $this->load->view('accounting/cashregister_listing');
           $this->load->view('includes/footer');
        }
        
    }
    
    
    public function custom_payments(){
        
        if ($this->session->userdata('role') === '' || $this->session->userdata('role') === 'Sh-Users') {
            $this->load->view('login_view');  
             
        } else {
            //storemanager,hr,reception...user serial
            checkroles(0, 0, 0);

            $data['nav'] = 'business';
            $data['subnav'] = 'accounting';

            $data['business'] = $this->business_model->get_business_details();
    
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/custom/custom_payments');
            $this->load->view('includes/footer');
        }
       
    }
    
    public function post_voucher($startdate, $enddate, $voucherid=null){
        if($voucherid !== null){
            
            $this->db->where('id_account_vouchers', $voucherid);
            $this->db->set('voucher_status', 'Active');
            $this->db->update('account_vouchers');     
         
            $this->account_vouchers($startdate, $enddate);
        }
        
    }

    public function ho_pnl(){

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

            if(null != $this->input->post('startdate')){
                $startdate = $this->input->post('startdate'); 
                $enddate = $this->input->post('enddate');
            }else{
                $startdate = date('Y-m-d');
                $enddate = date('Y-m-d');
            }


            if($this->input->post('business_id')!=null && $this->input->post('business_id')>0){
                $businesses = $this->business_model->getbusinessdetails($this->input->post('business_id'));
            } else if($this->input->post('business_id')!=null && $this->input->post('business_id') == 0){ 
                $businesses = $this->business_model->get_all_branches();            
            } else {
                $businesses = $this->business_model->getbusinessdetails($this->session->userdata('businessid'));
            }

            $data['businesses']=[];
            foreach($businesses  as $business){
                if(null != $startdate){                    

                    $d['accounts'] = $this->accounting_model->new_pl_query($startdate, $enddate, $business['id_business']);
                 //print_r($d); exit();
                    $accounts = array_merge($business, $d);
                    
                    array_push($data['businesses'], $accounts);

                } else {
                    array_push($business, []);
                }           
            }

            $data['startdate']=$startdate;
            $data['enddate']=$enddate;
            if($this->input->post('business_id')!=null){
                $data['business_id']=$this->input->post('business_id');
            }else{
                $data['business_id']=$this->session->userdata('businessid');
            }
            if($this->session->userdata('ho')=='Yes'){
                $data['business_list']=$this->business_model->get_all_branches();
            }else{
                $data['business_list']=$this->business_model->getbusinessdetails($this->session->userdata('businessid'));
            }   
            // echo '<pre>';
            // print_r($data['businesses']); exit();

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/statements/new_pnl');
            $this->load->view('includes/footer');

        }
    }    
 }
