<?php
	class Production_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
			
		}
		public function addProduction($postdata)
		{
			extract($postdata);
			$prd_date = date("Y-m-d", strtotime($prd_date));
			$prd_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$this->db->select('MAX(bno) as bno',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('billdetails')->row();
			$bno = $result->bno;
			if($bno == 0){
				$bno = 1;
			}
			else{
				$bno = $bno+1;
			}
			$bill_no = "BOM"."/".$branch_name."/".$bno;
			
			$k=0;
			for($j=0;$j<count($actual_yeild);$j++)
			{
				if($actual_yeild[$j]!='0' && $actual_yeild[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$data = array('bno'=> $bno,
			'bill_id'=> $bill_no,
			'pr_cat_id'=> $category,
			'orderdate'=> $prd_date,
			'ordertime'=> $prd_time,
			'chef'=> $chef,
			'outlet'=> $full_name,
			'branch_id'=> $branch_id
			);
			$this->db->insert('billdetails', $data);
			$bill_id = $this->db->insert_id();
			}
			
			if(isset($bill_id))
			{
			$count = count($pname);
			for($i=0;$i<$count;$i++)
			{
			    if($actual_yeild[$i]!='0' && $actual_yeild[$i]!='')
			    {
				$q1 = $this->db->query("select * from recipe where recipe_id='$pname[$i]'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$pstock[$i] = $row['production_stock'];
					$rc_yield[$i] = $row['yeild'];
					$pstock1[$i] = (float)$pstock[$i] + (float)$actual_yeild[$i];
					
					$rname[$i] = $row['rname'];
					$uoms[$i] = $row['uom'];
					$recipe_no[$i] = $row['recipe_no'];
					$recipe_id[$i] = $row['recipe_id'];
					$pr_cat_id[$i] = $row['pr_cat_id'];
					$q2 = $this->db->query("update recipe set production_stock='$pstock1[$i]' where recipe_id='$pname[$i]'");
				}
				$data2 = array(
					'pname' => $rname[$i],
					'uom' => $uoms[$i],
					'actual_yeild' => $actual_yeild[$i],
					'yeild' => $rc_yield[$i],
					'billno' => $bill_id,
					'recipe_id' => $recipe_id[$i],
					'recipe_no' => $recipe_no[$i],
					'pr_cat_id' => $pr_cat_id[$i]
					);
				$this->db->insert('bom_product', $data2);
				
				$data3 = array(
					'item_name' => $rname[$i],
					'trans_ref_no' => $bill_no,
					'trans_type'=>'BOM',
					'transaction_date' => $prd_date,
					'opening_stock' => $pstock[$i],
					'in_qty' => $actual_yeild[$i],
					'closing_stock' => $pstock1[$i],
					'last_modified' => $lm,
					'branchname' => $branch_name,
					'item_id' => $recipe_id[$i],
					'recipe_no' => $recipe_no[$i],
					'category_id' => $pr_cat_id[$i],
					'branch_id' => $branch_id
					);
				$this->db->insert('stock_detail', $data3);
				
				
				//consumption calculation
				//recipe
				$q2 = $this->db->query("select * from recipe_item where recipe_no='$recipe_no[$i]'");
				if($q2->num_rows() > 0)
				{
				    $res1 = $q2->result_array();
				    foreach($res1 as $rcres)
				    {
				        $rm_itemno = $rcres['itm_id'];
				        $rm_name = $rcres['item_name'];
				        $qtyused = $rcres['qty'];
				        $cons_no = "CONS"."/".$branch_name."/".$bno;
				        $q3 = $this->db->query("select * from item_creation where itm_id='$rm_itemno' and branch_id='$branch_id'");
				        if($q3->num_rows() > 0)
        				{
        				    $rcrow = $q3->row_array();
					        $rmstock = $rcrow['stock_item'];
					        $rmitemid = $rcrow['item_id'];
					        $rmcatid = $rcrow['category_id'];
					        $rmuom = $rcrow['uom'];
					        $rmrate = $rcrow['item_price'];
					        $rmconsumption = round((($qtyused*$actual_yeild[$i])/$rc_yield[$i]),2);
					        
					        $data4 = array(
            					'item_name' => $rm_name,
            					'trans_ref_no' => $cons_no,
            					'trans_type'=>'Consumption',
            					'transaction_date' => $prd_date,
            					'opening_stock' => $rmstock,
            					'closing_stock' => $rmstock,
            					'consumption_qty' => $rmconsumption,
            					'last_modified' => $lm,
            					'branchname' => $branch_name,
            					'item_id' => $rmitemid,
            					'itm_id' => $rm_itemno,
            					'category_id' => $rmcatid,
            					'branch_id' => $branch_id
            				);
            				$this->db->insert('stock_detail', $data4);
					    }
				    }
				}
				
				//subrecipe
				$q2 = $this->db->query("select * from sub_recipe_item where rc_no='$recipe_no[$i]'");
				if($q2->num_rows() > 0)
				{
				    $res1 = $q2->result_array();
				    foreach($res1 as $rcres)
				    {
				        $rc_itemno = $rcres['recipe_no'];
				        $rc_name = $rcres['item_name'];
				        $qtyused = $rcres['qty'];
				        $cons_no = "CONS"."/".$branch_name."/".$bno;
				        $q3 = $this->db->query("select * from recipe where recipe_no='$rc_itemno' and branch_id='$branch_id'");
				        if($q3->num_rows() > 0)
        				{
        				    $rcrow = $q3->row_array();
					        $rcstock = $rcrow['production_stock'];
					        $rcitemid = $rcrow['recipe_id'];
					        $rccatid = $rcrow['pr_cat_id'];
					        $rcuom = $rcrow['uom'];
					        $rcrate = $rcrow['recipe_price'];
					        $rcconsumption = round((($qtyused*$actual_yeild[$i])/$rc_yield[$i]),2);
					        
					        $data4 = array(
            					'item_name' => $rc_name,
            					'trans_ref_no' => $cons_no,
            					'trans_type'=>'Consumption',
            					'transaction_date' => $prd_date,
            					'opening_stock' => $rcstock,
            					'closing_stock' => $rcstock,
            					'consumption_qty' => $rcconsumption,
            					'last_modified' => $lm,
            					'branchname' => $branch_name,
            					'item_id' => $rcitemid,
            					'recipe_no' => $rc_itemno,
            					'category_id' => $rccatid,
            					'branch_id' => $branch_id
            				);
            				$this->db->insert('stock_detail', $data4);
					    }
				    }
				}
			}
			}
			return $bill_id;
			}
		}
		
		public function getProduction($bid)
		{
			$query = $this->db->query("SELECT *,DATE_FORMAT(orderdate, '%d-%m-%Y') as orderdate FROM billdetails where billno='$bid'");
			return $query->result();	
		}
		
		public function getProductionItem($bid)
		{
			$query = $this->db->query("SELECT * FROM  bom_product where billno='$bid'");
			return $query->result();	
		}
		
		public function getProductionList($branch_id)
		{
			$query = $this->db->query("select *,DATE_FORMAT(orderdate, '%d-%m-%Y') as orderdate from billdetails where branch_id='$branch_id' order by billno desc");
			return $query->result();	
		}
		
		
		
		public function get_RcpDetails($iname)
    	{
    		$br_id=$_SESSION['branch_id'];;
    		$query = $this->db->query("select rname,uom,price,tax,recipe_id,pr_cat_id,recipe_no,yeild,production_stock from recipe where recipe_id='$iname' and branch_id='$br_id'");//20 4
    		$row=$query->row_array();
    		if($row)
    		{
    			$price = $row['price'];
    			$tax = $row['tax'];
    			$tax_a = ($price * $tax)/100;
    			$tot_price = $price + $tax_a;
    			$data=array("rname" => $row['rname'], "uom" => $row['uom'], "price" => $tot_price, "recipe_id" => $row['recipe_id'], "pr_cat_id" => $row['pr_cat_id'], "recipe_no" => $row['recipe_no'], "yeild" => $row['yeild'], "aprice"=> $row['price'], "tax"=> $row['tax'], "stock"=> $row['production_stock']);
    			echo json_encode($data);
    		}
    		else
    		{
    			$data = array("uom" => '0');
    			echo json_encode($data);
    		}
    	}
    	public function getCatRecipe(){
    	    $branch_id=3;
			$query = $this->db->query("select * from product_category p inner join recipe r on p.pr_cat_id=r.pr_cat_id where r.branch_id='$branch_id' and r.status='Enabled' order by rname asc");
			return $query->result();
		}
		
		public function get_product($code,$branch_id)
        {
            //$this->db->distinct();
            $this->db->select('rname,recipe_id');
            $this->db->from('recipe');
            $this->db->where('pr_cat_id',$code);
            $this->db->where('branch_id',$branch_id);
            $query = $this->db->get();
            return $query->result();
        }
		
        public function addBOM($postdata)
		{
			extract($postdata);
			$prd_date = date("Y-m-d", strtotime($prd_date));
			$prd_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$this->db->select('MAX(bno) as bno',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('billdetails')->row();
			$bno = $result->bno;
			if($bno == 0){
				$bno = 1;
			}
			else{
				$bno = $bno+1;
			}
			$bill_no = "BOM"."/".$branch_name."/".$bno;
			
			$k=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$data = array('bno'=> $bno,
			'bill_id'=> $bill_no,
			'pr_cat_id'=> $category,
			'recipe_id'=>$pname,
			'recipe_no'=>$rc_no,
			'pname'=>$names,
			'uom'=>$uom,
			'actual_yeild'=>$actual_yeild,
			'yeild'=>$yeild,
			'actual_batch'=>$actual_batch,
			'batch'=>$batch,
			'total'=>$tot,
			'orderdate'=> $prd_date,
			'ordertime'=> $prd_time,
			'chef'=> $chef,
			'outlet'=> $full_name,
			'branch_id'=> $branch_id,
			'kitchen_status'=>'Pending',
			'store_status'=>'Pending'
			);
			$this->db->insert('billdetails', $data);
			$bill_id = $this->db->insert_id();
			}
			
			if($bill_id)
			{
			$count = count($name);
			for($i=0;$i<$count;$i++)
			{
			 
				$data2 = array(
					'iname' => $name[$i],
					'quantity' => $quantity[$i],
					'iuom' => $iuom[$i],
					'price' => $price[$i],
					'billno' => $bill_id,
					'item_id' => $item_id[$i],
					'itm_id'=>$it_id[$i]
				);
				$this->db->insert('bproduct', $data2);
		        
			}
			$count1 = count($sub_item_id);
			for($j=0;$j<$count1;$j++)
			{
    			if($sub_item_id[$j]=='0'){
    		           // echo 'j';
		        }
		        else{
		           // echo 'l';
    			    $data3 = array(
    					'iname' => $sub_name[$j],
    					'quantity' => $sub_quantity[$j],
    					'iuom' => $sub_uom[$j],
    					'price' => $sub_price[$j],
    					'billno' => $bill_id,
    					'recipe_id' => $sub_item_id[$j],
    					'recipe_no'=> $sub_rc_id[$i]
    				);
    				$this->db->insert('bproduct', $data3);
		        }
			}
		}
			return $bill_id;
    	}
    	
    	public function BomList()
        {
            $query = $this->db->query("select *,DATE_FORMAT(orderdate, '%d-%m-%Y') as orderdate from billdetails where store_status='Pending' order by billno desc");
            return $query->result();
        }
        
        public function BomVList($bid)
        {
            $query = $this->db->query("select * from billdetails b inner join bproduct bp on b.billno=bp.billno where b.billno='$bid'");
            return $query->result();
        }
        
        public function getCProductionList(){
            $branch_name = $_SESSION['branch_name'];
            $cw_ceo = $_SESSION['cw_ceo'];
			$query = $this->db->query("select *,DATE_FORMAT(orderdate, '%d-%m-%Y') as orderdate from billdetails order by billno desc");
			return $query->result();
		}
		
		public function searchCProduction($data)
		{
			$f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$query = $this->db->query("SELECT *, DATE_FORMAT(orderdate, '%d-%m-%Y') as orderdate FROM billdetails where orderdate between '$f_date' and '$t_date' order by billno desc");
			return $query->result_array();
		}
	}
?>