<?php
	class PstockManage_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
			
		}
		
		public function getPresentStock()
		{
			$branch = $_SESSION['branch_name'];
			$query = $this->db->query("SELECT * FROM recipe R inner join product_category C on R.pr_cat_id=C.pr_cat_id where R.branchname='$branch' and R.status='Enabled' ORDER BY rname asc");
			return $query->result();
		}		
		public function getCategory() {
			$query = $this->db->query("SELECT * FROM product_category");
			return $query->result();
		}

		public function getPrStock($data)
		{
			$branch = $data['branch'];
			$query = $this->db->query("SELECT * FROM recipe R inner join product_category C on R.pr_cat_id=C.pr_cat_id where R.branchname='$branch' and R.status='Enabled' ORDER BY rname asc");
			return $query->result();
		}

		public function getCStockLedger($data)
		{
			$f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$branch = $data['branch'];
			$iname = $data['item_name'];
			$query = $this->db->query("SELECT *,DATE_FORMAT(transaction_date, '%d-%m-%Y') as trans_date from stock_detail where item_name='$iname' and branchname='$branch' and (transaction_date between '$f_date' and '$t_date') and recipe_no!='' order by stock_id asc");
			return $query->result_array();
		}
		
		public function searchCStockMovement($data){
		    $f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$branch = $data['branch'];
		    $query = $this->db->query("select s2.item_name,s2.opening_stock,s2.inward,s2.outward,s3.closing_stock from (select opening_stock,item_name,recipe_no,transaction_date,sum(in_qty)as inward,sum(out_qty) as outward,branchname from stock_detail where branchname='$branch' and (transaction_date between '$f_date' and '$t_date') group by recipe_no) s2 inner join (select * from stock_detail where branchname='$branch') s3 on s2.recipe_no=s3.recipe_no inner join (select item_name,recipe_no,transaction_date,closing_stock,max(last_modified) as maxdate,branchname from stock_detail where branchname='$branch' and (transaction_date between '$f_date' and '$t_date') group by recipe_no) s4 on s3.recipe_no=s4.recipe_no and s3.last_modified=s4.maxdate");
		    return $query->result_array();
		}
	}
?>