<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Expence extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
         if (! $this->session->userdata ( 'user_logged_in' ) || !company_url($this->session->userdata ('company_slag'))) {
            redirect ( 'login', 'refresh' );
        }
        $this->load->vars ( array (
                'global' => 'Available to all views',
                'header' => 'common/header',
                'left' => 'common/left',
                'footer' => 'common/footer' 
        ) );

        $this->load->model('Expence_model');
    }

    public function index(){
        $data['expence_acount_list'] = $this->Expence_model->get_expence_items();
        $this->load->view('expence_acount_list',$data);
    }

    public function new_expence_type(){
        $data['expence_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        if ($data['expence_id'] > 0) {
            $data['expence_data'] = $this->Expence_model->get_expence_details($data['expence_id']);
        }
        $this->load->view('new_expence',$data);
    }

    public function save_expence_type(){
        $expence_id         = $this->input->post('expence_id');
        $account_name    = $this->input->post('account_name');
        $description     = $this->input->post('description');
        $status          = $this->input->post('status');

        $expence_data = array(
                'expence_acc_name' => $account_name,  
                'expence_acc_desc' => $description,   
                'date_added' => date('Y-m-d'),   
                'date_updated' => date('Y-m-d'),   
                'status' => $status        
            );

        if (($expence_id == 0)) {
            $this->Expence_model->insert_expence_type($expence_data);
        } else {
            unset($expence_data['date_added']);
            $this->Expence_model->update_expence_type($expence_data,$expence_id);
        }
        echo 1;   
    }

    public function list_expences(){
        $data['list_expence'] = $this->Expence_model->list_expences();
        // echo "<pre>";print_r($data['list_expence']);die();
        $this->load->view('expence_list',$data);
    }

    public function newExpence(){
        $data['expence_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['expence_data'][0] = array();
        if ($data['expence_id'] > 0) {
            $data['expence_data'] = $this->Expence_model->get_from_table('expences','*',array('expence_id'=>$data['expence_id']));
        }
        // echo "<pre>";print_r($data['expence_data']);die();
        $where_expece = array('status' => 1);
        $data['expence_acount_list'] = $this->Expence_model->get_from_table('expence_account','*',$where_expece);

        $where_bank = array('status' => 1);
        $data['bank_list'] = $this->Expence_model->get_from_table('bank_accounts','*',$where_bank);
        $this->load->view('add_new_expence',$data);
    }

    public function save_expence(){
        $expence_date         = $this->input->post('expence_date');
        $expence_id         = $this->input->post('expence_id');
        $expence_acc_id    = $this->input->post('expence_acc_id');
        $paid_through_acc    = $this->input->post('paid_through_acc');
        $hsn_code  = $this->input->post('hsn_code');
        $amount       = $this->input->post('amount');
        $gst_rate  = $this->input->post('gst_rate');
        $tax_amount     = $this->input->post('tax_amount');
        $vendor_customer_name      = $this->input->post('vendor_customer_name');
        $invoice_bill_id          = $this->input->post('invoice_bill_id');
        $note          = $this->input->post('note');
        $eligible_for_itc          = $this->input->post('eligible_for_itc');

        $expence_data = array(
                'expence_date' => date('Y-m-d',strtotime(str_replace('/', '-', $expence_date))),  
                'expence_acc_id' => $expence_acc_id,  
                'paid_through_acc' => $paid_through_acc,  
                'hsn_code' => $hsn_code,
                'amount' => $amount,     
                'gst_rate' => $gst_rate,
                'tax_amount' => $tax_amount,   
                'vendor_customer_name' => $vendor_customer_name,    
                'invoice_bill_id' => $invoice_bill_id,        
                'note' => $note,        
                'eligible_for_itc' => $eligible_for_itc,
                'date_added'    => date('Y-m-d'),     
                'date_edited'    => date('Y-m-d'),     
                'status'    => 1    
            );
        // echo "<pre>";print_r($expence_data);die();
        if (($expence_id == 0)) {
            $this->Expence_model->insert_expence($expence_data);
        } else {
            $this->Expence_model->update_expence($expence_data,$expence_id);
        }
        echo 1;   
    }

}?>