<?php
    #[\AllowDynamicProperties]
	class Productrequest_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
		}
		var $bill_year=bill_year;
		var $invoice_type = invoice_type;
		var $invoice_no = invoice_no;
		var $dc_invoice_type = dc_invoice_type;
		public function getKitchen($sub_mod_id,$branch_id)
		{
			$query = $this->db->query("SELECT * FROM user_access where branch_id='$branch_id' and module_id='$sub_mod_id'");
			return $query->result();	
		}
		
		public function fetchCategory($dept,$smd_id)
		{
			$query=$this->db->query("SELECT * FROM user_access where access_uid='$dept' and module_id='$smd_id' and access_category!=''");
			$result = $query->result();
			return $result;
		}
		
	/*	public function fetchProduct($cat,$branch_id,$dept)
		{
			$query=$this->db->query("select * from recipe where pr_cat_id='$cat' and branch_id='$branch_id' and product_indent like '%$dept%' and status='Enabled' order by rname asc");
			$result = $query->result();
			return $result;
		} */
		
		public function fetchProduct($cat,$branch_id,$dept)
    	{
    	    $this->db2 = $this->load->database('sales_demo', TRUE);
    	    $this->db3 = $this->load->database('otherdb', TRUE);
    	    $br_code = $_SESSION['branch_code'];
    		$query = $this->db->query("select * from recipe where pr_cat_id='$cat' and branch_id='$branch_id' and product_indent like '%$dept%' and status='Enabled' order by rname asc");
    		$row=$query->result();
    		if($row)
    		{
        		foreach ($row as $item)
        		{
        		    $mrno = $item->map_recipe_no;
        		    $cdate = date('Y-m-d');
    				$fdate = date('Y-m-d',strtotime("-15 days"));
    				$tdate = date('Y-m-d',strtotime("-1 days"));
    				$lwdate = date('Y-m-d',strtotime("-6 days"));
        		   
        		   if($dept == '33')
        		   {
            		   $q1 = $this->db->query("SELECT branch_code,branch_id from user where branch_code IS NOT NULL");
            		   $branches = $q1->result_array();
                        $count = 1;
                    	foreach($branches as $br)
                    	{
                    	    $branch1 = $br['branch_id'];
                    	    if($branch_id == $branch1)
                    		{
                    		    $pr_table = 'product_billdetail'.$count;
        			            $bp_table = 'billed_product'.$count;
        			            $q2 = $this->db2->query("SELECT B.product_name,AVG(B.quantity) as avg_sales FROM $pr_table A inner join $bp_table B on A.bill_id=B.bill_id WHERE A.invoice_date BETWEEN '$fdate' and '$tdate' and A.bno='0' and B.product_no='$mrno'");
        			            $res = $q2->row_array();
    							$avg_sales = round($res['avg_sales'],2);
    							$pr_name = $res['product_name'];
    							
    							$q3 = $this->db3->query("SELECT AVG(B.quantity) as avg_sales FROM social_orders A inner join items_orders_social B on A.socialorder_id=B.bill_id WHERE A.orders_date BETWEEN '$fdate' and '$tdate' and A.order_state='Success' and A.branch='$br_code' and B.product_name='$pr_name'");
        			            $res2 = $q3->row_array();
        			            $olavg_sales = round($res2['avg_sales'],2);
        			            $tot_avg_sales = $avg_sales + $olavg_sales;
    							
    							$q4 = $this->db2->query("SELECT sum(B.quantity) as lw_sales FROM $pr_table A inner join $bp_table B on A.bill_id=B.bill_id WHERE A.invoice_date='$lwdate' and A.bno='0' and B.product_no='$mrno'");
        			            $res3 = $q4->row_array();
    							$lw_sales = round($res3['lw_sales'],2);
    							
    							$q5 = $this->db3->query("SELECT sum(B.quantity) as lw_sales FROM social_orders A inner join items_orders_social B on A.socialorder_id=B.bill_id WHERE A.orders_date='$lwdate' and A.order_state='Success' and A.branch='$br_code' and B.product_name='$pr_name'");
        			            $res4 = $q5->row_array();
    							$ollw_sales = round($res4['lw_sales'],2);
    							$tot_lw_sales = $lw_sales + $ollw_sales;
                    		}
                    		$count++;
                    	}
            		    
            			$data[] = array(
            			"iname"          => $item->rname,
            			"uom"  			=> $item->uom,
            			"cat_id"          => $item->pr_cat_id,
            			"flavour"  			=> $item->flavour,
            			"f_code"          => $item->f_code,
            			"id"  			=> $item->recipe_id,
            			"qty"          => $item->req_qty,
            			"rno"  			=> $item->recipe_no,
            			"avg_qty"  			=> $tot_avg_sales,
            			"lw_qty"  			=> $tot_lw_sales
            			);
            		}
            		else
            		{
            		    $data[] = array(
            			"iname"          => $item->rname,
            			"uom"  			=> $item->uom,
            			"cat_id"          => $item->pr_cat_id,
            			"flavour"  			=> $item->flavour,
            			"f_code"          => $item->f_code,
            			"id"  			=> $item->recipe_id,
            			"qty"          => $item->req_qty,
            			"rno"  			=> $item->recipe_no,
            			"avg_qty"  			=> 'NA',
            			"lw_qty"  			=> 'NA'
            			);
            		}
        		}
        		echo json_encode($data);
    		}
    		else
    		{
    			$data = '';
    			echo json_encode($data);
    		}
    	}
		
		public function fetchStProduct($branch_id,$dept)
		{
			$query=$this->db->query("select * from recipe where branch_id='$branch_id' and product_indent='$dept' and status='Enabled'");
			$result = $query->result();
			return $result;
		}		
				
		public function addRequest($postdata)
		{
			extract($postdata);
			$req_date = date("Y-m-d", strtotime($req_date));
			$cur_date = date("Y-m-d");
			$order_time = date("h:i A");
			if($orders == 'Special Order')
			{
			    $delivery_date = date("Y-m-d", strtotime($delivery_date));
			}
			else if($orders == 'Running Order')
			{
			    $delivery_date = '';
			    $delivery_time = '';
			    $ord_remark = '';
			}
			if($orders == 'Running Order' || $orders == 'Special Order')
			{
			    $this->db->select('MAX(req_no) as req_no',false);
				$this->db->where('branch_id',$branch_id);
				$result = $this->db->get('product_request')->row();
				$req_no = $result->req_no;
				if($req_no == 0){
					$req_no = 1;
				}
				else{
					$req_no = $req_no+1;
				}
				$order_no = $branch_name."/PR-REQ/".$req_no;
				
				$k=0;
    			for($j=0;$j<count($quantity);$j++)
    			{
    				if($quantity[$j]!='0' && $quantity[$j]!='')
    				{
    					$k=$k+1;
    				}
    			}
    			if($k>0)
			{
				$data = array('req_no'=> $req_no,
					'pr_req_no'=> $order_no,
					'request_date'=> $req_date,
					'request_time'=> $order_time,
					'delivery_date'=> $delivery_date,
					'delivery_time'=> $delivery_time.':00',
					'dept'=> $dept1,
					'orders'=> $orders,
					'branchname'=> $branch_name,
					'status'=> 'Pending',
					'order_remarks'=> $ord_remark,
					'branch_id'=> $branch_id
				);
				$this->db->insert('product_request', $data);
				$rqid = $this->db->insert_id();
			}
				if(isset($rqid))
				{
				$count = count($product_name);
				for($i=0;$i<$count;$i++)
				{
					if($quantity[$i] != '0' && $quantity[$i] != '')
					{
						$cat_name = $cat_id[$i];
						$data2 = array(
							'item_name' => $product_name[$i],
							'uom' => $uom[$i],
							'required_qty' => $quantity[$i],
							'remarks' => $remarks[$i],
							'request_id' => $rqid,
							'recipe_id' => $item_id[$i],
							'pr_cat_id' => $cat_id[$i],
							'flavour' => $flavour[$i],
							'f_code' => $f_code[$i],
							'status' => 'Pending',
							'receipt_status' => 'Pending',
							'recipe_no' => $recipe_no[$i]
							);
						$this->db->insert('product_request_item', $data2);
					}
				}
			}
			}
			else if($orders == 'Regular Order')
			{
				$q1 = $this->db->query("select * from product_request where request_date='$cur_date' and orders='Regular Order' and receipt_status='Pending' and dept='$dept1' and branchname='$branch_name'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$rqid = $row['request_id'];
					
					$count = count($product_name);
					for($i=0;$i<$count;$i++)
					{
						if($quantity[$i] != '0' && $quantity[$i] != '')
						{
							$cat_name = $cat_id[$i];
							$q2 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");
							if($q2->num_rows() > 0)
							{
								$row2 = $q2->row_array();
								$req_item_id[$i] = $row2['request_item_id'];
								$qty[$i] = $row2['required_qty'];
								$qty1[$i] = $qty[$i] + $quantity[$i];
								
								$q3 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
							}
							else
							{
								$data3 = array(
									'item_name' => $product_name[$i],
									'uom' => $uom[$i],
									'required_qty' => $quantity[$i],
									'remarks' => $remarks[$i],
									'request_id' => $rqid,
									'recipe_id' => $item_id[$i],
									'pr_cat_id' => $cat_id[$i],
									'flavour' => $flavour[$i],
									'f_code' => $f_code[$i],
									'status' => 'Pending',
									'receipt_status' => 'Pending',
									'recipe_no' => $recipe_no[$i]
									);
								$this->db->insert('product_request_item', $data3);
							}
						}
					}
				}
				else
				{
					$this->db->select('MAX(req_no) as req_no',false);
					$this->db->where('branch_id',$branch_id);
					$result = $this->db->get('product_request')->row();
					$req_no = $result->req_no;
					if($req_no == 0){
						$req_no = 1;
					}
					else{
						$req_no = $req_no+1;
					}
					$order_no = $branch_name."/PR-REQ/".$req_no;
					
					$k=0;
        			for($j=0;$j<count($quantity);$j++)
        			{
        				if($quantity[$j]!='0' && $quantity[$j]!='')
        				{
        					$k=$k+1;
        				}
        			}
        			if($k>0)
			        {
					$data4 = array('req_no'=> $req_no,
						'pr_req_no'=> $order_no,
						'request_date'=> $req_date,
						'request_time'=> $order_time,
						'dept'=> $dept1,
						'orders'=> $orders,
						'branchname'=> $branch_name,
						'status'=> 'Pending',
						'branch_id'=> $branch_id
					);
					$this->db->insert('product_request', $data4);
					$rqid = $this->db->insert_id();
			        }
				if($rqid)
				{
					$count = count($product_name);
					for($i=0;$i<$count;$i++)
					{
						if($quantity[$i] != '0' && $quantity[$i] != '')
						{
							$cat_name = $cat_id[$i];
							$q3 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");
							if($q3->num_rows() > 0)
							{
								$row3 = $q3->row_array();
								$req_item_id[$i] = $row3['request_item_id'];
								$qty[$i] = $row3['required_qty'];
								$qty1[$i] = $qty[$i] + $quantity[$i];
								
								$q4 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
							}
							else
							{
								$data5 = array(
									'item_name' => $product_name[$i],
									'uom' => $uom[$i],
									'required_qty' => $quantity[$i],
									'remarks' => $remarks[$i],
									'request_id' => $rqid,
									'recipe_id' => $item_id[$i],
									'pr_cat_id' => $cat_id[$i],
									'flavour' => $flavour[$i],
									'f_code' => $f_code[$i],
									'status' => 'Pending',
									'receipt_status' => 'Pending',
									'recipe_no' => $recipe_no[$i]
									);
									$this->db->insert('product_request_item', $data5);
							}
						}
					}
				}
				}
			}
			$q5 = $this->db->query("update recipe set req_qty='' where branchname='$branch_name' and pr_cat_id='$cat_name'");
			if(isset($rqid))
			{
			return $rqid;
			}
		}
		
		public function addStRequest($postdata)
		{
			extract($postdata);
			$req_date = date("Y-m-d", strtotime($req_date));
			$cur_date = date("Y-m-d");
			$order_time = date("h:i A");
			
			if($orders == 'Santhe Special Order')
			{
			    $delivery_date = date("Y-m-d", strtotime($delivery_date));
				$this->db->select('MAX(req_no) as req_no',false);
				$this->db->where('branch_id',$branch_id);
				$result = $this->db->get('product_request')->row();
				$req_no = $result->req_no;
				if($req_no == 0){
					$req_no = 1;
				}
				else{
					$req_no = $req_no+1;
				}
				$order_no = $branch_name."/PR-REQ/".$req_no;
				
				$k=0;
    			for($j=0;$j<count($quantity);$j++)
    			{
    				if($quantity[$j]!='0' && $quantity[$j]!='')
    				{
    					$k=$k+1;
    				}
    			}
    			if($k>0)
    			{
				$data = array('req_no'=> $req_no,
					'pr_req_no'=> $order_no,
					'request_date'=> $req_date,
					'request_time'=> $order_time,
					'delivery_date'=> $delivery_date,
					'delivery_time'=> $delivery_time.':00',
					'dept'=> $dept1,
					'orders'=> $orders,
					'branchname'=> $branch_name,
					'status'=> 'Pending',
					'order_remarks'=> $ord_remark,
					'branch_id'=> $branch_id
				);
				$this->db->insert('product_request', $data);
				$rqid = $this->db->insert_id();
    			}
				if($rqid)
				{
				$count = count($product_name);
				for($i=0;$i<$count;$i++)
				{
					if($quantity[$i] != '0' && $quantity[$i] != '')
					{
						$cat_name = $cat_id[$i];
						$data2 = array(
							'item_name' => $product_name[$i],
							'uom' => $uom[$i],
							'required_qty' => $quantity[$i],
							'remarks' => $remarks[$i],
							'request_id' => $rqid,
							'recipe_id' => $item_id[$i],
							'pr_cat_id' => $cat_id[$i],
							'flavour' => $flavour[$i],
							'f_code' => $f_code[$i],
							'status' => 'Pending',
							'receipt_status' => 'Pending',
							'recipe_no' => $recipe_no[$i]
							);
						$this->db->insert('product_request_item', $data2);
					}
				}
			}
			}
			else if($orders == 'Santhe')
			{
			    
			    $order_day = date("l");
				if($order_day == 'Sunday')
    				{
    					$res1 = $this->db->query("select * from product_request where request_date='$req_date' and dept='$dept1' and receipt_status='Pending' and orders='$orders' and branchname='$branch_name'");
    					
    					if($res1->num_rows() > 0)
    					{
    						$row1 = $res1->row_array();
    						$req_id = $row1['request_id'];
    						$rqid = $row1['request_id'];
    						
    						$this->db->query("update product_request set requested_date='$req_date' where request_id='$rqid'");
    						for($i=0;$i<count($product_name);$i++) 
    						{
    							$cat_name = $cat_id[$i];
    							$res2 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");

    							if($res2->num_rows() > 0)
    							{	
    								$row2 = $res2->row_array();
    								$req_item_id[$i] = $row2['request_item_id'];
    								$qty[$i] = $row2['required_qty'];
    								$qty1[$i] = $qty[$i] + $quantity[$i];
    								$sql2 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
    								
    							}
    							else
    							{
    								if($quantity[$i] != '0' && $quantity[$i] != '')
    								{
    									$sql2 =  $this->db->query("insert into product_request_item(item_name,uom,required_qty,remarks,request_id,recipe_id,pr_cat_id,status,receipt_status,recipe_no) values ('$product_name[$i]','$uom[$i]','$quantity[$i]','$remarks[$i]','$rqid','$item_id[$i]','$cat_id[$i]','Pending','Pending','$recipe_no[$i]')");
    								}
    							}
    						}
    						$sql3 = $this->db->query("update recipe set req_qty='' where branchname='$branch_name' and pr_cat_id='$cat_name'");
    					}
    					else
    					{
    						$res = $this->db->query("SELECT MAX(req_no) FROM product_request where branchname='$branch_name'");
    						if($res->num_rows() > 0)
    						{
    							$rows = $res->row_array();
    						   							
    							if($rows['MAX(req_no)'] == 0) {
    								
    								$req_no = 1;
    							}
    							else {
    								$req_no = $rows['MAX(req_no)'] + 1;
    							} 
    							
    							//$branch_name = $_SESSION['branch_name'];
    							$order_no = $branch_name."/PR-REQ/".$req_no;
    						}
    						
    						$k=0;
                			for($j=0;$j<count($quantity);$j++)
                			{
                				if($quantity[$j]!='0' && $quantity[$j]!='')
                				{
                					$k=$k+1;
                				}
                			}
                			if($k>0)
                			{
    						$sql1 = $this->db->query("insert into product_request(req_no,pr_req_no,request_date,request_time,requested_date,dept,orders,branchname,status,day_status,branch_id) values ('$req_no','$order_no','$req_date','$order_time','$req_date','$dept1','$orders','$branch_name','Pending','Open','$branch_id')");
    						$rqid = $this->db->insert_id();
    					}
    					if($rqid)
    					{
    						for($i=0;$i<count($product_name);$i++) {
    							$cat_name = $cat_id[$i];
    							if($quantity[$i] != '0' && $quantity[$i] != '')
    							{
    								$sql2 =  $this->db->query("insert into product_request_item(item_name,uom,required_qty,remarks,request_id,recipe_id,pr_cat_id,status,receipt_status,recipe_no) values ('$product_name[$i]','$uom[$i]','$quantity[$i]','$remarks[$i]','$rqid','$item_id[$i]','$cat_id[$i]','Pending','Pending','$recipe_no[$i]')");
    							}
    						}
    					}
    						$sql3 = $this->db->query("update recipe set req_qty='' where branchname='$branch_name' and pr_cat_id='$cat_name'");
    					}
    				}
    				else{
    				
    					$day = date('w');
    					$week_start = date('Y-m-d', strtotime('-'.$day.' days'));
    					$res8 = $this->db->query("SELECT * FROM product_request where dept='$dept1' and branchname='$branch_name' and receipt_status='Pending' and orders='$orders'  and request_date='$week_start' ");
    					if($res8->num_rows() > 0)
    					{	
    						///$row2 = $res2->row_array();
    					
    					
    					$res = $this->db->query("SELECT MAX(request_id) FROM product_request where dept='$dept1' and orders='$orders' and branchname='$branch_name' ");
    					
    						$rows = $res->row_array();
    						//$rows = mysqli_fetch_array($res);
    						$rqid= $rows['MAX(request_id)'];
    						//echo $req_id;
    						
    						$sql3 = $this->db->query("update product_request set requested_date='$req_date' where request_id='$rqid'");
    						//$sql1 = mysqli_query($this->con,"insert into product_request(req_no,pr_req_no,request_date,dept,branchname,status) values ('$req_no','$order_no','$order_date','$dept','$branchname','Pending')");
    						//	$req_id = mysqli_insert_id($this->con);
    						for($i=0;$i<count($product_name);$i++) {
    							$cat_name = $cat_id[$i];
    							$res3 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");
    							if($res3->num_rows() > 0)
    							{
    								$row3 = $res3->row_array();
    								
    								$req_item_id[$i] = $row3['request_item_id'];
    								$qty[$i] = $row3['required_qty'];
    								$qty1[$i] = $qty[$i] + $quantity[$i];
    								$sql2 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
    							}
    							else
    							{
    								if($quantity[$i] != '0' && $quantity[$i] != '')
    								{
    									$sql2 =  $this->db->query("insert into product_request_item(item_name,uom,required_qty,remarks,request_id,recipe_id,pr_cat_id,status,receipt_status,recipe_no) values ('$product_name[$i]','$uom[$i]','$quantity[$i]','$remarks[$i]','$rqid','$item_id[$i]','$cat_id[$i]','Pending','Pending','$recipe_no[$i]')");
    								}
    							}
    						}
    						$sql3 = $this->db->query("update recipe set req_qty='' where branchname='$branch_name' and pr_cat_id='$cat_name'");
    					}
    					else
    					{
    						$res8 = $this->db->query("SELECT MAX(req_no) FROM product_request where branchname='$branch_name'");
    						//$rowc2 = mysqli_num_rows($res8);
    						//echo $rowc;
    						if($res8->num_rows() > 0)
							{
								$rows8 = $res8->row_array();
    						 	
    							
    							if($rows8['MAX(req_no)'] == '') {
    								
    								$req_no = 1;
    							}
    							else {
    								$req_no = $rows8['MAX(req_no)'] + 1;
    							} 
    							
    							//$branch_name = $_SESSION['branch_name'];
    							$order_no = $branch_name."/PR-REQ/".$req_no;
    						}
    					//	echo $order_no;
    						$k=0;
                			for($j=0;$j<count($quantity);$j++)
                			{
                				if($quantity[$j]!='0' && $quantity[$j]!='')
                				{
                					$k=$k+1;
                				}
                			}
                			if($k>0)
                			{
    						$sql1 = $this->db->query("insert into product_request(req_no,pr_req_no,request_date,request_time,requested_date,orders,dept,branchname,status,day_status,branch_id) values ('$req_no','$order_no','$week_start','$order_time','$req_date','$orders','$dept1','$branch_name','Pending','Open','$branch_id')");
    						$rqid = $this->db->insert_id();
    					}
    						if($rqid)
    						{
    						for($i=0;$i<count($product_name);$i++) {
    							$cat_name = $cat_id[$i];
    							if($quantity[$i] != '0' && $quantity[$i] != '')
    							{
    								$sql2 =  $this->db->query("insert into product_request_item(item_name,uom,required_qty,remarks,request_id,recipe_id,pr_cat_id,status,receipt_status,recipe_no) values ('$product_name[$i]','$uom[$i]','$quantity[$i]','$remarks[$i]','$rqid','$item_id[$i]','$cat_id[$i]','Pending','Pending','$recipe_no[$i]')");
    							}
    						}
    					}
    						$sql3 = $this->db->query("update recipe set req_qty='' where branchname='$branch_name' and pr_cat_id='$cat_name'");
    					}
    				}
			    /*$delivery_date = '';
			    $delivery_time = '';
			    $ord_remark = '';
				//$q1 = $this->db->query("select * from product_request where request_date='$cur_date' and orders='Regular Order' and receipt_status='Pending' and dept='$dept1' and branchname='$branch_name'");
				$q1 = $this->db->query("select * from product_request where request_date='$cur_date' and orders='Regular Order' and receipt_status='Pending' and dept='$dept1' and branchname='$branch_name'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$rqid = $row['request_id'];
					
					$count = count($product_name);
					for($i=0;$i<$count;$i++)
					{
						if($quantity[$i] != '0' && $quantity[$i] != '')
						{
							$cat_name = $cat_id[$i];
							$q2 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");
							if($q2->num_rows() > 0)
							{
								$row2 = $q2->row_array();
								$req_item_id[$i] = $row2['request_item_id'];
								$qty[$i] = $row2['required_qty'];
								$qty1[$i] = $qty[$i] + $quantity[$i];
								
								$q3 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
							}
							else
							{
								$data3 = array(
									'item_name' => $product_name[$i],
									'uom' => $uom[$i],
									'required_qty' => $quantity[$i],
									'remarks' => $remarks[$i],
									'request_id' => $rqid,
									'recipe_id' => $item_id[$i],
									'pr_cat_id' => $cat_id[$i],
									'flavour' => $flavour[$i],
									'f_code' => $f_code[$i],
									'status' => 'Pending',
									'receipt_status' => 'Pending',
									'recipe_no' => $recipe_no[$i]
									);
								$this->db->insert('product_request_item', $data3);
							}
						}
					}
				}
				else
				{
					$this->db->select('MAX(req_no) as req_no',false);
					$this->db->where('branch_id',$branch_id);
					$result = $this->db->get('product_request')->row();
					$req_no = $result->req_no;
					if($req_no == 0){
						$req_no = 1;
					}
					else{
						$req_no = $req_no+1;
					}
					$order_no = $branch_name."/PR-REQ/".$req_no;
					
					$data4 = array('req_no'=> $req_no,
						'pr_req_no'=> $order_no,
						'request_date'=> $req_date,
						'request_time'=> $order_time,
						'dept'=> $dept1,
						'orders'=> $orders,
						'branchname'=> $branch_name,
						'status'=> 'Pending',
						'branch_id'=> $branch_id
					);
					$this->db->insert('product_request', $data4);
					$rqid = $this->db->insert_id();
					
					$count = count($product_name);
					for($i=0;$i<$count;$i++)
					{
						if($quantity[$i] != '0' && $quantity[$i] != '')
						{
							$cat_name = $cat_id[$i];
							$q3 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");
							if($q3->num_rows() > 0)
							{
								$row3 = $q3->row_array();
								$req_item_id[$i] = $row3['request_item_id'];
								$qty[$i] = $row3['required_qty'];
								$qty1[$i] = $qty[$i] + $quantity[$i];
								
								$q4 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
							}
							else
							{
								$data5 = array(
									'item_name' => $product_name[$i],
									'uom' => $uom[$i],
									'required_qty' => $quantity[$i],
									'remarks' => $remarks[$i],
									'request_id' => $rqid,
									'recipe_id' => $item_id[$i],
									'pr_cat_id' => $cat_id[$i],
									'flavour' => $flavour[$i],
									'f_code' => $f_code[$i],
									'status' => 'Pending',
									'receipt_status' => 'Pending',
									'recipe_no' => $recipe_no[$i]
									);
									$this->db->insert('product_request_item', $data5);
							}
						}
					}
				}*/
			}
			$q5 = $this->db->query("update recipe set req_qty='' where branchname='$branch_name' and pr_cat_id='$cat_name'");
			
			return $rqid;
		}
		
		public function getPrdRequest($rid)
		{
			$query = $this->db->query("SELECT *,DATE_FORMAT(request_date, '%d-%m-%Y') as request_date FROM product_request where request_id='$rid'");
			return $query->result();	
		}
		
		public function getPrdRequestItem($rid)
		{
			$query = $this->db->query("SELECT * FROM  product_request_item where request_id='$rid'");
			return $query->result();	
		}
		
		public function getRequestList($branch_id)
		{
			$branch_name = $_SESSION['branch_name'];
			
			     $query = $this->db->query("SELECT *, DATE_FORMAT(request_date, '%d-%m-%Y') as request_date FROM product_request where branch_id='$branch_id' and receipt_status='Pending' ORDER BY request_id desc");
		   
			return $query->result();	
		}
		
		public function get_accessTab($sub_mod_id,$branch_id)
		{
			$query = $this->db->query("SELECT * FROM user_access where branch_id='$branch_id' and module_id='$sub_mod_id'");
			return $query->result();
		}

		public function getCounters($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$query = $this->db->query("select request_id,branchname from product_request where dept='$uname' and orders='Regular Order' and request_date='$r_date' and status='Pending' and branchname not like '%ESH%' and branchname not like '%STR%' order by branchname");
			return $query->result();
		}
		
		public function getVkitchens($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$query = $this->db->query("select request_id,branchname,to_branch,to_branchid from product_request where dept='$uname' and orders='Carnival Order' and request_date='$r_date' and status='Pending' and branchname not like '%ESH%' order by branchname");
			return $query->result();
		}
		
		public function getECounters($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$query = $this->db->query("select request_id,branchname from product_request where dept='$uname' and orders='Regular Order' and request_date='$r_date' and status='Pending' and branchname like '%ESH%' order by branchname");
			return $query->result();
		}
		
		public function getStrCounters($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$query = $this->db->query("select request_id,branchname from product_request where dept='$uname' and orders='Regular Order' and request_date='$r_date' and status='Pending' and branchname like '%STR%' order by branchname");
			return $query->result();
		}
		
		public function getStCounters($data)
		{
			function last_nday($date) {
				if (!is_numeric($date))
					$date = strtotime($date);
				if (date('w', $date) == 0)
					return $date;
				elseif (date('w', $date) == 0)
					return strtotime(
						'next sunday',
						 $date
					);
				else
					return strtotime(
						'last sunday',
						 $date
					);
			}

			$r_date = date('Y-m-d', last_nday($data['req_date']));
			//$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$query = $this->db->query("select request_id,branchname from product_request where dept='$uname' and orders='Santhe' and request_date='$r_date' and status='Pending' order by branchname");
			return $query->result();
		}
		
		public function getIndentItemsQty($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
            $query = $this->db->query("select PI.item_name,PI.f_code,SUM(CASE WHEN P.branchname = 'CW-CTR' THEN PI.required_qty ELSE 0 END) ctr1,SUM(CASE WHEN P.branchname = 'CW2-CTR' THEN PI.required_qty ELSE 0 END) ctr2,SUM(CASE WHEN P.branchname = 'CW3-CTR' THEN PI.required_qty ELSE 0 END) ctr3 ,SUM(CASE WHEN P.branchname = 'CW4-CTR' THEN PI.required_qty ELSE 0 END) ctr4,SUM(CASE WHEN P.branchname = 'CWC-1' THEN PI.required_qty ELSE 0 END) cwc1 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and P.branchname not like '%STR%' and P.branchname not like '%ESH%' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getCarnivalIndentItemsQty($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
            $query = $this->db->query("select PI.item_name,SUM(CASE WHEN P.branchname = 'CW-CTR' THEN PI.required_qty ELSE 0 END) ctr1,SUM(CASE WHEN P.branchname = 'CW2-CTR' THEN PI.required_qty ELSE 0 END) ctr2,SUM(CASE WHEN P.branchname = 'CW3-CTR' THEN PI.required_qty ELSE 0 END) ctr3 ,SUM(CASE WHEN P.branchname = 'CW4-CTR' THEN PI.required_qty ELSE 0 END) ctr4,SUM(CASE WHEN P.branchname = 'CWC-1' THEN PI.required_qty ELSE 0 END) cwc1 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Carnival Order' and P.status='Pending' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getStoreIndentItemsQty($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
            $query = $this->db->query("select PI.item_name,SUM(CASE WHEN P.branchname = 'CW-STR' THEN PI.required_qty ELSE 0 END) str1 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and P.branchname like '%STR%' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getEshIndentItemsQty($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
            $query = $this->db->query("select PI.item_name,SUM(CASE WHEN P.branchname = 'ESH-KTN' THEN PI.required_qty ELSE 0 END) esh1 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and P.branchname like '%ESH%' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function pr_userdata(){
		    $query = $this->db->query("select distinct(dept) as dept from product_request");
			return $query->result();
		}
		public function getStIndentItemsQty($data)
		{
			//$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
            
            function last_mon_day($date) {
				if (!is_numeric($date))
					$date = strtotime($date);
				if (date('w', $date) == 0)
					return $date;
				elseif (date('w', $date) == 0)
					return strtotime(
						'next sunday',
						 $date
					);
				else
					return strtotime(
						'last sunday',
						 $date
					);
			}

			$r_date = date('Y-m-d', last_mon_day($data['req_date']));
			$query = $this->db->query("select PI.item_name,SUM(CASE WHEN P.branchname = 'ST1' THEN PI.required_qty ELSE 0 END) st1,SUM(CASE WHEN P.branchname = 'ST2' THEN PI.required_qty ELSE 0 END) st2,SUM(CASE WHEN P.branchname = 'ST3' THEN PI.required_qty ELSE 0 END) st3,SUM(CASE WHEN P.branchname = 'ST4' THEN PI.required_qty ELSE 0 END) st4 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Santhe' and P.status='Pending' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getIndentItems($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$branch_id = $data['branch_id'];
			
			$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.recipe_no,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and R.branch_id='$branch_id' and P.branchname not like '%ESH%' and P.branchname not like '%STR%' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getCarIndentItems($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$branch_id = $data['branch_id'];
			
			$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.recipe_no,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Carnival Order' and P.status='Pending' and R.branch_id='$branch_id' and P.branchname not like '%ESH%' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getEIndentItems($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$branch_id = $data['branch_id'];
			
			$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.recipe_no,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and R.branch_id='$branch_id' and P.branchname like '%ESH%' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getStrIndentItems($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$branch_id = $data['branch_id'];
			
			$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.recipe_no,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and R.branch_id='$branch_id' and P.branchname like '%STR%' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getStIndentItems($data)
		{
			//$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$branch_id = $data['branch_id'];
			
			function last_onday($date) {
				if (!is_numeric($date))
					$date = strtotime($date);
				if (date('w', $date) == 0)
					return $date;
				elseif (date('w', $date) == 0)
					return strtotime(
						'next sunday',
						 $date
					);
				else
					return strtotime(
						'last sunday',
						 $date
					);
			}

			$r_date = date('Y-m-d', last_onday($data['req_date']));
		//	$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.recipe_no,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Santhe' and P.status='Pending' and R.branch_id='$branch_id' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.recipe_no,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Santhe' and P.status='Pending' and R.branch_id='$branch_id' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getSplOrders($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];

			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and orders='Special Order' and status='Pending'");
			return $query->result();
		}
		
		public function gettSplOrders($uname)
		{
			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (orders='Special Order' or orders='STSpecial Order') and issue_status='Pending' order by delivery_date desc,delivery_time desc");
			return $query->result();
		}
	
		public function getRunningOrders($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];

			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and orders='Running Order' and status='Pending'");
			return $query->result();
		}
		
		public function gettRunningOrders($uname)
		{
			$to_date = date('Y-m-d');
			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$to_date') and orders='Running Order' and issue_status='Pending'");
			return $query->result();
		}
		
		public function getStSplOrders($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];

			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and orders='Santhe Special Order' and status='Pending'");
			return $query->result();
		}
		
		
		public function addRegularIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$ctr = $ctrct;
			$itm = $itemct;
			$ttitm = $ttitemct;
			
			$isid = array();
			$tr_ref = array();
			for($i=0;$i<$ctr;$i++)
			{
				$this->db->select('MAX(is_no) as is_no',false);
				$this->db->where('branch_id',$branch_id);
				$result = $this->db->get('product_issues_creation')->row();
				$is_no = $result->is_no;
				if($is_no == 0){
					$is_no = 1;
				}
				else{
					$is_no = $is_no+1;
				}
				$issue_no = "ISSU"."/".$branch_name."/".$outlet_branch[$i]."/".$is_no;
				$tr_ref[] = $issue_no;
				
				$data = array('is_no'=> $is_no,
					'issue_no'=> $issue_no,
					'issue_date'=> $issue_date,
					'issue_time'=> $issue_time,
					'outlet'=> $dept[$i],
					'branchname'=> $branch_name,
					'request_id'=> $req_id[$i],
					'issue_status'=> 'Pending',
					'total_amount'=> $grand_total[$i],
					'user_name'=> $uname,
					'branch_id'=> $branch_id
				);
				$this->db->insert('product_issues_creation', $data);
				$isid[] = $this->db->insert_id();
			}
			
			//insert of items
			$m = 0;
			$o = $ttitm/$ctr;
            $n = $o;
			for($j=0;$j<$ttitm;$j++)
			{				
				if($j==$n)
				{
					$m = $m + 1;
            		$n = $n + $o;
				}
				if($inward_quantity[$j]!='')
				{
					$q1 = $this->db->query("select * from recipe where recipe_id='$item_id[$j]'");
					if($q1->num_rows() > 0)
					{
						$row = $q1->row_array();
						$qty[$j] = $row['production_stock'];
						$qty1[$j] = (float)$qty[$j] - (float)$inward_quantity[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$item_id[$j]'");
						
						//calculation
				        $totbasic[$j] = round(((float)$price[$j] * (float)$inward_quantity[$j]),2);
					    $taxamt[$j] = round((($totbasic[$j] * $tax[$j])/100),2);
					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
						
						$data1 = array(
							'issue_item_name'=> $item_name[$j],
							'issue_uom'=> $uom[$j],
							'issued_qty'=> $inward_quantity[$j],
							'price'=> $price[$j],
							'basic_price'=> $totbasic[$j],
							'tax'=> $tax[$j],
							'tax_amount'=> $taxamt[$j],
							'total_price'=> $totalprice[$j],
							'issue_id'=> $isid[$m],
							'request_item_id'=> $req_item_id[$j],
							'recipe_id'=> $item_id[$j],
							'rcv_status'=> 'Pending',
							'recipe_no'=> $recipe_no[$j],
							'pr_cat_id'=> $pr_cat_id[$j]
						);
						$this->db->insert('product_issues_item', $data1);
						$isitid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $item_name[$j],
							'trans_ref_no'=> $tr_ref[$m],
							'trans_type'=>'PrIssue',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $qty[$j],
							'out_qty'=> $inward_quantity[$j],
							'closing_stock'=> $qty1[$j],
							'last_modified'=> $lm,
							'branchname'=> $branch_name,
							'item_id'=> $item_id[$j],
							'recipe_no'=> $recipe_no[$j],
							'category_id'=> $pr_cat_id[$j],
							'branch_id'=> $branch_id
						);
						$this->db->insert('stock_detail', $data2);
						
						$rc_qty[$j] = (float)$inward_quantity[$j] + (float)$received_qty[$j];
						$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$req_item_id[$j]'");
						
						if($rc_qty[$j] == $quantity[$j] && $inward_quantity[$j]!='') {
							$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$req_item_id[$j]'");
						}
					}
				}
			}
			for($i=0;$i<$ctr;$i++)
			{
				$isq = $this->db->query("select * from product_issues_item where issue_id='$isid[$i]'");
				if($isq->num_rows() > 0)
				{
					//nothing
				}
				else
				{
					$this->db->query("delete from product_issues_creation where issue_id='$isid[$i]'");
				}
			}
			//return $isid;
		}
		
		public function addEshRegularIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$ctr = $ectrct;
			$itm = $eitemct;
			$ttitm = $ettitemct;
			
			$isid = array();
			$tr_ref = array();
			for($i=0;$i<$ctr;$i++)
			{
				$this->db->select('MAX(is_no) as is_no',false);
				$this->db->where('branch_id',$ebranch_id);
				$result = $this->db->get('product_issues_creation')->row();
				$is_no = $result->is_no;
				if($is_no == 0){
					$is_no = 1;
				}
				else{
					$is_no = $is_no+1;
				}
				$issue_no = "ISSU"."/".$ebranch_name."/".$eoutlet_branch[$i]."/".$is_no;
				$tr_ref[] = $issue_no;
				
				$data = array('is_no'=> $is_no,
					'issue_no'=> $issue_no,
					'issue_date'=> $issue_date,
					'issue_time'=> $issue_time,
					'outlet'=> $edept[$i],
					'branchname'=> $ebranch_name,
					'request_id'=> $ereq_id[$i],
					'issue_status'=> 'Pending',
					'total_amount'=> $egrand_total[$i],
					'user_name'=> $euname,
					'branch_id'=> $ebranch_id
				);
				$this->db->insert('product_issues_creation', $data);
				$isid[] = $this->db->insert_id();
			}
			
			//insert of items
			$m = 0;
			$o = $ttitm/$ctr;
            $n = $o;
			for($j=0;$j<$ttitm;$j++)
			{				
				if($j==$n)
				{
					$m = $m + 1;
					$n = $n + $o;
				}
				if($einward_quantity[$j]!='')
				{
					$q1 = $this->db->query("select * from recipe where recipe_id='$eitem_id[$j]'");
					if($q1->num_rows() > 0)
					{
						$row = $q1->row_array();
						$qty[$j] = $row['production_stock'];
						$qty1[$j] = $qty[$j] - $einward_quantity[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$eitem_id[$j]'");
						
						//calculation
				        $totbasic[$j] = round(($eprice[$j] * $einward_quantity[$j]),2);
					    $taxamt[$j] = round((($totbasic[$j] * $etax[$j])/100),2);
					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
						
						$data1 = array(
							'issue_item_name'=> $eitem_name[$j],
							'issue_uom'=> $euom[$j],
							'issued_qty'=> $einward_quantity[$j],
							'price'=> $eprice[$j],
							'basic_price'=> $totbasic[$j],
							'tax'=> $etax[$j],
							'tax_amount'=> $taxamt[$j],
							'total_price'=> $totalprice[$j],
							'issue_id'=> $isid[$m],
							'request_item_id'=> $ereq_item_id[$j],
							'recipe_id'=> $eitem_id[$j],
							'rcv_status'=> 'Pending',
							'recipe_no'=> $erecipe_no[$j],
							'pr_cat_id'=> $epr_cat_id[$j]
						);
						$this->db->insert('product_issues_item', $data1);
						$isitid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $eitem_name[$j],
							'trans_ref_no'=> $tr_ref[$m],
							'trans_type'=>'PrIssue',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $qty[$j],
							'out_qty'=> $einward_quantity[$j],
							'closing_stock'=> $qty1[$j],
							'last_modified'=> $lm,
							'branchname'=> $ebranch_name,
							'item_id'=> $eitem_id[$j],
							'recipe_no'=> $erecipe_no[$j],
							'category_id'=> $epr_cat_id[$j],
							'branch_id'=> $ebranch_id
						);
						$this->db->insert('stock_detail', $data2);
						
						$rc_qty[$j] = $einward_quantity[$j] + $ereceived_qty[$j];
						$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$ereq_item_id[$j]'");
						
						if($rc_qty[$j] == $equantity[$j] && $einward_quantity[$j]!='') {
							$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$ereq_item_id[$j]'");
						}
					}
				}
			}
			for($i=0;$i<$ctr;$i++)
			{
				$isq = $this->db->query("select * from product_issues_item where issue_id='$isid[$i]'");
				if($isq->num_rows() > 0)
				{
					//nothing
				}
				else
				{
					$this->db->query("delete from product_issues_creation where issue_id='$isid[$i]'");
				}
			}
			//return $isid;
		}
		
		public function addStrRegularIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$ctr = $strctrct;
			$itm = $stritemct;
			$ttitm = $strttitemct;
			
			$isid = array();
			$tr_ref = array();
			for($i=0;$i<$ctr;$i++)
			{
				$this->db->select('MAX(is_no) as is_no',false);
				$this->db->where('branch_id',$strbranch_id);
				$result = $this->db->get('product_issues_creation')->row();
				$is_no = $result->is_no;
				if($is_no == 0){
					$is_no = 1;
				}
				else{
					$is_no = $is_no+1;
				}
				$issue_no = "ISSU"."/".$strbranch_name."/".$stroutlet_branch[$i]."/".$is_no;
				$tr_ref[] = $issue_no;
				
				$data = array('is_no'=> $is_no,
					'issue_no'=> $issue_no,
					'issue_date'=> $issue_date,
					'issue_time'=> $issue_time,
					'outlet'=> $strdept[$i],
					'branchname'=> $strbranch_name,
					'request_id'=> $strreq_id[$i],
					'issue_status'=> 'Pending',
					'total_amount'=> $strgrand_total[$i],
					'user_name'=> $struname,
					'branch_id'=> $strbranch_id
				);
				$this->db->insert('product_issues_creation', $data);
				$isid[] = $this->db->insert_id();
			}
			
			//insert of items
			$m = 0;
			$o = $ttitm/$ctr;
            $n = $o;
			for($j=0;$j<$ttitm;$j++)
			{				
				if($j==$n)
				{
					$m = $m + 1;
					$n = $n + $o;
				}
				if($strinward_quantity[$j]!='')
				{
					$q1 = $this->db->query("select * from recipe where recipe_id='$stritem_id[$j]'");
					if($q1->num_rows() > 0)
					{
						$row = $q1->row_array();
						$qty[$j] = $row['production_stock'];
						$qty1[$j] = $qty[$j] - $strinward_quantity[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$stritem_id[$j]'");
						
						//calculation
				        $totbasic[$j] = round(($strprice[$j] * $strinward_quantity[$j]),2);
					    $taxamt[$j] = round((($totbasic[$j] * $strtax[$j])/100),2);
					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
						
						$data1 = array(
							'issue_item_name'=> $stritem_name[$j],
							'issue_uom'=> $struom[$j],
							'issued_qty'=> $strinward_quantity[$j],
							'price'=> $strprice[$j],
							'basic_price'=> $totbasic[$j],
							'tax'=> $strtax[$j],
							'tax_amount'=> $taxamt[$j],
							'total_price'=> $totalprice[$j],
							'issue_id'=> $isid[$m],
							'request_item_id'=> $strreq_item_id[$j],
							'recipe_id'=> $stritem_id[$j],
							'rcv_status'=> 'Pending',
							'recipe_no'=> $strrecipe_no[$j],
							'pr_cat_id'=> $strpr_cat_id[$j]
						);
						$this->db->insert('product_issues_item', $data1);
						$isitid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $stritem_name[$j],
							'trans_ref_no'=> $tr_ref[$m],
							'trans_type'=>'PrIssue',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $qty[$j],
							'out_qty'=> $strinward_quantity[$j],
							'closing_stock'=> $qty1[$j],
							'last_modified'=> $lm,
							'branchname'=> $strbranch_name,
							'item_id'=> $stritem_id[$j],
							'recipe_no'=> $strrecipe_no[$j],
							'category_id'=> $strpr_cat_id[$j],
							'branch_id'=> $strbranch_id
						);
						$this->db->insert('stock_detail', $data2);
						
						$rc_qty[$j] = $strinward_quantity[$j] + $strreceived_qty[$j];
						$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$strreq_item_id[$j]'");
						
						if($rc_qty[$j] == $strquantity[$j] && $strinward_quantity[$j]!='') {
							$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$strreq_item_id[$j]'");
						}
					}
				}
			}
			for($i=0;$i<$ctr;$i++)
			{
				$isq = $this->db->query("select * from product_issues_item where issue_id='$isid[$i]'");
				if($isq->num_rows() > 0)
				{
					//nothing
				}
				else
				{
					$this->db->query("delete from product_issues_creation where issue_id='$isid[$i]'");
				}
			}
			//return $isid;
		}
		
		public function addStRegularIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$ctr = $sctrct;
			$itm = $sitemct;
			$ttitm = $sttitemct;
			
			$isid = array();
			$tr_ref = array();
			for($i=0;$i<$ctr;$i++)
			{
				$this->db->select('MAX(is_no) as is_no',false);
				$this->db->where('branch_id',$sbranch_id);
				$result = $this->db->get('product_issues_creation')->row();
				$is_no = $result->is_no;
				if($is_no == 0){
					$is_no = 1;
				}
				else{
					$is_no = $is_no+1;
				}
				$issue_no = "ISSU"."/".$sbranch_name."/".$soutlet_branch[$i]."/".$is_no;
				$tr_ref[] = $issue_no;
				
				$data = array('is_no'=> $is_no,
					'issue_no'=> $issue_no,
					'issue_date'=> $issue_date,
					'issue_time'=> $issue_time,
					'outlet'=> $sdept[$i],
					'branchname'=> $sbranch_name,
					'request_id'=> $sreq_id[$i],
					'issue_status'=> 'Pending',
					'total_amount'=> $sgrand_total[$i],
					'user_name'=> $suname,
					'branch_id'=> $sbranch_id
				);
				$this->db->insert('product_issues_creation', $data);
				$isid[] = $this->db->insert_id();
			}
			
			//insert of items
			$m = 0;
			$o = $ttitm/$ctr;
            $n = $o;
			for($j=0;$j<$ttitm;$j++)
			{				
				if($j==$n)
				{
					$m = $m + 1;
					$n = $n + $o;
				}
				if($sinward_quantity[$j]!='')
				{
					$q1 = $this->db->query("select * from recipe where recipe_id='$sitem_id[$j]'");
					if($q1->num_rows() > 0)
					{
						$row = $q1->row_array();
						$qty[$j] = $row['production_stock'];
						$qty1[$j] = $qty[$j] - $sinward_quantity[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$sitem_id[$j]'");
						
						//calculation
				        $totbasic[$j] = round(($sprice[$j] * $sinward_quantity[$j]),2);
					    $taxamt[$j] = round((($totbasic[$j] * $stax[$j])/100),2);
					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
    					    
						$data1 = array(
							'issue_item_name'=> $sitem_name[$j],
							'issue_uom'=> $suom[$j],
							'issued_qty'=> $sinward_quantity[$j],
							'price'=> $sprice[$j],
							'basic_price'=> $totbasic[$j],
							'tax'=> $stax[$j],
							'tax_amount'=> $taxamt[$j],
							'total_price'=> $totalprice[$j],
							'issue_id'=> $isid[$m],
							'request_item_id'=> $sreq_item_id[$j],
							'recipe_id'=> $sitem_id[$j],
							'rcv_status'=> 'Pending',
							'recipe_no'=> $srecipe_no[$j],
							'pr_cat_id'=> $spr_cat_id[$j]
						);
						$this->db->insert('product_issues_item', $data1);
						$isitid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $sitem_name[$j],
							'trans_ref_no'=> $tr_ref[$m],
							'trans_type'=>'PrIssue',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $qty[$j],
							'out_qty'=> $sinward_quantity[$j],
							'closing_stock'=> $qty1[$j],
							'last_modified'=> $lm,
							'branchname'=> $sbranch_name,
							'item_id'=> $sitem_id[$j],
							'recipe_no'=> $srecipe_no[$j],
							'category_id'=> $spr_cat_id[$j],
							'branch_id'=> $sbranch_id
						);
						$this->db->insert('stock_detail', $data2);
						
						$rc_qty[$j] = $sinward_quantity[$j] + $sreceived_qty[$j];
						$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$sreq_item_id[$j]'");
						
						if($rc_qty[$j] == $squantity[$j] && $sinward_quantity[$j]!='') {
							$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$sreq_item_id[$j]'");
						}
					}
				}
			}
			for($i=0;$i<$ctr;$i++)
			{
				$isq = $this->db->query("select * from product_issues_item where issue_id='$isid[$i]'");
				if($isq->num_rows() > 0)
				{
					//nothing
				}
				else
				{
					$this->db->query("delete from product_issues_creation where issue_id='$isid[$i]'");
				}
			}
			//return $isid;
		}
				
		public function addProductionIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$ctr = $ctrct;
			$itm = $itemct;
			$ttitm = $ttitemct;
			$icount = count($prd_item);
			
			$rs = $this->db->query("select * from billdetails where orderdate='$issue_date' and chef IS NULL and branch_id='$branch_id'");
			if($rs->num_rows() > 0)
			{
				$r1 = $rs->row_array();
				$bill_id = $r1['billno'];
    			$bill_no = $r1['bill_id'];
    			$bno = $r1['bno'];
				
				for($i=0;$i<$icount;$i++)
				{
					if($pr_stock[$i]!='0' && $pr_stock[$i]!='')
    				{
						$q3 = $this->db->query("select * from recipe where recipe_id='$recipe_id[$i]'");
						if($q3->num_rows() > 0)
						{
							$rs1 = $q3->row_array();
							$pstock[$i] = $rs1['production_stock'];
    						$pstock1[$i] = $pstock[$i] + $pr_stock[$i];
    						$rc_yield[$i] = $rs1['yeild'];
    						if($rs1['yeild'] =='0'){
							    $rc_yield[$i] = '1';
							}else{
							    $rc_yield[$i] = $rs1['yeild'];
							}
    						$recipe_no[$i] = $rs1['recipe_no'];
    					    $pr_cat_id[$i] = $rs1['pr_cat_id'];
							
							$q4 = $this->db->query("update recipe set production_stock='$pstock1[$i]' where recipe_id='$recipe_id[$i]'");
						}
						$res2 = $this->db->query("select * from bom_product where billno='$bill_id' and pname='$prd_item[$i]'");
						if($res2->num_rows() > 0)
						{
							$r2 = $res2->row_array();
							$bp_id[$i] = $r2['bp_id'];
    						$qty[$i] = $r2['actual_yeild'];
    						$qty1[$i] = $qty[$i] + $pr_stock[$i];
							$sql2 = $this->db->query("update bom_product set actual_yeild='$qty1[$i]' where bp_id='$bp_id[$i]'");
						}
						else
						{
							$bdata = array('pname'=> $prd_item[$i],
								'uom'=> $puom[$i],
								'actual_yeild'=> $pr_stock[$i],
								'yeild'=> $yeild[$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', $bdata);
						}
						$bdata1 = array('item_name'=> $prd_item[$i],
							'trans_ref_no'=> $bill_no,
							'trans_type'=>'PrInward',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $pstock[$i],
							'in_qty'=> $pr_stock[$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', $bdata1);
					//}
					
					//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*$pr_stock[$i])/$rc_yield[$i]),2);
    					        
    					        $data4 = array(
                					'item_name' => $rm_name,
                					'trans_ref_no' => $cons_no,
                					'trans_type'=>'Consumption',
                					'transaction_date' => $issue_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*$pr_stock[$i])/$rc_yield[$i]),2);
    					        
    					        $data4 = array(
                					'item_name' => $rc_name,
                					'trans_ref_no' => $cons_no,
                					'trans_type'=>'Consumption',
                					'transaction_date' => $issue_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);
    					    }
    				    }
    				}
				}
				}
			}
			else
			{
				$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;
				
				$bdata2 = array('bno'=> $bno,
					'bill_id'=> $bill_no,
					'pr_cat_id'=> $cat_id,
					'orderdate'=> $issue_date,
					'ordertime'=> $issue_time,
					'outlet'=> $uname,
					'branch_id'=> $branch_id
				);
				$this->db->insert('billdetails', $bdata2);
				$bill_id = $this->db->insert_id();
				
				if($prd_item != '')
    			{
					for($i=0;$i<$icount;$i++)
					{
						if($pr_stock[$i]!='0' && $pr_stock[$i]!='')
    					{
							$q3 = $this->db->query("select * from recipe where recipe_id='$recipe_id[$i]'");
							if($q3->num_rows() > 0)
							{
								$rs = $q3->row_array();
								$pstock[$i] = $rs['production_stock'];
    							$pstock1[$i] = $pstock[$i] + $pr_stock[$i];
    							if($rs['yeild'] =='0'){
    							    $rc_yield[$i] = '1';
    							}else{
    							    $rc_yield[$i] = $rs['yeild'];
    							}
    							
    							$recipe_no[$i] = $rs['recipe_no'];
    					        $pr_cat_id[$i] = $rs['pr_cat_id'];
								
								$q4 = $this->db->query("update recipe set production_stock='$pstock1[$i]' where recipe_id='$recipe_id[$i]'");
							}
							$bdata3 = array('pname'=> $prd_item[$i],
								'uom'=> $puom[$i],
								'actual_yeild'=> $pr_stock[$i],
								'yeild'=> $yeild[$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', $bdata3);
							
							$bdata4 = array('item_name'=> $prd_item[$i],
								'trans_ref_no'=> $bill_no,
								'trans_type'=>'PrInward',
								'transaction_date'=> $issue_date,
								'opening_stock'=> $pstock[$i],
								'in_qty'=> $pr_stock[$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', $bdata4);
						
						
						//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*$pr_stock[$i])/$rc_yield[$i]),2);
        					        
        					        $data4 = array(
                    					'item_name' => $rm_name,
                    					'trans_ref_no' => $cons_no,
                    					'trans_type'=>'Consumption',
                    					'transaction_date' => $issue_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*$pr_stock[$i])/$rc_yield[$i]),2);
        					        
        					        $data4 = array(
                    					'item_name' => $rc_name,
                    					'trans_ref_no' => $cons_no,
                    					'trans_type'=>'Consumption',
                    					'transaction_date' => $issue_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);
        					    }
        				    }
        				}
    					}
					}
				}
			}
			
			$isid = array();
			$tr_ref = array();
			for($i=0;$i<$ctr;$i++)
			{
				$this->db->select('MAX(is_no) as is_no',false);
				$this->db->where('branch_id',$branch_id);
				$result = $this->db->get('product_issues_creation')->row();
				$is_no = $result->is_no;
				if($is_no == 0){
					$is_no = 1;
				}
				else{
					$is_no = $is_no+1;
				}
				$issue_no = "ISSU"."/".$branch_name."/".$outlet_branch[$i]."/".$is_no;
				$tr_ref[] = $issue_no;
				
				$data = array('is_no'=> $is_no,
					'issue_no'=> $issue_no,
					'issue_date'=> $issue_date,
					'issue_time'=> $issue_time,
					'outlet'=> $dept[$i],
					'branchname'=> $branch_name,
					'request_id'=> $req_id[$i],
					'issue_status'=> 'Pending',
					'total_amount'=> $grand_total[$i],
					'user_name'=> $uname,
					'branch_id'=> $branch_id
				);
				$this->db->insert('product_issues_creation', $data);
				$isid[] = $this->db->insert_id();
			}
			
			//insert of items
			$m = 0;
			$o = $ttitm/$ctr;
            $n = $o;
			for($j=0;$j<$ttitm;$j++)
			{				
				if($j==$n)
				{
					$m = $m + 1;
					$n = $n + $o;
				}
				if($inward_quantity[$j]!='')
				{
					$q1 = $this->db->query("select * from recipe where recipe_id='$item_id[$j]'");
					if($q1->num_rows() > 0)
					{
						$row = $q1->row_array();
						$qty[$j] = $row['production_stock'];
						$qty1[$j] = $qty[$j] - $inward_quantity[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$item_id[$j]'");
						
						//calculation
					        $totbasic[$j] = round(($price[$j] * $inward_quantity[$j]),2);
    					    $taxamt[$j] = round((($totbasic[$j] * $tax[$j])/100),2);
    					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
						
						$data1 = array(
							'issue_item_name'=> $item_name[$j],
							'issue_uom'=> $uom[$j],
							'issued_qty'=> $inward_quantity[$j],
							'price'=> $price[$j],
							'basic_price'=> $totbasic[$j],
							'tax'=> $tax[$j],
							'tax_amount'=> $taxamt[$j],
							'total_price'=> $totalprice[$j],
							'issue_id'=> $isid[$m],
							'request_item_id'=> $req_item_id[$j],
							'recipe_id'=> $item_id[$j],
							'rcv_status'=> 'Pending',
							'recipe_no'=> $recipe_no[$j],
							'pr_cat_id'=> $pr_cat_id[$j]
						);
						$this->db->insert('product_issues_item', $data1);
						$isitid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $item_name[$j],
							'trans_ref_no'=> $tr_ref[$m],
							'trans_type'=>'PrIssue',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $qty[$j],
							'out_qty'=> $inward_quantity[$j],
							'closing_stock'=> $qty1[$j],
							'last_modified'=> $lm,
							'branchname'=> $branch_name,
							'item_id'=> $item_id[$j],
							'recipe_no'=> $recipe_no[$j],
							'category_id'=> $pr_cat_id[$j],
							'branch_id'=> $branch_id
						);
						$this->db->insert('stock_detail', $data2);
						
						$rc_qty[$j] = $inward_quantity[$j] + $received_qty[$j];
						$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$req_item_id[$j]'");
						
						if($rc_qty[$j] == $quantity[$j] && $inward_quantity[$j]!='') {
							$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$req_item_id[$j]'");
						}
					}
				}
			}
			for($i=0;$i<$ctr;$i++)
			{
				$isq = $this->db->query("select * from product_issues_item where issue_id='$isid[$i]'");
				if($isq->num_rows() > 0)
				{
					//nothing
				}
				else
				{
					$this->db->query("delete from product_issues_creation where issue_id='$isid[$i]'");
				}
			}
			//return $isid;
		}
		
		public function addCarnivalIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$ctr = $cctrct;
			$itm = $citemct;
			$ttitm = $cttitemct;
			$icount = count($cprd_item);
			
			$rs = $this->db->query("select * from billdetails where orderdate='$issue_date' and chef IS NULL and branch_id='$cbranch_id'");
			if($rs->num_rows() > 0)
			{
				$r1 = $rs->row_array();
				$bill_id = $r1['billno'];
    			$bill_no = $r1['bill_id'];
				
				for($i=0;$i<$icount;$i++)
				{
					if($cpr_stock[$i]!='0' && $cpr_stock[$i]!='')
    				{
						$q3 = $this->db->query("select * from recipe where recipe_id='$crecipe_id[$i]'");
						if($q3->num_rows() > 0)
						{
							$rs1 = $q3->row_array();
							$pstock[$i] = $rs1['production_stock'];
    						$pstock1[$i] = $pstock[$i] + $cpr_stock[$i];
    						$recipe_no[$i] = $rs1['recipe_no'];
    					    $pr_cat_id[$i] = $rs1['pr_cat_id'];
							
							$q4 = $this->db->query("update recipe set production_stock='$pstock1[$i]' where recipe_id='$crecipe_id[$i]'");
						}
						$res2 = $this->db->query("select * from bom_product where billno='$bill_id' and pname='$cprd_item[$i]'");
						if($res2->num_rows() > 0)
						{
							$r2 = $res2->row_array();
							$bp_id[$i] = $r2['bp_id'];
    						$qty[$i] = $r2['actual_yeild'];
    						$qty1[$i] = $qty[$i] + $cpr_stock[$i];
							$sql2 = $this->db->query("update bom_product set actual_yeild='$qty1[$i]' where bp_id='$bp_id[$i]'");
						}
						else
						{
							$bdata = array('pname'=> $cprd_item[$i],
								'uom'=> $cpuom[$i],
								'actual_yeild'=> $cpr_stock[$i],
								'yeild'=> $cyeild[$i],
								'billno'=> $bill_id,
								'recipe_id'=> $crecipe_id[$i],
								'recipe_no'=> $recipe_no[$i],
								'pr_cat_id'=> $pr_cat_id[$i]
							);
							$this->db->insert('bom_product', $bdata);
						}
						$bdata1 = array('item_name'=> $cprd_item[$i],
							'trans_ref_no'=> $bill_no,
							'trans_type'=>'PrInward',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $pstock[$i],
							'in_qty'=> $cpr_stock[$i],
							'closing_stock'=> $pstock1[$i],
							'last_modified'=> $lm,
							'branchname'=> $cbranch_name,
							'item_id'=> $crecipe_id[$i],
							'recipe_no'=> $recipe_no[$i],
							'category_id'=> $pr_cat_id[$i],
							'branch_id'=> $cbranch_id
						);
						$this->db->insert('stock_detail', $bdata1);
					}
				}
			}
			else
			{
				$this->db->select('MAX(bno) as bno',false);
				$this->db->where('branch_id',$cbranch_id);
				$result = $this->db->get('billdetails')->row();
				$bno = $result->bno;
				if($bno == 0){
					$bno = 1;
				}
				else{
					$bno = $bno+1;
				}
				$bill_no = "BOM"."/".$cbranch_name."/".$bno;
				
				$bdata2 = array('bno'=> $bno,
					'bill_id'=> $bill_no,
					'pr_cat_id'=> $ccat_id,
					'orderdate'=> $issue_date,
					'ordertime'=> $issue_time,
					'outlet'=> $cuname,
					'branch_id'=> $cbranch_id
				);
				$this->db->insert('billdetails', $bdata2);
				$bill_id = $this->db->insert_id();
				
				if($cprd_item != '')
    			{
					for($i=0;$i<$icount;$i++)
					{
						if($cpr_stock[$i]!='0' && $cpr_stock[$i]!='')
    					{
							$q3 = $this->db->query("select * from recipe where recipe_id='$crecipe_id[$i]'");
							if($q3->num_rows() > 0)
							{
								$rs = $q3->row_array();
								$pstock[$i] = $rs['production_stock'];
    							$pstock1[$i] = $pstock[$i] + $cpr_stock[$i];
    							$recipe_no[$i] = $rs['recipe_no'];
    					        $pr_cat_id[$i] = $rs['pr_cat_id'];
								
								$q4 = $this->db->query("update recipe set production_stock='$pstock1[$i]' where recipe_id='$crecipe_id[$i]'");
							}
							$bdata3 = array('pname'=> $cprd_item[$i],
								'uom'=> $cpuom[$i],
								'actual_yeild'=> $cpr_stock[$i],
								'yeild'=> $cyeild[$i],
								'billno'=> $bill_id,
								'recipe_id'=> $crecipe_id[$i],
								'recipe_no'=> $recipe_no[$i],
								'pr_cat_id'=> $pr_cat_id[$i]
							);
							$this->db->insert('bom_product', $bdata3);
							
							$bdata4 = array('item_name'=> $cprd_item[$i],
								'trans_ref_no'=> $bill_no,
								'trans_type'=>'PrInward',
								'transaction_date'=> $issue_date,
								'opening_stock'=> $pstock[$i],
								'in_qty'=> $cpr_stock[$i],
								'closing_stock'=> $pstock1[$i],
								'last_modified'=> $lm,
								'branchname'=> $cbranch_name,
								'item_id'=> $crecipe_id[$i],
								'recipe_no'=> $recipe_no[$i],
								'category_id'=> $pr_cat_id[$i],
								'branch_id'=> $cbranch_id
							);
							$this->db->insert('stock_detail', $bdata4);
						}
					}
				}
			}
			
			$isid = array();
            $tr_ref = array();
            $inid = array();
            $tr_ref1 = array();
            for($i=0;$i<$ctr;$i++)
            {
            	$this->db->select('MAX(is_no) as is_no',false);
            	$this->db->where('branch_id',$cbranch_id);
            	$result = $this->db->get('product_issues_creation')->row();
            	$is_no = $result->is_no;
            	if($is_no == 0){
            		$is_no = 1;
            	}
            	else{
            		$is_no = $is_no + 1;
            	}
            	$issue_no = "ISSU"."/".$cbranch_name."/".$coutlet_branch[$i]."/".$is_no;
            	$tr_ref[] = $issue_no;
            	
            	$data = array('is_no'=> $is_no,
            	   	'issue_no'=> $issue_no,
            		'issue_date'=> $issue_date,
            		'issue_time'=> $issue_time,
            		'outlet'=> $cdept[$i],
            		'branchname'=> $cbranch_name,
            		'request_id'=> $creq_id[$i],
            		'issue_status'=> 'Pending',
            		'total_amount'=> $cgrand_total[$i],
            		'user_name'=> $cuname,
            		'issue_status'=>'Accepted',
            		'branch_id'=> $cbranch_id
            	);
            	$this->db->insert('product_issues_creation', $data);
            	$isid[] = $this->db->insert_id();
            
            	$this->db->select('MAX(rec_no) as rec_no',false);
                $this->db->where('branch_id',$cbranch_id);
                $result = $this->db->get('product_req_rec')->row();
                $rec_no = $result->rec_no;
                if($rec_no == 0){
                    $rec_no = 1;
                }
                else{
                    $rec_no = $rec_no + 1;
                }
                $receipt_no = "PR/IN"."/".$coutlet_branch[$i]."/".$rec_no;
                $tr_ref1[] = $receipt_no;
                
                $data = array('rec_no'=> $rec_no,
                    'req_rec_no'=> $receipt_no,
                    'receipt_date'=> $issue_date,
                    'receipt_time'=> $issue_time,
                    'branchname'=> $coutlet_branch[$i],
                    'dept'=> $cdept[$i],
                    'request_id'=> $creq_id[$i],
                    'issue_id'=> $isid[$i],
                    'total_amount'=> $cgrand_total[$i],
                    'branch_id'=> $coutlet_branchid[$i]
                );
                $this->db->insert('product_req_rec', $data);
                $inid[] = $this->db->insert_id();
            }
            
            //insert of items
            $m = 0;
            $o = $ttitm/$ctr;
            $n = $o;
            for($j=0;$j<$ttitm;$j++)
            {				
            	if($j==$n)
            	{
            		$m = $m + 1;
            		$n = $n + $o;
            	}
            	if($cinward_quantity[$j]!='0' && $cinward_quantity[$j]!='')
            	{
            		$q1 = $this->db->query("select * from recipe where recipe_id='$citem_id[$j]'");
            		if($q1->num_rows() > 0)
            		{
            			$row = $q1->row_array();
            			$qty[$j] = $row['production_stock'];
            			$recipe_no[$j] = $row['recipe_no'];
            			$pr_cat_id[$j] = $row['pr_cat_id'];
            			$qty1[$j] = $qty[$j] - $cinward_quantity[$j];
            			
            			$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$citem_id[$j]'");
            			
            			//calculation
				        $totbasic[$j] = round(($cprice[$j] * $cinward_quantity[$j]),2);
					    $taxamt[$j] = round((($totbasic[$j] * $ctax[$j])/100),2);
					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
            			
            			$data1 = array(
            				'issue_item_name'=> $citem_name[$j],
            				'issue_uom'=> $cuom[$j],
            				'issued_qty'=> $cinward_quantity[$j],
            				'price'=> $cprice[$j],
            				'basic_price'=> $totbasic[$j],
            				'tax'=> $ctax[$j],
            				'tax_amount'=> $taxamt[$j],
            				'total_price'=> $totalprice[$j],
            				'issue_id'=> $isid[$m],
            				'request_item_id'=> $creq_item_id[$j],
            				'recipe_id'=> $citem_id[$j],
            				'rcv_status'=> 'Closed',
            				'recipe_no'=> $recipe_no[$j],
            				'pr_cat_id'=> $pr_cat_id[$j]
            			);
            			$this->db->insert('product_issues_item', $data1);
            			$isitid = $this->db->insert_id();
            			
            			$data2 = array(
            				'item_name'=> $citem_name[$j],
            				'trans_ref_no'=> $tr_ref[$m],
            				'trans_type'=>'PrIssue',
            				'transaction_date'=> $issue_date,
            				'opening_stock'=> $qty[$j],
            				'out_qty'=> $cinward_quantity[$j],
            				'closing_stock'=> $qty1[$j],
            				'last_modified'=> $lm,
            				'branchname'=> $cbranch_name,
            				'item_id'=> $citem_id[$j],
            				'recipe_no'=> $recipe_no[$j],
            				'category_id'=> $pr_cat_id[$j],
            				'branch_id'=> $cbranch_id
            			);
            			$this->db->insert('stock_detail', $data2);
            			
            			$rc_qty[$j] = $cinward_quantity[$j] + $creceived_qty[$j];
            			$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$creq_item_id[$j]'");
            			
            			if($rc_qty[$j] == $cquantity[$j] && $cinward_quantity[$j]!='') {
            				$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$creq_item_id[$j]'");
            			}
            		}
            
            		$qty[$j] = $cinward_quantity[$j] + $creceived_qty[$j];
                    $qty2[$j] = $cinward_quantity[$j] + $creceived_qty[$j];
                    
                    $q1 = $this->db->query("update product_request_item set receipt_qty='$qty[$j]' where request_item_id='$creq_item_id[$j]'");
                    $q2 = $this->db->query("update product_issues_item set rec_issue_qty='$qty2[$j]' where issue_order_id='$isitid'");
            
                    $q3 = $this->db->query("select * from recipe where recipe_no='$recipe_no[$j]' and branch_id='$coutlet_branchid[$m]'");
                    if($q3->num_rows() > 0)
                    {
                    	$row = $q3->row_array();
                        $stock[$j] = $row['production_stock'];
                        $vrcp_id[$j] = $row['recipe_id'];
                        $vrcp_no[$j] = $row['recipe_no'];
                        $stk[$j] = $stock[$j] + $cinward_quantity[$j];
                        
                        $q2 = $this->db->query("UPDATE recipe set production_stock='$stk[$j]' WHERE  recipe_no='$recipe_no[$j]' and branch_id='$coutlet_branchid[$m]'");
            
                        $data1 = array(
                            'item_name'=> $citem_name[$j],
                            'quantity'=> $cinward_quantity[$j],
                            'issued_qty'=> $cinward_quantity[$j],
                            'price'=> $cprice[$j],
                            'total_price'=> $ctot_basic[$j],
                            'req_rec_id'=> $inid[$m],
                            'request_item_id'=> $creq_item_id[$j],
                            'issue_order_id'=> $isitid,
                            'recipe_id'=> $vrcp_id[$j],
                            'recipe_no'=> $vrcp_no[$j],
                            'pr_cat_id'=> $pr_cat_id[$j]
                        );
                        $this->db->insert('product_req_rec_item', $data1);
                        $initid = $this->db->insert_id();
            
                        $data2 = array(
                            'item_name'=> $citem_name[$j],
                            'trans_ref_no'=> $tr_ref1[$m],
                            'trans_type'=>'PrIssue',
                            'transaction_date'=> $issue_date,
                            'opening_stock'=> $stock[$j],
                            'in_qty'=> $cinward_quantity[$j],
                            'closing_stock'=> $stk[$j],
                            'last_modified'=> $lm,
                            'branchname'=> $coutlet_branch[$m],
                            'item_id'=> $vrcp_id[$j],
                            'recipe_no'=> $vrcp_no[$j],
                            'category_id'=> $pr_cat_id[$j],
                            'branch_id'=> $coutlet_branchid[$m]
                        );
                        $this->db->insert('stock_detail', $data2);
            
                        if(empty($cinward_quantity[$j])) {
                            $q3 = $this->db->query("DELETE FROM product_req_rec WHERE receipt_item_id='$initid'");
                        }
                        
                        $q4 = $this->db->query("select recieved_qty,receipt_qty from product_request_item where request_item_id='$creq_item_id[$j]'");
                        if($q4->num_rows() > 0)
                        {
                            $row2 = $q4->row_array();
                            $issued_quantity[$j] = $row2['receipt_qty'];
                        }
            
                        if($issued_quantity[$j] == $cquantity[$j] && $cinward_quantity[$j]!='') {
                            $q5 = $this->db->query("update product_request_item set receipt_status='Closed' where request_item_id='$creq_item_id[$j]'");
                        }
            
                        $q6 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$creq_id[$m]'");
                        if($q6->num_rows() > 0)
                        {
                            $row1 = $q6->row_array();
                            $count1 = $row1['num'];
                        }
                        $q7 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where receipt_status='Closed' and request_id='$creq_id[$m]'");
                        if($q7->num_rows() > 0)
                        {
                            $row2 = $q7->row_array();
                            $count2 = $row2['num1'];
                        }
                        if($count1 == $count2 ) {
                            $q8 = $this->db->query("update product_request set receipt_status='Closed',status='Closed' where request_id='$creq_id[$m]'");
                       }
                    }
            	}
            }
			
			for($i=0;$i<$ctr;$i++)
			{
				$isq = $this->db->query("select * from product_issues_item where issue_id='$isid[$i]'");
				if($isq->num_rows() > 0)
				{
					//nothing
				}
				else
				{
					$this->db->query("delete from product_issues_creation where issue_id='$isid[$i]'");
				}
				$inq = $this->db->query("select * from product_req_rec_item where req_rec_id='$inid[$i]'");
				if($inq->num_rows() > 0)
				{
					//nothing
				}
				else
				{
					$this->db->query("delete from product_req_rec where req_rec_id='$inid[$i]'");
				}
			}
			//return $isid;
		}
		
		public function getPrdRequestItm($req_id,$branch_id)
		{
			$query = $this->db->query("SELECT *, (PI.required_qty-PI.recieved_qty) as item_balance_qty FROM product_request P inner join product_request_item PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where PI.request_id='$req_id' and R.branch_id='$branch_id'");
			return $query->result();
		}
		
		public function addSplOrdIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id5);
			$result = $this->db->get('product_issues_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "ISSU"."/".$branchname5."/".$outlet_branch5."/".$is_no;
			
			$k=0;
			for($j=0;$j<count($inward_quantity5);$j++)
			{
				if($inward_quantity5[$j]!='0' && $inward_quantity5[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$data = array('is_no'=> $is_no,
				'issue_no'=> $issue_no,
				'issue_date'=> $issue_date,
				'issue_time'=> $issue_time,
				'outlet'=> $dept5,
				'branchname'=> $branchname5,
				'request_id'=> $req_id5,
				'issue_status'=> 'Pending',
				'total_amount'=> $gtotal5,
				'user_name'=> $uname5,
				'branch_id'=> $branch_id5
			);
			$this->db->insert('product_issues_creation', $data);
			$isid = $this->db->insert_id();
		}
			if(isset($isid))
			{
			//insert of items
			$icount = count($item_name5);
			for($j=0;$j<$icount;$j++)
			{				
				if($inward_quantity5[$j]!='')
				{
					$q1 = $this->db->query("select * from recipe where recipe_id='$item_id5[$j]'");
					if($q1->num_rows() > 0)
					{
						$row = $q1->row_array();
						$qty[$j] = $row['production_stock'];
						$qty1[$j] = $qty[$j] - $inward_quantity5[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$item_id5[$j]'");
						
						//calculation
				        $totbasic[$j] = round(($price5[$j] * $inward_quantity5[$j]),2);
					    $taxamt[$j] = round((($totbasic[$j] * $tax5[$j])/100),2);
					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
						
						$data1 = array(
							'issue_item_name'=> $item_name5[$j],
							'issue_uom'=> $uom5[$j],
							'issued_qty'=> $inward_quantity5[$j],
							'price'=> $price5[$j],
							'basic_price'=> $totbasic[$j],
							'tax'=> $tax5[$j],
							'tax_amount'=> $taxamt[$j],
							'total_price'=> $totalprice[$j],
							'issue_remarks'=> $remarks5[$j],
							'issue_id'=> $isid,
							'request_item_id'=> $req_item_id5[$j],
							'recipe_id'=> $item_id5[$j],
							'recipe_no'=> $recipe_no5[$j],
							'pr_cat_id'=> $cat_id5[$j],
							'rcv_status'=> 'Pending'
						);
						$this->db->insert('product_issues_item', $data1);
						$isitid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $item_name5[$j],
							'trans_ref_no'=> $issue_no,
							'trans_type'=>'PrIssue',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $qty[$j],
							'out_qty'=> $inward_quantity5[$j],
							'closing_stock'=> $qty1[$j],
							'last_modified'=> $lm,
							'branchname'=> $branchname5,
							'item_id'=> $item_id5[$j],
							'recipe_no'=> $recipe_no5[$j],
							'category_id'=> $cat_id5[$j],
							'branch_id'=> $branch_id5
						);
						$this->db->insert('stock_detail', $data2);
						
						$rc_qty[$j] = $inward_quantity5[$j] + $received_qty5[$j];
						$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$req_item_id5[$j]'");
						
						if($rc_qty[$j] == $req_qty5[$j] && $inward_quantity5[$j]!='') {
							$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$req_item_id5[$j]'");
						}
						
						$q6 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id5'");
						if($q6->num_rows() > 0)
						{
							$row1 = $q6->row_array();
							$count1 = $row1['num'];
						}
						$q7 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where status='Closed' and request_id='$req_id5'");
						if($q7->num_rows() > 0)
						{
							$row2 = $q7->row_array();
							$count2 = $row2['num1'];
						}
						if($count1 == $count2 ) {
							$q8 = $this->db->query("update product_request set issue_status='Closed' where request_id='$req_id5'");
						}
					}
				}
			}
		}
			//return $isid;
		}
		
		public function addProductionSplOrdIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$icount = count($item_name5);
			
			$rs = $this->db->query("select * from billdetails where orderdate='$issue_date' and branch_id='$branch_id5'");
			if($rs->num_rows() > 0)
			{
				$r1 = $rs->row_array();
				$bill_id = $r1['billno'];
    			$bill_no = $r1['bill_id'];
    			$bno = $r1['bno'];
				
				for($i=0;$i<$icount;$i++)
				{
					if($inward_quantity5[$i]!='0' && $inward_quantity5[$i]!='')
    				{
						$q3 = $this->db->query("select * from recipe where recipe_id='$item_id5[$i]'");
						if($q3->num_rows() > 0)
						{
							$rs1 = $q3->row_array();
							$pstock[$i] = $rs1['production_stock'];
    						$pstock1[$i] = $pstock[$i] + $inward_quantity5[$i];
    						
    						//$rc_yield[$i] = $rs1['yeild'];
    						if($rs1['yeild'] =='0'){
							    $rc_yield[$i] = '1';
							}else{
							    $rc_yield[$i] = $rs1['yeild'];
							}
    						$recipe_no[$i] = $rs1['recipe_no'];
    					    $pr_cat_id[$i] = $rs1['pr_cat_id'];
							
							$q4 = $this->db->query("update recipe set production_stock='$pstock1[$i]' where recipe_id='$item_id5[$i]'");
						}
						$res2 = $this->db->query("select * from bom_product where billno='$bill_id' and pname='$item_name5[$i]'");
						if($res2->num_rows() > 0)
						{
							$r2 = $res2->row_array();
							$bp_id[$i] = $r2['bp_id'];
    						$qty[$i] = $r2['actual_yeild'];
    						$qty1[$i] = $qty[$i] + $inward_quantity5[$i];
							$sql2 = $this->db->query("update bom_product set actual_yeild='$qty1[$i]' where bp_id='$bp_id[$i]'");
						}
						else
						{
							$bdata = array('pname'=> $item_name5[$i],
								'uom'=> $uom5[$i],
								'actual_yeild'=> $inward_quantity5[$i],
								'yeild'=> $yeild5[$i],
								'billno'=> $bill_id,
								'recipe_id'=> $item_id5[$i],
								'recipe_no'=> $recipe_no[$i],
								'pr_cat_id'=> $pr_cat_id[$i]
							);
							$this->db->insert('bom_product', $bdata);
						}
						$bdata1 = array('item_name'=> $item_name5[$i],
							'trans_ref_no'=> $bill_no,
							'trans_type'=>'PrInward',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $pstock[$i],
							'in_qty'=> $inward_quantity5[$i],
							'closing_stock'=> $pstock1[$i],
							'last_modified'=> $lm,
							'branchname'=> $branchname5,
							'item_id'=> $item_id5[$i],
							'recipe_no'=> $recipe_no[$i],
							'category_id'=> $pr_cat_id[$i],
							'branch_id'=> $branch_id5
						);
						$this->db->insert('stock_detail', $bdata1);
					//}
					
					//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"."/".$branchname5."/".$bno;
    				        $q3 = $this->db->query("select * from item_creation where itm_id='$rm_itemno' and branch_id='$branch_id5'");
    				        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*$inward_quantity5[$i])/$rc_yield[$i]),2);
    					        
    					        $data4 = array(
                					'item_name' => $rm_name,
                					'trans_ref_no' => $cons_no,
                					'trans_type'=>'Consumption',
                					'transaction_date' => $issue_date,
                					'opening_stock' => $rmstock,
                					'closing_stock' => $rmstock,
                					'consumption_qty' => $rmconsumption,
                					'last_modified' => $lm,
                					'branchname' => $branchname5,
                					'item_id' => $rmitemid,
                					'itm_id' => $rm_itemno,
                					'category_id' => $rmcatid,
                					'branch_id' => $branch_id5
                				);
                				$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"."/".$branchname5."/".$bno;
    				        $q3 = $this->db->query("select * from recipe where recipe_no='$rc_itemno' and branch_id='$branch_id5'");
    				        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*$inward_quantity5[$i])/$rc_yield[$i]),2);
    					        
    					        $data4 = array(
                					'item_name' => $rc_name,
                					'trans_ref_no' => $cons_no,
                					'trans_type'=>'Consumption',
                					'transaction_date' => $issue_date,
                					'opening_stock' => $rcstock,
                					'closing_stock' => $rcstock,
                					'consumption_qty' => $rcconsumption,
                					'last_modified' => $lm,
                					'branchname' => $branchname5,
                					'item_id' => $rcitemid,
                					'recipe_no' => $rc_itemno,
                					'category_id' => $rccatid,
                					'branch_id' => $branch_id5
                				);
                				$this->db->insert('stock_detail', $data4);
    					    }
    				    }
    				}
				}
				}
			}
			else
			{
				$this->db->select('MAX(bno) as bno',false);
				$this->db->where('branch_id',$branch_id5);
				$result = $this->db->get('billdetails')->row();
				$bno = $result->bno;
				if($bno == 0){
					$bno = 1;
				}
				else{
					$bno = $bno+1;
				}
				$bill_no = "BOM"."/".$branchname5."/".$bno;
				
				$k=0;
    			for($j=0;$j<count($inward_quantity5);$j++)
    			{
    				if($inward_quantity5[$j]!='0' && $inward_quantity5[$j]!='')
    				{
    					$k=$k+1;
    				}
    			}
    			if($k>0)
			{
				$bdata2 = array('bno'=> $bno,
					'bill_id'=> $bill_no,
					'pr_cat_id'=> $pcat_id5,
					'orderdate'=> $issue_date,
					'ordertime'=> $issue_time,
					'outlet'=> $uname5,
					'branch_id'=> $branch_id5
				);
				$this->db->insert('billdetails', $bdata2);
				$bill_id = $this->db->insert_id();
			}
				if($bill_id)
				{
				if($item_name5 != '')
    			{
					for($i=0;$i<$icount;$i++)
					{
						if($inward_quantity5[$i]!='0' && $inward_quantity5[$i]!='')
    					{
							$q3 = $this->db->query("select * from recipe where recipe_id='$item_id5[$i]'");
							if($q3->num_rows() > 0)
							{
								$rs = $q3->row_array();
								$pstock[$i] = $rs['production_stock'];
    							$pstock1[$i] = $pstock[$i] + $inward_quantity5[$i];
    							//$rc_yield[$i] = $rs['yeild'];
    							if($rs['yeild'] =='0'){
    							    $rc_yield[$i] = '1';
    							}else{
    							    $rc_yield[$i] = $rs['yeild'];
    							}
    							$recipe_no[$i] = $rs['recipe_no'];
    					        $pr_cat_id[$i] = $rs['pr_cat_id'];
								
								$q4 = $this->db->query("update recipe set production_stock='$pstock1[$i]' where recipe_id='$item_id5[$i]'");
							}
							$bdata3 = array('pname'=> $item_name5[$i],
								'uom'=> $uom5[$i],
								'actual_yeild'=> $inward_quantity5[$i],
								'yeild'=> $yeild5[$i],
								'billno'=> $bill_id,
								'recipe_id'=> $item_id5[$i],
								'recipe_no'=> $recipe_no[$i],
								'pr_cat_id'=> $pr_cat_id[$i]
							);
							$this->db->insert('bom_product', $bdata3);
							
							$bdata4 = array('item_name'=> $item_name5[$i],
								'trans_ref_no'=> $bill_no,
								'trans_type'=>'PrInward',
								'transaction_date'=> $issue_date,
								'opening_stock'=> $pstock[$i],
								'in_qty'=> $inward_quantity5[$i],
								'closing_stock'=> $pstock1[$i],
								'last_modified'=> $lm,
								'branchname'=> $branchname5,
								'item_id'=> $item_id5[$i],
								'recipe_no'=> $recipe_no[$i],
								'category_id'=> $pr_cat_id[$i],
								'branch_id'=> $branch_id5
							);
							$this->db->insert('stock_detail', $bdata4);
						//}
						
						//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"."/".$branchname5."/".$bno;
        				        $q3 = $this->db->query("select * from item_creation where itm_id='$rm_itemno' and branch_id='$branch_id5'");
        				        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*$inward_quantity5[$i])/$rc_yield[$i]),2);
        					        
        					        $data4 = array(
                    					'item_name' => $rm_name,
                    					'trans_ref_no' => $cons_no,
                    					'trans_type'=>'Consumption',
                    					'transaction_date' => $issue_date,
                    					'opening_stock' => $rmstock,
                    					'closing_stock' => $rmstock,
                    					'consumption_qty' => $rmconsumption,
                    					'last_modified' => $lm,
                    					'branchname' => $branchname5,
                    					'item_id' => $rmitemid,
                    					'itm_id' => $rm_itemno,
                    					'category_id' => $rmcatid,
                    					'branch_id' => $branch_id5
                    				);
                    				$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"."/".$branchname5."/".$bno;
        				        $q3 = $this->db->query("select * from recipe where recipe_no='$rc_itemno' and branch_id='$branch_id5'");
        				        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*$inward_quantity5[$i])/$rc_yield[$i]),2);
        					        
        					        $data4 = array(
                    					'item_name' => $rc_name,
                    					'trans_ref_no' => $cons_no,
                    					'trans_type'=>'Consumption',
                    					'transaction_date' => $issue_date,
                    					'opening_stock' => $rcstock,
                    					'closing_stock' => $rcstock,
                    					'consumption_qty' => $rcconsumption,
                    					'last_modified' => $lm,
                    					'branchname' => $branchname5,
                    					'item_id' => $rcitemid,
                    					'recipe_no' => $rc_itemno,
                    					'category_id' => $rccatid,
                    					'branch_id' => $branch_id5
                    				);
                    				$this->db->insert('stock_detail', $data4);
        					    }
        				    }
        				}
					}
					}
				}
			}
			}
			
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id5);
			$result = $this->db->get('product_issues_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "ISSU"."/".$branchname5."/".$outlet_branch5."/".$is_no;
			
			$k=0;
			for($j=0;$j<count($inward_quantity5);$j++)
			{
				if($inward_quantity5[$j]!='0' && $inward_quantity5[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$data = array('is_no'=> $is_no,
				'issue_no'=> $issue_no,
				'issue_date'=> $issue_date,
				'issue_time'=> $issue_time,
				'outlet'=> $dept5,
				'branchname'=> $branchname5,
				'request_id'=> $req_id5,
				'issue_status'=> 'Pending',
				'total_amount'=> $gtotal5,
				'user_name'=> $uname5,
				'branch_id'=> $branch_id5
			);
			$this->db->insert('product_issues_creation', $data);
			$isid = $this->db->insert_id();
			}
			
			if($isid)
			{
			//insert of items
			$icount = count($item_name5);
			for($j=0;$j<$icount;$j++)
			{				
				if($inward_quantity5[$j]!='')
				{
					$q1 = $this->db->query("select * from recipe where recipe_id='$item_id5[$j]'");
					if($q1->num_rows() > 0)
					{
						$row = $q1->row_array();
						$qty[$j] = $row['production_stock'];
						$qty1[$j] = $qty[$j] - $inward_quantity5[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$j]' WHERE  recipe_id='$item_id5[$j]'");
						
						//calculation
				        $totbasic[$j] = round(($price5[$j] * $inward_quantity5[$j]),2);
					    $taxamt[$j] = round((($totbasic[$j] * $tax5[$j])/100),2);
					    $totalprice[$j] = round(($totbasic[$j] + $taxamt[$j]),2);
						
						$data1 = array(
							'issue_item_name'=> $item_name5[$j],
							'issue_uom'=> $uom5[$j],
							'issued_qty'=> $inward_quantity5[$j],
							'price'=> $price5[$j],
							'basic_price'=> $totbasic[$j],
							'tax'=> $tax5[$j],
							'tax_amount'=> $taxamt[$j],
							'total_price'=> $totalprice[$j],
							'issue_remarks'=> $remarks5[$j],
							'issue_id'=> $isid,
							'request_item_id'=> $req_item_id5[$j],
							'recipe_id'=> $item_id5[$j],
							'recipe_no'=> $recipe_no5[$j],
							'pr_cat_id'=> $cat_id5[$j],
							'rcv_status'=> 'Pending'
						);
						$this->db->insert('product_issues_item', $data1);
						$isitid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $item_name5[$j],
							'trans_ref_no'=> $issue_no,
							'trans_type'=>'PrIssue',
							'transaction_date'=> $issue_date,
							'opening_stock'=> $qty[$j],
							'out_qty'=> $inward_quantity5[$j],
							'closing_stock'=> $qty1[$j],
							'last_modified'=> $lm,
							'branchname'=> $branchname5,
							'item_id'=> $item_id5[$j],
							'recipe_no'=> $recipe_no5[$j],
							'category_id'=> $cat_id5[$j],
							'branch_id'=> $branch_id5
						);
						$this->db->insert('stock_detail', $data2);
						
						$rc_qty[$j] = $inward_quantity5[$j] + $received_qty5[$j];
						$q3 = $this->db->query("update product_request_item set recieved_qty='$rc_qty[$j]' where request_item_id='$req_item_id5[$j]'");
						
						if($rc_qty[$j] == $req_qty5[$j] && $inward_quantity5[$j]!='') {
							$q4 = $this->db->query("update product_request_item set status='Closed' where request_item_id='$req_item_id5[$j]'");
						}
						
						$q6 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id5'");
						if($q6->num_rows() > 0)
						{
							$row1 = $q6->row_array();
							$count1 = $row1['num'];
						}
						$q7 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where status='Closed' and request_id='$req_id5'");
						if($q7->num_rows() > 0)
						{
							$row2 = $q7->row_array();
							$count2 = $row2['num1'];
						}
						if($count1 == $count2 ) {
							$q8 = $this->db->query("update product_request set issue_status='Closed' where request_id='$req_id5'");
						}
					}
				}
			}
		}
			//return $isid;
		}
		
		public function getIssuetList($branch_id)
		{
			$query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where I.branch_id='$branch_id' ORDER BY I.issue_id desc");
			return $query->result();
		}
		
		public function getIssuetCList($username)
		{
			$query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where I.outlet='$username' ORDER BY I.issue_id desc");
			return $query->result();
		}
		
				
		public function getIssue($isid)
		{
			$query = $this->db->query("SELECT *,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date,R.branchname as out_branch FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where I.issue_id='$isid'");
			return $query->result();
		}
		
		public function getIssueItem($isid)
		{
			$query = $this->db->query("SELECT * FROM product_request R inner join product_request_item RI on R.request_id=RI.request_id inner join product_issues_item PI on RI.request_item_id=PI.request_item_id where PI.issue_id='$isid' and PI.issued_qty !=''");
			return $query->result();
		}
				
		public function getRequesIssuetLists($req_id)
		{
			$query = $this->db->query("SELECT *,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where I.request_id='$req_id'");
			return $query->result();
		}
		
		public function getIssuedItemDetail($isid)
		{
			$query = $this->db->query("SELECT *, RI.recipe_id as itm_id,(RI.required_qty-RI.recieved_qty) as item_balance_qty FROM product_request R inner join product_request_item RI on  R.request_id=RI.request_id inner join product_issues_item PI on RI.request_item_id=PI.request_item_id inner join  recipe RC on PI.recipe_id=RC.recipe_id where PI.issue_id='$isid'");
			return $query->result();
		}
		
		public function addInward($postdata)
		{
			extract($postdata);
			$inward_date = date("Y-m-d");
			$inward_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			$this->db->select('MAX(rec_no) as rec_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('product_req_rec')->row();
			$rec_no = $result->rec_no;
			if($rec_no == 0){
				$rec_no = 1;
			}
			else{
				$rec_no = $rec_no+1;
			}
			$receipt_no = "PR/IN"."/".$branchname."/".$rec_no;
			
			$k=0;
			$gtotal=0;
			for($j=0;$j<count($in_quantity);$j++)
			{
				if($in_quantity[$j]!='0' && $in_quantity[$j]!='')
				{
					$k=$k+1;
				    $totalprice[$j] = round(((float)$price[$j] * (float)$in_quantity[$j]),2);
    				$gtotal=$gtotal+$totalprice[$j];
				}
			}
			if($k>0)
			{
    			$data = array('rec_no'=> $rec_no,
    				'req_rec_no'=> $receipt_no,
    				'receipt_date'=> $inward_date,
    				'receipt_time'=> $inward_time,
    				'branchname'=> $branchname,
    				'dept'=> $dept,
    				'request_id'=> $req_id,
    				'issue_id'=> $iss_id,
    				'total_amount'=> $gtotal,
    				'branch_id'=> $branch_id
    			);
    			$this->db->insert('product_req_rec', $data);
    			$inid = $this->db->insert_id();
			}
			
			//insert of items
			if(isset($inid))
			{
			$icount = count($item_name);
			for($j=0;$j<$icount;$j++)
			{				
				if($in_quantity[$j] != '0' && $in_quantity[$j] != '')
				{
					$qty[$j] = $in_quantity[$j] + $received_qty[$j];
					$qty2[$j] = $in_quantity[$j] + $rec_issue_qty[$j];
					
					$q1 = $this->db->query("update product_request_item set receipt_qty='$qty[$j]' where request_item_id='$req_item_id[$j]'");
					$q2 = $this->db->query("update product_issues_item set rec_issue_qty='$qty2[$j]' where issue_order_id='$issue_item_id[$j]'");
					
					$q3 = $this->db->query("select * from recipe where recipe_id='$item_id[$j]'");
					if($q3->num_rows() > 0)
					{
						$row = $q3->row_array();
						$stock[$j] = $row['production_stock'];
						$stk[$j] = $stock[$j] + $in_quantity[$j];
						
						$q2 = $this->db->query("UPDATE recipe set production_stock='$stk[$j]' WHERE  recipe_id='$item_id[$j]'");
						
						//calculation
        		        $totalprice[$j] = round(((float)$price[$j] * (float)$in_quantity[$j]),2);
        			   
						$data1 = array(
							'item_name'=> $item_name[$j],
							'quantity'=> $in_quantity[$j],
							'issued_qty'=> $issued_qty[$j],
							'price'=> $price[$j],
							'total_price'=> $totalprice[$j],
							'req_rec_id'=> $inid,
							'request_item_id'=> $req_item_id[$j],
							'issue_order_id'=> $issue_item_id[$j],
							'recipe_id'=> $item_id[$j],
							'recipe_no'=> $itm_id[$j],
							'pr_cat_id'=> $cat_id[$j]
						);
						$this->db->insert('product_req_rec_item', $data1);
						$initid = $this->db->insert_id();
						
						$data2 = array(
							'item_name'=> $item_name[$j],
							'trans_ref_no'=> $receipt_no,
							'trans_type'=>'PrInward',
							'transaction_date'=> $inward_date,
							'opening_stock'=> $stock[$j],
							'in_qty'=> $in_quantity[$j],
							'closing_stock'=> $stk[$j],
							'last_modified'=> $lm,
							'branchname'=> $branchname,
							'item_id'=> $item_id[$j],
							'recipe_no'=> $itm_id[$j],
							'category_id'=> $cat_id[$j],
							'branch_id'=> $branch_id
						);
						$this->db->insert('stock_detail', $data2);
						
						if(empty($in_quantity[$j])) {
							$q3 = $this->db->query("DELETE FROM product_req_rec WHERE receipt_item_id='$initid'");
						}
						
						$q4 = $this->db->query("select recieved_qty,receipt_qty from product_request_item where request_item_id='$req_item_id[$j]'");
						if($q4->num_rows() > 0)
						{
							$row2 = $q4->row_array();
							$issued_quantity[$j] = $row2['receipt_qty'];
						}
						
						if($issued_quantity[$j] == $req_qty[$j] && $in_quantity[$j]!='') {
							$q5 = $this->db->query("update product_request_item set receipt_status='Closed' where request_item_id='$req_item_id[$j]'");
						}
						
						$q6 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id'");
						if($q6->num_rows() > 0)
						{
							$row1 = $q6->row_array();
							$count1 = $row1['num'];
						}
						$q7 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where receipt_status='Closed' and request_id='$req_id'");
						if($q7->num_rows() > 0)
						{
							$row2 = $q7->row_array();
							$count2 = $row2['num1'];
						}
						if($count1 == $count2 ) {
							$q8 = $this->db->query("update product_request set receipt_status='Closed' where request_id='$req_id'");
							$q9 = $this->db->query("update product_request set status='Closed' where request_id='$req_id'");
						}
						$q10 = $this->db->query("select issued_qty,rec_issue_qty from product_issues_item where issue_order_id='$issue_item_id[$j]'");
						if($q10->num_rows() > 0)
						{
							$row3 = $q10->row_array();
							$isd_qty[$j] = $row3['issued_qty'];
							$rcvd_qty[$j] = $row3['rec_issue_qty'];
							
							if($isd_qty[$j] == $rcvd_qty[$j])
							{
								$q11 = $this->db->query("update product_issues_item set rcv_status='Closed' where issue_order_id='$issue_item_id[$j]'");
							}
							
							$q12 = $this->db->query("SELECT count(*) as `num` FROM product_issues_item where issue_id='$iss_id'");
							if($q12->num_rows() > 0)
							{
								$row4 = $q12->row_array();
								$count3 = $row4['num'];
							}
							$q13 = $this->db->query("SELECT count(*) as `num1` FROM product_issues_item where rcv_status='Closed' and issue_id='$iss_id'");
							if($q13->num_rows() > 0)
							{
								$row5 = $q13->row_array();
								$count4 = $row5['num1'];
							}
							if($count3 == $count4 ) {
								$q14 = $this->db->query("update product_issues_creation set issue_status='Accepted' where issue_id='$iss_id'");
							}
						}
					}
				}
			}
		}
		if(isset($inid))
		{
			return $inid;
		}
		}
		
		public function getInwardtList($branch_id) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(RC.receipt_date, '%d-%m-%Y') as receipt_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id inner join product_req_rec RC on I.issue_id=RC.issue_id where RC.branch_id='$branch_id' ORDER BY RC.req_rec_id desc");
			return $query->result();
		}
		
		public function getInward($inid) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(RC.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(PI.issue_date, '%d-%m-%Y') as issue_date FROM product_request R inner join product_issues_creation PI on R.request_id=PI.request_id inner join product_req_rec RC on PI.issue_id=RC.issue_id where RC.req_rec_id='$inid'");
			return $query->result();
		}
		
		public function getInwardItem($inid) {
			$query = $this->db->query("SELECT *,PS.issued_qty as iss_qty FROM product_request P inner join product_request_item PI on P.request_id=PI.request_id inner join product_issues_item PS on PI.request_item_id=PS.request_item_id inner join product_req_rec_item PC on PS.issue_order_id=PC.issue_order_id where PC.req_rec_id='$inid' and PC.quantity != ''");
			return $query->result();
		}
		
		public function getCInwardtList($branch_id) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(RC.receipt_date, '%d-%m-%Y') as receipt_date FROM product_issues_creation I inner join product_req_rec RC on I.issue_id=RC.issue_id where RC.branch_id='$branch_id' and I.branchname like '%VNK%' ORDER BY RC.req_rec_id desc");
			return $query->result();
		}
		
		public function getCInward($inid) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(RC.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(PI.issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation PI inner join product_req_rec RC on PI.issue_id=RC.issue_id where RC.req_rec_id='$inid'");
			return $query->result();
		}
		
		public function getCInwardItem($inid) {
			$query = $this->db->query("SELECT *,PS.issued_qty as iss_qty FROM product_issues_item PS inner join product_req_rec_item PC on PS.issue_order_id=PC.issue_order_id where PC.req_rec_id='$inid' and PC.quantity != ''");
			return $query->result();
		}
		
		public function getRequestClosedList($branch_id) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(request_date, '%d-%m-%Y') as request_date FROM product_request where branch_id='$branch_id' and receipt_status='Closed' ORDER BY request_id desc");
			return $query->result();
		}
		
		public function getPrdReceipt($rid) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(RC.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id inner join product_req_rec RC on I.issue_id=RC.issue_id inner join product_req_rec_item RI on RC.req_rec_id=RI.req_rec_id where RC.request_id='$rid' group by RI.req_rec_id ORDER BY RC.req_rec_id desc");
			return $query->result();
		}
		
		public function getIssueClosedList($uname) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join user L on R.branch_id=L.branch_id where R.status='Closed' and R.dept='$uname' ORDER BY R.request_id desc");
			return $query->result();
		}
		
		public function getPrdRequestIss($rid) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date FROM product_request P inner join user L on P.branch_id=L.branch_id where P.request_id='$rid'");
			return $query->result();
		}
		
		public function getPrdIssues($rid) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,R.branchname as branch FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id inner join product_issues_item IT on I.issue_id=IT.issue_id where I.request_id='$rid' group by I.issue_id ORDER BY I.issue_id desc");
			return $query->result();
		}
		
		public function getIssuePendingList($uname) {
		//	$query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(P.approval_date, '%d-%m-%Y') as approval_date FROM product_request P inner join user L on P.branch_id=L.branch_id where (P.status='Pending' or status='Approved') and P.dept='$uname' ORDER BY request_id desc");
			$query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(P.approval_date, '%d-%m-%Y') as approval_date FROM product_request P inner join user L on P.branch_id=L.branch_id where (P.issue_status='Pending') and P.dept='$uname' ORDER BY request_id desc");
			return $query->result();
		}
		
		public function get_santhe_req_item($req_date,$dept)
		{
			$req_date = date("Y-m-d", strtotime($req_date));
			$query = $this->db->query("SELECT DISTINCT(PI.item_name) from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$dept' and P.receipt_status='Pending' and (P.request_date='$req_date') and (P.branchname like '%ST%') group by P.branchname,PI.item_name order by PI.item_name");
			return $query->result();
		}

		public function get_santhe_req_qty($req_date,$dept)
		{
			$req_date = date("Y-m-d", strtotime($req_date));
			$query = $this->db->query("SELECT PI.item_name,sum(PI.required_qty) as required_qty,P.branchname from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$dept' and P.receipt_status='Pending' and (P.request_date='$req_date') and (P.branchname like '%ST%') group by P.branchname,PI.item_name order by PI.item_name");
			return $query->result();
		}

		public function get_req_item_special($req_date,$dept)
		{
			$req_date = date("Y-m-d", strtotime($req_date));
			$query = $this->db->query("SELECT DISTINCT(PI.item_name) from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$dept' and (P.request_date='$req_date') and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and P.orders='Special Order' group by P.branchname,PI.item_name order by PI.item_name");
			return $query->result();
		}
		public function get_req_qty_special($req_date,$dept)
		{
			$req_date = date("Y-m-d", strtotime($req_date));
			$query = $this->db->query("SELECT PI.item_name,sum(PI.required_qty) as required_qty,P.branchname from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$dept' and (P.request_date='$req_date') and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and P.orders='Special Order' group by P.branchname,PI.item_name order by PI.item_name");
			return $query->result();
		}
		public function get_req_item_regular($req_date,$dept)
		{
			$req_date = date("Y-m-d", strtotime($req_date));
			//$query = $this->db->query("SELECT DISTINCT(PI.item_name) from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$dept' and (P.request_date='$req_date') and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and P.orders='Regular Order' and P.status='Pending' group by P.branchname,PI.item_name order by PI.item_name");
			
			$query = $this->db->query("select DISTINCT(PI.item_name) from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$dept' and (P.request_date='$req_date') and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and P.orders='Regular Order' and P.status='Pending' group by P.branchname,PI.item_name order by PI.item_name");
			return $query->result();
		}

		public function get_req_qty_regular($req_date,$dept)
		{
			$req_date = date("Y-m-d", strtotime($req_date));
			$query = $this->db->query("SELECT PI.item_name,sum(PI.required_qty) as required_qty,P.branchname from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$dept' and (P.request_date='$req_date') and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and P.orders='Regular Order' and P.status='Pending' group by P.branchname,PI.item_name order by PI.item_name");
			
			return $query->result();
		}
		
		public function getIndentItemsKit($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$branch_id = $data['branch_id'];
			$branch = $data['department'];
			$query = $this->db->query("select PI.item_name,SUM(CASE WHEN P.branchname = 'CW-CTR' THEN PI.required_qty ELSE 0 END) ctr1,SUM(CASE WHEN P.branchname = 'CW-CTR2' THEN PI.required_qty ELSE 0 END) ctr2,SUM(CASE WHEN P.branchname = 'CW-CTR3' THEN PI.required_qty ELSE 0 END) ctr3 ,SUM(CASE WHEN P.branchname = 'CW-CTR4' THEN PI.required_qty ELSE 0 END) ctr4 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and P.branchname='$branch' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getIndentItemsKitchen($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			$branch_id = $data['branch_id'];
			$brname = $data['brname'];
			
			if($uname == 'Commissary')
			{
			    $query = $this->db->query("select PI.item_name,R.recipe_id,R.pr_cat_id,R.uom,R.yeild,R.production_stock,SUM(CASE WHEN P.branchname = 'CW-CTR' THEN PI.required_qty ELSE 0 END) ctr1,SUM(CASE WHEN P.branchname = 'CW-CTR2' THEN PI.required_qty ELSE 0 END) ctr2,SUM(CASE WHEN P.branchname = 'CW-CTR3' THEN PI.required_qty ELSE 0 END) ctr3 ,SUM(CASE WHEN P.branchname = 'CW-CTR4' THEN PI.required_qty ELSE 0 END) ctr4 from product_request  P inner join product_request_item  PI on  P.request_id=PI.request_id inner join recipe R on PI.item_name=R.rname where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and R.branchname='$brname' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			    //$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.item_name=R.rname where P.dept='$uname' and (P.request_date='$r_date') and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and P.orders='Regular Order' and P.status='Pending' and R.branchname='$brname' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			}
			else
			{
			    $query = $this->db->query("select PI.item_name,R.recipe_id,R.pr_cat_id,R.uom,R.yeild,R.production_stock,SUM(CASE WHEN P.branchname = 'CW-CTR' THEN PI.required_qty ELSE 0 END) ctr1,SUM(CASE WHEN P.branchname = 'CW-CTR2' THEN PI.required_qty ELSE 0 END) ctr2,SUM(CASE WHEN P.branchname = 'CW-CTR3' THEN PI.required_qty ELSE 0 END) ctr3 ,SUM(CASE WHEN P.branchname = 'CW-CTR4' THEN PI.required_qty ELSE 0 END) ctr4 from product_request P inner join product_request_item PI on P.request_id=PI.request_id inner join recipe R on PI.item_name=R.rname where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and R.branchname='$brname' group by PI.item_name order by PI.item_name");
			    //select PI.item_name,R.recipe_id,R.pr_cat_id,R.uom,R.yeild,R.production_stock,SUM(CASE WHEN P.branchname = 'CW-CTR' THEN PI.required_qty ELSE 0 END) ctr1,SUM(CASE WHEN P.branchname = 'CW-CTR2' THEN PI.required_qty ELSE 0 END) ctr2,SUM(CASE WHEN P.branchname = 'CW-CTR3' THEN PI.required_qty ELSE 0 END) ctr3 ,SUM(CASE WHEN P.branchname = 'CW-CTR4' THEN PI.required_qty ELSE 0 END) ctr4 from product_request  P inner join product_request_item  PI on  P.request_id=PI.request_id inner join recipe R on PI.item_name=R.rname where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' and (P.branchname like '%CTR%' or P.branchname like '%SVC%') group by PI.item_name order by PI.item_name");
			    //$query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.item_name=R.rname where P.dept='$uname' and (P.request_date='$r_date') and (P.branchname like '%CTR%' or P.branchname like '%SVC%') and P.orders='Regular Order' and P.status='Pending' and R.branchname='$brname' group by P.branchname,PI.item_name order by PI.item_name");
			}
			return $query->result_array();
		}
		
		
		public function get_pride_inward_list($branch_name) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where R.branchname='$branch_name' and R.dept='Pride Bakers' ORDER BY R.req_rec_id desc");
			return $query->result_array();
		}

		public function getPrideInward($inid) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where R.req_rec_id='$inid'");
			return $query->result_array();
		}

		public function getPrideInwardItem($inid) {
			$query = $this->db->query("SELECT * FROM product_request P inner join product_request_item PI on P.request_id=PI.request_id inner join product_req_rec_item RI on PI.request_item_id=RI.request_item_id where RI.req_rec_id='$inid' and RI.quantity !=''");
			return $query->result_array();
		}

		public function getPrideInwardRemark($inid)
		{
			$query = $this->db->query("select * from cmn_ticket as A inner join(select ticket_no,max(ticket_id) as maxtid,inw_id from cmn_ticket where inw_id='$inid' group by inw_id) as B on A.inw_id=B.inw_id and A.ticket_id=B.maxtid");
			return $query->result();
		}

		public function getDcUpload($inid)
		{
			$query = $this->db->query("select * from uploaded_invoice where invoice_ref_id='$inid'");
			return $query->result();
		}
		
		public function pride_orders($uname) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(P.approval_date, '%d-%m-%Y') as approval_date FROM product_request P where (P.status='Pending' or P.status='Approved') and P.dept='$uname' ORDER BY request_id desc");
			return $query->result();
		}
		
		public function pride_order_approve($req_id) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date FROM product_request P  where request_id='$req_id'");
			return $query->result();
		}
		
		public function get_request_item_detail($req_id) {
			$query = $this->db->query("SELECT * FROM  product_request_item where request_id='$req_id'");
			return $query->result();
		}
		
		public function get_recieved_product($req_id) {
			$query = $this->db->query("SELECT *, (PR.required_qty-PR.recieved_qty) as item_balance_qty,R.price as rate FROM product_request P inner join product_request_item PR on P.request_id=PR.request_id inner join recipe R on R.recipe_id=PR.recipe_id where PR.request_id='$req_id'");
			return $query->result();
		}
		
		public function get_product_request_detail($req_id) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(request_date, '%d-%m-%Y') as request_date FROM product_request where request_id='$req_id'");
			return $query->result();
		}
		
		public function add_pride_product($postdata)
		{
			extract($postdata);
			$branch_id = $_SESSION['branch_id'];
			$rcp_date = date("Y-m-d", strtotime($rcp_date));
			$lm=date("Y-m-dH:i:s",time());
			$rtime=date("H:i:s",time());
			
			$this->db->select('MAX(rec_no) as rec_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('product_req_rec')->row();
			$rec_no = $result->rec_no;
			if($rec_no == 0){
				$rec_no = 1;
			}
			else{
				$rec_no = $rec_no+1;
			}
			$receipt_no = "PR/IN"."/".$branchname."/".$rec_no;
			
			$k=0;
			for($j=0;$j<count($inward_quantity);$j++)
			{
				if($inward_quantity[$j]!='0' && $inward_quantity[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$insertValuesSQL = $insertfiles = '';
    			$randno=rand(10000, 990000);
    			foreach($_FILES['dc_upload']['name'] as $key=>$val){
    				$extension = pathinfo($_FILES["dc_upload"]["name"][$key], PATHINFO_EXTENSION);
    				$fileName = 'bill_upload/'.$randno. "_". time().".".$extension;
    				$insertfiles .= $fileName;
			}
			
			if(!empty(array_filter($_FILES['dc_upload']['name']))){
			    $data = array('rec_no'=> $rec_no,
					'req_rec_no'=> $receipt_no,
					'dc_no' => $invoice,
					'receipt_date'=> $rcp_date,
					'receipt_time'=> $rtime,
					'branchname'=> $branchname,
					'dept'=>$store,
					'request_id'=> $req_id,
					'issue_id'=> '0',
					'bill_upload'=>$insertfiles,
					'branch_id' => $branch_id
				);
				$this->db->insert('product_req_rec', $data);
			}
			else{
				$data = array('rec_no'=> $rec_no,
					'req_rec_no'=> $receipt_no,
					'dc_no' => $invoice,
					'receipt_date'=> $rcp_date,
					'receipt_time'=> $rtime,
					'branchname'=> $branchname,
					'dept'=>$store,
					'request_id'=> $req_id,
					'issue_id'=> '0',
					'branch_id' => $branch_id
				);
				$this->db->insert('product_req_rec', $data);
			}
			$rec_id = $this->db->insert_id();
			function compressImage($source, $destination, $quality) {

				$info = getimagesize($source);

				if ($info['mime'] == 'image/jpeg') 
				$image = imagecreatefromjpeg($source);

				elseif ($info['mime'] == 'image/gif') 
				$image = imagecreatefromgif($source);

				elseif ($info['mime'] == 'image/png') 
				$image = imagecreatefrompng($source);

				imagejpeg($image, $destination, $quality);

			}
			
			$targetDir = "bill_upload/";
			$allowTypes = array('jpg','png','jpeg','gif', 'JPG', 'JPEG', 'PNG', 'GIF');
	
			//$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
			if(!empty(array_filter($_FILES['dc_upload']['name']))){
				foreach($_FILES['dc_upload']['name'] as $key=>$val){
					// File upload path
					//$fileName = basename($_FILES['in_upload']['name'][$key]);
					$extension = pathinfo($_FILES["dc_upload"]["name"][$key], PATHINFO_EXTENSION);
					$fileName = $randno. "_". time().".".$extension;
					//$fileName1 = $grn_no . $fileName;
					$targetFilePath = $targetDir . $fileName;
				
					// Check whether file type is valid
					$fileType = pathinfo($_FILES["dc_upload"]["name"][$key],PATHINFO_EXTENSION);
					//$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
					if(in_array($fileType, $allowTypes)){
						// Upload file to server
						if(move_uploaded_file($_FILES["dc_upload"]["tmp_name"][$key], $targetFilePath)){
						$location = "bill_upload/".$fileName;
							compressImage($targetFilePath,$location,25);
							// Image db insert sql
							$insertValuesSQL .= "('".$location."', NOW(), '".$rec_id."','Pride Grn'),";
						}else{
							//$errorUpload .= $_FILES['in_upload']['name'][$key].', ';
						}
					}else{
						//$errorUploadType .= $_FILES['in_upload']['name'][$key].', ';
					}
				}
			
			$insertValuesSQL = trim($insertValuesSQL,',');
			$this->db->query("INSERT INTO uploaded_invoice (file_name, uploaded_on,invoice_ref_id,gstatus) VALUES $insertValuesSQL");
				//
			}
		}
		
			if($rec_id)
			{
			for($i=0;$i<count($inward_quantity);$i++) {
				
				$qty[$i] = $inward_quantity[$i] + $received_qty[$i];
				
				$totalprice[$i] = round(($inward_price[$i] * $inward_quantity[$i]),2);
				$taxamt[$i] = round((($totalprice[$i] * $rcp_tax[$i])/100),2);
				$totalamount[$i] = round(($totalprice[$i] + $taxamt[$i]),2);
				
				$sql2 =  $this->db->query("insert into product_req_rec_item (item_name,quantity,price,total_price,tax,tax_amt,total_amt,rc_remarks,req_rec_id,request_item_id,issue_order_id,recipe_id) values ('$item_name[$i]','$inward_quantity[$i]','$inward_price[$i]','$totalprice[$i]','$rcp_tax[$i]','$taxamt[$i]','$totalamount[$i]','$remarks[$i]','$rec_id','$req_item_id[$i]','0','$item_sid[$i]')");
				$last_id = $this->db->insert_id();
			
				$sql3 =  $this->db->query("update product_request_item set recieved_qty='$qty[$i]' where request_item_id='$req_item_id[$i]'");
				
				$sql4=$this->db->query("select production_stock from recipe where rname='$item_name[$i]' and branchname='$branchname'");
				
				if($sql4->num_rows() > 0)
				{
					$res = $sql4->row_array();
					$stock[$i] = $res['production_stock'];
					$stk[$i] = $stock[$i] + $inward_quantity[$i];
					
					$sql5 =  $this->db->query("UPDATE recipe set production_stock='$stk[$i]' WHERE rname='$item_name[$i]' and branchname='$branchname'");
					if($inward_quantity[$i] != '0' && $inward_quantity[$i] != '')
					{
						$sql7 = $this->db->query("insert into stock_detail(item_name,trans_ref_no,transaction_date,opening_stock,in_qty,closing_stock,last_modified,branchname,branch_id)values('$item_name[$i]','$receipt_no','$rcp_date','$stock[$i]','$inward_quantity[$i]','$stk[$i]','$lm','$branchname','$branch_id')");
					}					
				}
				else{
					//$sql5 =  mysqli_query($this->con,"insert into item_creation(item_name,uom,item_price,item_tax,stock_item,category_id,login_id,branchname)values('$item_name[$i]','$uom[$i]','0','0','$inward_quantity[$i]','$cat_id[$i]','$login_id','$branchname')");
					if($inward_quantity[$i] != '0' && $inward_quantity[$i] != '')
					{
						$sql7 = $this->db->query("insert into stock_detail(item_name,trans_ref_no,transaction_date,opening_stock,in_qty,closing_stock,last_modified,branchname,branch_id)values('$item_name[$i]','$receipt_no','$rcp_date','0','$inward_quantity[$i]','$inward_quantity[$i]','$lm','$branchname','$branch_id')");
					}	
				}
			
				if (empty($inward_quantity[$i])) {
					$sql6 =  $this->db->query("DELETE FROM product_req_rec_item WHERE receipt_item_id='$last_id'");	
				}
				if($qty[$i] == $quantity[$i] && $inward_quantity[$i]!='') {
					$status = "Closed";
					$sql8 =  $this->db->query("update product_request_item set status='$status',receipt_status='$status' where request_item_id='$req_item_id[$i]'");
				}
				else {
					//echo "failure|0";
				}
				$sql9 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id'");
			
				//$row = mysqli_fetch_array($sql9);
			    $row = $sql9->row_array();
				$count1 = $row['num'];
			
				$sql10 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where status='Closed' and request_id='$req_id'");
			
				//$row1 = mysqli_fetch_array($sql10);
			    $row1 = $sql10->row_array();
				$count2 = $row1['num1'];
							
				if($count1 == $count2 ) {
			
					$status = "Closed";
					$sql9 = $this->db->query("update product_request set status='$status',receipt_status='$status' where request_id='$req_id'");
								
				}
				else {
				
				}
			}
		}
		/*	if($branchname == 'CW-STR')
			{
				header('Location: str_pride_product_inward.php?rc_id='.$rec_id.'&page_id=pr-request-pending-list');
			}
			else
			{
				header('Location: pride_product_inward.php?rc_id='.$rec_id.'&page_id=pr-request-pending-list');
			}*/
			return $rec_id;
		}
		
		public function get_pride_product_detail($rc_id) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where R.req_rec_id='$rc_id'");
			return $query->result();
		}
		
		public function get_pride_product_inward($rcp_id) {
			$query = $this->db->query("SELECT * FROM product_request P inner join product_request_item PI on P.request_id=PI.request_id inner join product_req_rec_item RI on PI.request_item_id=RI.request_item_id where RI.req_rec_id='$rcp_id' and RI.quantity !=''");
			return $query->result();
		}
		
		public function get_inw_remark($inw_id)
		{
			//$sql = mysqli_query($this->con,"select * from cmn_ticket where inw_id='$inw_id' order by ticket_id desc");
			$query = $this->db->query("select * from cmn_ticket as A inner join(select ticket_no,max(ticket_id) as maxtid,inw_id from cmn_ticket where inw_id='$inw_id' group by inw_id) as B on A.inw_id=B.inw_id and A.ticket_id=B.maxtid");
			return $query->result();
		}
		
		public function inward_ticket($postdata)
		{
			extract($postdata);
		    $branch_id = $_SESSION['branch_id'];
			$inw_date = date("Y-m-d", strtotime($inw_date));
			$ticket_time = date("h:i A");
			
			$this->db->select('MAX(tno) as tno',false);
			$result = $this->db->get('cmn_ticket')->row();
			$tno = $result->tno;
			if($tno == 0){
				$tno = 1;
			}
			else{
				$tno = $tno+1;
			}
			$ticket_no = "TCKT"."/".$tno;
					
			
			$query = $this->db->query("insert into cmn_ticket(tno,ticket_no,ticket_date,ticket_time,remarks,inw_no,inw_date,dept,dc_no,branch,inw_id,branch_id)values('$tno','$ticket_no','$rmk_date','$ticket_time','$remark','$inw_no','$inw_date','$dept','$dc_no','$branch','$inw_id','$branch_id')");
		
		}
		
		public function addDcUpload($postdata)
		{
			extract($postdata);
			function compressImage($source, $destination, $quality) {

				$info = getimagesize($source);

				if ($info['mime'] == 'image/jpeg') 
				$image = imagecreatefromjpeg($source);

				elseif ($info['mime'] == 'image/gif') 
				$image = imagecreatefromgif($source);

				elseif ($info['mime'] == 'image/png') 
				$image = imagecreatefrompng($source);

				imagejpeg($image, $destination, $quality);

			}
			foreach($_FILES['bill_upload']['name'] as $key=>$val){
				$extension = pathinfo($_FILES["bill_upload"]["name"][$key], PATHINFO_EXTENSION);
				$fileName = rand(10000, 990000). "_". time().".".$extension;
				$insertfiles .= $fileName;
			}
			
			
			/*(!empty($_FILES['bill_upload']['name'])){
				$config['upload_path'] = 'upload/';
				$config['allowed_types'] = 'jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF';
				$filename = 'upload/'.$_FILES['bill_upload']['name'];
			}*/
			
			if(!empty(array_filter($_FILES['bill_upload']['name']))){
			    $this->db->query("update request_receipt set bill_upload='$insertfiles' where req_rec_id='$rcp_id'");
			}
			//invoice upload
			$targetDir = "bill_upload/";
			$allowTypes = array('jpg','png','jpeg','gif');
    
			if(!empty(array_filter($_FILES['bill_upload']['name']))){
				foreach($_FILES['bill_upload']['name'] as $key=>$val){
					// File upload path
					//$fileName = basename($_FILES['grn_in_upload']['name'][$key]);
					$extension = pathinfo($_FILES["bill_upload"]["name"][$key], PATHINFO_EXTENSION);
					$fileName = rand(10000, 990000). "_". time().".".$extension;
					//$fileName1 = $grn_no . $fileName;
					$targetFilePath = $targetDir . $fileName;
				
					// Check whether file type is valid
					$fileType = pathinfo($_FILES["bill_upload"]["name"][$key],PATHINFO_EXTENSION);
					//$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
					if(in_array($fileType, $allowTypes)){
						// Upload file to server
						if(move_uploaded_file($_FILES["bill_upload"]["tmp_name"][$key], $targetFilePath)){
						$location = "bill_upload/".$fileName;
							compressImage($targetFilePath,$location,25);
							// Image db insert sql
							$insertValuesSQL .= "('".$location."', NOW(), '".$rcp_id."','Pride Grn'),";
						}else{
							//$errorUpload .= $_FILES['grn_in_upload']['name'][$key].', ';
						}
					}else{
						//$errorUploadType .= $_FILES['grn_in_upload']['name'][$key].', ';
					}
				}
			
			$insertValuesSQL = trim($insertValuesSQL,',');
			$this->db->query("INSERT INTO uploaded_invoice (file_name, uploaded_on,invoice_ref_id,gstatus) VALUES $insertValuesSQL");
			}
			
			
			/*if(!empty($_FILES['bill_upload']['name'])){
				$config['upload_path'] = 'dc_upload/';
				$config['allowed_types'] = 'jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF';
				$filename = $_FILES['bill_upload']['name'];
				$file_name = 'dc_upload/'.$_FILES['bill_upload']['name'];
				$config['file_name'] = $filename;
				//Load upload library and initialize configuration
				$this->load->library('upload',$config);
				$this->upload->initialize($config);
				
				if($this->upload->do_upload('bill_upload')){
					
					$data = $this->upload->data();  
					 $config['image_library'] = 'gd2';  
					 $config['source_image'] = 'dc_upload/'.$data["file_name"];  
					 $config['create_thumb'] = FALSE;  
					 $config['maintain_ratio'] = FALSE;  
					 $config['quality'] = '60%';  
					 $config['width'] = 200;  
					 $config['height'] = 200;  
					 $config['new_image'] = 'dc_upload/'.$data["file_name"];  
					 $this->load->library('image_lib', $config);  
					 $this->image_lib->resize();  
					 $image_data = array( 
						  'file_name' => $file_name,
						  'uploaded_on'=>'NOW()',
						   'invoice_ref_id'=>$rcp_id
						  );  
					  $this->db->insert("uploaded_invoice", $image_data); 
				}else{
					$bill_upload = '';
				}
			}else{
				$bill_upload = '';
			}*/
		}
		
		public function get_inw_tickets($cdate,$c_date,$branch_name)
		{
			$query = $this->db->query("select * from cmn_ticket where ticket_date between '$cdate' and '$c_date' and branch='$branch_name' order by ticket_id desc");
			return $query->result();
		}
		
		public function get_inw_count($cdate,$c_date,$branch_name)
		{
			$query = $this->db->query("select count(*) as inwcount from cmn_ticket where ticket_date between '$cdate' and '$c_date' and branch='$branch_name' order by ticket_id desc");
			return $query->result();
		}
		
		public function get_pinw_tickets($cdate,$c_date)
		{
			$query = $this->db->query("select * from cmn_ticket where ticket_date between '$cdate' and '$c_date' order by ticket_id desc");
			return $query->result();
		}
		
		public function get_pinw_count($cdate,$c_date)
		{
			$query = $this->db->query("select count(*) as inwcount from cmn_ticket where ticket_date between '$cdate' and '$c_date' order by ticket_id desc");
			return $query->result();
		}
		
		public function get_prequest_detail($req_id) {
			$query = $this->db->query("SELECT * FROM product_request P inner join product_request_item PI on P.request_id=PI.request_id where P.request_id='$req_id'");
			return $query->result();
		}
		
		public function get_request_issue_details($req_id) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where I.request_id='$req_id'");
			return $query->result();
		}
		
		public function getAccIssuetList()
		{
			$branch_name = $_SESSION['branch_name'];
			
			if($branch_name=='CW4_CEO' || $branch_name=='CW4_ACC')
			{
			    $query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where (I.branchname='CMS' or I.branchname='CW3-CK' or I.branchname='CW4-SK') and I.outlet='CW4-Counter' ORDER BY I.issue_id desc");
			}
			else if($branch_name=='CW5_CEO' || $branch_name=='CW5-ACC')
			{
			    $query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where (I.branchname='CMS' or I.branchname='CW3-CK' or I.branchname='CW4-SK') and I.outlet='CW5-Counter' ORDER BY I.issue_id desc");
			}
			else if($branch_name=='CW6_CEO' || $branch_name=='CW6_ACC')
			{
			    $query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where (I.branchname='CMS' or I.branchname='CW3-CK' or I.branchname='CW6-SK') and I.outlet='CW6-Counter' ORDER BY I.issue_id desc");
			}
			else{
			    $query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R right join product_issues_creation I on R.request_id=I.request_id where (I.branchname='CMS' or I.branchname='CW3-CK' or I.branchname='CW-SVR')  ORDER BY I.issue_id desc");
			}
			return $query->result();
		}
		
		public function getkitchenList()
		{
			$query = $this->db->query("SELECT * from user U inner join user_group G on G.group_id=U.user_group where G.group_name='KItchen'");
			return $query->result();
		}
		
		public function getCounterList()
		{
			$query = $this->db->query("SELECT * from user U inner join user_group G on G.group_id=U.user_group where G.group_name='Counter'");
			return $query->result();
		}
		
		public function getIssuesReport($data)
		{
			$f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$ktn = $data['ktn'];
			$ctr = $data['ctr'];
			$query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R right join product_issues_creation I on R.request_id=I.request_id where I.issue_date between '$f_date' and '$t_date' and I.branchname='$ktn' and I.outlet='$ctr' ORDER BY I.issue_id desc");
			return $query->result();
		}
		
		public function getAIndentItemsQty($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			//$query = $this->db->query("select PI.item_name,SUM(CASE WHEN P.branchname = 'CW-CTR' THEN PI.required_qty ELSE 0 END) ctr1,SUM(CASE WHEN P.branchname = 'CW-CTR2' THEN PI.required_qty ELSE 0 END) ctr2,SUM(CASE WHEN P.branchname = 'CW-CTR3' THEN PI.required_qty ELSE 0 END) ctr3 ,SUM(CASE WHEN P.branchname = 'CW-CTR4' THEN PI.required_qty ELSE 0 END) ctr4 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
            $query = $this->db->query("select PI.item_name,PI.required_qty as ctr4 from product_request as P,product_request_item as PI where P.request_id=PI.request_id and P.dept='$uname' and (P.request_date='$r_date') and P.orders='Regular Order' and P.status='Pending' group by PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getAStIndentItems($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
			//$branch_id = $data['branch_id'];
			
		    $query = $this->db->query("select DISTINCT(PI.item_name),R.recipe_id,R.recipe_no,R.pr_cat_id,R.uom,R.yeild,R.production_stock from product_request as P inner join product_request_item as PI on P.request_id=PI.request_id inner join recipe R on PI.recipe_no=R.recipe_no where P.dept='$uname' and (P.request_date='$r_date') and P.orders='Santhe' and P.branchname='CW4-CTR' and P.status='Pending' group by P.branchname,PI.item_name order by PI.f_code,PI.pr_cat_id,PI.item_name");
			return $query->result_array();
		}
		
		public function getASplOrders($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];
            
			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and branchname='CW4-CTR' and orders='Special Order' and status='Pending'");
			return $query->result();
		}
	
		public function getARunningOrders($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];

			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and branchname='CW4-CTR' and orders='Running Order' and status='Pending'");
			return $query->result();
		}
		
		public function getAStSplOrders($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];

			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and branchname='CW4-CTR' and orders='Santhe Special Order' and status='Pending'");
			return $query->result();
		}
		
		public function get_strpride_inward_list() {
		    $branch_name = $_SESSION['branch_name'];
		    
            if($branch_name=='CW4_ACC')
            {
                $query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(R.approval_date, '%d-%m-%Y') as approval_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where R.branchname='ST4' and R.dept='Pride Bakers' ORDER BY R.req_rec_id desc");
            }
			else if($branch_name=='CW5-ACC')
            {
                $query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(R.approval_date, '%d-%m-%Y') as approval_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where R.branchname='ST5' and R.dept='Pride Bakers' ORDER BY R.req_rec_id desc");
            }
            else if($branch_name=='CW6-ACC')
            {
                $query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(R.approval_date, '%d-%m-%Y') as approval_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where R.branchname='ST6' and R.dept='Pride Bakers' ORDER BY R.req_rec_id desc");
            }
            else
            {
                $query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(R.approval_date, '%d-%m-%Y') as approval_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where (R.branchname='ST1' or R.branchname='ST2' or R.branchname='ST3') and R.dept='Pride Bakers' ORDER BY R.req_rec_id desc");
            }
			return $query->result_array();
		}
		
		public function searchInwardList($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 *, DATE_FORMAT(receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(approval_date, '%d-%m-%Y') as approval_date FROM product_req_rec where receipt_date between '$f_date' and '$t_date' and branchname='$branch' and dept='Pride Bakers' ORDER BY req_rec_id desc");
			return $query->result();
		}
		
		public function getSplOrdersKit($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];

			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and orders='Special Order' and status='Pending'");
			return $query->result();
		}
		
		public function getRunningOrdersKit($data)
		{
			$r_date =date('Y-m-d', strtotime($data['req_date']));
			$uname = $data['uname'];

			$query = $this->db->query("select *,@curRow := @curRow + 1 AS row_number from product_request JOIN (SELECT @curRow := 0) r where dept='$uname' and (request_date='$r_date') and orders='Running Order' and status='Pending'");
			return $query->result();
		}
		
		public function getIssueCwList() {
		    $branch="('CW-SVR','CW-FFK','CW-SK','CW-DCK')";
			$query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where I.branchname in $branch ORDER BY I.issue_id desc");
			return $query->result();
		}
		
		
		public function addCarnivalRequest($postdata)
		{
			extract($postdata);
			$req_date = date("Y-m-d", strtotime($req_date));
			$cur_date = date("Y-m-d");
			$order_time = date("h:i A");
			
			$q1 = $this->db->query("select * from product_request where request_date='$cur_date' and orders='Carnival Order' and receipt_status='Pending' and dept='$dept1' and branchname='$branch_name'");
			if($q1->num_rows() > 0)
			{
				$row = $q1->row_array();
				$rqid = $row['request_id'];
				
				$count = count($product_name);
				for($i=0;$i<$count;$i++)
				{
					if($quantity[$i] != '0' && $quantity[$i] != '')
					{
						$cat_name = $cat_id[$i];
						$q2 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");
						if($q2->num_rows() > 0)
						{
							$row2 = $q2->row_array();
							$req_item_id[$i] = $row2['request_item_id'];
							$qty[$i] = $row2['required_qty'];
							$qty1[$i] = $qty[$i] + $quantity[$i];
							
							$q3 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
						}
						else
						{
							$data3 = array(
								'item_name' => $product_name[$i],
								'uom' => $uom[$i],
								'required_qty' => $quantity[$i],
								'remarks' => $remarks[$i],
								'request_id' => $rqid,
								'recipe_id' => $item_id[$i],
								'pr_cat_id' => $cat_id[$i],
								'flavour' => $flavour[$i],
								'f_code' => $f_code[$i],
								'status' => 'Pending',
								'receipt_status' => 'Pending',
								'recipe_no' => $recipe_no[$i]
								);
							$this->db->insert('product_request_item', $data3);
						}
					}
				}
			}
			else
			{
				$this->db->select('MAX(req_no) as req_no',false);
				$this->db->where('branch_id',$branch_id);
				$result = $this->db->get('product_request')->row();
				$req_no = $result->req_no;
				if($req_no == 0){
					$req_no = 1;
				}
				else{
					$req_no = $req_no+1;
				}
				$order_no = $branch_name."/PR-REQ/".$req_no;
				
				$k=0;
    			for($j=0;$j<count($quantity);$j++)
    			{
    				if($quantity[$j]!='0' && $quantity[$j]!='')
    				{
    					$k=$k+1;
    				}
    			}
				if($k>0)
			{
				$data4 = array('req_no'=> $req_no,
					'pr_req_no'=> $order_no,
					'request_date'=> $req_date,
					'request_time'=> $order_time,
					'dept'=> $dept1,
					'orders'=> $orders,
					'branchname'=> $branch_name,
					'status'=> 'Pending',
					'to_branch'=>$to_branch,
					//'to_branchid'=>$to_branch_id,
					'branch_id'=> $branch_id
				);
				$this->db->insert('product_request', $data4);
				$rqid = $this->db->insert_id();
			}
			if($rqid)
			{
				$count = count($product_name);
				for($i=0;$i<$count;$i++)
				{
					if($quantity[$i] != '0' && $quantity[$i] != '')
					{
						$cat_name = $cat_id[$i];
						$q3 = $this->db->query("select * from product_request_item where request_id='$rqid' and recipe_id='$item_id[$i]'");
						if($q3->num_rows() > 0)
						{
							$row3 = $q3->row_array();
							$req_item_id[$i] = $row3['request_item_id'];
							$qty[$i] = $row3['required_qty'];
							$qty1[$i] = $qty[$i] + $quantity[$i];
							
							$q4 = $this->db->query("update product_request_item set required_qty='$qty1[$i]' where request_item_id='$req_item_id[$i]'");
						}
						else
						{
							$data5 = array(
								'item_name' => $product_name[$i],
								'uom' => $uom[$i],
								'required_qty' => $quantity[$i],
								'remarks' => $remarks[$i],
								'request_id' => $rqid,
								'recipe_id' => $item_id[$i],
								'pr_cat_id' => $cat_id[$i],
								'flavour' => $flavour[$i],
								'f_code' => $f_code[$i],
								'status' => 'Pending',
								'receipt_status' => 'Pending',
								'recipe_no' => $recipe_no[$i]
								);
								$this->db->insert('product_request_item', $data5);
						}
					}
				}
			}
			}
			$q5 = $this->db->query("update recipe set req_qty='' where branchname='$branch_name' and pr_cat_id='$cat_name'");
			
			return $rqid;
		}
        
        
        public function getCarnivalRequestList($branch_id)
		{
			$query = $this->db->query("SELECT *, DATE_FORMAT(request_date, '%d-%m-%Y') as request_date FROM product_request where branch_id='$branch_id' and receipt_status='Pending' and orders='Carnival Order' ORDER BY request_id desc");
			return $query->result();	
		}
		
		public function getRequestCarnivalClosedList($branch_id) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(request_date, '%d-%m-%Y') as request_date FROM product_request where branch_id='$branch_id' and receipt_status='Closed' and orders='Carnival Order' ORDER BY request_id desc");
			return $query->result();
		}
		
		public function getCounter($sub_mod_id,$branch_id)
		{
			$query = $this->db->query("SELECT * FROM user_access where branch_id='$branch_id' and module_id='$sub_mod_id'");
			return $query->result();	
		}
		
		public function get_Counter()
		{
			$query = $this->db->query("SELECT * FROM user where branch_name like '%CTR'");
			return $query->result();	
		}
		
		public function get_Vkitchens()
		{
			$query = $this->db->query("SELECT * FROM user where branch_name like '%VNK%'");
			return $query->result();	
		}
		
		public function get_ProductDetails($iname,$br_id)
    	{
    	    $query = $this->db->query("SELECT rname,recipe_id as item_id,recipe_no,pr_cat_id as cat_id,uom,production_stock as stk,cw_price,cw_tax,cw_tax_amount,cw_total_price from recipe where rname='$iname' and branch_id='$br_id'");
    			$row=$query->row_array();
			if($row)
			{
				$data=array("item_name" => $row['rname'], "uom" => $row['uom'], "item_price" => '0', "item_tax" => '0', "item_id" => $row['item_id'], "stock_item"=> $row['stk'], "category_id" => $row['cat_id'], "itm_id" => $row['recipe_no'], "dc_price" => $row['cw_price'], "dc_tax" => $row['cw_tax'], "dc_taxamt" => $row['cw_tax_amount'], "dc_tprice" => $row['cw_total_price']);
				echo json_encode($data);
			}
			else
			{
				$data = array("uom" => '0');
				echo json_encode($data);
			}
    	}
		
		public function addPrCarnivalIssue($postdata)
		{
			extract($postdata);
			$uname = $_SESSION['username'];
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('product_issues_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "PISSU"."/".$branch_name."/".$is_no;
			
			$t1 = $this->db->query("select * from user where branch_name='$outlet'");
				
			$w1 = $t1->row_array();
			$inward_user = $w1['username'];
			$inward_branch = $outlet;
			$inward_branchId = $w1['branch_id'];
			
			$k=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$data = array(
			'is_no'=> $is_no,
			'issue_no'=> $issue_no,
			'issue_date'=> $issue_date,
			'issue_time'=> $issue_time,
			'outlet'=> $inward_user,
			'total_amount'=> $total_amt,
			'branchname'=> $branch_name,
			'request_id'=> '0',
			'branch_id'=> $branch_id
			);
			$this->db->insert('product_issues_creation', $data);
			$iss_id = $this->db->insert_id();
			
			
			$inward_date = date("Y-m-d");
			$inward_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			$this->db->select('MAX(rec_no) as rec_no',false);
			$this->db->where('branch_id',$inward_branchId);
			$result = $this->db->get('product_req_rec')->row();
			$rec_no = $result->rec_no;
			if($rec_no == 0){
				$rec_no = 1;
			}
			else{
				$rec_no = $rec_no+1;
			}
			$receipt_no = "PR/IN"."/".$inward_branch."/".$rec_no;
						
			$data = array('rec_no'=> $rec_no,
				'req_rec_no'=> $receipt_no,
				'receipt_date'=> $inward_date,
				'receipt_time'=> $inward_time,
				'branchname'=> $inward_branch,
				'dept'=> $uname,
				'request_id'=> '0',
				'issue_id'=> $iss_id,
				'total_amount'=> $total_amt,
				'branch_id'=> $inward_branchId
			);
			$this->db->insert('product_req_rec', $data);
			$inid = $this->db->insert_id();
			}
			
			if($iss_id)
			{
			$count = count($item_name);
			for($i=0;$i<$count;$i++)
			{
				$q1 = $this->db->query("select * from recipe where recipe_id='$item_id[$i]'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$qty[$i] = $row['production_stock'];
					$qty1[$i] = $qty[$i] - $quantity[$i];
					
					$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$i]' WHERE  recipe_id= '$item_id[$i]'");
				}
				$data2 = array(
					'issue_item_name' => $item_name[$i],
					'issue_uom' => $uom[$i],
					'issued_qty' => $quantity[$i],
					'price' => $price[$i],
					'basic_price' => $ttl_price[$i],
					'tax' => $tax[$i],
					'tax_amount' => $tax_amt[$i],
					'total_price' => $total[$i],
					'issue_remarks' => $remarks[$i],
					'issue_id' => $iss_id,
					'request_item_id' => '0',
					'recipe_id' => $item_id[$i],
					'pr_cat_id' => $cat_id[$i],
					'recipe_no' => $itm_id[$i]
				);
				$this->db->insert('product_issues_item', $data2);
				$issue_item_id[$i]=$this->db->insert_id();
				
				$data3 = array(
					'item_name' => $item_name[$i],
					'trans_ref_no' => $issue_no,
					'trans_type'=>'PrIssue',
					'transaction_date' => $issue_date,
					'opening_stock' => $qty[$i],
					'out_qty' => $quantity[$i],
					'closing_stock' => $qty1[$i],
					'last_modified' => $lm,
					'branchname' => $branch_name,
					'item_id' => $item_id[$i],
					'category_id' => $cat_id[$i],
					'itm_id' => $itm_id[$i],
					'branch_id' => $branch_id
				);
				$this->db->insert('stock_detail', $data3);
				
				$q9 = $this->db->query("update product_issues_item set rec_issue_qty='$quantity[$i]' where issue_order_id='$issue_item_id[$i]'");
					
					$q3 = $this->db->query("select * from recipe where recipe_no='$itm_id[$i]' and branchname='$inward_branch'");
					if($q3->num_rows() > 0)
					{
						$row1 = $q3->row_array();
						$stock[$i] = $row1['production_stock'];
						$stk[$i] = $stock[$i] + $quantity[$i];
						$in_recipe_id[$i] = $row1['recipe_id']; 
						
						$q10 = $this->db->query("UPDATE recipe set production_stock='$stk[$i]' WHERE  recipe_id='$in_recipe_id[$i]'");
					
						$data5 = array(
							'item_name'=> $item_name[$i],
							'quantity'=> $quantity[$i],
							'issued_qty'=> $quantity[$i],
							'price'=> $price[$i],
							'total_price'=> $total[$i],
							'req_rec_id'=> $inid,
							'request_item_id'=> '0',
							'issue_order_id'=> $issue_item_id[$i],
							'recipe_id'=> $in_recipe_id[$i],
							'recipe_no'=> $itm_id[$i],
							'pr_cat_id'=> $cat_id[$i]
						);
						$this->db->insert('product_req_rec_item', $data5);
						$initid = $this->db->insert_id();
						
						$data6 = array(
							'item_name'=> $item_name[$i],
							'trans_ref_no'=> $receipt_no,
							'trans_type'=>'PrInward',
							'transaction_date'=> $inward_date,
							'opening_stock'=> $stock[$i],
							'in_qty'=> $quantity[$i],
							'closing_stock'=> $stk[$i],
							'last_modified'=> $lm,
							'branchname'=> $inward_branch,
							'item_id'=> $in_recipe_id[$i],
							'recipe_no'=> $itm_id[$i],
							'category_id'=> $cat_id[$i],
							'branch_id'=> $inward_branchId
						);
						$this->db->insert('stock_detail', $data6);
						
						if(empty($quantity[$i])) {
							$q3 = $this->db->query("DELETE FROM product_req_rec_item WHERE receipt_item_id='$initid'");
						}
						
						
					
						$q10 = $this->db->query("select issued_qty,rec_issue_qty from product_issues_item where issue_order_id='$issue_item_id[$i]'");
						if($q10->num_rows() > 0)
						{
							$row3 = $q10->row_array();
							$isd_qty[$i] = $row3['issued_qty'];
							$rcvd_qty[$i] = $row3['rec_issue_qty'];
							
							if($isd_qty[$i] == $rcvd_qty[$i])
							{
								$q11 = $this->db->query("update product_issues_item set rcv_status='Closed' where issue_order_id='$issue_item_id[$i]'");
							}
							
							$q12 = $this->db->query("SELECT count(*) as `num` FROM product_issues_item where issue_id='$iss_id'");
							if($q12->num_rows() > 0)
							{
								$row4 = $q12->row_array();
								$count3 = $row4['num'];
							}
							$q13 = $this->db->query("SELECT count(*) as `num1` FROM product_issues_item where rcv_status='Closed' and issue_id='$iss_id'");
							if($q13->num_rows() > 0)
							{
								$row5 = $q13->row_array();
								$count4 = $row5['num1'];
							}
							if($count3 == $count4 ) {
								$q14 = $this->db->query("update product_issues_creation set issue_status='Accepted' where issue_id='$iss_id'");
							}
						}
					}
				}
		}
			return $iss_id;
		}
		
		public function addPrCCarnivalIssue($postdata)
		{
			extract($postdata);
			
			$t1 = $this->db->query("select * from user where branch_id='$branch_id'");
			$w1 = $t1->row_array();
			$uname = $w1['username'];
			$branch_name = $w1['branch_name'];
			
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('product_issues_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "PISSU"."/".$branch_name."/".$is_no;
			
			$t2 = $this->db->query("select * from user where branch_name='$outlet'");
				
			$w2 = $t2->row_array();
			$inward_user = $w2['username'];
			$inward_branch = $outlet;
			$inward_branchId = $w2['branch_id'];
			$k=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$data = array(
			'is_no'=> $is_no,
			'issue_no'=> $issue_no,
			'issue_date'=> $issue_date,
			'issue_time'=> $issue_time,
			'outlet'=> $inward_user,
			'total_amount'=> $total_amt,
			'branchname'=> $branch_name,
			'request_id'=> '0',
			'branch_id'=> $branch_id
			);
			$this->db->insert('product_issues_creation', $data);
			$iss_id = $this->db->insert_id();
			
			
			$inward_date = date("Y-m-d");
			$inward_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			$this->db->select('MAX(rec_no) as rec_no',false);
			$this->db->where('branch_id',$inward_branchId);
			$result = $this->db->get('product_req_rec')->row();
			$rec_no = $result->rec_no;
			if($rec_no == 0){
				$rec_no = 1;
			}
			else{
				$rec_no = $rec_no+1;
			}
			$receipt_no = "PR/IN"."/".$inward_branch."/".$rec_no;
						
			$data = array('rec_no'=> $rec_no,
				'req_rec_no'=> $receipt_no,
				'receipt_date'=> $inward_date,
				'receipt_time'=> $inward_time,
				'branchname'=> $inward_branch,
				'dept'=> $uname,
				'request_id'=> '0',
				'issue_id'=> $iss_id,
				'total_amount'=> $total_amt,
				'branch_id'=> $inward_branchId
			);
			$this->db->insert('product_req_rec', $data);
			$inid = $this->db->insert_id();
		}
			if($iss_id)
			{
			$count = count($item_name);
			for($i=0;$i<$count;$i++)
			{
				$q1 = $this->db->query("select * from recipe where recipe_no='$itm_id[$i]' and branch_id='$branch_id'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$item_id[$i] = $row['recipe_id'];
					$qty[$i] = $row['production_stock'];
					$qty1[$i] = $qty[$i] - $quantity[$i];
					
					$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$i]' WHERE  recipe_id= '$item_id[$i]'");
				}
				$data2 = array(
					'issue_item_name' => $item_name[$i],
					'issue_uom' => $uom[$i],
					'issued_qty' => $quantity[$i],
					'price' => $price[$i],
					'basic_price' => $ttl_price[$i],
					'tax' => $tax[$i],
					'tax_amount' => $tax_amt[$i],
					'total_price' => $total[$i],
					'issue_remarks' => $remarks[$i],
					'issue_id' => $iss_id,
					'request_item_id' => '0',
					'recipe_id' => $item_id[$i],
					'pr_cat_id' => $cat_id[$i],
					'recipe_no' => $itm_id[$i]
				);
				$this->db->insert('product_issues_item', $data2);
				$issue_item_id[$i]=$this->db->insert_id();
				
				$data3 = array(
					'item_name' => $item_name[$i],
					'trans_ref_no' => $issue_no,
					'trans_type'=>'PrIssue',
					'transaction_date' => $issue_date,
					'opening_stock' => $qty[$i],
					'out_qty' => $quantity[$i],
					'closing_stock' => $qty1[$i],
					'last_modified' => $lm,
					'branchname' => $branch_name,
					'item_id' => $item_id[$i],
					'category_id' => $cat_id[$i],
					'itm_id' => $itm_id[$i],
					'branch_id' => $branch_id
				);
				$this->db->insert('stock_detail', $data3);
				
				$q9 = $this->db->query("update product_issues_item set rec_issue_qty='$quantity[$i]' where issue_order_id='$issue_item_id[$i]'");
					
					$q3 = $this->db->query("select * from recipe where recipe_no='$itm_id[$i]' and branchname='$inward_branch'");
					if($q3->num_rows() > 0)
					{
						$row1 = $q3->row_array();
						$stock[$i] = $row1['production_stock'];
						$stk[$i] = $stock[$i] + $quantity[$i];
						$in_recipe_id[$i] = $row1['recipe_id']; 
						
						$q10 = $this->db->query("UPDATE recipe set production_stock='$stk[$i]' WHERE  recipe_id='$in_recipe_id[$i]'");
					
						$data5 = array(
							'item_name'=> $item_name[$i],
							'quantity'=> $quantity[$i],
							'issued_qty'=> $quantity[$i],
							'price'=> $price[$i],
							'total_price'=> $total[$i],
							'req_rec_id'=> $inid,
							'request_item_id'=> '0',
							'issue_order_id'=> $issue_item_id[$i],
							'recipe_id'=> $in_recipe_id[$i],
							'recipe_no'=> $itm_id[$i],
							'pr_cat_id'=> $cat_id[$i]
						);
						$this->db->insert('product_req_rec_item', $data5);
						$initid = $this->db->insert_id();
						
						$data6 = array(
							'item_name'=> $item_name[$i],
							'trans_ref_no'=> $receipt_no,
							'trans_type'=>'PrInward',
							'transaction_date'=> $inward_date,
							'opening_stock'=> $stock[$i],
							'in_qty'=> $quantity[$i],
							'closing_stock'=> $stk[$i],
							'last_modified'=> $lm,
							'branchname'=> $inward_branch,
							'item_id'=> $in_recipe_id[$i],
							'recipe_no'=> $itm_id[$i],
							'category_id'=> $cat_id[$i],
							'branch_id'=> $inward_branchId
						);
						$this->db->insert('stock_detail', $data6);
						
						if(empty($quantity[$i])) {
							$q3 = $this->db->query("DELETE FROM product_req_rec_item WHERE receipt_item_id='$initid'");
						}
						
						
					
						$q10 = $this->db->query("select issued_qty,rec_issue_qty from product_issues_item where issue_order_id='$issue_item_id[$i]'");
						if($q10->num_rows() > 0)
						{
							$row3 = $q10->row_array();
							$isd_qty[$i] = $row3['issued_qty'];
							$rcvd_qty[$i] = $row3['rec_issue_qty'];
							
							if($isd_qty[$i] == $rcvd_qty[$i])
							{
								$q11 = $this->db->query("update product_issues_item set rcv_status='Closed' where issue_order_id='$issue_item_id[$i]'");
							}
							
							$q12 = $this->db->query("SELECT count(*) as `num` FROM product_issues_item where issue_id='$iss_id'");
							if($q12->num_rows() > 0)
							{
								$row4 = $q12->row_array();
								$count3 = $row4['num'];
							}
							$q13 = $this->db->query("SELECT count(*) as `num1` FROM product_issues_item where rcv_status='Closed' and issue_id='$iss_id'");
							if($q13->num_rows() > 0)
							{
								$row5 = $q13->row_array();
								$count4 = $row5['num1'];
							}
							if($count3 == $count4 ) {
								$q14 = $this->db->query("update product_issues_creation set issue_status='Accepted' where issue_id='$iss_id'");
							}
						}
					}
				}
		}
			return $iss_id;
		}
		
		public function getPrIssue($isid)
		{
			$query = $this->db->query("SELECT * FROM product_issues_creation  where issue_id='$isid'");
			return $query->result();
		}
		
		public function getPrIssueItem($isid)
		{
			$query = $this->db->query("SELECT * FROM product_issues_creation P inner join product_issues_item PI on P.issue_id=PI.issue_id where PI.issue_id='$isid' and PI.issued_qty !=''");
			return $query->result();
		}
		
		public function issuePrList($branch_id){
		    $query = $this->db->query("SELECT * FROM product_issues_creation  where branch_id='$branch_id'");
			return $query->result();
		}
		
		public function issueCPrList(){
		    $query = $this->db->query("SELECT * FROM product_issues_creation  where branchname like '%VNK%'");
			return $query->result();
		}
		
		public function get_Products($branch_id)
    	{
    		$query = $this->db->query("SELECT * from recipe where branch_id='$branch_id' and status='Enabled'");
    		return $query->result();
    	}
		
		public function get_CarnivalProducts($branch_id)
    	{
    		$query = $this->db->query("SELECT * from recipe where branch_id='$branch_id' and carnival_sale='1' and status='Enabled'");
    		return $query->result();
    	}
    	
    	public function get_pride_receipt_byOutlet($data)
		{
			$from_date = date("Y-m-d", strtotime($data['from_date']));
			$to_date = date("Y-m-d", strtotime($data['to_date']));
			$branch=$data['branch'];
			$query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date,DATE_FORMAT(R.receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(R.approval_date, '%d-%m-%Y') as approval_date FROM product_request P inner join product_req_rec R on P.request_id=R.request_id where R.receipt_date between '$from_date' and '$to_date' and R.branchname='$branch' and R.dept='Pride Bakers' ORDER BY R.req_rec_id desc");
			//$query = $this->db->query("SELECT *, DATE_FORMAT(receipt_date, '%d-%m-%Y') as receipt_date,DATE_FORMAT(approval_date, '%d-%m-%Y') as approval_date FROM product_req_rec where receipt_date between '$from_date' and '$to_date' and branchname='$branch' and dept='Pride Bakers' ORDER BY req_rec_id desc");
			return $query->result();
		}
		
		public function getCarIssue($data) {
            $kitchen = $data['kitchen'];
            //echo $branch_name;
            if($kitchen!="")
            {
                $query = $this->db->query("SELECT * FROM product_issues_creation  where branch_id='$kitchen'");
            }
            return $query->result_array();
        }
        
        public function addPr_RequestIOF($postdata)
		{
			extract($postdata);
			
            $branch_id = $_SESSION['branch_id'];
            $branchname = $_SESSION['branch_name'];
            $user=$_SESSION['username'];
			$order_date = date("Y-m-d", strtotime($order_date));
			//$delivery_date = date("Y-m-d", strtotime($delivery_date));
			$cur_date = date("Y-m-d");
			$order_time = date("h:i A");
			$this->db->select('MAX(req_no) as req_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('product_request')->row();
			$req_no = $result->req_no;
			if($req_no == 0){
				$req_no = 1;
			}
			else{
				$req_no = $req_no+1;
			}
			$order_no = $branchname."/PR-REQ/".$req_no;
		
				
			$s1 = $this->db->query("insert into product_request(req_no,pr_req_no,request_date,request_time,dept,branchname,status,branch_id) values ('$req_no','$order_no','$order_date','$order_time','$dept','$branchname','Pending','$branch_id')");
			$req_id = $this->db->insert_id();
			for($i=0;$i<count($product_name);$i++) {
			    if($quantity[$i] != '0' && $quantity[$i] != '')
				{
				    $s3=$this->db->query("select * from recipe where rname='$product_name[$i]' and branchname='$branchname'");
					$res = $s3->row_array();
					$recipe_id[$i] = $res['recipe_id'];
				    $cate_id[$i] = $res['pr_cat_id'];
				    $recipe_no[$i]=$res['recipe_no'];
				    $flavours[$i]=$res['f_code'];
				    $uoms[$i]=$res['uom'];
				    //$cat_name = $cat_id[$i];
					$s2 =  $this->db->query("insert into product_request_item(item_name,uom,required_qty,remarks,request_id,recipe_id,pr_cat_id,flavour,status,receipt_status,recipe_no) values ('$product_name[$i]','$uoms[$i]','$quantity[$i]','$remarks[$i]','$req_id','$recipe_id[$i]','$cate_id[$i]','$flavours[$i]','Pending','Pending','$recipe_no[$i]')");
				}
			}
			
			return $req_id;
			
		}

		public function get_pr_request_list_for_issue($uname) {
			$query = $this->db->query("SELECT *, DATE_FORMAT(P.request_date, '%d-%m-%Y') as request_date FROM product_request P where P.status='Pending' and P.dept='Store' and (P.orders!='Regular Order' or P.orders IS NULL) ORDER BY request_id desc");
			return $query->result();
			//P.dept='$uname'
		}

		

		public function get_product_issue_item($req_id,$branch) {
			$query = $this->db->query("SELECT *, (PI.required_qty-PI.recieved_qty) as item_balance_qty FROM product_request P inner join product_request_item PI on P.request_id=PI.request_id inner join recipe R on PI.item_name=R.rname where PI.request_id='$req_id' and R.branchname='$branch'");
			return $query->result();
		}

		//issueing item for request
		public function add_product_Issue($postdata)
		{
			extract($postdata);
			$branchname = $_SESSION['branch_name'];
			$user=$_SESSION['username'];
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$lm=date("Y-m-dH:i:s",time());
			$time=date("h:i A");
			$cur_date=date("Y-m-d");
			
			if($dept=='NB'){
			    $this->db->select('MAX(is_no) as is_no',false);
    			$this->db->where('branch_id',$branch_id);
    			$this->db->where('invoice_type',$this->invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','tax_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$is_no = $result->is_no;
    			if($is_no == 0){
    				$is_no = 1;
    			}
    			else{
    				$is_no = $is_no+1;
    			}
    			$issue_no = "TISSU"."/".$branchname."/".$is_no;
    			
    			
    			$this->db->select('MAX(in_no) as in_no',false);
    			$this->db->where('branch_id',$branch_id);
    			$this->db->where('invoice_type',$this->invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','tax_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$in_no = $result->in_no;
    			if($in_no == 0){
    				$in_no = 1;
    			}
    			else{
    				$in_no = $in_no+1;
    			}
    			
    			$number = sprintf('%03d',$in_no);
    		    //$dc_no = 'By2CNH-202425-'.$number;
    		    $dc_no = $this->invoice_no.$number;
    		    $invice_type=$this->invoice_type;//'tax_invoice24';
			}else{
			    
			    $this->db->select('MAX(is_no) as is_no',false);
    			$this->db->where('branchname',$branchname);
    			$this->db->where('invoice_type',$this->dc_invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','DC_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$is_no = $result->is_no;
    			if($is_no == 0){
    				$is_no = 1;
    			}
    			else{
    				$is_no = $is_no+1;
    			}
    			$issue_no = "ISSU"."/".$branchname."/".$outlet_branch."/".$is_no;
			
			
			    $this->db->select('MAX(in_no) as in_no',false);
    			$this->db->where('branchname',$branchname);
    			$this->db->where('invoice_type',$this->dc_invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','DC_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$in_no = $result->in_no;
    			if($in_no == 0){
    				$in_no = 1;
    			}
    			else{
    				$in_no = $in_no+1;
    			}
    			
    			$number = sprintf('%03d',$in_no);
    		    //$dc_no = 'By2CNH-202324-'.$number;
    		    $dc_no = "DC-".$branchname.'-'.$outlet_branch.'-'.$number;
    		    $invice_type=$this->dc_invoice_type;//'DC_invoice24';
			}
			
			
			
			$inward_date = date("Y-m-d");
			$inward_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			$s1 = $this->db->query("insert into product_issues_creation(is_no,issue_no,in_no,invoice_no,issue_date,issue_time,outlet,branchname,request_id,issue_status,total_amount,user_name,branch_id,vehicle_id,vehicle_name,invoice_type,entry_date,bill_year) values ('$is_no','$issue_no','$in_no','$dc_no','$issue_date','$time','$dept','$branchname','$req_id','Pending','$grand_total','$user','$branch_id','$vehicle_id','$vehicle_name','$invice_type','$cur_date','$this->bill_year')");
			$is_id = $this->db->insert_id();
			
			$this->db->select('MAX(rec_no) as rec_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('product_req_rec')->row();
			$rec_no = $result->rec_no;
			if($rec_no == 0){
				$rec_no = 1;
			}
			else{
				$rec_no = $rec_no+1;
			}
			$receipt_no = "PR/IN"."/".$branchname."/".$rec_no;
			
			$k=0;
			$gtotal=0;
			for($j=0;$j<count($inward_quantity);$j++)
			{
				if($inward_quantity[$j]!='0' && $inward_quantity[$j]!='')
				{
					$k=$k+1;
				    $totalprice[$j] = round(((float)$price[$j] * (float)$inward_quantity[$j]),2);
    				$gtotal=$gtotal+$totalprice[$j];
				}
			}
			if($k>0)
			{
    			$data = array('rec_no'=> $rec_no,
    				'req_rec_no'=> $receipt_no,
    				'receipt_date'=> $inward_date,
    				'receipt_time'=> $inward_time,
    				'branchname'=> $outlet_branch,
    				'dept'=> $user,
    				'request_id'=> $req_id,
    				'issue_id'=> $is_id,
    				'total_amount'=> $grand_total,
    				'branch_id'=> $outlet_branch_id
    			);
    			$this->db->insert('product_req_rec', $data);
    			$inid = $this->db->insert_id();
			}
			
			
						
			for($i=0;$i<count($item_name);$i++) {
				if($inward_quantity[$i]!='0' && $inward_quantity[$i]!='')
			    {
    				$s3=$this->db->query("select * from recipe where recipe_id='$item_id[$i]'");
    				//if($s3->num_rows() > 0)
    			//	{
					$res = $s3->row_array();
					//$res = mysqli_fetch_assoc($sql3);
					$qty[$i] = $res['production_stock'];
					$hsncode[$i] = $res['hsn_code'];
					$qty1[$i] = (float)$qty[$i] - (float)$inward_quantity[$i];
					$recipe_no[$i] = $res['recipe_no'];
					$pr_cat_id[$i] = $res['pr_cat_id'];
					
					$this->db->query("insert into product_issues_item(issue_item_name,hsn_code,issue_uom,issued_qty,price,basic_price,tax,tax_amount,total_price,issue_remarks,issue_id,request_item_id,recipe_id,recipe_no,pr_cat_id,rcv_status) values ('$item_name[$i]','$hsncode[$i]','$uom[$i]','$inward_quantity[$i]','$price[$i]','$tot_basic[$i]','$tax[$i]','$tax_amt[$i]','$total_price[$i]','$remarks[$i]','$is_id','$req_item_id[$i]','$item_id[$i]','$recipe_no[$i]','$pr_cat_id[$i]','Pending')");
					$issue_item_id = $this->db->insert_id();
					
					$sql4 =  $this->db->query("UPDATE recipe set production_stock='$qty1[$i]' WHERE  recipe_id='$item_id[$i]'");
				//}
			/*	if($inward_quantity[$i]!='0' && $inward_quantity[$i]!='')
				{*/
					$sql5 = $this->db->query("insert into stock_detail(item_name,trans_ref_no,transaction_date,opening_stock,out_qty,closing_stock,last_modified,branchname,item_id,recipe_no,category_id,branch_id)values('$item_name[$i]','$issue_no','$issue_date','$qty[$i]','$inward_quantity[$i]','$qty1[$i]','$lm','$branchname','$item_id[$i]','$recipe_no[$i]','$pr_cat_id[$i]','$branch_id')");
				}
				else{
				    //echo '0';
				}
				$rc_qty[$i] = (float)$inward_quantity[$i] + (float)$received_qty[$i];
											
				$sql6 =  $this->db->query("update product_request_item set recieved_qty='$rc_qty[$i]' where request_item_id='$req_item_id[$i]'");
																				
				if (empty($inward_quantity[$i])) {
					//$sql7 =  $this->db->query("DELETE FROM product_issues_item WHERE issue_order_id='$issue_item_id'");	
				}
											
				if($rc_qty[$i] == $quantity[$i] && $inward_quantity[$i]!='') {
					$status = "Closed";
					$sql9 =  $this->db->query("update product_request_item set status='$status' where request_item_id='$req_item_id[$i]'");
				}
				else {
					//echo "failure|0";
				}
												
				$sql10 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id'");
				
				$row = $sql10->row_array();//mysqli_fetch_array($sql10);
				
				$count1 = $row['num'];
				
				$sql11 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where status='Closed' and request_id='$req_id'");
				
				$row1 = $sql11->row_array();//mysqli_fetch_array($sql11);
				
				$count2 = $row1['num1'];
				
				if($count1 == $count2 ) {
				
					$status = "Closed";
					$sql12 = $this->db->query("update product_request set issue_status='$status' where request_id='$req_id'");
									
				}
				else {
					$status = "Pending";
					$sql13 = $this->db->query("update product_request set issue_status='$status' where request_id='$req_id'");
				}
				
				
				//inward
				if($inward_quantity[$i] != '0' && $inward_quantity[$i] != '')
    				{
    					
    					
    					$s1 = $this->db->query("SELECT * FROM product_issues_item where issue_order_id='$issue_item_id'");
				
        				$r1 = $s1->row_array();//mysqli_fetch_array($sql11);
        				
        				$rec_issue_qty1[$i] = $r1['rec_issue_qty'];
				        $qty[$i] = $inward_quantity[$i] + $received_qty[$i];
    					$qty2[$i] = $inward_quantity[$i] + $rec_issue_qty1[$i];
    					$q1 = $this->db->query("update product_request_item set receipt_qty='$qty[$i]' where request_item_id='$req_item_id[$i]'");
    					$q2 = $this->db->query("update product_issues_item set rec_issue_qty='$qty2[$i]' where issue_order_id='$issue_item_id'");
    					
    					$q3 = $this->db->query("select * from recipe where recipe_no='$recipe_no[$i]' and branchname='$outlet_branch'");
    					if($q3->num_rows() > 0)
    					{
    						$row = $q3->row_array();
    						$stock1[$i] = $row['production_stock'];
    						$stk1[$i] = $stock1[$i] + $inward_quantity[$i];
    						$recipe_id_b[$i] = $res['recipe_id'];
					        $pr_cat_id_b[$i] = $res['pr_cat_id'];
    					
    						$q2 = $this->db->query("UPDATE recipe set production_stock='$stk1[$i]' WHERE  recipe_no='$recipe_no[$i]' and branchname='$outlet_branch'");
    						
    						//calculation
            		        $totalprice[$i] = round(((float)$price[$i] * (float)$inward_quantity[$i]),2);
            			   
    						$data1 = array(
    							'item_name'=> $item_name[$i],
    							'quantity'=> $inward_quantity[$i],
    							'issued_qty'=> $inward_quantity[$i],
    							'price'=> $price[$i],
    							'total_price'=> $totalprice[$i],
    							'req_rec_id'=> $inid,
    							'request_item_id'=> $req_item_id[$i],
    							'issue_order_id'=> $issue_item_id,
    							'recipe_id'=> $recipe_id_b[$i],
    							'recipe_no'=> $recipe_no[$i],
    							'pr_cat_id'=> $pr_cat_id_b[$i]
    						);
    						$this->db->insert('product_req_rec_item', $data1);
    						$initid = $this->db->insert_id();
    						
    						$data2 = array(
    							'item_name'=> $item_name[$i],
    							'trans_ref_no'=> $receipt_no,
    							'trans_type'=>'PrInward',
    							'transaction_date'=> $inward_date,
    							'opening_stock'=> $stock1[$i],
    							'in_qty'=> $inward_quantity[$i],
    							'closing_stock'=> $stk1[$i],
    							'last_modified'=> $lm,
    							'branchname'=> $outlet_branch,
    							'item_id'=> $recipe_id_b[$i],
    							'recipe_no'=> $recipe_no[$i],
    							'category_id'=> $pr_cat_id_b[$i],
    							'branch_id'=> $outlet_branch_id
    						);
    						$this->db->insert('stock_detail', $data2);
    						
    						if(empty($inward_quantity[$i])) {
    							$q3 = $this->db->query("DELETE FROM product_req_rec WHERE receipt_item_id='$initid'");
    						}
    						
    						$q4 = $this->db->query("select recieved_qty,receipt_qty from product_request_item where request_item_id='$req_item_id[$i]'");
    						if($q4->num_rows() > 0)
    						{
    							$row2 = $q4->row_array();
    							$issued_quantity[$i] = $row2['receipt_qty'];
    						}
    						
    						if($issued_quantity[$i] == $inward_quantity[$i] && $inward_quantity[$i]!='') {
    							$q5 = $this->db->query("update product_request_item set receipt_status='Closed' where request_item_id='$req_item_id[$i]'");
    						}
    						
    						$q6 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id'");
    						if($q6->num_rows() > 0)
    						{
    							$row1 = $q6->row_array();
    							$count1 = $row1['num'];
    						}
    						$q7 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where receipt_status='Closed' and request_id='$req_id'");
    						if($q7->num_rows() > 0)
    						{
    							$row2 = $q7->row_array();
    							$count2 = $row2['num1'];
    						}
    						if($count1 == $count2 ) {
    							$q8 = $this->db->query("update product_request set receipt_status='Closed' where request_id='$req_id'");
    							//$q14 = $this->db->query("update product_request set mclose_status='Approved' where request_id='$req_id'");
    							$q9 = $this->db->query("update product_request set status='Closed' where request_id='$req_id'");
    						}
    						$q10 = $this->db->query("select issued_qty,rec_issue_qty from product_issues_item where issue_order_id='$issue_item_id'");
    						if($q10->num_rows() > 0)
    						{
    							$row3 = $q10->row_array();
    							$isd_qty[$i] = $row3['issued_qty'];
    							$rcvd_qty[$i] = $row3['rec_issue_qty'];
    							
    							if($isd_qty[$i] == $rcvd_qty[$i])
    							{
    								$q11 = $this->db->query("update product_issues_item set rcv_status='Closed' where issue_order_id='$issue_item_id'");
    							}
    							
    							$q12 = $this->db->query("SELECT count(*) as `num` FROM product_issues_item where issue_id='$is_id'");
    							if($q12->num_rows() > 0)
    							{
    								$row4 = $q12->row_array();
    								$count3 = $row4['num'];
    							}
    							$q13 = $this->db->query("SELECT count(*) as `num1` FROM product_issues_item where rcv_status='Closed' and issue_id='$is_id'");
    							if($q13->num_rows() > 0)
    							{
    								$row5 = $q13->row_array();
    								$count4 = $row5['num1'];
    							}
    							if($count3 == $count4 ) {
    								$q14 = $this->db->query("update product_issues_creation set issue_status='Accepted' where issue_id='$is_id'");
    								
    							}
    						}
    					}
    				}
				
				
				
			}
			
			return $is_id;
		}

		public function get_pr_issue($issue_id) {
			$query = $this->db->query("SELECT *,DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where I.issue_id='$issue_id'");
			return $query->result();
		}
		
		public function get_pr_issue_item($issue_id) {
			$query = $this->db->query("SELECT * FROM product_request R inner join product_request_item RI on R.request_id=RI.request_id inner join product_issues_item PI on RI.request_item_id=PI.request_item_id where PI.issue_id='$issue_id' and PI.issued_qty !=''");
			return $query->result();
		}
		
		public function getIssuetDCList()
		{
			$query = $this->db->query("SELECT *, DATE_FORMAT(I.issue_date, '%d-%m-%Y') as issue_date,DATE_FORMAT(R.request_date, '%d-%m-%Y') as request_date FROM product_request R inner join product_issues_creation I on R.request_id=I.request_id where  I.invoice_type='$this->dc_invoice_type' ORDER BY I.issue_id desc");
			return $query->result();
		}
		
		//issueing item for request
		public function add_product_OutletIssue($postdata)
		{
			extract($postdata);
			$branchname = $_SESSION['branch_name'];
			$user=$_SESSION['username'];
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$lm=date("Y-m-dH:i:s",time());
			$time=date("h:i A");
			$cur_date=date("Y-m-d");
			
			if($dept=='NB'){
			    $this->db->select('MAX(is_no) as is_no',false);
    			$this->db->where('branch_id',$branch_id);
    			$this->db->where('invoice_type',$this->invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','tax_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$is_no = $result->is_no;
    			if($is_no == 0){
    				$is_no = 1;
    			}
    			else{
    				$is_no = $is_no+1;
    			}
    			$issue_no = "TISSU"."/".$branchname."/".$is_no;
    			
    			
    			$this->db->select('MAX(in_no) as in_no',false);
    			$this->db->where('branch_id',$branch_id);
    			$this->db->where('invoice_type',$this->invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','tax_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$in_no = $result->in_no;
    			if($in_no == 0){
    				$in_no = 1;
    			}
    			else{
    				$in_no = $in_no+1;
    			}
    			
    			$number = sprintf('%03d',$in_no);
    			$dc_no = $this->invoice_no.$number;
    		    //$dc_no = 'By2CNH-202425-'.$number;
    		    $invice_type=$this->invoice_type;//'tax_invoice24';
			}else{
			    
			    $this->db->select('MAX(is_no) as is_no',false);
    			$this->db->where('branchname',$branchname);
    			$this->db->where('invoice_type',$this->dc_invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','DC_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$is_no = $result->is_no;
    			if($is_no == 0){
    				$is_no = 1;
    			}
    			else{
    				$is_no = $is_no+1;
    			}
    			$issue_no = "ISSU"."/".$branchname."/".$outlet_branch."/".$is_no;
			
			
			    $this->db->select('MAX(in_no) as in_no',false);
    			$this->db->where('branchname',$branchname);
    			$this->db->where('invoice_type',$this->dc_invoice_type);
			    $this->db->where('bill_year',$this->bill_year);
    			//$this->db->where('invoice_type','DC_invoice24');
    			$result = $this->db->get('product_issues_creation')->row();
    			$in_no = $result->in_no;
    			if($in_no == 0){
    				$in_no = 1;
    			}
    			else{
    				$in_no = $in_no+1;
    			}
    			
    			$number = sprintf('%03d',$in_no);
    		    //$dc_no = 'By2CNH-202324-'.$number;
    		    $dc_no = "DC-".$branchname.'-'.$outlet_branch.'-'.$number;
    		    $invice_type=$this->dc_invoice_type;//'DC_invoice24';
			}
			
			
			
			$inward_date = date("Y-m-d");
			$inward_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			$s1 = $this->db->query("insert into product_issues_creation(is_no,issue_no,in_no,invoice_no,issue_date,issue_time,outlet,branchname,request_id,issue_status,total_amount,user_name,branch_id,vehicle_id,vehicle_name,invoice_type,entry_date) values ('$is_no','$issue_no','$in_no','$dc_no','$issue_date','$time','$dept','$branchname','$req_id','Pending','$grand_total','$user','$branch_id','$vehicle_id','$vehicle_name','$invice_type','$cur_date')");
			$is_id = $this->db->insert_id();
			
			$this->db->select('MAX(rec_no) as rec_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('product_req_rec')->row();
			$rec_no = $result->rec_no;
			if($rec_no == 0){
				$rec_no = 1;
			}
			else{
				$rec_no = $rec_no+1;
			}
			$receipt_no = "PR/IN"."/".$branchname."/".$rec_no;
			
			$k=0;
			$gtotal=0;
			for($j=0;$j<count($inward_quantity);$j++)
			{
				if($inward_quantity[$j]!='0' && $inward_quantity[$j]!='')
				{
					$k=$k+1;
				    $totalprice[$j] = round(((float)$price[$j] * (float)$inward_quantity[$j]),2);
    				$gtotal=$gtotal+$totalprice[$j];
				}
			}
		/*	if($k>0)
			{
    			$data = array('rec_no'=> $rec_no,
    				'req_rec_no'=> $receipt_no,
    				'receipt_date'=> $inward_date,
    				'receipt_time'=> $inward_time,
    				'branchname'=> $outlet_branch,
    				'dept'=> $user,
    				'request_id'=> $req_id,
    				'issue_id'=> $is_id,
    				'total_amount'=> $grand_total,
    				'branch_id'=> $outlet_branch_id
    			);
    			$this->db->insert('product_req_rec', $data);
    			$inid = $this->db->insert_id();
			}*/
			
			
						
			for($i=0;$i<count($item_name);$i++) {
				if($inward_quantity[$i]!='0' && $inward_quantity[$i]!='')
			    {
    				$s3=$this->db->query("select * from recipe where recipe_id='$item_id[$i]'");
    				//if($s3->num_rows() > 0)
    			//	{
					$res = $s3->row_array();
					//$res = mysqli_fetch_assoc($sql3);
					$qty[$i] = $res['production_stock'];
					$hsncode[$i] = $res['hsn_code'];
					$qty1[$i] = (float)$qty[$i] - (float)$inward_quantity[$i];
					$recipe_no[$i] = $res['recipe_no'];
					$pr_cat_id[$i] = $res['pr_cat_id'];
					
					$this->db->query("insert into product_issues_item(issue_item_name,hsn_code,issue_uom,issued_qty,price,basic_price,tax,tax_amount,total_price,issue_remarks,issue_id,request_item_id,recipe_id,recipe_no,pr_cat_id,rcv_status) values ('$item_name[$i]','$hsncode[$i]','$uom[$i]','$inward_quantity[$i]','$price[$i]','$tot_basic[$i]','$tax[$i]','$tax_amt[$i]','$total_price[$i]','$remarks[$i]','$is_id','$req_item_id[$i]','$item_id[$i]','$recipe_no[$i]','$pr_cat_id[$i]','Pending')");
					$issue_item_id = $this->db->insert_id();
					
					$sql4 =  $this->db->query("UPDATE recipe set production_stock='$qty1[$i]' WHERE  recipe_id='$item_id[$i]'");
				//}
			/*	if($inward_quantity[$i]!='0' && $inward_quantity[$i]!='')
				{*/
					$sql5 = $this->db->query("insert into stock_detail(item_name,trans_ref_no,transaction_date,opening_stock,out_qty,closing_stock,last_modified,branchname,item_id,recipe_no,category_id,branch_id)values('$item_name[$i]','$issue_no','$issue_date','$qty[$i]','$inward_quantity[$i]','$qty1[$i]','$lm','$branchname','$item_id[$i]','$recipe_no[$i]','$pr_cat_id[$i]','$branch_id')");
				}
				else{
				    //echo '0';
				}
				$rc_qty[$i] = (float)$inward_quantity[$i] + (float)$received_qty[$i];
											
				$sql6 =  $this->db->query("update product_request_item set recieved_qty='$rc_qty[$i]' where request_item_id='$req_item_id[$i]'");
																				
				if (empty($inward_quantity[$i])) {
					//$sql7 =  $this->db->query("DELETE FROM product_issues_item WHERE issue_order_id='$issue_item_id'");	
				}
											
				if($rc_qty[$i] == $quantity[$i] && $inward_quantity[$i]!='') {
					$status = "Closed";
					$sql9 =  $this->db->query("update product_request_item set status='$status' where request_item_id='$req_item_id[$i]'");
				}
				else {
					//echo "failure|0";
				}
												
				$sql10 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id'");
				
				$row = $sql10->row_array();//mysqli_fetch_array($sql10);
				
				$count1 = $row['num'];
				
				$sql11 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where status='Closed' and request_id='$req_id'");
				
				$row1 = $sql11->row_array();//mysqli_fetch_array($sql11);
				
				$count2 = $row1['num1'];
				
				if($count1 == $count2 ) {
				
					$status = "Closed";
					$sql12 = $this->db->query("update product_request set issue_status='$status' where request_id='$req_id'");
									
				}
				else {
					$status = "Pending";
					$sql13 = $this->db->query("update product_request set issue_status='$status' where request_id='$req_id'");
				}
				
				
				//inward
				if($inward_quantity[$i] != '0' && $inward_quantity[$i] != '')
    				{
    					
    					
    				/*	$s1 = $this->db->query("SELECT * FROM product_issues_item where issue_order_id='$issue_item_id'");
				
        				$r1 = $s1->row_array();//mysqli_fetch_array($sql11);
        				
        				$rec_issue_qty1[$i] = $r1['rec_issue_qty'];
				        $qty[$i] = $inward_quantity[$i] + $received_qty[$i];
    					$qty2[$i] = $inward_quantity[$i] + $rec_issue_qty1[$i];
    					$q1 = $this->db->query("update product_request_item set receipt_qty='$qty[$i]' where request_item_id='$req_item_id[$i]'");
    					$q2 = $this->db->query("update product_issues_item set rec_issue_qty='$qty2[$i]' where issue_order_id='$issue_item_id'");
    					
    					$q3 = $this->db->query("select * from recipe where recipe_no='$recipe_no[$i]' and branchname='$outlet_branch'");
    					if($q3->num_rows() > 0)
    					{
    						$row = $q3->row_array();
    						$stock1[$i] = $row['production_stock'];
    						$stk1[$i] = $stock1[$i] + $inward_quantity[$i];
    						$recipe_id_b[$i] = $res['recipe_id'];
					        $pr_cat_id_b[$i] = $res['pr_cat_id'];
    					
    						$q2 = $this->db->query("UPDATE recipe set production_stock='$stk1[$i]' WHERE  recipe_no='$recipe_no[$i]' and branchname='$outlet_branch'");
    						
    						//calculation
            		        $totalprice[$i] = round(((float)$price[$i] * (float)$inward_quantity[$i]),2);
            			   
    						$data1 = array(
    							'item_name'=> $item_name[$i],
    							'quantity'=> $inward_quantity[$i],
    							'issued_qty'=> $inward_quantity[$i],
    							'price'=> $price[$i],
    							'total_price'=> $totalprice[$i],
    							'req_rec_id'=> $inid,
    							'request_item_id'=> $req_item_id[$i],
    							'issue_order_id'=> $issue_item_id,
    							'recipe_id'=> $recipe_id_b[$i],
    							'recipe_no'=> $recipe_no[$i],
    							'pr_cat_id'=> $pr_cat_id_b[$i]
    						);
    						$this->db->insert('product_req_rec_item', $data1);
    						$initid = $this->db->insert_id();
    						
    						$data2 = array(
    							'item_name'=> $item_name[$i],
    							'trans_ref_no'=> $receipt_no,
    							'trans_type'=>'PrInward',
    							'transaction_date'=> $inward_date,
    							'opening_stock'=> $stock1[$i],
    							'in_qty'=> $inward_quantity[$i],
    							'closing_stock'=> $stk1[$i],
    							'last_modified'=> $lm,
    							'branchname'=> $outlet_branch,
    							'item_id'=> $recipe_id_b[$i],
    							'recipe_no'=> $recipe_no[$i],
    							'category_id'=> $pr_cat_id_b[$i],
    							'branch_id'=> $outlet_branch_id
    						);
    						$this->db->insert('stock_detail', $data2);
    						
    						if(empty($inward_quantity[$i])) {
    							$q3 = $this->db->query("DELETE FROM product_req_rec WHERE receipt_item_id='$initid'");
    						}
    						
    						$q4 = $this->db->query("select recieved_qty,receipt_qty from product_request_item where request_item_id='$req_item_id[$i]'");
    						if($q4->num_rows() > 0)
    						{
    							$row2 = $q4->row_array();
    							$issued_quantity[$i] = $row2['receipt_qty'];
    						}
    						
    						if($issued_quantity[$i] == $inward_quantity[$i] && $inward_quantity[$i]!='') {
    							$q5 = $this->db->query("update product_request_item set receipt_status='Closed' where request_item_id='$req_item_id[$i]'");
    						}
    						
    						$q6 = $this->db->query("SELECT count(*) as `num` FROM product_request_item where request_id='$req_id'");
    						if($q6->num_rows() > 0)
    						{
    							$row1 = $q6->row_array();
    							$count1 = $row1['num'];
    						}
    						$q7 = $this->db->query("SELECT count(*) as `num1` FROM product_request_item where receipt_status='Closed' and request_id='$req_id'");
    						if($q7->num_rows() > 0)
    						{
    							$row2 = $q7->row_array();
    							$count2 = $row2['num1'];
    						}
    						if($count1 == $count2 ) {
    							$q8 = $this->db->query("update product_request set receipt_status='Closed' where request_id='$req_id'");
    							//$q14 = $this->db->query("update product_request set mclose_status='Approved' where request_id='$req_id'");
    							$q9 = $this->db->query("update product_request set status='Closed' where request_id='$req_id'");
    						}
    						$q10 = $this->db->query("select issued_qty,rec_issue_qty from product_issues_item where issue_order_id='$issue_item_id'");
    						if($q10->num_rows() > 0)
    						{
    							$row3 = $q10->row_array();
    							$isd_qty[$i] = $row3['issued_qty'];
    							$rcvd_qty[$i] = $row3['rec_issue_qty'];
    							
    							if($isd_qty[$i] == $rcvd_qty[$i])
    							{
    								$q11 = $this->db->query("update product_issues_item set rcv_status='Closed' where issue_order_id='$issue_item_id'");
    							}
    							
    							$q12 = $this->db->query("SELECT count(*) as `num` FROM product_issues_item where issue_id='$is_id'");
    							if($q12->num_rows() > 0)
    							{
    								$row4 = $q12->row_array();
    								$count3 = $row4['num'];
    							}
    							$q13 = $this->db->query("SELECT count(*) as `num1` FROM product_issues_item where rcv_status='Closed' and issue_id='$is_id'");
    							if($q13->num_rows() > 0)
    							{
    								$row5 = $q13->row_array();
    								$count4 = $row5['num1'];
    							}
    							if($count3 == $count4 ) {
    								$q14 = $this->db->query("update product_issues_creation set issue_status='Accepted' where issue_id='$is_id'");
    								
    							}
    						}
    					}*/
    				}
				
			}
			
			return $is_id;
		}
		
		public function getPIssueListReport($data)
		{
			$f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$branch = $data['branch'];
			if($f_date!='' && $t_date!='' && $branch=='All'){
			    $query = $this->db->query("SELECT i.invoice_type,i.issue_no,i.`invoice_no`,i.issue_date,i.`outlet`,sum(it.`basic_price`) as `basic`,sum(it.tax_amount) as `tax_amt`,sum(it.`total_price`) as tot_amt,i.issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation i inner join product_issues_item it on i.issue_id=it.issue_id where i.invoice_type='$this->dc_invoice_type' and (i.issue_date between '$f_date' and '$t_date')  group by i.issue_id ORDER BY i.issue_id desc");
			}
			else{
			    $query = $this->db->query("SELECT i.invoice_type,i.issue_no,i.`invoice_no`,i.issue_date,i.`outlet`,sum(it.`basic_price`) as `basic`,sum(it.tax_amount) as `tax_amt`,sum(it.`total_price`) as tot_amt,i.issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation i inner join product_issues_item it on i.issue_id=it.issue_id where i.invoice_type='$this->dc_invoice_type' and (i.issue_date between '$f_date' and '$t_date') and i.outlet='$branch' group by i.issue_id ORDER BY i.issue_id desc");
			}
			 
			return $query->result();
		}
	}
?>