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

class Petty_Cash_Model extends CI_Model {
	 function __construct(){
		 parent::__construct();
		 }
		 
		 
		  public function get_prety_cash_in_sum($srh_to_date,$warehouse_id)
	{
		$this->db->select_sum('pc.acctrnss_amount');
		$this->db->from('petty_cash pc');
		//$this->db->join('sales s', 's.sale_id = si.sale_id', 'left');
		$this->db->where('pc.fxd_ass_id',19);
		$this->db->where('pc.cat_status',1);
		$this->db->where('pc.fxd_ass_id != ',2);
		if($warehouse_id){
		$this->db->where('pc.warehouse_id',$warehouse_id);
		}
		if($srh_to_date){
			$srh_to_date=date('Y-m-d',strtotime($srh_to_date));
			$this->db->where("date(pc.acctrnss_date) < ",$srh_to_date);//("id !=",$id);
		}
		
		
		$query=$this->db->get();
		//echo $this->db->last_query();
		return $query->row()->acctrnss_amount;
	}
	
	
	 public function get_prety_cash_sum($srh_to_date,$warehouse_id)
	{
		$this->db->select_sum('pc.acctrnss_amount');
		$this->db->from('petty_cash pc');
		//$this->db->join('sales s', 's.sale_id = si.sale_id', 'left');
		$this->db->where('pc.fxd_ass_id != ',19);
		$this->db->where('pc.fxd_ass_id != ',2);
		$this->db->where('pc.cat_status',1);
		if($warehouse_id){
		$this->db->where('pc.warehouse_id',$warehouse_id);
		}
		if($srh_to_date){
			$srh_to_date=date('Y-m-d',strtotime($srh_to_date));
			$this->db->where("date(pc.acctrnss_date) < ",$srh_to_date);//("id !=",$id);
		}
		
		
		$query=$this->db->get();
		//echo $this->db->last_query();
		return $query->row()->acctrnss_amount;
	}
	
	
		 
		 function pc_change_status($category_tbl_id='',$status='')

{

  $cat_id = ($status ==0) ? 1 : 0 ;

  $data  = array(

                 'cat_status'  => $cat_id

              );



  $this->db->where('acctrnss_id', $category_tbl_id);

   if($this->db->update('petty_cash', $data))

   {

     return true;

   }

   else

   {

     return false;

   }

}


    function transactions_type_list(){
	$this->db->select('*');	
	$this->db->from('fixed_assets_type');
	$this->db->where('fa_type_status',1);
	$query=$this->db->get();
	return $query->result_array();
		}		 
	function save_transactions(&$data,$acctrnss_id=false)
	{
		if (!$acctrnss_id)
		{
			$this->db->insert('petty_cash',$data);
		}else {
			$this->db->where('acctrnss_id', $acctrnss_id);
			return $this->db->update('petty_cash',$data);
			
		}
	}
	function get_transactions_list($warehouse_id='',$srh_from_date='',$srh_to_date='',$fxd_ass_id='')
	{
		$this->db->select('petty_cash.*,warehouses.name,etp.*,u.user_first_name,bank_accounts.*');
		$this->db->select('fixed_asset.*');
		$this->db->select('fixed_assets_type.*');
		$this->db->select('fixed_assets_master.*');
		$this->db->from('petty_cash');
		$this->db->join('fixed_asset','fixed_asset.fxd_ass_id=petty_cash.fxd_ass_id','left');
		$this->db->join('fixed_assets_type','fixed_asset.fa_type_id=fixed_assets_type.fa_type_id','left');
		$this->db->join('fixed_assets_master','fixed_assets_master.fam_id=fixed_assets_type.fam_id','left');
		$this->db->join('warehouses', 'petty_cash.warehouse_id = warehouses.id', 'left');
			$this->db->join('bank_accounts', 'petty_cash.b_id = bank_accounts.b_id', 'left');
			$this->db->join('mstr_expences_type etp','petty_cash.etp_id=etp.etp_id','left');
			$this->db->join('user u','petty_cash.rep_id=u.user_id','left');
			if($srh_from_date){
			$srh_from_date=date('Y-m-d',strtotime($srh_from_date));
			$this->db->where("date(petty_cash.acctrnss_date) >=",$srh_from_date);
		}
			if($srh_to_date){
			$srh_to_date=date('Y-m-d',strtotime($srh_to_date));
			$this->db->where("date(petty_cash.acctrnss_date) <=",$srh_to_date);
		}
		if($warehouse_id){
		$this->db->where('petty_cash.warehouse_id',$warehouse_id);
		}
			if($fxd_ass_id){
		$this->db->where('petty_cash.fxd_ass_id',$fxd_ass_id);
		}
		
		$this->db->order_by("petty_cash.acctrnss_date", "asc");
		
		$query=$this->db->get();
		
		
		//echo $this->db->last_query();
		return $query->result_array();
	}
	
