<?php

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

class Finance extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Salary_model');
        $this->load->model('Salary_type_model');
        $this->load->model('User_model');
        $this->load->model('Common_Model');
		$this->load->model('Warehouse_Model');
		 $this->load->model('Salary_payment_model');
		 $this->load->model('Transactions_Model');
		 $this->load->model('Sales_Model');
		  $this->load->model('finance_model');
		    //$this->load->model('Issue_Cards_Model');
		 
		
		
    }

    var $main_menu_name = 'finance';
    var $sub_menu_name = 'finance';

    public function index_del() {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name'] = $this->sub_menu_name;
		 $data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
        $this->load->view("salary", $data);
    }
	
	public function cash_book()
	{
		$data['main_menu_name'] = $this->main_menu_name;
    	$data['sub_menu_name'] = 'cash_book';
		$data['fixed_assets_master_list']=array();//$this->Transactions_Model->get_fixed_assets_master_list();	
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['srh_from_date'] = $this->input->post('srh_from_date');
		$data['srh_to_date'] =$this->input->post('srh_to_date');
		$data['srh_warehouse_id'] =$this->input->post('srh_warehouse_id');
		
		//echo "srh_warehouse_id :".$data['srh_warehouse_id'];
		$this->load->view('cash_book',$data);
	}
	
	
		public function cash_book_02()
	{
		$data['main_menu_name'] = $this->main_menu_name;
    	$data['sub_menu_name'] = 'cash_book_02';
		$data['fixed_assets_master_list']=array();//$this->Transactions_Model->get_fixed_assets_master_list();	
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['srh_from_date'] = $this->input->post('srh_from_date');
		$data['srh_to_date'] =$this->input->post('srh_to_date');
		$data['srh_warehouse_id'] =$this->input->post('srh_warehouse_id');
		$data['mstr_expences_type_list'] = $this->finance_model->get_mstr_expences_type_list();
		
		//echo "srh_warehouse_id :".$data['srh_warehouse_id'];
		$this->load->view('cash_book_02',$data);
	}
	
	public function cash_book_03()
	{
		$data['main_menu_name'] = $this->main_menu_name;
    	$data['sub_menu_name'] = 'cash_book_02';
		$data['fixed_assets_master_list']=array();//$this->Transactions_Model->get_fixed_assets_master_list();	
		$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		$data['srh_from_date'] = $this->input->post('srh_from_date');
		$data['srh_to_date'] =$this->input->post('srh_to_date');
		$data['srh_warehouse_id'] =$this->input->post('srh_warehouse_id');
		$data['mstr_expences_type_list'] = $this->finance_model->get_mstr_expences_type_list();
		
		//echo "srh_warehouse_id :".$data['srh_warehouse_id'];
		$this->load->view('cash_book_03',$data);
	}
	
	
	 public function cash_book_get_grn_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $grn_payment_val= $this->finance_model->get_grn_payments_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'grn_payment_val'=>$grn_payment_val));
    }
	
	 public function cash_book_get_cash_sale_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $cash_sales_payment= $this->finance_model->get_cash_sales_payment_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'cash_sales_payment'=>$cash_sales_payment));
    }
    
     public function cash_book_get_cash_sale_payments_v3() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $cash_sales_payment= $this->finance_model->get_cash_sales_payment_by_date_range_v3($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 $cash_sales_data="";
		 $cash_sales_data.="<table  width='100%' class='table table-bordered table-condensed table-hover table-striped dataTable'>";
		$cash_sales_data.="<tr>";
		$cash_sales_data.="<td width='50%'>";
		$cash_sales_data.="<b>Warehouse</b></td>";
		$cash_sales_data.="<td width='50%'>";
		$cash_sales_data.="<b>Amount</b></td>";
		$cash_sales_data.="</tr>";
		
		
		$tot_warehouse_cash_sales=0;
		
		if(!$srh_warehouse_id){
		    $warehouse_list = $this->Warehouse_Model->get_all_warehouse();
		    foreach ($warehouse_list as $row) {
		        $bank_deposit_amount=0;
		        $srh_warehouse_id=$row->id;
    		     $warehouse_cash_sales= $this->finance_model->get_cash_sales_payment_by_date_range_v3($srh_from_date,$srh_to_date,$srh_warehouse_id);
        		$cash_sales_data.="<tr>";
        		$cash_sales_data.="<td>";
        		$cash_sales_data.="$row->name</td>";
        		$cash_sales_data.="<td align='right'>";
        		$cash_sales_data.="$warehouse_cash_sales</td>";
        		$cash_sales_data.="</tr>";
        		$tot_warehouse_cash_sales+=$warehouse_cash_sales;
		     //$bank_deposit_amount=$this->finance_model->cash_book_get_owner_withdrawls_payments_v2($srh_from_date,$srh_to_date,$srh_warehouse_id,$fxd_ass_id,$etp_id,$row->b_id);
		     
		    }
		    
		}else{
		    $row = $this->Warehouse_Model->get_warehouse_info($srh_warehouse_id);
		    //$srh_warehouse_id=$row->id;
        		     $warehouse_cash_sales= $this->finance_model->get_cash_sales_payment_by_date_range_v3($srh_from_date,$srh_to_date,$srh_warehouse_id);
            		$cash_sales_data.="<tr>";
            		$cash_sales_data.="<td>";
            		$cash_sales_data.=$row['name']."</td>";
            		$cash_sales_data.="<td align='right'>";
            		$cash_sales_data.="$warehouse_cash_sales</td>";
            		$cash_sales_data.="</tr>";
            		$tot_warehouse_cash_sales+=$warehouse_cash_sales;
		}
		
		
		
		
		
		
		$cash_sales_data.="<tr>";
		$cash_sales_data.="<td>";
		$cash_sales_data.="</td>";
		$cash_sales_data.="<td align='right'><b> $tot_warehouse_cash_sales </b>";
		$cash_sales_data.="</td>";
		$cash_sales_data.="</tr>";
		
		$cash_sales_data.="</table>";
		
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'cash_sales_data'=>$cash_sales_data));
    }
    
    public function cash_book_get_cash_sale_return_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $cash_sales_return_payment= $this->finance_model->get_cash_sales_return_payment_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'cash_sales_return_payment'=>$cash_sales_return_payment));
    }
	
	public function cash_book_get_cash_advance_payment() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $advance_payment= $this->finance_model->get_advance_payment_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'advance_payment'=>$advance_payment));
    }
	
	public function cash_book_get_downpayments_payment() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $downpayments_payment= $this->finance_model->get_downpayments_payment_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'downpayments_payment'=>$downpayments_payment));
    }
	
	public function cash_book_get_installments_payment() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $installments_payment= $this->finance_model->cash_book_get_installments_payment($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'installments_payment'=>$installments_payment));
    }
	
	public function cash_book_get_visiting_charges_payment() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $visiting_charges_payment= $this->finance_model->cash_book_get_visiting_charges_payment($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'visiting_charges_payment'=>$visiting_charges_payment));
    }
	
	public function cash_book_get_rep_expences_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $rep_expences_payments= $this->finance_model->cash_book_get_rep_expences_payments($srh_from_date,$srh_to_date,$srh_warehouse_id);
		// echo "rep_expences_payments:$rep_expences_payments";
		//  $rep_expences_payments=0;
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'rep_expences_payments'=>$rep_expences_payments));
    }
	
	public function cash_book_get_cash_expences_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $cash_expences_payments= $this->finance_model->cash_book_get_cash_expences_payments($srh_from_date,$srh_to_date,$srh_warehouse_id);
		// echo "rep_expences_payments:$rep_expences_payments";
		//  $rep_expences_payments=0;
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'cash_expences_payments'=>$cash_expences_payments));
    }
	
	
		public function cash_book_get_owner_withdrawls_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		$fxd_ass_id=$this->input->get('fxd_ass_id');
		$etp_id=$this->input->get('etp_id');
		 $owner_withdrawls_payments= $this->finance_model->cash_book_get_owner_withdrawls_payments($srh_from_date,$srh_to_date,$srh_warehouse_id,$fxd_ass_id,$etp_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'owner_withdrawls_payments'=>$owner_withdrawls_payments));
    }
	
	
		public function cash_book_get_owner_withdrawls_payments_v2() {
		    
		  
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		$fxd_ass_id=$this->input->get('fxd_ass_id');
		$etp_id=$this->input->get('etp_id');
		  //echo "test";
		 $owner_withdrawls_payments= $this->finance_model->cash_book_get_owner_withdrawls_payments_v2($srh_from_date,$srh_to_date,$srh_warehouse_id,$fxd_ass_id,$etp_id);
		  
		// echo $this->db->last_query();
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'owner_withdrawls_payments'=>$owner_withdrawls_payments));
    }
    
    
    public function cash_book_get_bank_deposit_data_v3() {
		    
		  
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		$fxd_ass_id=$this->input->get('fxd_ass_id');
		$etp_id=$this->input->get('etp_id');
		  //echo "test";
		 $owner_withdrawls_payments= $this->finance_model->cash_book_get_owner_withdrawls_payments_v2($srh_from_date,$srh_to_date,$srh_warehouse_id,$fxd_ass_id,$etp_id);
		 
		 $this->load->model('Supplier_Multiple_Payment_Model');
		 $bank_details= $this->Supplier_Multiple_Payment_Model->get_all_bank_accounts();
		  
		// echo $this->db->last_query();
		$bank_diposit_data='';
		$bank_diposit_data.="<table  width='100%' class='table table-bordered table-condensed table-hover table-striped dataTable'>";
		$bank_diposit_data.="<tr>";
		$bank_diposit_data.="<td width='50%'>";
		$bank_diposit_data.="<b>Bank</b></td>";
		$bank_diposit_data.="<td width='50%'>";
		$bank_diposit_data.="<b>Amount</b></td>";
		$bank_diposit_data.="</tr>";
		$tot_bank_deposit=0;
		foreach ($bank_details as $row) {
		    $bank_deposit_amount=0;
		     $bank_deposit_amount=$this->finance_model->cash_book_get_owner_withdrawls_payments_v2($srh_from_date,$srh_to_date,$srh_warehouse_id,$fxd_ass_id,$etp_id,$row->b_id);
    		$bank_diposit_data.="<tr>";
    		$bank_diposit_data.="<td>";
    		$bank_diposit_data.="$row->b_name $row->account_holder_name ($row->b_account_no)</td>";
    		$bank_diposit_data.="<td align='right'>";
    		$bank_diposit_data.="$bank_deposit_amount</td>";
    		$bank_diposit_data.="</tr>";
    		$tot_bank_deposit+=$bank_deposit_amount;
		}
		
		$bank_diposit_data.="<tr>";
		$bank_diposit_data.="<td>";
		$bank_diposit_data.="</td>";
		$bank_diposit_data.="<td align='right'><b> $tot_bank_deposit </b>";
		$bank_diposit_data.="</td>";
		$bank_diposit_data.="</tr>";
		
		
		$bank_diposit_data.="<table>";
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'bank_diposit_data'=>$bank_diposit_data));
    }
    
	
	public function cash_book_get_sales_related_expences_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $sales_related_expences_payments= $this->finance_model->cash_book_get_sales_related_expences_payments($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'sales_related_expences_payments'=>$sales_related_expences_payments));
    }
    
    
    	public function get_tot_petty_cash_by_date_range() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $tot_petty_cash= $this->finance_model->get_tot_petty_cash_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'tot_petty_cash'=>$tot_petty_cash));
    }
	
	 public function cash_book_get_salary_advance_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $salary_advance_payments= $this->finance_model->get_salary_advance_payments_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'salary_advance_payments'=>$salary_advance_payments));
    }
	/*
	public function cash_book_get_salary_advance_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $salary_advance_payments= $this->finance_model->get_salary_advance_payments_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'salary_advance_payments'=>$salary_advance_payments));
    }
	*/
	public function cash_book_get_salary_payments() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		 $salary_payments= $this->finance_model->get_salary_payments_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'salary_payments'=>$salary_payments));
    }
	
	public function get_cash_collector_short_by_date_range() {
		 $error=0;
		 $disMsg=0;
		 $grn_payment_val=0;
		 $cash_collector_short=0;
		 $cash_collector_ex=0;
		 $srh_from_date = $this->input->get('srh_from_date');
		$srh_to_date =$this->input->get('srh_to_date');
		$srh_warehouse_id = $this->input->get('srh_warehouse_id');
		
		// echo $this->db->last_query();
		 //print_r($cash_collector_short_arry);
		 $excess_short=0;
		 $tot_amount=0;
		 $tot_vist_charge=0;
		 
		 /* cal total amount */
		 $issue_card_list=array();
		// echo "Test 1 ,";
		 $issue_card_list= $this->finance_model->get_list_issue_card_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id);
		// echo "Test 2 ,";
		 foreach ($issue_card_list as $ici_row)
 {
	// echo "Test 3 ,";
	// print_r($ici_row);
	 $issue_card_id=$ici_row->issue_card_id;//$ici_row->issue_card_id;
	 
	 //get issue card item paid amount
	 $payment_des=$this->finance_model->get_payment_des_by_issue_card_id($issue_card_id);
	// echo "<br/><br/>";
	// echo $this->db->last_query();
	// print_r($payment_des);
	 //echo "<br/>";
	 $tot_amount=$payment_des[0]->sale_pymnt_amount;
	 $tot_vist_charge=$payment_des[0]->tot_vist_charge;
	 
		
		$cash_collector_short_arry= $this->finance_model->get_cash_collector_short_by_date_range($srh_from_date,$srh_to_date,$srh_warehouse_id,$issue_card_id);
		foreach ($cash_collector_short_arry as $row)
		{
		$cr_amount_tot=$row->cr_amount_tot;
		$cr_expenses_tot=$row->cr_expenses_tot;
		$cr_no_of_code_tot=$row->cr_no_of_code_tot;
		$excess_short_1=($cr_amount_tot+$cr_expenses_tot)-($tot_amount+$tot_vist_charge);
		//echo "  excess_short_1:$excess_short_1, issue_card_id:$issue_card_id , cr_amount_tot:$cr_amount_tot , cr_expenses_tot:$cr_expenses_tot , tot_amount:$tot_amount , tot_vist_charge:$tot_vist_charge";
		if($excess_short_1>0)
		{
			$cash_collector_short=$cash_collector_short+$excess_short_1;
		}else{
			$cash_collector_ex=$cash_collector_ex+$excess_short_1;
		}
		}
	 
 }
		 /* end cal total amount */
		 
		 
		 $cash_collector_ex=abs($cash_collector_ex);
		
		
		// $cash_collector_short=$cash_collector_short;
		 
         echo json_encode(array('error'=>"$error",'disMsg'=>$disMsg,'cash_collector_short'=>$cash_collector_short,'cash_collector_ex'=>$cash_collector_ex));
    }
	
	 public function day_end() {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name'] = 'day_end';
		 $data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
        $this->load->view("day_end", $data);
    }
	
	public function day_end_submit()
	{
		$warehouse_id=$this->input->post('warehouse_id');
		$status=$this->input->post('status');
		$data=array(
			'status'=>$status
		);
		
		$this->Warehouse_Model->save_warehouse($data,$warehouse_id);
		
		$this->Common_Model->add_user_activitie("Day End, warehouse_id:$warehouse_id , status:$status");
		//echo $this->db->last_query();
		echo json_encode(array('status' => 'done'));
		//  echo json_encode($json_data);
		//$lastid=$this->db->insert_id();
	}
	
   

}
