<?php

class Expense_model extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_daily_expense_list($startdate, $enddate) {
//        $cash = $this->accounting_model->get_event_account(1, 'paid_cash');
//        $cash_account = $cash->account_head_id;

        $this->db->select('*, ah.account_head, a.account_head as "cashaccount", DATE_FORMAT(av.created_on, "%d-%m-%Y") created_date, DATE_FORMAT(av.voucher_date, "%d-%m-%Y") voucher_date');
        $this->db->join('account_voucher_detail avd','av.id_account_vouchers = account_voucher_id');
        $this->db->join('account_heads ah', 'ah.id_account_heads = avd.account_head_id');
        $this->db->join('account_sub_control asc', 'ah.account_sub_control_id = asc.id_account_sub_control');
        $this->db->join("(select av.id_account_vouchers, avd.account_head_id, ah.account_head
            from `account_voucher_detail` `avd`
            join account_vouchers as av on av.id_account_vouchers = avd.account_voucher_id
            join account_heads as ah on ah.id_account_heads = avd.account_head_id
            where ah.account_sub_control_id = 2
            and date(voucher_date)>='".$startdate."' 
            and date(voucher_date)<='".$enddate."'
            and av.business_id = ".$this->session->userdata('businessid')." 
            and av.voucher_status= 'Active'
            and av.voucher_type = 1
            ) as a", 'a.id_account_vouchers = av.id_account_vouchers', false);
        
        $this->db->order_by('av.created_on', 'desc');
        $query = $this->db->get('account_vouchers av');
        
        //echo $query; exit();

