<?php
require_once("report.php");
class Inventory_summary extends Report
{
	function __construct()
	{
		parent::__construct();
	}
	
	public function getDataColumns()
	{
		//featching employee specific stores locations
		$employee_data = $this->Employee->get_logged_in_employee_info();
		
		$this->db->select('location_id');
		$this->db->from('employees_locations');
		$this->db->where('employee_id',$employee_data->person_id);
		$result_emp_lcations = $this->db->get();
		$employee_locations = $result_emp_lcations->result_array();
		foreach($employee_locations as $employee_locations_row){
			 $arr[] = $employee_locations_row[location_id];
		}
		$employee_locations_array = implode(',',$arr);
		
		if($this->params['all_stors_inventory']==1){
			$this->db->select('name, location_id,location_type');
			$this->db->from('locations');
			$this->db->where_in('location_id',explode(",",$employee_locations_array));
			$this->db->where('deleted',0);
			$result = $this->db->get();
			$locations = $result->result_array();
			$locations_name ='';
			foreach ($locations as $row){
				
				$damage_array = array('DAMAGE_STORE','OUT_2_IN');
				if(in_array($row['location_type'],$damage_array)){
					$tabular_data_locations_head[] = array('data'=>'<font color="red">'.$row['name'].'</font>', 'align'=> 'left');
				}elseif($row['location_type']=='IN_2_OUT') {
					$tabular_data_locations_head[] = array('data'=>'<font color="blue">'.$row['name'].'</font>', 'align'=> 'left');
				}else{
					$tabular_data_locations_head[] = array('data'=>$row['name'], 'align'=> 'left');
				}

			}
				$tabular_data_locations_head[] = array('data'=>'Total-Quantity', 'align'=> 'left');
				$tabular_data_locations_head[] = array('data'=>'Total-Price', 'align'=> 'left');
				$tabular_data_locations_head[] = array('data'=>'Price After Deprication', 'align'=> 'left');
				
		}
				
		$table_header = array(array('data'=>lang('items_category'), 'align'=>'left'),  array('data'=>lang('reports_item_number'), 'align' => 'left'),
		 //array('data'=>lang('items_product_id'), 'align' => 'left'),
		 array('data'=>lang('reports_item_name'), 'align' => 'left'));
		if($this->params['all_stors_inventory']==0){
				$store_only = array(array('data' => lang('items_cost_price'), 'align' => 'right'),array('data' => lang('items_unit_price'), 'align' => 'right'), array('data'=>lang('reports_count'), 'align' => 'left'), array('data'=>lang('reports_reorder_level'), 'align' => 'left'));
				$table_header = array_merge($table_header,$store_only);
				return $table_header;
			}
		
		return $tabular_head = array_merge($table_header,$tabular_data_locations_head);

	}
	
