<?php

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

class Accountreports_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('accounting_model');
        $this->load->model('accountreports_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');
       
    }

     public function account_reports(){
        
        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['chart']='chart';
            $data['nodatatable']='nodatatable';
            
            $data['business'] = $this->business_model->get_business_details();
            
            $startdate = $this->input->post('startdate');
            $enddate = $this->input->post('enddate');
            //echo $enddate; exit();
            if(null==$startdate || $startdate==''){
                $startdate= date('Y-m-d');
            }
            if(null==$enddate || $enddate==''){
                $enddate= date('Y-m-d');
            }   
            
            $event_mappings = $this->accounting_model->get_event_mappings(1);
            
            $list = ''; $salesaccount=0;$salestaxaccount=0;$salesdiscount=0;
            
            foreach($event_mappings as $mapping){
                $list .= $mapping['account_head_id'].',';
                if($mapping['entity_name']=='grosstotal_service'){
                    $salesaccount=$mapping['account_head_id'];
                }else if($mapping['entity_name']=='grosstotal_retail'){
                    $retailaccount=$mapping['account_head_id'];
                }else if($mapping['entity_name']=='sales_tax'){
                    $salestaxaccount=$mapping['account_head_id'];
                }else if($mapping['entity_name']=='sale_discount_service'){
                    $salesdiscount=$mapping['account_head_id'];
                }else if($mapping['entity_name']=='grosstotal_training'){
                    $salestraining=$mapping['account_head_id'];
                }else if($mapping['entity_name']=='grosstotal_recurring'){
                    $salesrecurring=$mapping['account_head_id'];
                }
            }
            
            $account_status = $this->accounting_model->get_account_status($startdate, $enddate, null,rtrim($list, ","));
           
            $data['account_details']=[];
            foreach($account_status as $d){
               
                foreach($event_mappings as $mapping){
                    if($mapping['account_head_id']== $d['account_head_id']){
                        $data['account_details'][$mapping['entity_name'].$d['sale_type']] = $d;
                    } 
                }
            }
           // echo '<pre>'; print_r($data['account_details']); exit();
            
            $data['servicetaxcal']=$this->accountreports_model->get_sale_and_tax($startdate, $enddate, $salesaccount, $salestaxaccount,$salesdiscount,'service');
            $data['retailtaxcal']=$this->accountreports_model->get_sale_and_tax($startdate, $enddate, $retailaccount, $salestaxaccount,$salesdiscount,'sale');
            $data['trainingtaxcal']=$this->accountreports_model->get_training_sale_and_tax($startdate, $enddate, $salestraining, $salestaxaccount);
            
            //Franchise sale
            $mappings = $this->accounting_model->get_event_mappings(37);
            $franchisesaleaccount=0;
            foreach ($mappings as $account){
                if($account['entity_name']=='total_invoice_value'){
                    $franchisesaleaccount = $account['account_head_id'];
                }
            }
            
            $data['franchisetaxcal']=$this->accountreports_model->get_franchise_sale_and_tax($startdate, $enddate, $franchisesaleaccount, $salestaxaccount);
            
            $data['startdate']=$startdate; $data['enddate']=$enddate;
            
           
            $this->load->view('includes/header', $data);
            $this->load->view('accounting/reports/dashboard');
            $this->load->view('includes/footer');
        }
        
    }
    
   public function salestaxreport(){
       
       //storemanager,hr,reception...user serial
        checkroles(0, 0, 0);

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

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

        }
        $business_id=$this->session->userdata('businessid');

        if($this->input->post('businessid')!=null){           
            $business_id=$this->input->post('businessid');
        }
        $data['data']=$this->accountreports_model->salestaxreport($startdate, $enddate, $business_id);
        $data['business'] = $this->business_model->get_business_details();
        $data['businesses']=$this->business_model->get_all_businesses();
        $data['selected_business']=$this->input->post('businessid');
           // print_r($data['businesses']);exit();
        
        $data['invoicetype'] =  $this->input->post('invoicetype');
        
        $this->load->view('includes/header', $data);
        $this->load->view('accounting/reports/salestaxreport');
        $this->load->view('includes/footer');
       
       
   }
   
   public function vendor_account_report(){
       checkroles(0, 0, 0);

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

        if(null!==$this->input->post('startdate')){
            $startdate = $this->input->post('startdate');
            $enddate = $this->input->post('enddate');
            $data['startdate']=$startdate; $data['enddate']=$enddate;
        } else {
            $startdate = date('Y-m-d');
            $enddate = date('Y-m-d');
            $data['startdate']=$startdate; $data['enddate']=$enddate;           
        }
//exit();
        $business_partner_type= $this->input->post('bptype')!==null ? $this->input->post('bptype') : 0;
        $business_partner_id=  $this->input->post('business_partner')!==null && $this->input->post('business_partner')!==''  ? $this->input->post('business_partner_id') : 0;
        $id_business = $this->session->userdata('businessid');
      //  echo $this->input->post('account_type');
        if(null!==$this->input->post('account_type') && $this->input->post('account_type')!==''){
            $passed_account = explode("|", $this->input->post('account_type'));
            $account_id = $passed_account[0];
            $account_type = $passed_account[1];
        }else{
            $account_id=1;
            $account_type='Receivable';
        }
        
        $data['opening'] = $this->accountreports_model->vendor_account_report_openging($startdate, $enddate, $business_partner_type, $business_partner_id, $id_business, $account_id, $account_type);
        
        
        $data['data']=$this->accountreports_model->vendor_account_report($startdate, $enddate, $business_partner_type, $business_partner_id, $id_business, $account_id);
               
        $data['bptypes'] = $this->accounting_model->get_account_business_partners();
        //rsort($data['bptypes']);
        usort($data['bptypes'], function ($item1, $item2) {
                return $item2['account_business_partner'] <=> $item1['account_business_partner'];
            });
        
        
        
        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');
            $data['business_partner_name_selected']=$this->input->post('business_partner_name');
        }else {$data['business_partner_selected']='';$data['business_partner_id_selected']='';}
        
        $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();
        $data['business'] = $this->business_model->get_business_details();
       
        $data['accounttypes']=[];
        //payables account
        $data['accounttypes'][] = $this->accounting_model->get_event_account(20, 'payables');
        $data['accounttypes'][] = $this->accounting_model->get_event_account(1, 'balance');
        
       // print_r($data['accounttypes']); exit();
        $data['account_type_selected'] = $account_id;
        
        $this->load->view('includes/header', $data);
        $this->load->view('accounting/reports/vendor_account_report');
        $this->load->view('includes/footer');
       
       
   }
   
   public function payable_aging_report(){
      
        checkroles(0, 0, 0);

        $data['nav'] = 'accounting';
        $data['subnav'] = 'payable_aging';
       
        $payable_accounts = $this->accounting_model->get_event_account(20, 'payables');       
        if($payable_accounts){
            $account_head_id = $payable_accounts->account_head_id;
        } else {
            die('Mapping not available');
        }
        
        $data['title'] = 'Payable';
        $data['aging'] = $this->accountreports_model->payable_aging_report($account_head_id);    
        $data['enddate'] = $this->input->post('enddate');
        
        
        $data['business'] = $this->business_model->get_business_details();
        
        $this->load->view('includes/header', $data);
        $this->load->view('accounting/reports/payable_aging_report');
        $this->load->view('includes/footer');
       
       
   }
   
   public function receivable_aging_report(){
      
        checkroles(0, 0, 0);

        $data['nav'] = 'accounting';
        $data['subnav'] = 'receivable_aging';
       
        $payable_accounts = $this->accounting_model->get_event_account(1, 'balance');       
        if($payable_accounts){
            $account_head_id = $payable_accounts->account_head_id;
        } else {
            die('Mapping not available');
        }
        
        $data['title'] = 'Receivable';
        $data['aging'] = $this->accountreports_model->receivable_aging_report($account_head_id);    
        $data['business'] = $this->business_model->get_business_details();
        $data['enddate'] = $this->input->post('enddate');
        
        $this->load->view('includes/header', $data);
        $this->load->view('accounting/reports/payable_aging_report');
        $this->load->view('includes/footer');
       
       
   }
   
}