	function get_fixed_assets_master_list()
	{
		$this->db->select('fixed_assets_master.*');
		$this->db->from('fixed_assets_master');
		$this->db->order_by("fam_id", "asc");
		$query=$this->db->get();
		return $query->result();
	}	

	function get_fixed_assets_type_list($fam_id='')
	{
		$this->db->select('fixed_assets_type.*');
		$this->db->from('fixed_assets_type');
		$this->db->where('fam_id',$fam_id);
		$this->db->order_by("fa_type_id", "asc");
		$query=$this->db->get();
		return $query->result();
	}

	function get_fixed_assets_list($fa_type_id='')
	{
		$this->db->select('fa.*');
		$this->db->from('fixed_asset fa');
		$this->db->where('fa.fa_type_id',$fa_type_id);
		$this->db->order_by("fa.fxd_ass_id", "asc");
		$query=$this->db->get();
		return $query->result();
	}

	function get_acc_transactions_list($fa_type_id='')
	{
		$this->db->select('fa.*,at.*');
		$this->db->from('petty_cash at');
		$this->db->join('fixed_asset fa','fa.fxd_ass_id=at.fxd_ass_id','left');
		$this->db->where('fa.fa_type_id',$fa_type_id);
		$this->db->order_by("at.acctrnss_id", "asc");
		$query=$this->db->get();
		return $query->result();
	}
				
	function get_transactions_details($acctrnss_id)
	{
	$this->db->select('t.*,u.*');
	$this->db->select('fixed_asset.*');
		$this->db->select('fixed_assets_type.*');
		$this->db->select('fixed_assets_master.*');
		$this->db->select('etp.*');
	$this->db->from('petty_cash t');	
	$this->db->join('fixed_asset','fixed_asset.fxd_ass_id=t.fxd_ass_id','left');
		$this->db->join('fixed_assets_type','fixed_asset.fa_type_id=fixed_assets_type.fa_type_id','left');
		$this->db->join('fixed_assets_master','fixed_assets_master.fam_id=fixed_assets_type.fam_id','left');
	//	$this->db->join('warehouses', 'petty_cash.warehouse_id = warehouses.id', 'left');
			$this->db->join('mstr_expences_type etp','t.etp_id=etp.etp_id','left');
	$this->db->join('user u','t.user_id=u.user_id','left');
	$this->db->where('t.acctrnss_id',$acctrnss_id);
	$query=$this->db->get();
	return $query->row_array();
	}
	
	function get_fixed_asset_vehical(){
		$this->db->select('fixed_asset.fxd_ass_id,fixed_asset.fxd_ass_name');
		$this->db->from('fixed_asset');
		$this->db->join('fixed_assets_type','fixed_assets_type.fa_type_id=fixed_asset.fa_type_id','left');
		$this->db->where('fixed_asset.fxd_ass_status',1);
		$this->db->where('fixed_assets_type.fa_type_id',1);
		$this->db->where('fixed_assets_type.fa_type_status',1);
		$query=$this->db->get();
		return $query->result_array();
		}
}