	public function getData()
	{
		//$this->params['all_stors_inventory'];
		$current_location=$this->Employee->get_logged_in_employee_current_location_id();
		$this->db->select('name, category, company_name, item_number, product_id, items.item_id,
		IFNULL('.$this->db->dbprefix('location_items').'.cost_price, '.$this->db->dbprefix('items').'.cost_price) as cost_price, 
		IFNULL('.$this->db->dbprefix('location_items').'.unit_price, '.$this->db->dbprefix('items').'.unit_price) as unit_price,
		quantity, 
		IFNULL('.$this->db->dbprefix('location_items').'.reorder_level, '.$this->db->dbprefix('items').'.reorder_level) as reorder_level, 
		description', FALSE);
		$this->db->from('items');
		$this->db->join('suppliers', 'items.supplier_id = suppliers.person_id', 'left outer');
		$this->db->join('location_items', 'location_items.item_id = items.item_id and location_id = '.$current_location, 'left');
		$this->db->where('items.deleted', 0);
		if ($this->params['category_search'] !='0')
		{
			$this->db->where($this->db->dbprefix('items').'.category', $this->params['category_search']);
		}
		if ($this->params['item_search'] !='0')
		{
			$this->db->like($this->db->dbprefix('items').'.name', $this->params['item_search']);
		}		
		if ($this->params['supplier'] != -1)
		{
			$this->db->where('suppliers.person_id', $this->params['supplier']);
		}
		$this->db->where('is_service !=', 1);
		$this->db->order_by('name');
		
		//If we are exporting NOT exporting to excel make sure to use offset and limit
		if (isset($this->params['export_excel']) && !$this->params['export_excel'])
		{
			$this->db->limit($this->report_limit);
			$this->db->offset($this->params['offset']);
		}
		
		return $this->db->get()->result_array();

	}
	
	function getTotalRows()
	{
		$current_location=$this->Employee->get_logged_in_employee_current_location_id();
		$this->db->select('name, category, company_name, item_number, product_id, 
		IFNULL('.$this->db->dbprefix('location_items').'.cost_price, '.$this->db->dbprefix('items').'.cost_price) as cost_price, 
		IFNULL('.$this->db->dbprefix('location_items').'.unit_price, '.$this->db->dbprefix('items').'.unit_price) as unit_price,
		quantity, 
		IFNULL('.$this->db->dbprefix('location_items').'.reorder_level, '.$this->db->dbprefix('items').'.reorder_level) as reorder_level, 
		description', FALSE);
		$this->db->from('items');
		$this->db->join('suppliers', 'items.supplier_id = suppliers.person_id', 'left outer');
		$this->db->join('location_items', 'location_items.item_id = items.item_id and location_id = '.$current_location, 'left');
		$this->db->where('items.deleted', 0);
		if ($this->params['category_search'] !='0')
		{
			$this->db->where($this->db->dbprefix('items').'.category', $this->params['category_search']);
		}
		if ($this->params['item_search'] !='0')
		{
			$this->db->like($this->db->dbprefix('items').'.name', $this->params['item_search']);
		}	
		if ($this->params['supplier'] != -1)
		{
			$this->db->where('suppliers.person_id', $this->params['supplier']);
		}
		$this->db->where('is_service !=', 1);
		$this->db->order_by('name');
		
		return $this->db->count_all_results();
	}
	
	public function getSummaryData()
	{
		$current_location=$this->Employee->get_logged_in_employee_current_location_id();
		
		$this->db->select('sum(IFNULL('.$this->db->dbprefix('location_items').'.cost_price, '.$this->db->dbprefix('items').'.cost_price) * quantity) as inventory_total,
		sum(IFNULL('.$this->db->dbprefix('location_items').'.unit_price, '.$this->db->dbprefix('items').'.unit_price) * quantity) as inventory_sale_total', FALSE);
		$this->db->from('items');
		$this->db->join('location_items', 'location_items.item_id = items.item_id and location_id = '.$current_location, 'left');
		$this->db->join('suppliers', 'items.supplier_id = suppliers.person_id', 'left outer');
		$this->db->where('is_service !=', 1);
		$this->db->where('items.deleted', 0);
		
		if ($this->params['supplier'] != -1)
		{
			$this->db->where('suppliers.person_id', $this->params['supplier']);
		}
		return $this->db->get()->row_array();
	}
	
	function get_locations_stock_item_wise($item_id,$location){
	
		$this->db->from('location_items');
		$this->db->where('item_id',$item_id);
		$this->db->where('location_id',$location);
		$query = $this->db->get();

		if($query->num_rows()==1)
		{
			$row=$query->row();
			return $row->quantity;
		}
	
	}
	function get_item_wise_costprice($item_id){
	
		$this->db->from('items');
		$this->db->where('item_id',$item_id);
		//$this->db->where('location_id',$location);
		$query = $this->db->get();

		if($query->num_rows()==1)
		{
			$row=$query->row();
			return $row->cost_price;
		}
	
	}
	function get_item_data_for_deprication_calculation($item_id){
	
		$this->db->from('items');
		$this->db->where('item_id',$item_id);
		$this->db->where('deprication_status','active');
		$this->db->where('deprication_rate !=',"");
		$this->db->where('deprication_period !=',"");
		$this->db->where('deleted','0');
		$query = $this->db->get();
		
		if($query->num_rows()>0)
		{
			
			 $row=$query->result_array();
			 return ($row[0]);
		}
		return false;
	}
}
?>