        return $query->result_array();
    }

    
    // function get_daily_petty_expense_old($startdate, $enddate) {
        
       
    //     $cash = $this->accounting_model->get_event_account(50, 'petty_cash');
        
    //     if(empty($cash)){
    //         $cash = $this->accounting_model->get_event_account(1, 'paid_cash');
    //     }
        
    //     $cash_account = $cash->account_head_id;
        
    //     $this->db->select('account_sub_control_id');
    //     $this->db->where('id_account_heads', $cash_account);
    //     $query = $this->db->get('account_heads');
        
    //     $subcontrol = $query->row();
    //     $subcontrol_id = $subcontrol->account_sub_control_id;
        
    //     $sql ="SELECT 
    //             id_account_vouchers, business_partner_name,
    //             replace(GROUP_CONCAT(account_head),',',' -> ') as account_head,  
    //             sum(debit) as voucher_amount, sum(credit) as credit,
    //             voucher_status,created_by,
    //             CASE
    //                 WHEN account_vouchers.description LIKE 'Salary%' THEN 'Salaries'
    //                 ELSE account_vouchers.description
    //             END AS description,
    //             DATE_FORMAT(account_vouchers.created_on, '%d-%m-%Y') created_date,
    //             DATE_FORMAT(account_vouchers.voucher_date,'%d-%m-%Y') voucher_date

    //         from account_vouchers  
    //         JOIN account_voucher_detail  on account_vouchers.id_account_vouchers = account_voucher_detail.account_voucher_id  
    //         JOIN account_heads ON account_voucher_detail.account_head_id = account_heads.id_account_heads 
    //         where id_account_vouchers in
    //         (SELECT 
    //             id_account_vouchers
    //         FROM
    //             `account_vouchers`
    //             JOIN
    //             `account_voucher_detail` ON `account_vouchers`.`id_account_vouchers` = `account_voucher_detail`.`account_voucher_id`
    //             JOIN
    //             `account_heads` ON `account_voucher_detail`.`account_head_id` = `account_heads`.`id_account_heads`
    //         WHERE
    //             `account_vouchers`.`business_id` = '".$this->session->userdata('businessid')."' 
    //                 AND DATE(`account_vouchers`.`voucher_date`) >= '".$startdate."' 
    //                 AND DATE(`account_vouchers`.`voucher_date`) <= '".$enddate."' 
    //                 AND `account_vouchers`.`voucher_status` = 'Active'
    //                 AND `account_vouchers`.`voucher_type` = 1
                    
    //                 AND `account_heads`.`account_sub_control_id` = ".$subcontrol_id." ) 

    //                 AND `account_vouchers`.`business_id` = '".$this->session->userdata('businessid')."' 
    //                 AND DATE(`account_vouchers`.`voucher_date`) >= '".$startdate."' 
    //                 AND DATE(`account_vouchers`.`voucher_date`) <= '".$enddate."'
    //                 AND `account_vouchers`.`voucher_status` = 'Active'
                    
    //                 AND `account_vouchers`.`voucher_type` = 1       
    //                  group by id_account_vouchers
    //                 order by id_account_vouchers    

    //                 ";
        
    //    //echo $sql; exit();
    //     $query = $this->db->query($sql);
       
    //     return $query->result_array();
    // }
    
    function get_daily_petty_expense($startdate, $enddate) {
        
       
        $cash = $this->accounting_model->get_event_account(50, 'petty_cash');
        
        if(empty($cash)){
            $cash = $this->accounting_model->get_event_account(1, 'paid_cash');
        }
        
        $cash_account = $cash->account_head_id;
        
        $this->db->select('account_sub_control_id');
        $this->db->where('id_account_heads', $cash_account);
        $query = $this->db->get('account_heads');
        
        $subcontrol = $query->row();
        $subcontrol_id = $subcontrol->account_sub_control_id;
        
        $sql ="SELECT 
                id_account_vouchers, business_partner_name,
                replace(GROUP_CONCAT(account_head),',',' -> ') as account_head,  
                round(sum(credit),2) as voucher_amount, round(sum(credit),2) as credit,
                voucher_status,created_by,
                CASE
                    WHEN account_vouchers.description LIKE 'Salary%' THEN 'Salaries'
                    ELSE account_vouchers.description
                END AS description,
                DATE_FORMAT(account_vouchers.created_on, '%d-%m-%Y') created_date,
                DATE_FORMAT(account_vouchers.voucher_date,'%d-%m-%Y') voucher_date

                from account_vouchers  
                JOIN account_voucher_detail  on account_vouchers.id_account_vouchers = account_voucher_detail.account_voucher_id  
                JOIN account_heads ON account_voucher_detail.account_head_id = account_heads.id_account_heads 
                where
                
                    account_heads.account_sub_control_id = ".$subcontrol_id." 
                    AND `account_vouchers`.`business_id` = '".$this->session->userdata('businessid')."' 
                    AND DATE(`account_vouchers`.`voucher_date`) >= '".$startdate."' 
                    AND DATE(`account_vouchers`.`voucher_date`) <= '".$enddate."'
                    AND `account_vouchers`.`voucher_status` = 'Active'
                    
                    AND `account_vouchers`.`voucher_type` = 1       
                    group by id_account_vouchers
                    order by id_account_vouchers    

                    ";
        
       //echo $sql; exit();
        $query = $this->db->query($sql);
       
        return $query->result_array();
    }
    


    function get_daily_till_expense($startdate, $enddate) {
        
       
        $cash = $this->accounting_model->get_event_account(1, 'paid_cash');
        $cash_account = $cash->account_head_id;
        
        $this->db->select('account_sub_control_id');
        $this->db->where('id_account_heads', $cash_account);
        $query = $this->db->get('account_heads');
        
        $subcontrol = $query->row();
        $subcontrol_id = $subcontrol->account_sub_control_id;
        
        $sql ="select sum(debit) as till_expense
                        from account_vouchers  
                        JOIN account_voucher_detail  on account_vouchers.id_account_vouchers = account_voucher_detail.account_voucher_id  
                        JOIN account_heads ON account_voucher_detail.account_head_id = account_heads.id_account_heads 
                        where id_account_vouchers in
            (SELECT 
                id_account_vouchers
            FROM
                `account_vouchers`
                JOIN
                `account_voucher_detail` ON `account_vouchers`.`id_account_vouchers` = `account_voucher_detail`.`account_voucher_id`
                JOIN
                `account_heads` ON `account_voucher_detail`.`account_head_id` = `account_heads`.`id_account_heads`
            WHERE
                `account_vouchers`.`business_id` = '".$this->session->userdata('businessid')."' 
                    AND DATE(`account_vouchers`.`voucher_date`) >= '".$startdate."' 
                    AND DATE(`account_vouchers`.`voucher_date`) <= '".$enddate."' 
                    AND `account_vouchers`.`voucher_status` = 'Active'
                    AND `account_vouchers`.`voucher_type` = 1
                    
                    AND `account_heads`.`id_account_heads` = ".$cash_account." ) 

                    AND `account_vouchers`.`business_id` = '".$this->session->userdata('businessid')."' 
                    AND DATE(`account_vouchers`.`voucher_date`) >= '".$startdate."' 
                    AND DATE(`account_vouchers`.`voucher_date`) <= '".$enddate."'
                    AND `account_vouchers`.`voucher_status` = 'Active'
                    
                    AND `account_vouchers`.`voucher_type` = 1    group by  DATE(`account_vouchers`.`voucher_date`)   ";
        
        //echo $sql; exit();
        $query = $this->db->query($sql);
       
        return $query->row();
    }
    
    function account_voucher_cancelled() {
        $data = array(
            'voucher_status' => 'cancelled',
            'cancelled_by' => $this->session->userdata('username'),
            'cancelled_on' => date('Y-m-d H:i:s')
        );
        $this->db->where('id_account_vouchers', $this->input->post('id_account_voucher'));
        $result = $this->db->update('account_vouchers', $data);
        return $this->input->post('id_account_voucher');
    }

    function add_account_voucher() {
        $data = array(
            'description' => $this->input->post('description'),
            'voucher_amount' => $this->input->post('voucher_amount'),
            'voucher_status' => $this->input->post('voucher_status'),
            'voucher_date' => $this->input->post('voucher_date'),
            'created_by' => $this->session->userdata('username'),
            'business_id' => $this->session->userdata('businessid')
        );
        $this->db->insert('account_vouchers', $data);
        return $this->db->insert_id();
    }

    function get_account_voucher_by_id() {
        $this->db->select('*');
        $this->db->where('av.business_id', $this->session->userdata('businessid'));
        $this->db->where('av.id_account_vouchers', $this->input->post('id_account_vouchers'));
        $query = $this->db->get('account_vouchers av');

        return $query->row();
    }

    function update_account_voucher() {
        $data = array(
            'description' => $this->input->post('description'),
            'voucher_amount' => $this->input->post('voucher_amount'),
            'voucher_status' => $this->input->post('voucher_status'),
            'voucher_date' => $this->input->post('voucher_date'),
            'created_by' => $this->session->userdata('username')
        );
        $this->db->where('id_account_vouchers', $this->input->post('id_account_vouchers'));
        $this->db->update('account_vouchers', $data);
        return $this->input->post('id_account_vouchers');
    }

}
