<?php

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

class Cashtill_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('invoice_model');
        
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }

   
    
    public function get_cashtillaccounts() {
        
        $today=$this->input->post('calendar_date', TRUE);
        $show_previous = $this->input->post('show_previous', TRUE);
        
        if($this->session->userdata('role')== "Reception" && $show_previous=="N" && $today !== date('Y-m-d')){
            echo json_encode("Not Allowed");
            return;
        }        
        
        $event_mappings = $this->accounting_model->get_mapped_accounts(1);
       
        foreach($event_mappings as $event_mapping){
            //get glbalance for debit accounts
            if($event_mapping['entity_name']=='paid_cash'){
                $data['cash_till'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='paid_card'){
                $data['card'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='paid_check'){
                $data['check'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='paid_voucher'){
                $data['voucher'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='grosstotal_training'){
                $data['training'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='grosstotal_recurring'){
                $data['membership'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='cc_fee'){
                $data['cc_fee'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='retained_amount'){
                $data['retained_amount'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='cc_tip'){
                $data['cc_tip'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='retained_amount_used'){
                $data['retained_amount_used'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            }else if($event_mapping['entity_name']=='grosstotal_retail'){
                $data['grosstotal_retail'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='grosstotal_service'){
                $data['grosstotal_service'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='sale_discount_service'){
                $data['sale_discount_service'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } else if($event_mapping['entity_name']=='sale_discount_retail'){
                $data['sale_discount_retail'] = $this->accounting_model->get_account_status($today, $today, $event_mapping['account_head_id'],'', 'group');
            } 
        }
       
        //$today= date('Y-m-d');
        $data['denominations']= $this->invoice_model->get_cash_register($today);
        $data['yesterday']= $this->invoice_model->get_yesterday_till_amount($today);
        
        $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['cash_accounts'] = array_reverse($data['cash_accounts']);
        
        echo(json_encode($data));
        
    }
    
    public function cashtillpost(){
       //exit();
        
        $tilldate = $this->input->post('tilldate');
        $calanderdate = $this->input->post('tilldate');
        
        $id_cash_register=$this->input->post('id_cash_register');
        
        $yesterdaytill = $this->input->post('yesterdaytill');
        $totalsystemcash = $this->input->post('systemtillcash');
        $totaldenominationcash = $this->input->post('denominationcash');
        
        $cashinhandamount = $this->input->post('cihamount');
        $cashaccount = $this->input->post('cashaccount');
        
        $bankamount = $this->input->post('bankamount');
        $bankaccount = $this->input->post('bankaccount');
        
        $missingexpense = $this->input->post('missingexpense');
        $unaccountedincome = $this->input->post('unaccounted');
        
        $cctippayable = $this->input->post('cctip');
        $tomorrowtill = $this->input->post('tomorrowtill');
        $openingcash = $this->input->post('openingcash');
        
         //////////////////////////////////////////////////////        
        //if extra income recorded then make voucher for increasing cash till and extra income
        if($unaccountedincome > 0){
            $account_heads = $this->accounting_model->get_mapped_accounts(39);

            $data=array(
                'business_id'=> $this->session->userdata('businessid'),  
                'description'=> 'Extra Cash Found in Physical Till Adjusted at Day Closing on '.$calanderdate,
                'voucher_date'=>$tilldate,
                'created_by'=>$this->session->userdata('username'),  
                'voucher_status'=> 'Active',
                'voucher_type'=> 3,
                'cost_center'=> 1,
                'cost_center_name'=> 'Front Desk',
                'payment_mode'=>'Cash',
                'cashregister_id'=>$id_cash_register
            );
            $debit_accounts=[];
            $credit_accounts=[];
            foreach($account_heads as $ah){
                if($ah['transaction_type']=='debit'){
                   array_push($debit_accounts,$ah['account_head_id']."|".$unaccountedincome."|".$ah['entity_name']);
                } else {
                    array_push($credit_accounts, $ah['account_head_id']."|".$unaccountedincome."|".$ah['entity_name']);
                }
            }        
            $income_voucher_id = $this->accounting_model->add_account_voucher($data, $debit_accounts, $credit_accounts);       


            $totalsystemcash = $totalsystemcash+$unaccountedincome;
        }
        
        //////////////////////////////////////////////////////        
        //if cash is missing then make voucher for decreasing cash till with missing cash expense
        if($missingexpense > 0){
            $account_heads = $this->accounting_model->get_mapped_accounts(40);

            $data=array(
                'business_id'=> $this->session->userdata('businessid'),  
                'description'=> 'Cash Found Missing in Physical Till Recorded at Day Closing on '.$calanderdate,
                'voucher_date'=>$tilldate,
                'created_by'=>$this->session->userdata('username'),  
                'voucher_status'=> 'Active',
                'voucher_type'=> 3,
                'cost_center'=> 1,
                'cost_center_name'=> 'Front Desk',
                'payment_mode'=>'Cash',
                'cashregister_id'=>$id_cash_register
            );
            $debit_accounts=[];
            $credit_accounts=[];
            foreach($account_heads as $ah){
                if($ah['transaction_type']=='debit'){
                   array_push($debit_accounts,$ah['account_head_id']."|".$missingexpense."|".$ah['entity_name']);
                } else {
                    array_push($credit_accounts, $ah['account_head_id']."|".$missingexpense."|".$ah['entity_name']);
                }
            }        
            $income_voucher_id = $this->accounting_model->add_account_voucher($data, $debit_accounts, $credit_accounts);       


            $totalsystemcash = $totalsystemcash - $missingexpense;
        }
        
        
        ////////////////////////////////////////////
        //if cctip recorded then first make voucher to subtract tip amount from cash till 
        if($cctippayable>0){
            $account_heads = $this->accounting_model->get_mapped_accounts(38);

            $data=array(
                'business_id'=> $this->session->userdata('businessid'),  
                'description'=> 'Tips Payable To Staff Adjusted at Day Closing on '.$calanderdate,
                'voucher_date'=>$tilldate,
                'created_by'=>$this->session->userdata('username'),  
                'voucher_status'=> 'Active',
                'voucher_type'=> 3,
                'cost_center'=> 1,
                'cost_center_name'=> 'Front Desk',
                'payment_mode'=>'Cash',
                'cashregister_id'=>$id_cash_register
            );
            $debit_accounts=[];
            $credit_accounts=[];
            foreach($account_heads as $ah){
                if($ah['transaction_type']=='debit'){
                   array_push($debit_accounts,$ah['account_head_id']."|".$cctippayable."|".$ah['entity_name']);
                } else {
                    array_push($credit_accounts, $ah['account_head_id']."|".$cctippayable."|".$ah['entity_name']);
                }
            }        
            $tip_voucher_id = $this->accounting_model->add_account_voucher($data, $debit_accounts, $credit_accounts);       

            $totalsystemcash=$totalsystemcash-$cctippayable;
        }
       
        
        ///////////////////////////////////////////////////////////////   
        //Make Final Transfer Voucher with any missing amount declared
        $account_heads = $this->accounting_model->get_mapped_accounts(40);
        
        $data=array(
            'business_id'=> $this->session->userdata('businessid'),  
            'description'=> 'Cash Till Transfer Voucher on Day Closing for '.$calanderdate,
            'voucher_date'=>$tilldate,
            'created_by'=>$this->session->userdata('username'),  
            'voucher_status'=> 'Active',
            'voucher_type'=> 3,
            'cost_center'=> 1,
            'cost_center_name'=> 'Back Office',
            'payment_mode'=>'Cash',
            'cashregister_id'=>$id_cash_register
        );
        $debit_accounts=[];
        $credit_accounts=[];
        
        if($cashinhandamount>0){
            array_push($debit_accounts, $cashaccount."|".$cashinhandamount."|user_cash_account");
        }
        if($bankamount>0){
            array_push($debit_accounts, $bankaccount."|".$bankamount."|user_bank_amount");
        }
                
        foreach($account_heads as $ah){
            if($ah['transaction_type']=='debit'){
                if($missingexpense>0){
                 //   array_push($debit_accounts,$ah['account_head_id']."|".$missingexpense."|".$ah['entity_name']);
                }
            } else {
                if($totaldenominationcash>0){
                    $totaltill = $totaldenominationcash - ($tomorrowtill );//+ $cctippayable
                    array_push($credit_accounts, $ah['account_head_id']."|".$totaltill."|".$ah['entity_name']);//changed from $totalsystemcash 9-21
                }
            }
        }        
        $transfer_voucher_id = $this->accounting_model->add_account_voucher($data, $debit_accounts, $credit_accounts);       
                
        
        $this->accounting_model->status_cash_register($id_cash_register, 'closed');       
        
        redirect('scheduler/'.$tilldate);
        
    }
    
    
    public function generatetransfervoucher(){
        
        
        
    }
    
    
}