<?php
    #[\AllowDynamicProperties]
	class Master_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
			$this->db2 = $this->load->database('otherdb', TRUE);
			$this->db3 = $this->load->database('otherdb1', TRUE);
		}
		
	 var $pcategory='product_category1'; //product_category1
	 var $category_master='category_master1';
	 var $product='product1';
	public function addRawCategory($postdata){
		extract($postdata);
		$br_name = $_SESSION['branch_name'];
		$br_id = $_SESSION['branch_id'];
		$data = array(
			'category_name' => $cname,
			'branchname' => $br_name,
			'branch_id' => $br_id
		);
		$this->db->insert('category', $data);	
	}
	
	public function addRecipeCategory($postdata){
		extract($postdata);
	   
	   
	   $crt_date = date('Y-m-d H:i:s');
		$data = array(
			'pr_cat_name' => $cat_name
		);
		$this->db->insert('product_category', $data);
		$rc_id = $this->db->insert_id();
		
	}
	
	public function addChef($postdata){
		extract($postdata);
		$data = array(
			'chef_name' => $chef_name
		);
		$this->db->insert('chef', $data);	
	}
	
	public function addSupplier($postdata)
	{
		extract($postdata);
		$this->db->select('MAX(vender_ref_no) as vno',false);
		$result = $this->db->get('vendor_creation')->row();
		$vno = $result->vno;
		if($vno == 0){
			$vno = 1;
		}
		else{
			$vno = $vno+1;
		}
		//$br = explode(',',$branch_name);
		$bcount = count($branch);
		for($i=0;$i<$bcount;$i++)
		{
			$data = array(
			'vender_ref_no' => $vno,
			'company_name' => $sname,
			'contact_person	' => $contact_person,
			'designation' => $designation,
			'department' => $dept,
			'contact_no' => $contact_no,
			'email_id' => $email_id,
			'fax_no' => $fax_no,
			'bank_name' => $bank_name,
			'benificiary_name' => $benificiary_name,
			'ifsc_code' => $ifsc_code,
			'bank_branch' => $branch_addr,
			'bank_acc_no' => $bank_acc_no,
			'pan_no	' => $pan_no,
			'gstin_no' => $gstin_no,
			'mop' => $mop,
			'address' => $address,
			'address1' => $city,
			'postal_code' => $postal_code,
			'state' => $state,
			'country' => $country,
			'area_code_phone' => $area_code_phone,
			'land_phone' => $landline,
			'cr_prd' => $crt_period,
			'rgst_msme' => $rgst_msme,
			'rgst_msme_no' => $msme_rgst_no,
			'branchname' => $branch_name[$i],
			'branch_id' => $branch[$i]
		);
		$this->db->insert('vendor_creation', $data);
		}
	}
	
	public function getSupplier(){
	    $branch_name = $_SESSION['branch_name'];
	    $cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_name,bank_branch,bank_acc_no,address,address1,aprv_status,benificiary_name,cr_prd,branchname,branch_id from vendor_creation where  vendor_status='Approved' group by vender_ref_no order by vender_id desc");
        }
        else{
            $query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='$cw_ceo' and  vendor_status='Approved'");
        }
        /*if($branch_name=='CW4_CEO')
        {
    		$query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='CW4' and  vendor_status='Approved'");
        }
        else if($branch_name=='CW5_CEO')
        {
    		$query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='CW5' and  vendor_status='Approved'");
        }
        else if($branch_name=='CW6_CEO')
        {
    		$query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='CW6' and  vendor_status='Approved'");
        }
        else
	    {
    		$query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_name,bank_branch,bank_acc_no,address,address1,aprv_status,benificiary_name,cr_prd,branchname,branch_id from vendor_creation where  vendor_status='Approved' group by vender_ref_no order by vender_id desc");
	    }*/
		return $query->result();
	}
	
	public function getSupplierDetail($vid){
		$query = $this->db->query("select * from vendor_creation where vender_id='$vid'");
		return $query->result();
	}
	
	public function getSupplierDetailBranch($vref){
		$query = $this->db->query("select * from vendor_creation where vender_ref_no='$vref'");
		return $query->result();
	}
	
	public function getSupplierList($branch_id)
	{
		$branch_name = $_SESSION['branch_name'];
		$cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_branch,bank_acc_no,address,address1,aprv_status,cr_prd,branchname,branch_id from vendor_creation where branch_id='$branch_id' and vendor_status='Approved'");
        }
        else{
            $query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_branch,bank_acc_no,address,address1,aprv_status,cr_prd,branchname,branch_id from vendor_creation where branchname='$cw_ceo' and vendor_status='Approved'");
        }
        /*if($branch_name=='CW4_CEO')
        {
    		$query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_branch,bank_acc_no,address,address1,aprv_status,cr_prd,branchname,branch_id from vendor_creation where branchname='CW4' and vendor_status='Approved'");
        }
        else if($branch_name=='CW5_CEO')
        {
    		$query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_branch,bank_acc_no,address,address1,aprv_status,cr_prd,branchname,branch_id from vendor_creation where branchname='CW5' and vendor_status='Approved'");
        }
        else if($branch_name=='CW6_CEO')
        {
    		$query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_branch,bank_acc_no,address,address1,aprv_status,cr_prd,branchname,branch_id from vendor_creation where branchname='CW6' and vendor_status='Approved'");
        }
        else
	    {
    		$query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_branch,bank_acc_no,address,address1,aprv_status,cr_prd,branchname,branch_id from vendor_creation where branch_id='$branch_id' and vendor_status='Approved'");
	    }*/
		return $query->result();
	}
	
	public function getAllSupplier()
	{
		$query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_branch,bank_acc_no,address,address1,aprv_status,cr_prd,branchname,branch_id from vendor_creation where vendor_status='Approved' group by vender_ref_no");
		return $query->result();
	}
	
	public function edtSupplier($postdata)
	{
		extract($postdata);
		
			$data = array(
			
			'company_name' => $sname,
			'contact_person	' => $contact_person,
			'designation' => $designation,
			'department' => $dept,
			'contact_no' => $contact_no,
			'email_id' => $email_id,
			'fax_no' => $fax_no,
			'bank_name' => $bank_name,
			'benificiary_name' => $benificiary_name,
			'ifsc_code' => $ifsc_code,
			'bank_branch' => $branch_addr,
			'bank_acc_no' => $bank_acc_no,
			'pan_no	' => $pan_no,
			'gstin_no' => $gstin_no,
			'mop' => $mop,
			'address' => $address,
			'address1' => $city,
			'postal_code' => $postal_code,
			'state' => $state,
			'country' => $country,
			'area_code_phone' => $area_code_phone,
			'land_phone' => $landline,
			'cr_prd' => $crt_period,
			'rgst_msme' => $rgst_msme,
			'rgst_msme_no' => $msme_rgst_no
		);
		$this->db->where('vender_ref_no', $vref);
		$this->db->update('vendor_creation', $data);
		
	//	$br = explode(',',$branch_name);
	//	$bcount = count($branch);
		
	/*	for($i=0;$i<$bcount;$i++)
		{
			$q2 = $this->db->query("select * from vendor_creation where vender_ref_no='$vref' and branch_id='$branch[$i]'");
			if($q2->num_rows() > 0){
			}
			else
			{
				$data = array(
					'vender_ref_no' => $vref,
					'company_name' => $sname,
					'contact_person	' => $contact_person,
					'designation' => $designation,
					'department' => $dept,
					'contact_no' => $contact_no,
					'email_id' => $email_id,
					'fax_no' => $fax_no,
					'bank_name' => $bank_name,
					'benificiary_name' => $benificiary_name,
					'ifsc_code' => $ifsc_code,
					'bank_branch' => $branch_addr,
					'bank_acc_no' => $bank_acc_no,
					'pan_no	' => $pan_no,
					'gstin_no' => $gstin_no,
					'mop' => $mop,
					'address' => $address,
					'address1' => $city,
					'postal_code' => $postal_code,
					'state' => $state,
					'country' => $country,
					'area_code_phone' => $area_code_phone,
					'land_phone' => $landline,
					'cr_prd' => $crt_period,
					'rgst_msme' => $rgst_msme,
					'rgst_msme_no' => $msme_rgst_no,
					'branchname' => $br[$i],
					'branch_id' => $branch[$i]
				);
				$this->db->insert('vendor_creation', $data);
			}
		} */
	}
		
	public function getCategory(){
		$query = $this->db->query("select category_id,category_name,status from category where status='Enabled'");
		return $query->result();
	}
		
	public function get_salesBranch(){
		$query = $this->db->query("SELECT U.user_id,U.username,U.full_name,U.password,U.branch_code,U.branch_id,G.group_name,B.branch_name,U.ph_no,A.outlet,U.user_group,U.branch_id,A.address_id FROM user U,user_group G,branch B,address A where U.user_group=G.group_id and U.branch_id=B.branch_id and U.address_id=A.address_id and G.group_name='Counter' and U.branch_code!=''");
		return $query->result();
	}
	
	public function get_santheBranch(){
		$query = $this->db->query("SELECT U.user_id,U.username,U.full_name,U.password,G.group_name,B.branch_name,U.ph_no,A.outlet,U.user_group,U.branch_id,A.address_id FROM user U,user_group G,branch B,address A where U.user_group=G.group_id and U.branch_id=B.branch_id and U.address_id=A.address_id and G.group_name='Santhe'");
		return $query->result();
	}
	
	public function addItem($postdata){
		extract($postdata);
		
		$pname1 = preg_replace('/[^a-zA-Z0-9_ \[\]\.\(\)-]/s','',$product_name);
        $pname = trim($pname1);
            
		$create_date = date('Y-m-d');
		$data =array(
		'item_name' =>$pname,
		'uom' =>$unit_of_measure,
		'category_id' =>$category_id,
		'creation_date' =>$create_date
		);
		$this->db->insert('item', $data);
		$itm_id = $this->db->insert_id();
		//$br_names = explode(',',$branch_name);
		$bcount = count($branch);
		
		/*$this->db->select('MAX(recipe_no) AS recipe_no',false);
		$result = $this->db->get('recipe')->row();
		$recipe_no = $result->recipe_no;
		if($recipe_no == 0){
			$r_no = 1;
		}
		else{
			$r_no = $recipe_no+1;
		}*/
		for($i=0;$i<$bcount;$i++)
		{
			 $data1 = array('item_name'=>$pname,
			 'hsn_code'=>$hsn_code,
			 'uom'=>$unit_of_measure,
			 'item_price'=>$price,
			 'item_tax'=>$tax,
			 'itm_id'=>$itm_id,
			 'category_id'=>$category_id,
			 'branchname'=>$branch_name[$i],
			 'branch_id'=>$branch[$i]
			 );
			 $this->db->insert('item_creation', $data1);
		}
		
		$aq1 = $this->db->query("select * from item_creation where itm_id='$itm_id' and branchname='STR'");
		$arow = $aq1->row_array();
		$aitem_id = $arow['item_id'];
		$adata1 = array(
		        'item_id'=>$aitem_id,
		        'item_name'=>$pname,
    			 'uom'=>$unit_of_measure,
    			 'itm_id'=>$itm_id,
    			 'category_id'=>$category_id,
    			 'branchname'=>'STR',
    			 'branch_id'=>'2'
    		);
			$this->db->insert('item_avg_price', $adata1);
		
		
	}
	
	public function updateItem($postdata)
	{
		extract($postdata);
		
		$pname1 = preg_replace('/[^a-zA-Z0-9_ \[\]\.\(\)-]/s','',$product_name);
        $pname = trim($pname1);
        $create_date = date('Y-m-d');
		$update_date = date('Y-m-d');
		$update_time = date("h:i A");
		$data =array(
    		'item_name' =>$pname,
    		'uom' =>$unit_of_measure,
    		'category_id' =>$category_id,
    		'direct_sales' =>$direct_sales,	
    		'santhe' => $santhe,
    		'expiry_date' => $expirydate,
    		'min_stk' => $min_stk,
    		'updated_date' =>$update_date,
    		'updated_time' =>$update_time
		);
		$this->db->where('itm_id', $itm_id);
		$this->db->update('item', $data);
		
		$data1 = array(
		    'item_name'=>$pname,
		    'uom'=>$unit_of_measure,
		    'category_id'=>$category_id,
		    'direct_sales'=>$direct_sales,
		    'expiry_date' =>$expirydate,
		    'min_stk' => $min_stk
		 );
		$this->db->where('itm_id', $itm_id);
		$this->db->update('item_creation', $data1);
		
		$adata1 = array(
		    'item_name'=>$pname,
		    'uom'=>$unit_of_measure,
		    'category_id'=>$category_id
	     );
		$this->db->where('itm_id', $itm_id);
		$this->db->update('item_avg_price', $adata1);
		
		
		$q1 = $this->db->query("select recipe_no from recipe where raw_item_code='$itm_id'");
		if($q1->num_rows() > 0)
		{
		    $row = $q1->row_array();
		    $r_no = $row['recipe_no'];
		}
		else
    	{
    		$this->db->select('MAX(recipe_no) AS recipe_no',false);
    		$result = $this->db->get('recipe')->row();
    		$recipe_no = $result->recipe_no;
    		if($recipe_no == 0){
    			$r_no = 1;
    		}
    		else{
    			$r_no = $recipe_no+1;
    		}
    	}
		
		if($direct_sales == 'Yes')
		{
		    $rcbcount = count($rcbranch);
			for($j=0;$j<$rcbcount;$j++)
			{
			    $q3 = $this->db->query("select * from recipe where raw_item_code='$itm_id' and branch_id='$rcbranch[$j]'");
			    if($q3->num_rows() > 0)
        		{
        		    $data3 = array(
        				'rname'=>$pname,
        				'uom'=>$unit_of_measure,
        				'status'=>'Enabled',
        				'aprvl_status'=>'Approved',
        				'santhe_sales'=>$santhe,
        				'updated_date' =>$update_date,
        				'updated_time' =>$update_time
        			);
        			$this->db->where('raw_item_code', $itm_id);
					$this->db->where('branch_id', $rcbranch[$j]);
					$this->db->update('recipe', $data3);
				}
        		else
        		{
        		     $data2 = array('recipe_no' =>$r_no,
        				'raised_date'=>$create_date,
        				'rname'=>$pname,
        				'uom'=>$unit_of_measure,
        				'status'=>'Enabled',
        				'aprvl_status'=>'Approved',
        				'pr_cat_id'=>$category_id,
        				'branchname'=>$rcbranch_name[$j],
        				'raw_item_code'=>$itm_id,
        				'santhe_sales'=>$santhe,
        				'food_delivery'=>'No',
        				'spl_ord'=>'No',
        				'branch_id'=>$rcbranch[$j]);
        			$this->db->insert('recipe', $data2);
        		}
			}
			
			$rq3 = $this->db->query("select * from recipe_sales_price where recipe_no='$r_no'");
			if($rq3->num_rows() > 0)
    		{
    		    $rdata3 = array(
    				'rname' => $pname,
    				'uom' => $unit_of_measure,
    			);
    			$this->db->where('recipe_no', $r_no);
    			$this->db->update('recipe_avg_cost', $rdata3);
    			
    			$rcdata3 = array(
    				'rname' => $pname,
    				'uom' => $unit_of_measure,
    			);
    			$this->db->where('recipe_no', $r_no);
    			$this->db->update('recipe_sales_price', $rcdata3);
    		}
    		else
    		{
    		    $rcq = $this->db->query("select * from recipe where branchname='CW_CHF' and recipe_no='$r_no'");
        		if($rcq->num_rows() > 0)
    			{
    			    $row = $rcq->row_array();
    			    $rc_id = $row['recipe_id'];
    			}
    			$adata = array(
    				'recipe_id' => $rc_id,
    				'recipe_no' => $r_no,
    				'rname' => $pname,
    				'yeild' => '0',
    				'uom' => $unit_of_measure,
    				'pr_cat_id' => $category_id,
    				'branchname' => 'CW_CHF',
    				'branch_id' => '34',
    				'raw_item_code' => $itm_id
    			);
    			$this->db->insert('recipe_avg_cost', $adata);
    			
    			//insert to sales price
    			$pcount = count($sale_br_id);
    			for($m=0;$m<$pcount;$m++)
    			{
    				$rcdata2 = array(
    					'recipe_no' => $r_no,
    					'rname' => $pname,
    					'uom' => $unit_of_measure,
    					'price' => '0',
    					'tax' => '0',
    					'tax_amount' => '0',
    					'total_price' => '0',
    					'pr_cat_id' => $category_id,
    					'recipe_id' => $rc_id,
    					'branchname' => $sale_br_name[$m],
    					'branch_id' => $sale_br_id[$m],
    					'raw_item_code' => $itm_id
    				);
    				$this->db->insert('recipe_sales_price', $rcdata2);
    			}
    		}
			
			$sbcount = count($sale_br_id);
			for($k=0;$k<$sbcount;$k++)
			{
			    $q3 = $this->db3->query("select * from $this->product where product_no='$r_no' and branch_id='$sale_br_id[$k]'");
			    if($q3->num_rows() > 0)
        		{
        		    $data4 = array(
        				'product_name'=>$pname,
        				'uom'=>$unit_of_measure,
        				'product_status'=>'Active'
        			);
        			$this->db3->where('product_no', $r_no);
        			$this->db3->where('branch_id', $sale_br_id[$k]);
        			$this->db3->update($this->product, $data4);
        			
        			$data4 = array(
        				'product_name'=>$pname,
        				'uom'=>$unit_of_measure,
        				'product_status'=>'Active'
        			);
        			$this->db2->where('product_no', $r_no);
        			$this->db2->where('branch_id', $sale_br_id[$k]);
        			$this->db2->update($this->product, $data4);
        		}
			    else
			    {
			        $data3 = array(
    					'product_name'=>$pname,
    					'product_no'=>$r_no,
    					'uom'=>$unit_of_measure,
    					'product_status'=>'Active',
    					'pr_cat_id'=>$category_id,
    					'branch'=>$sale_br_code[$k],
    					'branch_id'=>$sale_br_id[$k],
    					'client_id'=>'0'
    					);
					$this->db3->insert($this->product, $data3);
					$pr_id = $this->db3->insert_id();
    					
    				$data3 = array(
    				    'pr_id'=>$pr_id,
    					'product_name'=>$pname,
    					'product_no'=>$r_no,
    					'uom'=>$unit_of_measure,
    					'product_status'=>'Active',
    					'pr_cat_id'=>$category_id,
    					'branch'=>$sale_br_code[$k],
    					'branch_id'=>$sale_br_id[$k],
    					'client_id'=>'0'
    					);
    				$this->db2->insert($this->product, $data3);
			    }
			}
		}
		else
		{
		    $rcbcount = count($rcbranch);
			for($j=0;$j<$rcbcount;$j++)
			{
			    $data3 = array(
    				'rname'=>$pname,
    				'uom'=>$unit_of_measure,
    				'status'=>'Disabled',
    				'aprvl_status'=>'Approved',
    				'santhe_sales'=>$santhe,
    				'updated_date' =>$update_date,
    				'updated_time' =>$update_time
    			);
    			$this->db->where('raw_item_code', $itm_id);
    			$this->db->where('branch_id', $rcbranch[$j]);
    			$this->db->update('recipe', $data3);
			}
		   
		    $sbcount = count($sale_br_id);
			for($k=0;$k<$sbcount;$k++)
			{
    			$data4 = array(
    				'product_name'=>$pname,
    				'uom'=>$unit_of_measure,
    				'product_status'=>'InActive'
    			);
    			$this->db3->where('product_no', $r_no);
    			$this->db3->where('branch_id', $sale_br_id[$k]);
    			$this->db3->update($this->product, $data4);
    			
    			$data4 = array(
        			'product_name'=>$pname,
        			'uom'=>$unit_of_measure,
        			'product_status'=>'InActive'
    			);
    			$this->db2->where('product_no', $r_no);
    			$this->db2->where('branch_id', $sale_br_id[$k]);
    			$this->db2->update($this->product, $data4);
    		}
		}
		
		if($santhe == 'Yes')
		{
		    $sb_count = count($santhe_br_id);
		    for($j=0;$j<$sb_count;$j++)
			{
    		    $q2 = $this->db->query("select * from item_creation where itm_id='$itm_id' and branch_id='$santhe_br_id[$j]'");
    			if($q2->num_rows() > 0)
    			{
    			    $data10 = array(
    				    'item_name'=>$pname,
        				'uom'=>$unit_of_measure,
        			    'category_id'=>$category_id,
        				'direct_sales'=>$direct_sales,
        				'expiry_date' =>$expirydate,
		                'min_stk' => $min_stk
    				 );
    				$this->db->where('itm_id', $itm_id);
    				$this->db->where('branch_id', $santhe_br_id[$j]);	
    				$this->db->update('item_creation', $data10);
    			}
    			else
    			{
    			       $data10 = array(
    				     'item_name'=>$pname,
    				     'uom'=>$unit_of_measure,
    				     'itm_id'=>$itm_id,
        				 'category_id'=>$category_id,
        				 'branchname'=>$santhe_br_name[$j],
        				 'branch_id'=>$santhe_br_id[$j],
        				 'direct_sales'=>$direct_sales,
        				 'expiry_date' =>$expirydate,
        				 'min_stk' => $min_stk
    				 );
    				 $this->db->insert('item_creation', $data10);
    			}
    			
    			$q3 = $this->db->query("select * from recipe where raw_item_code='$itm_id' and branch_id='$santhe_br_id[$j]'");
				if($q3->num_rows() > 0){
				
					$data9 = array(
    					'rname'=>$pname,
    					'uom'=>$unit_of_measure,
    					'status'=>'Enabled',
    					'aprvl_status'=>'Approved',
    					'santhe_sales'=>$santhe,
    					'updated_date' =>$update_date,
				        'updated_time' =>$update_time
    				);
    				$this->db->where('raw_item_code', $itm_id);
					$this->db->where('branch_id', $santhe_br_id[$j]);
					$this->db->update('recipe', $data9);
					
					$data4 = array(
        				'product_name'=>$pname,
        				'uom'=>$unit_of_measure,
        				'product_status'=>'Active'
        			);
        			$this->db3->where('product_no', $r_no);
        			$this->db3->where('branch_id', $santhe_br_id[$k]);
        			$this->db3->update($this->product, $data4);
        			
        			$data4 = array(
        				'product_name'=>$pname,
        				'uom'=>$unit_of_measure,
        				'product_status'=>'Active'
        			);
        			$this->db2->where('product_no', $r_no);
        			$this->db2->where('branch_id', $santhe_br_id[$k]);
        			$this->db2->update($this->product, $data4);
				}
				else{
					$data9 = array(
					    'recipe_no' =>$r_no,
    					'raised_date'=>$create_date,
    					'rname'=>$pname,
    					'uom'=>$unit_of_measure,
    					'status'=>'Enabled',
    					'aprvl_status'=>'Approved',
    					'pr_cat_id'=>$category_id,
    					'branchname'=>$santhe_br_name[$j],
    					'raw_item_code'=>$itm_id,
    					'santhe_sales'=>$santhe,
    					'food_delivery'=>'No',
    					'spl_ord'=>'No',
    					'branch_id'=>$santhe_br_id[$j]
    				);
					$this->db->insert('recipe', $data9);
					
					$data3 = array(
    					'product_name'=>$pname,
    					'product_no'=>$r_no,
    					'uom'=>$unit_of_measure,
    					'product_status'=>'Active',
    					'pr_cat_id'=>$category_id,
    					'branch'=>$sale_br_code[$k],
    					'branch_id'=>$sale_br_id[$k],
    					'client_id'=>'0'
    					);
					$this->db3->insert($this->product, $data3);
					$pr_id = $this->db3->insert_id();
    					
    				$data3 = array(
    				    'pr_id'=>$pr_id,
    					'product_name'=>$pname,
    					'product_no'=>$r_no,
    					'uom'=>$unit_of_measure,
    					'product_status'=>'Active',
    					'pr_cat_id'=>$category_id,
    					'branch'=>$sale_br_code[$k],
    					'branch_id'=>$sale_br_id[$k],
    					'client_id'=>'0'
    					);
    				$this->db2->insert($this->product, $data3);
				}
			}		
		}
		else
		{
		    $sb_count = count($santhe_br_id);
			for($j=0;$j<$sb_count;$j++)
			{
			    $q3 = $this->db->query("select * from recipe where raw_item_code='$itm_id' and branch_id='$santhe_br_id[$j]'");
				if($q3->num_rows() > 0){
				    $data11 = array(
    					'rname'=>$pname,
    					'uom'=>$unit_of_measure,
    					'status'=>'Disabled',
    					'aprvl_status'=>'Approved',
    					'santhe_sales'=>$santhe,
    					'updated_date' =>$update_date,
    				    'updated_time' =>$update_time
				    );
					$this->db->where('raw_item_code', $itm_id);
					$this->db->where('branch_id', $santhe_br_id[$j]);
					$this->db->update('recipe', $data11);
					
					$data4 = array(
        			'product_name'=>$pname,
        			'uom'=>$unit_of_measure,
        			'product_status'=>'InActive'
        			);
        			$this->db2->where('product_no', $r_no);
        			$this->db2->where('branch_id', $santhe_br_id[$k]);
        			$this->db2->update($this->product, $data4);
        			
        			$data4 = array(
        			'product_name'=>$pname,
        			'uom'=>$unit_of_measure,
        			'product_status'=>'InActive'
        			);
        			$this->db3->where('product_no', $r_no);
        			$this->db3->where('branch_id', $santhe_br_id[$k]);
        			$this->db3->update($this->product, $data4);
				}
			}
		}
	
		$uq1 = $this->db->query("update damage_order_item set damage_item_name='$pname',damage_uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq2 = $this->db->query("update godown_stock set item_name='$pname',uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq3 = $this->db->query("update grn_price set item_name='$pname' where itm_id='$itm_id'");
		$uq4 = $this->db->query("update invoice_item set item_name='$pname',uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq5 = $this->db->query("update issues_order_item set indent_item_name='$pname',indent_uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq6 = $this->db->query("update material_request_item set item_name='$pname',uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq7 = $this->db->query("update purchase_order_item set item_name='$pname',unit_measurement='$unit_of_measure' where itm_id='$itm_id'");
		$uq8 = $this->db->query("update rcp_rejection_item set item_name='$pname' where itm_id='$itm_id'");
		$uq9 = $this->db->query("update recipe_item set iname='$product_name',uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq10 = $this->db->query("update stock_detail set item_name='$pname' where itm_id='$itm_id'");
		$uq11 = $this->db->query("update stock_transfer_item set item_name='$pname',uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq12 = $this->db->query("update stock_update_item set item_name='$pname',uom='$unit_of_measure' where itm_id='$itm_id'");
		$uq13 = $this->db->query("update transfer_inward_item set item_name='$pname',uom='$unit_of_measure' where item_no='$itm_id'");
		$uq14 = $this->db->query("update transfer_item set item_name='$pname',uom='$unit_of_measure' where item_no='$itm_id'");
	}
	
	//Recipe
	
	public function get_CounterUser()
	{
		$query = $this->db->query("SELECT user_id,branch_name,branch_id,branch_code,ibranch_code from user where user_group='9'");
		return $query->result();
	}
	
	public function get_SantheUser()
	{
		$query = $this->db->query("SELECT user_id,branch_name,branch_id from user where user_group='10'");
		return $query->result();
	}
	
	public function get_Category()
	{
		$query = $this->db->query("SELECT pr_cat_id,pr_cat_name from product_category");
		return $query->result();
	}
		
	public function get_Tax()
	{
		$query = $this->db->query("SELECT tax_percentage from tax");
		return $query->result();
	}
	
	public function get_KitchenUser()
	{
		$query = $this->db->query("SELECT user_id,username from user where user_group='8'");
		return $query->result();
	}
	
	public function get_Chef()
	{
		$query = $this->db->query("SELECT chef_id,chef_name from chef");
		return $query->result();
	}
	
	public function get_Items($branch_id)
	{
		$query = $this->db->query("SELECT item_name,uom,item_price from item_creation where branch_id='$branch_id' and status='Active'");
		return $query->result();
	}
	
	public function get_cwItems()
	{
		$query = $this->db->query("select * from item_creation where branchname='CW'");
		return $query->result();
	}
	
	public function get_GDWItems()
	{
		$query = $this->db->query("SELECT * from godown_stock");
		return $query->result();
	}
	
	public function get_vendors($branch_id)
	{
		$query = $this->db->query("SELECT * from vendor_creation where branch_id='$branch_id'");
		return $query->result();
	}
	
	public function get_Products($branch_id)
	{
		$query = $this->db->query("SELECT rname from recipe where branch_id='$branch_id'");
		return $query->result();
	}
	
	public function get_RcpItems()
	{
		$query = $this->db->query("SELECT item_name,uom,item_price from item_creation where branchname='CW'");
		return $query->result();
	}
	
	public function get_ItemDetails($iname,$br_id)
	{
		$query = $this->db->query("select item_name,uom,item_price,item_tax,stock_item,item_id,category_id,itm_id from item_creation where item_name='$iname' and branchname='CW'");
		$row=$query->row_array();
		if($row)
		{
			$price = $row['item_price'];
			$tax = $row['item_tax'];
			$tax_a = ($price * $tax)/100;
			$tot_price = $price + $tax_a;
			$data=array("item_name" => $row['item_name'], "uom" => $row['uom'], "item_price" => $tot_price, "item_tax" => $row['item_tax'], "item_id" => $row['item_id'], "stock_item"=> $row['stock_item'], "category_id" => $row['category_id'], "itm_id" => $row['itm_id']);
			echo json_encode($data);
		}
		else
		{
			$data = array("uom" => '0');
			echo json_encode($data);
		}
	}
	
	public function get_PItemDetails($iname,$br_id)
	{
		$query = $this->db->query("select item_name,uom,item_price,item_tax,stock_item,item_id,category_id,itm_id from item_creation where item_name='$iname' and branch_id='$br_id'");
		$row=$query->row_array();
		if($row)
		{
			$price = $row['item_price'];
			$tax = $row['item_tax'];
			$tax_a = ($price * $tax)/100;
			$tot_price = $price + $tax_a;
			$data=array("item_name" => $row['item_name'], "uom" => $row['uom'], "item_price" => $row['item_price'], "item_tax" => $row['item_tax'], "item_id" => $row['item_id'], "stock_item"=> $row['stock_item'], "category_id" => $row['category_id'], "itm_id" => $row['itm_id']);
			echo json_encode($data);
		}
		else
		{
			$data = array("uom" => '0');
			echo json_encode($data);
		}
	}
	
	public function get_GDWItemDetails($iname,$br_id)
	{
		$query = $this->db->query("SELECT `gd_id`, `item_name`, `uom`, `price`, `tax`, `stock`, `from_branchname`, `branchname`, `item_id`, `itm_id`, `category_id` FROM `godown_stock` WHERE  item_name='$iname'");
		$row=$query->row_array();
		if($row)
		{
			$price = $row['price'];
			$tax = $row['tax'];
			$tax_a = ($price * $tax)/100;
			$tot_price = $price + $tax_a;
			$data=array("item_name" => $row['item_name'], "uom" => $row['uom'], "item_price" => $tot_price, "item_tax" => $row['tax'], "item_id" => $row['item_id'], "stock_item"=> $row['stock'], "category_id" => $row['category_id'], "itm_id" => $row['itm_id']);
			echo json_encode($data);
		}
		else
		{
			$data = array("uom" => '0');
			echo json_encode($data);
		}
	}
	
	public function get_OutItemDetails($iname,$br_id)
	{
		$query = $this->db->query("select i.item_name,i.stock_item,i.item_id,i.itm_id,i.uom,i.category_id,ic.item_price,ic.item_tax from(select * from item_creation where item_name='$iname' and branch_id='$br_id') i inner join (select * from item_creation where item_name='$iname' and branchname='CW') ic on i.item_name=ic.item_name");
		$row=$query->row_array();
		if($row)
		{
			$data=array("item_name" => $row['item_name'], "uom" => $row['uom'], "item_price" => $row['item_price'], "item_tax" => $row['item_tax'], "item_id" => $row['item_id'], "stock_item"=> $row['stock_item'], "category_id" => $row['category_id'], "itm_id" => $row['itm_id']);
			echo json_encode($data);
		}
		else
		{
			$data = array("uom" => '0');
			echo json_encode($data);
		}
	}
	
	public function get_ItemProductDetails($itype,$iname,$br_id)
	{
		if($itype == 'Raw Material')
		{
			$query = $this->db->query("select i.item_name,i.stock_item,i.item_id,i.itm_id,i.uom,i.category_id,ic.item_price,ic.item_tax from(select * from item_creation where item_name='$iname' and branch_id='$br_id') i inner join (select * from item_creation where item_name='$iname' and branchname='CW') ic on i.item_name=ic.item_name");
			$row=$query->row_array();
			if($row)
			{
				$data=array("item_name" => $row['item_name'], "uom" => $row['uom'], "item_price" => $row['item_price'], "item_tax" => $row['item_tax'], "item_id" => $row['item_id'], "stock_item"=> $row['stock_item'], "category_id" => $row['category_id'], "itm_id" => $row['itm_id']);
				echo json_encode($data);
			}
			else
			{
				$data = array("uom" => '0');
				echo json_encode($data);
			}
		}
		if($itype == 'Finished Product')
		{
			$query = $this->db->query("SELECT rname,recipe_id as item_id,recipe_no,pr_cat_id as cat_id,uom,production_stock as stk 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']);
				echo json_encode($data);
			}
			else
			{
				$data = array("uom" => '0');
				echo json_encode($data);
			}
		}
	}
	
	public function get_RecipeDetails($iname,$br_id)
	{
		$query = $this->db->query("select rname,uom,recipe_cost,tax,recipe_id,pr_cat_id,recipe_no,production_stock from recipe where rname='$iname' and branch_id='$br_id'");
		$row=$query->row_array();
		if($row)
		{
			$price = $row['recipe_cost'];
			//$tax = $row['tax'];
			//$tax_a = ($price * $tax)/100;
			//$tot_price = $price + $tax_a;
			$data=array("rname" => $row['rname'], "uom" => $row['uom'], "price" => $price, "recipe_id" => $row['recipe_id'], "pr_cat_id" => $row['pr_cat_id'], "recipe_no" => $row['recipe_no'], "aprice" => $row['recipe_cost'], "tax" => '0', "stock" => $row['production_stock']);
			echo json_encode($data);
		}
		else
		{
			$data = array("uom" => '0');
			echo json_encode($data);
		}
	}
	
	public function get_RcpDetails($iname,$br_id)
	{
		$query = $this->db->query("select rname,uom,price,tax,recipe_id,pr_cat_id,recipe_no,yeild,production_stock from recipe where recipe_id='$iname' and branch_id='$br_id'");//20 4
		$row=$query->row_array();
		if($row)
		{
			$price = $row['price'];
			$tax = $row['tax'];
			$tax_a = ($price * $tax)/100;
			$tot_price = $price + $tax_a;
			$data=array("rname" => $row['rname'], "uom" => $row['uom'], "price" => $tot_price, "recipe_id" => $row['recipe_id'], "pr_cat_id" => $row['pr_cat_id'], "recipe_no" => $row['recipe_no'], "yeild" => $row['yeild'], "aprice"=> $row['price'], "tax"=> $row['tax'], "stock"=> $row['production_stock']);
			echo json_encode($data);
		}
		else
		{
			$data = array("uom" => '0');
			echo json_encode($data);
		}
	}
	
	public function addProduct($postdata)
		{
			extract($postdata);
			$pname1 = preg_replace('/[^a-zA-Z0-9_ \[\]\.\(\)-]/s','',$product_name);
            $pname = trim($pname1);
            
            //recipe cost
            if($yield == '0' || $yield == '')
            {
                $rc_cost = '0';
            }
            else
            {
                $rc_cost = round(($ttl_amt/$yield),2);
            }
            
            if($effective != '')
			{
				$effctv = date('Y-m-d',strtotime($effective));
			}
			else
			{
			    $effctv = $effective;
			}
			
			$cq = $this->db->query("SELECT rname FROM recipe where rname='$pname'");
			if($cq->num_rows() > 0)
			{
			    return $cq;
			}
			else
			{
    			if(!empty($combination))
    			{
    				$comb = implode(',',$combination);
    			}
    			else
    			{
    			    $comb = '';
    			}
    			//product image upload
    			if(!empty($_FILES['pd_upload']['name'])){
    				$config['upload_path'] = 'product_image/';
    				$config['allowed_types'] = 'gif|jpg|png|jpeg';
    				$filename = $_FILES['pd_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('pd_upload')){
    					$uploadData = $this->upload->data();
    					//$pd_upload = $uploadData['file_name'];
    					$pd_upload = 'product_image/'.$uploadData['file_name'];
    					//$file_name = 'upload/'.$_FILES['in_upload']['name'];
    				}else{
    					$pd_upload = '';
    				}
    			}else{
    				$pd_upload = '';
    			}
    			
    			//recipe number
    			$this->db->select('MAX(recipe_no) as recipe_no',false);
    			$result = $this->db->get('recipe')->row();
    			$recipe_no = $result->recipe_no;
    			if($recipe_no == 0){
    				$recipe_no = 1;
    			}
    			else{
    				$recipe_no = $recipe_no+1;
    			}
    			
    			$cur_date = date('Y-m-d');
    	//		$upd_in = implode(',',$updated_in);
    			$data = array(
    				'recipe_no' => $recipe_no,
    				'hsn_code' => $hsn_code,
    				'raised_date' => $cur_date,
    				'rname' => $pname,
    				'f_code' => $fcode,
    				'yeild' => $yield,
    				'uom' => $uom,
    				'price' => $base_price,
    				'tax' => $gst_rate,
    				'tax_amount' => $gst,
    				'total_price' => $tprice,
    				'discount' => $discount,
    				'recipe_price' => $ttl_amt,
    				'recipe_cost' => $rc_cost,
    				'product_image' => $pd_upload,
    				'raised_on' => $raised_on,
    				'od_time' => $od_time,
    				'shelf_life' => $s_life,
    				'spl_ord' => $spl_ord,
    				'food_delivery' => $food_delivery,
    				'tprice' => $tbase_price,
    				'ttax' => $tgst_rate,
    				'ttax_amount' => $tgst,
    				'ttotal_price' => $ttprice,
    				'santhe_sales' => $santhe,
    				'prepared_by' => $prepared,
    				'effective_from' => $effctv,
    			//	'updated_in' => $upd_in,
    				'recipe_cat' => $recipe_cat,
    				'flavour' => $fcode,
    				'sqcode' => $sqcode,
    				'status' => 'Disabled',
    				'aprvl_status' => 'Pending',
    				'pr_cat_id' => $category,
    				'branchname' => $branch_name,
    				'branch_id' => $branch_id,
    			    'combination' => $comb,
    			    'product_type' => $prtype,
    			    'carnival_sale' => $carnival,
    			    'fd_status' => 'Pending',
    			    'institution_sales' => $inst_sales
    			);
    			$this->db->insert('recipe', $data);
    			$rc_id = $this->db->insert_id();
    			
    			$adata = array(
    				'recipe_id' => $rc_id,
    				'recipe_no' => $recipe_no,
    				'hsn_code' => $hsn_code,
    				'rname' => $pname,
    				'yeild' => $yield,
    				'uom' => $uom,
    				'pr_cat_id' => $category,
    				'branchname' => $branch_name,
    				'branch_id' => $branch_id
    			);
    			$this->db->insert('recipe_avg_cost', $adata);
    		
    			//insert to sales price
    			$pcount = count($cbase_price);
    			for($i=0;$i<$pcount;$i++)
    			{
    				$data2 = array(
    					'recipe_no' => $recipe_no,
    					'hsn_code' => $hsn_code,
    					'rname' => $pname,
    					'uom' => $uom,
    					'price' => $cbase_price[$i],
    					'tax' => $cgst_rate[$i],
    					'tax_amount' => $cgst[$i],
    					'total_price' => $ctprice[$i],
    					'pr_cat_id' => $category,
    					'recipe_id' => $rc_id,
    					'branchname' => $cbranch[$i],
    					'branch_id' => $cbranch_id[$i]
    				);
    				$this->db->insert('recipe_sales_price', $data2);
    			}
    			
    			//insert to item
    			
    			$icount = count($item_name);
    		//	if($icount > 1)
    		//	{
    				for($i=0;$i<$icount;$i++)
    				{
    				    if(!empty($item_name[$i]))
    				    {
        					$data3 = array(
        						'item_name' => $item_name[$i],
        						'qty' => $quantity[$i],
        						'price' => $price[$i],
        						'uom' => $iuom[$i],
        						'total' => $total[$i],
        						'recipe_id' => $rc_id,
        						'recipe_no' => $recipe_no,
        						'item_id' => $item_id[$i],
        						'category_id' => $cat_id[$i],
        						'itm_id' => $itm_id[$i]
        					);
        					$this->db->insert('recipe_item', $data3);
    				    }
    				}
    		//	}
    			
    			//insert to sub item
    			$scount = count($sub_item_name);
    			//if($scount > 1)
    			//{
    				for($i=0;$i<$scount;$i++)
    				{
    				    if(!empty($sub_item_name[$i]))
    				    {
        					$data4 = array(
        						'item_name' => $sub_item_name[$i],
        						'qty' => $sub_quantity[$i],
        						'price' => $sub_price[$i],
        						'uom' => $sub_uom[$i],
        						'total' => $sub_total[$i],
        						'recipe_id' => $rc_id,
        						'rc_no' => $recipe_no,
        						'sub_recipe_id' => $sub_item_id[$i],
        						'pr_cat_id' => $sub_cat_id[$i],
        						'recipe_no' => $rcp_no[$i]
        					);
        					$this->db->insert('sub_recipe_item', $data4);
    				    }
    				}
    			//}
			}
		}
	
	public function get_thead_permission($user_id,$sub_mod_id){
		$query = $this->db->query("select M.module_id,M.module_name,S.sub_mod_id,S.sub_mod_name,P.permission_id,P.permission_name,PA.username,PA.user_id,PA.user_module_id,PA.user_permission_id,PA.url from module M inner join sub_module S on M.module_id=S.module_id inner join permission_access PA on S.sub_mod_id=PA.sub_mod_id inner join permission P on PA.permission_id=P.permission_id where PA.user_id='$user_id' and PA.sub_mod_id='$sub_mod_id'");
		return $query->result();
	}
	
	public function getProducts($branch_id){
		$query = $this->db->query("select *,R.status as rstatus from recipe R inner join product_category C on R.pr_cat_id=C.pr_cat_id where R.branch_id='$branch_id'");
		return $query->result();
	}
	
	public function getApproveProducts(){
		$query = $this->db->query("select recipe_id,rname from recipe where aprvl_status='Pending'");
		return $query->result();
	}
	
	public function getProductDetails($id){
		$query = $this->db->query("select recipe_id,recipe_no,hsn_code,raised_date,f_code,rname,yeild,uom,price,tax,tax_amount,total_price,discount,recipe_price,product_image,raised_on,od_time,shelf_life,spl_ord,food_delivery,tprice,ttax,ttax_amount,ttotal_price,santhe_sales,prepared_by,effective_from,updated_in,status_effective,recipe_cat,flavour,sqcode,status,aprvl_status,pr_cat_id,branchname,branch_id,combination,product_type,carnival_sale,institution_sales,product_code from recipe where recipe_id='$id'");
		return $query->result();
	}
	
	public function getProductBranch($rc_no){
		$query = $this->db->query("select recipe_id,recipe_no,hsn_code,raised_date,f_code,rname,yeild,uom,price,tax,tax_amount,total_price,discount,recipe_price,product_image,raised_on,od_time,shelf_life,spl_ord,food_delivery,tprice,ttax,ttax_amount,ttotal_price,santhe_sales,prepared_by,effective_from,updated_in,status_effective,recipe_cat,flavour,sqcode,status,aprvl_status,pr_cat_id,branchname,branch_id from recipe where recipe_no='$rc_no' and branchname not like 'ST%'");
		return $query->result();
	}
	
	public function get_CounterSalesPrice($id){
		$query = $this->db->query("select R.recipe_price_id,R.recipe_id,R.rname,R.price,R.tax,R.tax_amount,R.total_price,R.pr_cat_id,R.branchname,R.branch_id,U.branch_code,U.ibranch_code from recipe_sales_price R inner join user U on R.branch_id=U.branch_id where recipe_id='$id'");
		return $query->result();
	}
	
	public function get_RecipeItems($id){
		$query = $this->db->query("select rc_item_id,item_name,iname,qty,price,uom,total,recipe_id,item_id,category_id,itm_id,recipe_no from recipe_item where recipe_id='$id'");
		return $query->result();
	}
	
	public function get_SubRecipeItems($id){
		$query = $this->db->query("select sub_rc_item_id,item_name,qty,price,uom,total,recipe_id,sub_recipe_id,pr_cat_id,recipe_no,rc_no from sub_recipe_item where recipe_id='$id'");
		return $query->result();
	}
	
	public function approveProduct($postdata)
	{
			$db2 = $this->load->database('otherdb', TRUE);
			$db3 = $this->load->database('otherdb1', TRUE);
			extract($postdata);
			$pname1 = preg_replace('/[^a-zA-Z0-9_ \[\]\.\(\)-]/s','',$product_name);
			$pname1 = preg_replace("/'/", "", $pname1);
            $pname = trim($pname1);
            
            //recipe cost
            if($yield == '0' || $yield == '')
            {
                $rc_cost = '0';
            }
            else
            {
                $rc_cost = round(($ttl_amt/$yield),2);
            }
            
            $updated_date = date("Y-m-d");
			$updated_time = date("h:i A");
			if(!empty($combination))
			{
				$comb = implode(',',$combination);
			}
			else
			{
			    $comb = '';
			}
			
			if($effective != '')
			{
				$effctv = date('Y-m-d',strtotime($effective));
			}
			else
			{
			    $effctv = $effective;
			}
			
			//product image upload
			if(!empty($_FILES['pd_upload']['name'])){
			    //removing previous file
			    $ImagePathAndName = $pd_upload1;
    			$do = unlink($ImagePathAndName);
              /*  if($do == "1")
                {
                    echo "The file was deleted successfully.";
                }
                else
                {
                    echo "There was an error trying to delete the file.";
                } */
				$config['upload_path'] = 'product_image/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$filename = $_FILES['pd_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('pd_upload')){
					$uploadData = $this->upload->data();
					$pd_upload = $uploadData['file_name'];
					$pdimage = $fpath.$pd_upload;
				}else{
					$pd_upload = $pd_upload1;
					if($pd_upload1 == '')
    			    {
    			        $pdimage = '';
    			    }
    			    else
    			    {
    			        $pdimage = $fpath.$pd_upload1;
    			    }
				}
			}else{
				$pd_upload = $pd_upload1;
				if($pd_upload1 == '')
			    {
			        $pdimage = '';
			    }
			    else
			    {
			        $pdimage = $fpath.$pd_upload1;
			    }
			}
									
			$cur_date = date('Y-m-d');
		//	$upd_in = implode(',',$updated_in);
			$data = array(
				'hsn_code' => $hsn_code,
				'rname' => $pname,
				'f_code' => $fcode,
				'yeild' => $yield,
				'uom' => $uom,
				'price' => $base_price,
				'tax' => $gst_rate,
				'tax_amount' => $gst,
				'total_price' => $tprice,
				'discount' => $discount,
				'recipe_price' => $ttl_amt,
				'recipe_cost' => $rc_cost,
				'product_image' => $pd_upload,
				'raised_on' => $raised_on,
				'od_time' => $od_time,
				'shelf_life' => $s_life,
				'spl_ord' => $spl_ord,
				'food_delivery' => $food_delivery,
				'tprice' => $tbase_price,
				'ttax' => $tgst_rate,
				'ttax_amount' => $tgst,
				'ttotal_price' => $ttprice,
				'santhe_sales' => $santhe,
				'prepared_by' => $prepared,
				'effective_from' => $effctv,
			//	'updated_in' => $upd_in,
				'recipe_cat' => $recipe_cat,
				'flavour' => $fcode,
				'sqcode' => $sqcode,
				'status' => 'Enabled',
				'aprvl_status' => 'Approved',
				'pr_cat_id' => $category,
				'combination' => $comb,
				'product_type' => $prtype,
				'carnival_sale' => $carnival,
                'fd_status' => 'Pending',
                'updated_date' => $updated_date,
                'updated_time' => $updated_time,
                'institution_sales' => $inst_sales
			);
			$this->db->where('recipe_id', $recipe_id);
			$this->db->update('recipe', $data);
			
			$adata = array(
            	'hsn_code' => $hsn_code,
            	'rname' => $pname,
            	'yeild' => $yield,
            	'uom' => $uom,
            	'pr_cat_id' => $category
            );
            $this->db->where('recipe_id', $recipe_id);
			$this->db->update('recipe_avg_cost', $adata);
			
			//insert to sales price
		//$pcount = count($cbase_price);
		$pcount = count($brchk);
		
			for($i=0;$i<$pcount;$i++)
			{
			    $ind = $brchk[$i];
				$data2 = array(
					'recipe_no' => $recipe_no,
					'hsn_code' => $hsn_code,
					'rname' => $pname,
					'uom' => $uom,
					'price' => $cbase_price[$ind],
					'tax' => $cgst_rate[$ind],
					'tax_amount' => $cgst[$ind],
					'total_price' => $ctprice[$ind],
					'pr_cat_id' => $category,
					'recipe_id' => $recipe_id,
					'branchname' => $cbranch[$ind],
					'branch_id' => $cbranch_id[$ind]
				);
				$this->db->where('recipe_price_id',$recipe_price_id[$ind]);
				$this->db->update('recipe_sales_price', $data2);
			}
		
			$brcount = count($up_branch);
			//$br = explode(',',$up_brname);
			for($i=0;$i<$brcount;$i++)
			{
				$data3 = array(
					'recipe_no' => $recipe_no,
					'hsn_code' => $hsn_code,
					'raised_date' => $raised_date,
					'rname' => $pname,
					'f_code' => $fcode,
					'yeild' => $yield,
					'uom' => $uom,
					'price' => $base_price,
					'tax' => $gst_rate,
					'tax_amount' => $gst,
					'total_price' => $tprice,
					'discount' => $discount,
					'recipe_price' => $ttl_amt,
					'recipe_cost' => $rc_cost,
					'product_image' => $pd_upload,
					'raised_on' => $raised_on,
					'od_time' => $od_time,
					'shelf_life' => $s_life,
					'spl_ord' => $spl_ord,
					'food_delivery' => $food_delivery,
					'tprice' => $tbase_price,
					'ttax' => $tgst_rate,
					'ttax_amount' => $tgst,
					'ttotal_price' => $ttprice,
					'santhe_sales' => $santhe,
					'prepared_by' => $prepared,
					'effective_from' => $effctv,
				//	'updated_in' => $upd_in,
					'recipe_cat' => $recipe_cat,
					'flavour' => $fcode,
					'sqcode' => $sqcode,
					'status' => 'Enabled',
					'aprvl_status' => 'Approved',
					'pr_cat_id' => $category,
					'branchname' => $up_brname[$i],
					'branch_id' => $up_branch[$i],
					'combination' => $comb,
					'product_type' => $prtype,
					'carnival_sale' => $carnival,
                    'fd_status' => 'Pending',
                    'updated_date' => $updated_date,
                    'updated_time' => $updated_time,
                    'institution_sales' => $inst_sales
				);
				$this->db->insert('recipe', $data3);
			}
			
			if($inst_sales == 'No')
			{
    			$q1 = $this->db2->query("select * from $this->category_master where cat_ref='$category'");
    			if($q1->num_rows() > 0)
    			{
    				$row = $q1->row_array();
    				$cat_ref = $row['category_master_id'];
    			}
    			
    			if($base_price != '' && $base_price != '0')
    			{
    				//$slcount = count($cbase_price);
    				$slcount = count($brchk);
    				for($i=0;$i<$slcount;$i++)
    				{
    				    $ind = $brchk[$i];
    					$ctsgst_tax = $cgst_rate[$i]/2;
    					$ctsgst_tax_amt = $cgst[$i]/2;
    					$q2 = $this->db2->query("select * from $this->product_category where cat_ref='$category' and branch='$cbranch_code[$ind]'");
                		$row1 = $q2->row_array();
                		$cat_ref_id = $row1['pr_cat_id'];
                		
    					if($cbase_price != '' && $cbase_price != '0')
    					{
    						$datag4 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $cbase_price[$ind],
    							'basic_tax_percent' => $cgst_rate[$ind],
    							'basic_tax_amount' => $cgst[$ind],
    							'sgst_tax' => $ctsgst_tax,
    							'sgst_tax_amount' => $ctsgst_tax_amt,
    							'cgst_tax' => $ctsgst_tax,
    							'cgst_tax_amount' => $ctsgst_tax_amt,
    							'price' => $ctprice[$ind],
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'combination' => $comb,
    							'product_image' => $pdimage,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => $carnival
    						);
    						$db3->insert($this->product, $datag4);
    						$prd_id1 = $db3->insert_id();
    						
    						$data4 = array(
    						    'pr_id' => $prd_id1,
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $cbase_price[$ind],
    							'basic_tax_percent' => $cgst_rate[$ind],
    							'basic_tax_amount' => $cgst[$ind],
    							'sgst_tax' => $ctsgst_tax,
    							'sgst_tax_amount' => $ctsgst_tax_amt,
    							'cgst_tax' => $ctsgst_tax,
    							'cgst_tax_amount' => $ctsgst_tax_amt,
    							'price' => $ctprice[$ind],
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'combination' => $comb,
    							'product_image' => $pdimage,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => $carnival
    						);
    						$db2->insert($this->product, $data4);
    					}
    				}
    				
    				if($santhe == 'Yes')
    				{
    					$stcount = count($st_branch);
    					for($i=0;$i<$stcount;$i++)
    					{
    					    $sgst_tax = $gst_rate/2;
    						$sgst_tax_amt = $gst/2;
    						
    						$data5 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $base_price,
    							'basic_tax_percent' => $gst_rate,
    							'basic_tax_amount' => $gst,
    							'sgst_tax' => $sgst_tax,
    							'sgst_tax_amount' => $sgst_tax_amt,
    							'cgst_tax' => $sgst_tax,
    							'cgst_tax_amount' => $sgst_tax_amt,
    							'price' => $tprice,
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $st_branch[$i],
    							'branch_id' => $st_branch_id[$i],
    							'data_transfer' => 'pending',
    							'combination' => $comb,
    							'product_image' => $pdimage,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => $carnival
    						);
    						$db3->insert($this->product, $data5);
    						$prd_id2 = $db3->insert_id();
    						
    						$data5 = array(
    						    'pr_id' => $prd_id2,
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $base_price,
    							'basic_tax_percent' => $gst_rate,
    							'basic_tax_amount' => $gst,
    							'sgst_tax' => $sgst_tax,
    							'sgst_tax_amount' => $sgst_tax_amt,
    							'cgst_tax' => $sgst_tax,
    							'cgst_tax_amount' => $sgst_tax_amt,
    							'price' => $tprice,
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $st_branch[$i],
    							'branch_id' => $st_branch_id[$i],
    							'data_transfer' => 'pending',
    							'combination' => $comb,
    							'product_image' => $pdimage,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => $carnival
    						);
    						$db2->insert($this->product, $data5);
    						
    						$data6 = array(
    							'recipe_no' => $recipe_no,
    							'hsn_code' => $hsn_code,
    							'raised_date' => $raised_date,
    							'rname' => $pname,
    							'f_code' => $fcode,
    							'yeild' => $yield,
    							'uom' => $uom,
    							'price' => $base_price,
    							'tax' => $gst_rate,
    							'tax_amount' => $gst,
    							'total_price' => $tprice,
    							'discount' => $discount,
    							'recipe_price' => $ttl_amt,
    							'recipe_cost' => $rc_cost,
    							'product_image' => $pd_upload,
    							'raised_on' => $raised_on,
    							'od_time' => $od_time,
    							'shelf_life' => $s_life,
    							'spl_ord' => $spl_ord,
    							'food_delivery' => $food_delivery,
    							'tprice' => $tbase_price,
    							'ttax' => $tgst_rate,
    							'ttax_amount' => $tgst,
    							'ttotal_price' => $ttprice,
    							'santhe_sales' => $santhe,
    							'prepared_by' => $prepared,
    							'effective_from' => $effctv,
    						//	'updated_in' => $upd_in,
    							'recipe_cat' => $recipe_cat,
    							'flavour' => $fcode,
    							'sqcode' => $sqcode,
    							'status' => 'Enabled',
    							'aprvl_status' => 'Approved',
    							'pr_cat_id' => $category,
    							'branchname' => $st_branch[$i],
    							'branch_id' => $st_branch_id[$i],
    							'combination' => $comb,
    							'product_type' => $prtype,
    							'carnival_sale' => $carnival,
                                'fd_status' => 'Pending',
                                'updated_date' => $updated_date,
                                'updated_time' => $updated_time,
                                'institution_sales' => $inst_sales
    						);
    						$this->db->insert('recipe', $data6);
    					}
    				}
    				
    				if($food_delivery == 'Yes')
    				{
    				    $slcount = count($brchk);
    				    for($j=0;$j<$slcount;$j++)
    					{
    					    $ind = $brchk[$j];
    					    $q3 = $this->db2->query("select * from $this->product_category where cat_ref='$category' and branch='$cbranch_code[$ind]'");
                    		
                    			$row3 = $q3->row_array();
                    			$cat_ref_id = $row3['pr_cat_id'];
                    		
    						$tsgst_tax = $tgst_rate/2;
    						$tsgst_tax_amt = $tgst/2;
    						
    				/*		$data7 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $tbase_price,
    							'basic_tax_percent' => $tgst_rate,
    							'basic_tax_amount' => $tgst,
    							'sgst_tax' => $tsgst_tax,
    							'sgst_tax_amount' => $tsgst_tax_amt,
    							'cgst_tax' => $tsgst_tax,
    							'cgst_tax_amount' => $tsgst_tax_amt,
    							'price' => $ttprice,
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'social_order' => '1',
    							'combination' => $comb,
    							'client_id' => '4',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => '0',
    							'product_image' => $pdimage,
                                'fd_status' => 'Pending'
    						);
    						$db3->insert('product', $data7);
    						$prd_id3 = $db3->insert_id();
    						
    						$data7 = array(
    						    'pr_id' => $prd_id3,
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $tbase_price,
    							'basic_tax_percent' => $tgst_rate,
    							'basic_tax_amount' => $tgst,
    							'sgst_tax' => $tsgst_tax,
    							'sgst_tax_amount' => $tsgst_tax_amt,
    							'cgst_tax' => $tsgst_tax,
    							'cgst_tax_amount' => $tsgst_tax_amt,
    							'price' => $ttprice,
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'social_order' => '1',
    							'combination' => $comb,
    							'client_id' => '4',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => '0',
    							'product_image' => $pdimage,
                                'fd_status' => 'Pending'
    						);
    						$db2->insert('product', $data7);
    						$prid1 = $this->db->insert_id(); */
    						
    						$data7 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => '0',
    							'basic_tax_percent' => '0',
    							'basic_tax_amount' => '0',
    							'sgst_tax' => '0',
    							'sgst_tax_amount' => '0',
    							'cgst_tax' => '0',
    							'cgst_tax_amount' => '0',
    							'price' => '0',
    							'discount_perc' => '0',
    							'product_status' => 'InActive',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'social_order' => '1',
    							'combination' => $comb,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => '0',
    							'product_image' => $pdimage,
                                'fd_status' => 'Pending'
    						);
    						$db3->insert($this->product, $data7);
    						$prd_id3 = $db3->insert_id();
    						
    						$data7 = array(
    						    'pr_id' => $prd_id3,
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => '0',
    							'basic_tax_percent' => '0',
    							'basic_tax_amount' => '0',
    							'sgst_tax' => '0',
    							'sgst_tax_amount' => '0',
    							'cgst_tax' => '0',
    							'cgst_tax_amount' => '0',
    							'price' => '0',
    							'discount_perc' => '0',
    							'product_status' => 'InActive',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'social_order' => '1',
    							'combination' => $comb,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => '0',
    							'product_image' => $pdimage,
                                'fd_status' => 'Pending'
    						);
    						$db2->insert($this->product, $data7);
    						$prid1 = $this->db->insert_id();
    						
    						$data8 = array(
    							'tag' => 'default',
    							'url' => $pdimage,
    							'product_no' => $recipe_no,
    							'pr_id' => $prid1,
    							'branch' => $cbranch_code[$j]
    						);
    						$db2->insert('tag1', $data8);
    						
    						$data9 = array(
    							'tag' => 'zomato',
    							'url' => $pdimage,
    							'product_no' => $recipe_no,
    							'pr_id' => $prid1,
    							'branch' => $cbranch_code[$j]
    						);
    						$db2->insert('tag1', $data9);
    						
    						$data10 = array(
    							'tag' => 'swiggy',
    							'url' => $pdimage,
    							'product_no' => $recipe_no,
    							'pr_id' => $prid1,
    							'branch' => $cbranch_code[$j]
    						);
    						$db2->insert('tag1', $data10);
    					}
    				}
    			}
	        }
	        else
	        {
	            $q1 = $this->db2->query("select * from $this->category_master where cat_ref='$category'");
    			if($q1->num_rows() > 0)
    			{
    				$row = $q1->row_array();
    				$cat_ref = $row['category_master_id'];
    			}
    		//	if($base_price != '' && $base_price != '0')
    		//	{
    			    $slcount = count($brchk);
    				for($i=0;$i<$slcount;$i++)
    				{
    				    $ind = $brchk[$i];
    					$ctsgst_tax = $cgst_rate[$i]/2;
    					$ctsgst_tax_amt = $cgst[$i]/2;
    					$q2 = $this->db2->query("select * from $this->product_category where cat_ref='$category' and branch='$cbranch_code[$ind]'");
                		$row1 = $q2->row_array();
                		$cat_ref_id = $row1['pr_cat_id'];
                		
    				//	if($cbase_price != '' && $cbase_price != '0')
    				//	{
    						$datag4 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $cbase_price[$ind],
    							'basic_tax_percent' => $cgst_rate[$ind],
    							'basic_tax_amount' => $cgst[$ind],
    							'sgst_tax' => $ctsgst_tax,
    							'sgst_tax_amount' => $ctsgst_tax_amt,
    							'cgst_tax' => $ctsgst_tax,
    							'cgst_tax_amount' => $ctsgst_tax_amt,
    							'price' => $ctprice[$ind],
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'institute',
    							'combination' => $comb,
    							'product_image' => $pdimage,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => $carnival
    						);
    						$db3->insert($this->product, $datag4);
    						$prd_id1 = $db3->insert_id();
    						
    						$data4 = array(
    						    'pr_id' => $prd_id1,
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $cbase_price[$ind],
    							'basic_tax_percent' => $cgst_rate[$ind],
    							'basic_tax_amount' => $cgst[$ind],
    							'sgst_tax' => $ctsgst_tax,
    							'sgst_tax_amount' => $ctsgst_tax_amt,
    							'cgst_tax' => $ctsgst_tax,
    							'cgst_tax_amount' => $ctsgst_tax_amt,
    							'price' => $ctprice[$ind],
    							'discount_perc' => $discount,
    							'product_status' => 'Active',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
    							'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'institute',
    							'combination' => $comb,
    							'product_image' => $pdimage,
    							'client_id' => '0',
    							'api_status' => '0',
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => $carnival
    						);
    						$db2->insert($this->product, $data4);
    				//	}
    				}
    		//	}
	        }
		}

		public function updateProduct($postdata)
		{
			$db2 = $this->load->database('otherdb', TRUE);
			$db3 = $this->load->database('otherdb1', TRUE);
			extract($postdata);
			$updated_date = date("Y-m-d");
			$updated_time = date("h:i A");
			$pname1 = preg_replace('/[^a-zA-Z0-9_ \[\]\.\(\)-]/s','',$product_name);
            $pname = trim($pname1);
            
            //recipe cost
            if($yield == '0' || $yield == '')
            {
                $rc_cost = '0';
            }
            else
            {
                $rc_cost = round(($ttl_amt/$yield),2);
            }
			
			if(!empty($combination))
			{
				$comb = implode(',',$combination);
			}
			else
			{
			    $comb = '';
			}
			
			if($effective != '')
			{
				$effctv = date('Y-m-d',strtotime($effective));
			}
			else
			{
			    $effctv = $effective;
			}
			
			//product image upload
			if(!empty($_FILES['pd_upload']['name'])){
			    //removing previous file
			    $ImagePathAndName = $pd_upload1;
    			$do = unlink($ImagePathAndName);
              /*  if($do == "1")
                {
                    echo "The file was deleted successfully.";
                }
                else
                {
                    echo "There was an error trying to delete the file.";
                } */
				$config['upload_path'] = 'product_image/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$filename = $_FILES['pd_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('pd_upload')){
					$uploadData = $this->upload->data();
					$pd_upload = 'product_image/'.$uploadData['file_name'];
					$pdimage = $fpath.$pd_upload;
				}else{
					$pd_upload = $pd_upload1;
					if($pd_upload1 == '')
				    {
				        $pdimage = '';
				    }
				    else
				    {
				        $pdimage = $fpath.$pd_upload1;
				    }
				}
			}else{
				$pd_upload = $pd_upload1;
				if($pd_upload1 == '')
			    {
			        $pdimage = '';
			    }
			    else
			    {
			        $pdimage = $fpath.$pd_upload1;
			    }
			}
									
			$cur_date = date('Y-m-d');
			//$upd_in = implode(',',$updated_in);
			$data = array(
				'hsn_code' => $hsn_code,
				'rname' => $pname,
				'f_code' => $fcode,
				'yeild' => $yield,
				'uom' => $uom,
				'price' => $base_price,
				'tax' => $gst_rate,
				'tax_amount' => $gst,
				'total_price' => $tprice,
				'discount' => $discount,
				'recipe_price' => $ttl_amt,
				'recipe_cost' => $rc_cost,
				'product_image' => $pd_upload,
				'raised_on' => $raised_on,
				'od_time' => $od_time,
				'shelf_life' => $s_life,
				'spl_ord' => $spl_ord,
				'food_delivery' => $food_delivery,
				'tprice' => $tbase_price,
				'ttax' => $tgst_rate,
				'ttax_amount' => $tgst,
				'ttotal_price' => $ttprice,
				'santhe_sales' => $santhe,
				'prepared_by' => $prepared,
				'effective_from' => $effctv,
				//'updated_in' => $upd_in,
				'recipe_cat' => $recipe_cat,
				'flavour' => $fcode,
				'sqcode' => $sqcode,
				'status' => 'Enabled',
				'aprvl_status' => 'Approved',
				'pr_cat_id' => $category,
				'combination' => $comb,
				'product_type' => $prtype,
				'carnival_sale' => $carnival,
                'fd_status' => 'Pending',
                'updated_date' => $updated_date,
                'updated_time' => $updated_time,
                'institution_sales' => $inst_sales
			);
			$this->db->where('recipe_id',$recipe_id);
			$this->db->update('recipe', $data);
			
			$adata = array(
            	'hsn_code' => $hsn_code,
            	'rname' => $pname,
            	'yeild' => $yield,
            	'uom' => $uom,
            	'pr_cat_id' => $category
            );
            $this->db->where('recipe_id', $recipe_id);
			$this->db->update('recipe_avg_cost', $adata);
			
			//insert to sales price
		//$pcount = count($cbase_price);
		if (!empty($brchk)) {
		$pcount = count($brchk);
			for($i=0;$i<$pcount;$i++)
			{
			    $ind = $brchk[$i];
				$data2 = array(
					'hsn_code' => $hsn_code,
					'rname' => $pname,
					'uom' => $uom,
					'price' => $cbase_price[$ind],
					'tax' => $cgst_rate[$ind],
					'tax_amount' => $cgst[$ind],
					'total_price' => $ctprice[$ind],
					'pr_cat_id' => $category,
					'recipe_id' => $recipe_id,
					'branchname' => $cbranch[$ind],
					'branch_id' => $cbranch_id[$ind]
				);
				$this->db->where('recipe_price_id',$recipe_price_id[$ind]);
				$this->db->update('recipe_sales_price', $data2);
			}
		}
		else{
		    
		}
		
			$brcount = count($up_branch);
			//$br = explode(',',$up_brname);
			
			for($i=0;$i<$brcount;$i++)
			{
				$q2 = $this->db->query("select * from recipe where recipe_no='$recipe_no' and branch_id='$up_branch[$i]'");
				if($q2->num_rows() > 0){
				//	$upd_in = implode(',',$updated_in);
					$data = array(
						'hsn_code' => $hsn_code,
						'rname' => $pname,
						'f_code' => $fcode,
						'yeild' => $yield,
						'uom' => $uom,
						'price' => $base_price,
						'tax' => $gst_rate,
						'tax_amount' => $gst,
						'total_price' => $tprice,
						'discount' => $discount,
						'recipe_price' => $ttl_amt,
						'recipe_cost' => $rc_cost,
						'product_image' => $pd_upload,
						'raised_on' => $raised_on,
						'od_time' => $od_time,
						'shelf_life' => $s_life,
						'spl_ord' => $spl_ord,
						'food_delivery' => $food_delivery,
						'tprice' => $tbase_price,
						'ttax' => $tgst_rate,
						'ttax_amount' => $tgst,
						'ttotal_price' => $ttprice,
						'santhe_sales' => $santhe,
						'prepared_by' => $prepared,
						'effective_from' => $effctv,
					//	'updated_in' => $upd_in,
						'recipe_cat' => $recipe_cat,
						'flavour' => $fcode,
						'sqcode' => $sqcode,
						'status' => 'Enabled',
						'aprvl_status' => 'Approved',
						'pr_cat_id' => $category,
						'branchname' => $up_brname[$i],
						'branch_id' => $up_branch[$i],
						'combination' => $comb,
						'product_type' => $prtype,
						'carnival_sale' => $carnival,
                        'fd_status' => 'Pending',
                        'updated_date' => $updated_date,
                        'updated_time' => $updated_time,
                        'institution_sales' => $inst_sales
					);
					$this->db->where('recipe_no',$recipe_no);
					$this->db->where('branch_id',$up_branch[$i]);
					$this->db->update('recipe', $data);
				}
				else
				{
					$data3 = array(
						'recipe_no' => $recipe_no,
						'hsn_code' => $hsn_code,
						'raised_date' => $raised_date,
						'rname' => $pname,
						'f_code' => $fcode,
						'yeild' => $yield,
						'uom' => $uom,
						'price' => $base_price,
						'tax' => $gst_rate,
						'tax_amount' => $gst,
						'total_price' => $tprice,
						'discount' => $discount,
						'recipe_price' => $ttl_amt,
						'recipe_cost' => $rc_cost,
						'product_image' => $pd_upload,
						'raised_on' => $raised_on,
						'od_time' => $od_time,
						'shelf_life' => $s_life,
						'spl_ord' => $spl_ord,
						'food_delivery' => $food_delivery,
						'tprice' => $tbase_price,
						'ttax' => $tgst_rate,
						'ttax_amount' => $tgst,
						'ttotal_price' => $ttprice,
						'santhe_sales' => $santhe,
						'prepared_by' => $prepared,
						'effective_from' => $effctv,
					//	'updated_in' => $upd_in,
						'recipe_cat' => $recipe_cat,
						'flavour' => $fcode,
						'sqcode' => $sqcode,
						'status' => 'Enabled',
						'aprvl_status' => 'Approved',
						'pr_cat_id' => $category,
						'branchname' => $up_brname[$i],
						'branch_id' => $up_branch[$i],
						'combination' => $comb,
						'product_type' => $prtype,
						'carnival_sale' => $carnival,
                        'fd_status' => 'Pending',
                        'updated_date' => $updated_date,
                        'updated_time' => $updated_time,
                        'institution_sales' => $inst_sales
					);
					$this->db->insert('recipe', $data3);
				}
			}
			
			if($inst_sales == 'No')
			{
    		//	if($base_price != '' && $base_price != '0')
    		//	{
    			    $q1 = $this->db2->query("select * from $this->category_master where cat_ref='$category'");
        			if($q1->num_rows() > 0)
        			{
        				$row = $q1->row_array();
        				$cat_ref = $row['category_master_id'];
        			}
    			    if (!empty($brchk)) 
    			    {
    				//$slcount = count($cbase_price);
    				$slcount = count($brchk);
    				for($i=0;$i<$slcount;$i++)
    				{
    				    $ind = $brchk[$i];
    					$ctsgst_tax = $cgst_rate[$ind]/2;
    					$ctsgst_tax_amt = $cgst[$ind]/2;
    					$q1 = $this->db2->query("select * from $this->product_category where cat_ref='$category' and branch='$cbranch_code[$ind]'");
                		if($q1->num_rows() > 0)
                		{
                			$row = $q1->row_array();
                			$cat_ref_id = $row['pr_cat_id'];
                		}
                		
    					if($cbase_price != '' && $cbase_price != '0')
    					{
    						$q4 = $db2->query("select * from $this->product where product_no='$recipe_no' and branch='$cbranch_code[$ind]' and social_order!='1'");
    					
    						if($q4->num_rows() > 0){
    							$data4 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'uom' => $uom,
    								'basic_rate' => $cbase_price[$ind],
    								'basic_tax_percent' => $cgst_rate[$ind],
    								'basic_tax_amount' => $cgst[$ind],
    								'sgst_tax' => $ctsgst_tax,
    								'sgst_tax_amount' => $ctsgst_tax_amt,
    								'cgst_tax' => $ctsgst_tax,
    								'cgst_tax_amount' => $ctsgst_tax_amt,
    								'price' => $ctprice[$ind],
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
    								'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db3->where('product_no',$recipe_no);
    							$db3->where('branch',$cbranch_code[$ind]);
    							$db3->where('social_order !=','1');
    							$db3->update($this->product, $data4);
    							
    							$data4 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'uom' => $uom,
    								'basic_rate' => $cbase_price[$ind],
    								'basic_tax_percent' => $cgst_rate[$ind],
    								'basic_tax_amount' => $cgst[$ind],
    								'sgst_tax' => $ctsgst_tax,
    								'sgst_tax_amount' => $ctsgst_tax_amt,
    								'cgst_tax' => $ctsgst_tax,
    								'cgst_tax_amount' => $ctsgst_tax_amt,
    								'price' => $ctprice[$ind],
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
    								'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db2->where('product_no',$recipe_no);
    							$db2->where('branch',$cbranch_code[$ind]);
    							$db2->where('social_order !=','1');
    							$db2->update($this->product, $data4);
    							
    						}
    						else
    						{
    							$data4 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => $cbase_price[$ind],
    								'basic_tax_percent' => $cgst_rate[$ind],
    								'basic_tax_amount' => $cgst[$ind],
    								'sgst_tax' => $ctsgst_tax,
    								'sgst_tax_amount' => $ctsgst_tax_amt,
    								'cgst_tax' => $ctsgst_tax,
    								'cgst_tax_amount' => $ctsgst_tax_amt,
    								'price' => $ctprice[$ind],
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
    								'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'client_id' => '0',
    								'api_status' => '0',
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db3->insert($this->product, $data4);
    							$prd_id1 = $db3->insert_id();
    							
    							$data4 = array(
    							    'pr_id' => $prd_id1,
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => $cbase_price[$ind],
    								'basic_tax_percent' => $cgst_rate[$ind],
    								'basic_tax_amount' => $cgst[$ind],
    								'sgst_tax' => $ctsgst_tax,
    								'sgst_tax_amount' => $ctsgst_tax_amt,
    								'cgst_tax' => $ctsgst_tax,
    								'cgst_tax_amount' => $ctsgst_tax_amt,
    								'price' => $ctprice[$ind],
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
    								'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'client_id' => '0',
    								'api_status' => '0',
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db2->insert($this->product, $data4);
    						}
    					}
    				}
    			    }
    				
    				if($santhe == 'Yes')
    				{
    					$stcount = count($st_branch);
    					for($i=0;$i<$stcount;$i++)
    					{
    						$sgst_tax = $gst_rate/2;
    						$sgst_tax_amt = $gst/2;
    						$q2 = $this->db->query("select * from recipe where recipe_no='$recipe_no' and branchname='$st_branch[$i]'");
    						if($q2->num_rows() > 0){
    							
    							$data6 = array(
    								'recipe_no' => $recipe_no,
    								'hsn_code' => $hsn_code,
    								'raised_date' => $raised_date,
    								'rname' => $pname,
    								'f_code' => $fcode,
    								'yeild' => $yield,
    								'uom' => $uom,
    								'price' => $base_price,
    								'tax' => $gst_rate,
    								'tax_amount' => $gst,
    								'total_price' => $tprice,
    								'discount' => $discount,
    								'recipe_price' => $ttl_amt,
    								'recipe_cost' => $rc_cost,
    								'product_image' => $pd_upload,
    								'raised_on' => $raised_on,
    								'od_time' => $od_time,
    								'shelf_life' => $s_life,
    								'spl_ord' => $spl_ord,
    								'food_delivery' => $food_delivery,
    								'tprice' => $tbase_price,
    								'ttax' => $tgst_rate,
    								'ttax_amount' => $tgst,
    								'ttotal_price' => $ttprice,
    								'santhe_sales' => $santhe,
    								'prepared_by' => $prepared,
    								'effective_from' => $effctv,
    								//'updated_in' => $upd_in,
    								'recipe_cat' => $recipe_cat,
    								'flavour' => $fcode,
    								'sqcode' => $sqcode,
    								'status' => 'Enabled',
    								'aprvl_status' => 'Approved',
    								'pr_cat_id' => $category,
    								'branchname' => $st_branch[$i],
    								'branch_id' => $st_branch_id[$i],
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'carnival_sale' => $carnival,
    								'updated_date' => $updated_date,
                                    'updated_time' => $updated_time
    							);
    							$this->db->where('recipe_no',$recipe_no);
    							$this->db->where('branch_id',$st_branch_id[$i]);
    							$this->db->update('recipe', $data6);
    							
    							$data5 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => $base_price,
    								'basic_tax_percent' => $gst_rate,
    								'basic_tax_amount' => $gst,
    								'sgst_tax' => $sgst_tax,
    								'sgst_tax_amount' => $sgst_tax_amt,
    								'cgst_tax' => $sgst_tax,
    								'cgst_tax_amount' => $sgst_tax_amt,
    								'price' => $tprice,
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $st_branch[$i],
    								'branch_id' => $st_branch_id[$i],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db2->where('product_no',$recipe_no);
    							$db2->where('branch',$st_branch[$i]);
    							$db2->update($this->product, $data5);
    							
    							$data5 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => $base_price,
    								'basic_tax_percent' => $gst_rate,
    								'basic_tax_amount' => $gst,
    								'sgst_tax' => $sgst_tax,
    								'sgst_tax_amount' => $sgst_tax_amt,
    								'cgst_tax' => $sgst_tax,
    								'cgst_tax_amount' => $sgst_tax_amt,
    								'price' => $tprice,
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $st_branch[$i],
    								'branch_id' => $st_branch_id[$i],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db3->where('product_no',$recipe_no);
    							$db3->where('branch',$st_branch[$i]);
    							$db3->update($this->product, $data5);
    						}
    						else
    						{
    						    $data5 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => $base_price,
    								'basic_tax_percent' => $gst_rate,
    								'basic_tax_amount' => $gst,
    								'sgst_tax' => $sgst_tax,
    								'sgst_tax_amount' => $sgst_tax_amt,
    								'cgst_tax' => $sgst_tax,
    								'cgst_tax_amount' => $sgst_tax_amt,
    								'price' => $tprice,
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $st_branch[$i],
    								'branch_id' => $st_branch_id[$i],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'client_id' => '0',
    								'api_status' => '0',
    								'product_type' => $prtype,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db3->insert($this->product, $data5);
    							$prd_ids1 = $db3->insert_id();
    							
    							$data5 = array(
    							    'pr_id' => $prd_ids1,
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => $base_price,
    								'basic_tax_percent' => $gst_rate,
    								'basic_tax_amount' => $gst,
    								'sgst_tax' => $sgst_tax,
    								'sgst_tax_amount' => $sgst_tax_amt,
    								'cgst_tax' => $sgst_tax,
    								'cgst_tax_amount' => $sgst_tax_amt,
    								'price' => $tprice,
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $st_branch[$i],
    								'branch_id' => $st_branch_id[$i],
    								'data_transfer' => 'pending',
    								'combination' => $comb,
    								'client_id' => '0',
    								'api_status' => '0',
    								'product_type' => $prtype,
    								'product_image' => $pdimage,
    								'newyear_sale' => $carnival
    							);
    							$db2->insert($this->product, $data5);
    							
    							$data6 = array(
    								'recipe_no' => $recipe_no,
    								'hsn_code' => $hsn_code,
    								'raised_date' => $raised_date,
    								'rname' => $pname,
    								'f_code' => $fcode,
    								'yeild' => $yield,
    								'uom' => $uom,
    								'price' => $base_price,
    								'tax' => $gst_rate,
    								'tax_amount' => $gst,
    								'total_price' => $tprice,
    								'discount' => $discount,
    								'recipe_price' => $ttl_amt,
    								'recipe_cost' => $rc_cost,
    								'product_image' => $pd_upload,
    								'raised_on' => $raised_on,
    								'od_time' => $od_time,
    								'shelf_life' => $s_life,
    								'spl_ord' => $spl_ord,
    								'food_delivery' => $food_delivery,
    								'tprice' => $tbase_price,
    								'ttax' => $tgst_rate,
    								'ttax_amount' => $tgst,
    								'ttotal_price' => $ttprice,
    								'santhe_sales' => $santhe,
    								'prepared_by' => $prepared,
    								'effective_from' => $effctv,
    							//	'updated_in' => $upd_in,
    								'recipe_cat' => $recipe_cat,
    								'flavour' => $fcode,
    								'sqcode' => $sqcode,
    								'status' => 'Enabled',
    								'aprvl_status' => 'Approved',
    								'pr_cat_id' => $category,
    								'branchname' => $st_branch[$i],
    								'branch_id' => $st_branch_id[$i],
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'carnival_sale' => $carnival,
                                    'fd_status' => 'Pending',
                                    'updated_date' => $updated_date,
                                    'updated_time' => $updated_time
    							);
    							$this->db->insert('recipe', $data6);
    						}
    					}
    				}
    				else{
    					$stcount = count($st_branch);
    					for($i=0;$i<$stcount;$i++)
    					{
    						$sgst_tax = $gst_rate/2;
    						$sgst_tax_amt = $gst/2;
    						
    						$data5 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $base_price,
    							'basic_tax_percent' => $gst_rate,
    							'basic_tax_amount' => $gst,
    							'sgst_tax' => $sgst_tax,
    							'sgst_tax_amount' => $sgst_tax_amt,
    							'cgst_tax' => $sgst_tax,
    							'cgst_tax_amount' => $sgst_tax_amt,
    							'price' => $tprice,
    							'discount_perc' => $discount,
    							'product_status' => 'Inactive',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $st_branch[$i],
    							'branch_id' => $st_branch_id[$i],
    							'data_transfer' => 'pending',
    							'combination' => $comb,
    							'product_type' => $prtype,
    							'product_image' => $pdimage,
    							'newyear_sale' => $carnival
    						);
    						$db3->where('product_no',$recipe_no);
    						$db3->where('branch',$st_branch[$i]);
    						$db3->update($this->product, $data5);
    						
    						$data5 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'product_no' => $recipe_no,
    							'uom' => $uom,
    							'basic_rate' => $base_price,
    							'basic_tax_percent' => $gst_rate,
    							'basic_tax_amount' => $gst,
    							'sgst_tax' => $sgst_tax,
    							'sgst_tax_amount' => $sgst_tax_amt,
    							'cgst_tax' => $sgst_tax,
    							'cgst_tax_amount' => $sgst_tax_amt,
    							'price' => $tprice,
    							'discount_perc' => $discount,
    							'product_status' => 'Inactive',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $st_branch[$i],
    							'branch_id' => $st_branch_id[$i],
    							'data_transfer' => 'pending',
    							'combination' => $comb,
    							'product_type' => $prtype,
    							'product_image' => $pdimage,
    							'newyear_sale' => $carnival
    						);
    						$db2->where('product_no',$recipe_no);
    						$db2->where('branch',$st_branch[$i]);
    						$db2->update($this->product, $data5);
    					
    					
    						$data6 = array(
    							'recipe_no' => $recipe_no,
    							'hsn_code' => $hsn_code,
    							'raised_date' => $raised_date,
    							'rname' => $pname,
    							'f_code' => $fcode,
    							'yeild' => $yield,
    							'uom' => $uom,
    							'price' => $base_price,
    							'tax' => $gst_rate,
    							'tax_amount' => $gst,
    							'total_price' => $tprice,
    							'discount' => $discount,
    							'recipe_price' => $ttl_amt,
    							'recipe_cost' => $rc_cost,
    							'product_image' => $pd_upload,
    							'raised_on' => $raised_on,
    							'od_time' => $od_time,
    							'shelf_life' => $s_life,
    							'spl_ord' => $spl_ord,
    							'food_delivery' => $food_delivery,
    							'tprice' => $tbase_price,
    							'ttax' => $tgst_rate,
    							'ttax_amount' => $tgst,
    							'ttotal_price' => $ttprice,
    							'santhe_sales' => $santhe,
    							'prepared_by' => $prepared,
    							'effective_from' => $effctv,
    							//'updated_in' => $upd_in,
    							'recipe_cat' => $recipe_cat,
    							'flavour' => $fcode,
    							'sqcode' => $sqcode,
    							'status' => 'Disabled',
    							'aprvl_status' => 'Approved',
    							'pr_cat_id' => $category,
    							'branchname' => $st_branch[$i],
    							'branch_id' => $st_branch_id[$i],
    							'combination' => $comb,
    							'product_type' => $prtype,
    							'carnival_sale' => $carnival,
                                'fd_status' => 'Pending',
                                'updated_date' => $updated_date,
                                'updated_time' => $updated_time
    						);
    						$this->db->where('recipe_id',$recipe_id);
    						$this->db->where('branch_id',$st_branch_id[$i]);
    						$this->db->update('recipe', $data6);
    					}
    				}
    				
    				if($food_delivery == 'Yes')
    				{
    				    if (!empty($brchk)) 
            		    {
    				    $slcount = count($brchk);
    				    for($j=0;$j<$slcount;$j++)
    					{
    					    $ind = $brchk[$j];
    					    $q1 = $this->db2->query("select * from $this->product_category where cat_ref='$category' and branch='$cbranch_code[$ind]'");
                        		if($q1->num_rows() > 0)
                        		{
                        			$row = $q1->row_array();
                        			$cat_ref_id = $row['pr_cat_id'];
                        		}
                        		
    						$tsgst_tax = $tgst_rate/2;
    						$tsgst_tax_amt = $tgst/2;
    						$q2 = $db2->query("select * from $this->product where product_no='$recipe_no' and branch='$cbranch_code[$ind]' and social_order='1'");
    						if($q2->num_rows() > 0){
    						 /*   $data7 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'uom' => $uom,
    								'basic_rate' => $tbase_price,
    								'basic_tax_percent' => $tgst_rate,
    								'basic_tax_amount' => $tgst,
    								'sgst_tax' => $tsgst_tax,
    								'sgst_tax_amount' => $tsgst_tax_amt,
    								'cgst_tax' => $tsgst_tax,
    								'cgst_tax_amount' => $tsgst_tax_amt,
    								'price' => $ttprice,
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
        							'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'social_order' => '1',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'newyear_sale' => '0',
    								'product_image' => $pdimage,
                                    'fd_status' => 'Pending'
    							);
    							$db3->where('product_no',$recipe_no);
    							//$db2->where('branch_id',$cbranch_id[$i]);
    							$db3->where('branch',$cbranch_code[$ind]);
    							$db3->where('social_order','1');
    							$db3->update('product', $data7);
    							
    							$data7 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'uom' => $uom,
    								'basic_rate' => $tbase_price,
    								'basic_tax_percent' => $tgst_rate,
    								'basic_tax_amount' => $tgst,
    								'sgst_tax' => $tsgst_tax,
    								'sgst_tax_amount' => $tsgst_tax_amt,
    								'cgst_tax' => $tsgst_tax,
    								'cgst_tax_amount' => $tsgst_tax_amt,
    								'price' => $ttprice,
    								'discount_perc' => $discount,
    								'product_status' => 'Active',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
        							'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'social_order' => '1',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'newyear_sale' => '0',
    								'product_image' => $pdimage,
                                    'fd_status' => 'Pending'
    							);
    							$db2->where('product_no',$recipe_no);
    							//$db2->where('branch_id',$cbranch_id[$i]);
    							$db2->where('branch',$cbranch_code[$ind]);
    							$db2->where('social_order','1');
    							$db2->update('product', $data7); */
    							
    							$data7 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'uom' => $uom,
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
        							'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'social_order' => '1',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'newyear_sale' => '0',
    								'product_image' => $pdimage,
                                    'fd_status' => 'Pending'
    							);
    							$db3->where('product_no',$recipe_no);
    							//$db2->where('branch_id',$cbranch_id[$i]);
    							$db3->where('branch',$cbranch_code[$ind]);
    							$db3->where('social_order','1');
    							$db3->update($this->product, $data7);
    							
    							$data7 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'uom' => $uom,
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
        							'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'social_order' => '1',
    								'combination' => $comb,
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'newyear_sale' => '0',
    								'product_image' => $pdimage,
                                    'fd_status' => 'Pending'
    							);
    							$db2->where('product_no',$recipe_no);
    							//$db2->where('branch_id',$cbranch_id[$i]);
    							$db2->where('branch',$cbranch_code[$ind]);
    							$db2->where('social_order','1');
    							$db2->update($this->product, $data7);
    							
    							$datat1 = array(
        							'url' => $pdimage,
        						);
        						$db2->where('product_no',$recipe_no);
        						$db2->update('tag1', $datat1);
    						}else{
    						    
    						    $data7 = array(
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => '0',
    								'basic_tax_percent' => '0',
    								'basic_tax_amount' => '0',
    								'sgst_tax' => '0',
    								'sgst_tax_amount' => '0',
    								'cgst_tax' => '0',
    								'cgst_tax_amount' => '0',
    								'price' => '0',
    								'discount_perc' => '0',
    								'product_status' => 'InActive',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
    								'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'social_order' => '1',
    								'combination' => $comb,
    								'client_id' => '0',
    								'api_status' => '0',
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'newyear_sale' => '0',
    								'product_image' => $pdimage,
                                    'fd_status' => 'Pending'
    							);
    							$db3->insert($this->product, $data7);
    							$prd_id2 = $db3->insert_id();
    												
    							$data7 = array(
    							    'pr_id'=>$prd_id2,
    								'product_name' => $pname,
    								'hsn_code' => $hsn_code,
    								'product_no' => $recipe_no,
    								'uom' => $uom,
    								'basic_rate' => '0',
    								'basic_tax_percent' => '0',
    								'basic_tax_amount' => '0',
    								'sgst_tax' => '0',
    								'sgst_tax_amount' => '0',
    								'cgst_tax' => '0',
    								'cgst_tax_amount' => '0',
    								'price' => '0',
    								'discount_perc' => '0',
    								'product_status' => 'InActive',
    								'pr_cat_id' => $cat_ref,
    								'effective_from' => $effctv,
    								'branch' => $cbranch_code[$ind],
    								'branch_id' => $cbranch_id[$ind],
    								'data_transfer' => 'pending',
    								'social_order' => '1',
    								'combination' => $comb,
    								'client_id' => '0',
    								'api_status' => '0',
    								'product_type' => $prtype,
    								'cat_id' => $cat_ref_id,
    								'newyear_sale' => '0',
    								'product_image' => $pdimage,
                                    'fd_status' => 'Pending'
    							);
    							$db2->insert($this->product, $data7);
    							$prid1 = $this->db->insert_id();
    						
        						$datat8 = array(
        							'tag' => 'default',
        							'url' => $pdimage,
        							'product_no' => $recipe_no,
        							'pr_id' => $prid1,
        							'branch' => $cbranch_code[$ind]
        						);
        						$db2->insert('tag1', $datat8);
        						
        						$datat9 = array(
        							'tag' => 'zomato',
        							'url' => $pdimage,
        							'product_no' => $recipe_no,
        							'pr_id' => $prid1,
        							'branch' => $cbranch_code[$ind]
        						);
        						$db2->insert('tag1', $datat9);
        						
        						$datat10 = array(
        							'tag' => 'swiggy',
        							'url' => $pdimage,
        							'product_no' => $recipe_no,
        							'pr_id' => $prid1,
        							'branch' => $cbranch_code[$ind]
        						);
        						$db2->insert('tag1', $datat10);
    						}
    					}
    				}
    				}
    				else if($food_delivery == 'No' && $tbase_price != '' && $tbase_price != '0')
    				{
    				    if (!empty($brchk)) 
            		    {
    				    $slcount = count($brchk);
    				    for($j=0;$j<$slcount;$j++)
    					{
    					    $ind = $brchk[$j];
    					    $q1 = $this->db2->query("select * from $this->product_category where cat_ref='$category' and branch='$cbranch_code[$ind]'");
                    		if($q1->num_rows() > 0)
                    		{
                    			$row = $q1->row_array();
                    			$cat_ref_id = $row['pr_cat_id'];
                    		}
    					    $tsgst_tax = $tgst_rate/2;
    						$tsgst_tax_amt = $tgst/2;
    						$data7 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'uom' => $uom,
    							'product_status' => 'Inactive',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
        						'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'social_order' => '1',
    							'combination' => $comb,
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => '0',
    							'product_image' => $pdimage,
                                'fd_status' => 'Pending'
    						);
    						$db3->where('product_no',$recipe_no);
    						$db3->where('branch',$cbranch_code[$ind]);
    						$db3->where('social_order','1');
    						$db3->update($this->product, $data7);
    						
    						$data7 = array(
    							'product_name' => $pname,
    							'hsn_code' => $hsn_code,
    							'uom' => $uom,
    							'product_status' => 'Inactive',
    							'pr_cat_id' => $cat_ref,
    							'effective_from' => $effctv,
    							'branch' => $cbranch_code[$ind],
        						'branch_id' => $cbranch_id[$ind],
    							'data_transfer' => 'pending',
    							'social_order' => '1',
    							'combination' => $comb,
    							'product_type' => $prtype,
    							'cat_id' => $cat_ref_id,
    							'newyear_sale' => '0',
    							'product_image' => $pdimage,
                                'fd_status' => 'Pending'
    						);
    						$db2->where('product_no',$recipe_no);
    						$db2->where('branch',$cbranch_code[$ind]);
    						$db2->where('social_order','1');
    						$db2->update($this->product, $data7);
    						
    						$datat1 = array(
    							'url' => $pdimage,
    						);
    						$db2->where('product_no',$recipe_no);
    						$db2->update('tag1', $datat1);
    					}
            		    }
    				}
    		//	}
			}
			else
			{
			    $q1 = $this->db2->query("select * from $this->category_master where cat_ref='$category'");
    			if($q1->num_rows() > 0)
    			{
    				$row = $q1->row_array();
    				$cat_ref = $row['category_master_id'];
    			}
    			if (!empty($brchk)) 
			    {
			        $slcount = count($brchk);
			        for($i=0;$i<$slcount;$i++)
    				{
    				    $ind = $brchk[$i];
    					$ctsgst_tax = $cgst_rate[$ind]/2;
    					$ctsgst_tax_amt = $cgst[$ind]/2;
    					$q1 = $this->db2->query("select * from $this->product_category where cat_ref='$category' and branch='$cbranch_code[$ind]'");
                		if($q1->num_rows() > 0)
                		{
                			$row = $q1->row_array();
                			$cat_ref_id = $row['pr_cat_id'];
                		}
                		$q4 = $db2->query("select * from $this->product where product_no='$recipe_no' and branch='$cbranch_code[$ind]' and social_order!='1' and data_transfer='institute'");
                		if($q4->num_rows() > 0)
                		{
                		    $data4 = array(
								'product_name' => $pname,
								'hsn_code' => $hsn_code,
								'uom' => $uom,
								'basic_rate' => $cbase_price[$ind],
								'basic_tax_percent' => $cgst_rate[$ind],
								'basic_tax_amount' => $cgst[$ind],
								'sgst_tax' => $ctsgst_tax,
								'sgst_tax_amount' => $ctsgst_tax_amt,
								'cgst_tax' => $ctsgst_tax,
								'cgst_tax_amount' => $ctsgst_tax_amt,
								'price' => $ctprice[$ind],
								'discount_perc' => $discount,
								'product_status' => 'Active',
								'pr_cat_id' => $cat_ref,
								'effective_from' => $effctv,
								'branch' => $cbranch_code[$ind],
								'branch_id' => $cbranch_id[$ind],
								'data_transfer' => 'institute',
								'combination' => $comb,
								'product_type' => $prtype,
								'cat_id' => $cat_ref_id,
								'product_image' => $pdimage,
								'newyear_sale' => $carnival
							);
							$db3->where('product_no',$recipe_no);
							$db3->where('branch',$cbranch_code[$ind]);
							$db3->where('social_order !=','1');
							$db2->where('data_transfer','institute');
							$db3->update($this->product, $data4);
							
							$data4 = array(
								'product_name' => $pname,
								'hsn_code' => $hsn_code,
								'uom' => $uom,
								'basic_rate' => $cbase_price[$ind],
								'basic_tax_percent' => $cgst_rate[$ind],
								'basic_tax_amount' => $cgst[$ind],
								'sgst_tax' => $ctsgst_tax,
								'sgst_tax_amount' => $ctsgst_tax_amt,
								'cgst_tax' => $ctsgst_tax,
								'cgst_tax_amount' => $ctsgst_tax_amt,
								'price' => $ctprice[$ind],
								'discount_perc' => $discount,
								'product_status' => 'Active',
								'pr_cat_id' => $cat_ref,
								'effective_from' => $effctv,
								'branch' => $cbranch_code[$ind],
								'branch_id' => $cbranch_id[$ind],
								'data_transfer' => 'institute',
								'combination' => $comb,
								'product_type' => $prtype,
								'cat_id' => $cat_ref_id,
								'product_image' => $pdimage,
								'newyear_sale' => $carnival
							);
							$db2->where('product_no',$recipe_no);
							$db2->where('branch',$cbranch_code[$ind]);
							$db2->where('social_order !=','1');
							$db2->where('data_transfer','institute');
							$db2->update($this->product, $data4);
                		}
                		else
						{
							$data4 = array(
								'product_name' => $pname,
								'hsn_code' => $hsn_code,
								'product_no' => $recipe_no,
								'uom' => $uom,
								'basic_rate' => $cbase_price[$ind],
								'basic_tax_percent' => $cgst_rate[$ind],
								'basic_tax_amount' => $cgst[$ind],
								'sgst_tax' => $ctsgst_tax,
								'sgst_tax_amount' => $ctsgst_tax_amt,
								'cgst_tax' => $ctsgst_tax,
								'cgst_tax_amount' => $ctsgst_tax_amt,
								'price' => $ctprice[$ind],
								'discount_perc' => $discount,
								'product_status' => 'Active',
								'pr_cat_id' => $cat_ref,
								'effective_from' => $effctv,
								'branch' => $cbranch_code[$ind],
								'branch_id' => $cbranch_id[$ind],
								'data_transfer' => 'institute',
								'combination' => $comb,
								'client_id' => '0',
								'api_status' => '0',
								'product_type' => $prtype,
								'cat_id' => $cat_ref_id,
								'product_image' => $pdimage,
								'newyear_sale' => $carnival
							);
							$db3->insert($this->product, $data4);
							$prd_id1 = $db3->insert_id();
							
							$data4 = array(
							    'pr_id' => $prd_id1,
								'product_name' => $pname,
								'hsn_code' => $hsn_code,
								'product_no' => $recipe_no,
								'uom' => $uom,
								'basic_rate' => $cbase_price[$ind],
								'basic_tax_percent' => $cgst_rate[$ind],
								'basic_tax_amount' => $cgst[$ind],
								'sgst_tax' => $ctsgst_tax,
								'sgst_tax_amount' => $ctsgst_tax_amt,
								'cgst_tax' => $ctsgst_tax,
								'cgst_tax_amount' => $ctsgst_tax_amt,
								'price' => $ctprice[$ind],
								'discount_perc' => $discount,
								'product_status' => 'Active',
								'pr_cat_id' => $cat_ref,
								'effective_from' => $effctv,
								'branch' => $cbranch_code[$ind],
								'branch_id' => $cbranch_id[$ind],
								'data_transfer' => 'institute',
								'combination' => $comb,
								'client_id' => '0',
								'api_status' => '0',
								'product_type' => $prtype,
								'cat_id' => $cat_ref_id,
								'product_image' => $pdimage,
								'newyear_sale' => $carnival
							);
							$db2->insert($this->product, $data4);
						}
    				}
			    }
			}
			
			if(!empty($inames))
			{
    			for($i=0;$i<count($inames);$i++) 
    			{
    				$data3 = array(
    					'item_name' => $inames[$i],
    					'qty' => $quantitys[$i],
    					'price' => $prices[$i],
    					'uom' => $uoms[$i],
    					'total' => $totals[$i]
    				);
    				$this->db->where('recipe_id',$recipe_id);
    				$this->db->where('rc_item_id',$rcitem_ids[$i]);
    				$this->db->update('recipe_item', $data3);
    				
    			}
    		}
			//insert to item
			$icount = count($item_name);
			for($i=0;$i<$icount;$i++)
			{
				if(!empty($item_name[$i]))
			    {
					$data3 = array(
						'item_name' => $item_name[$i],
						'qty' => $quantity[$i],
						'price' => $price[$i],
						'uom' => $iuom[$i],
						'total' => $total[$i],
						'recipe_id' => $recipe_id,
						'recipe_no' => $recipe_no,
						'item_id' => $item_id[$i],
						'category_id' => $cat_id[$i],
						'itm_id' => $itm_id[$i]
					);
					$this->db->insert('recipe_item', $data3);
			    }
			}
			if(!empty($sub_inames))
			{
    			for($i=0;$i<count($sub_inames);$i++)
    			{
    
    				$data4 = array(
    					'item_name' => $sub_inames[$i],
    					'qty' => $sub_quantitys[$i],
    					'price' => $sub_prices[$i],
    					'uom' => $sub_uoms[$i],
    					'total' => $sub_totals[$i]
    				);
    				$this->db->where('recipe_id',$recipe_id);
    				$this->db->where('sub_rc_item_id',$sub_item_ids[$i]);
    				$this->db->update('sub_recipe_item', $data4);
    				
    			}
    		}
			//insert to sub item
			$scount = count($sub_item_name);
			for($k=0;$k<$scount;$k++)
			{
				if(!empty($sub_item_name[$k]))
				{
    				$data4 = array(
    					'item_name' => $sub_item_name[$k],
    					'qty' => $sub_quantity[$k],
    					'price' => $sub_price[$k],
    					'uom' => $sub_uom[$k],
    					'total' => $sub_total[$k],
    					'recipe_id' => $recipe_id,
    					'rc_no' => $recipe_no,
    					'sub_recipe_id' => $sub_item_id[$k],
    					'pr_cat_id' => $sub_cat_id[$k],
    					'recipe_no' => $rcp_no[$k]
    				);
    				$this->db->insert('sub_recipe_item', $data4);
    			}
			}
		}
		
		
		
		public function get_vendor($keyword){
			$this->db->distinct();
			$this->db->select('company_name,contact_no');
			$this->db->from('vendor_creation');
			$this->db->like('company_name', $keyword);
			$query = $this->db->get();
			foreach($query->result_array() as $row){
				//$data[$row['friendly_name']];
				$data[] = $row;
			}
			return $query;
		}
		
		public function get_item_branchid($keyword,$branch_id) {
			$this->db->distinct();
			$this->db->select('item_name,price');
			$this->db->from('maintainance_product');
			$this->db->like('item_name', $keyword);
			$this->db->where('branch_id',$branch_id);
			$query = $this->db->get();
			foreach($query->result_array() as $row){
				//$data[$row['friendly_name']];
				$data[] = $row;
			}
			return $query;
		}
		
		public function get_supplier_name($keyword){
			$this->db->distinct();
			$this->db->select('vendor_name');
			$this->db->from('maintainance_grn');
			$this->db->like('vendor_name', $keyword);
			$query = $this->db->get();
			foreach($query->result_array() as $row){
				//$data[$row['friendly_name']];
				$data[] = $row;
			}
			return $query;
		}
		
		public function addCombination($postdata)
		{
			extract($postdata);
			
			$data = array(
				'combination' => $combination,
				'quantity' => $qty,
				'price	' => $price
			);
			$this->db->insert('combination', $data);
			$cmb_id = $this->db->insert_id();
			
			$data2 = array(
				'comb_id' => $cmb_id,
				'combination' => $combination,
				'quantity' => $qty,
				'price	' => $price,
				'client_id' => '0'
			);
			$this->db2->insert('combination', $data2);
		}
		
		public function getCombination(){
			$query = $this->db->query("select comb_id,combination,quantity,price from combination");
			return $query->result();
		}
		
		public function getCatRecipe($branch_id){
			$query = $this->db->query("select * from product_category p inner join recipe r on p.pr_cat_id=r.pr_cat_id where r.branch_id='$branch_id' and r.status='Enabled' order by rname asc");
			return $query->result();
		}
		
		public function getDCCatRecipe($branch_id){
			$query = $this->db->query("select * from product_category p inner join recipe r on p.pr_cat_id=r.pr_cat_id where r.branch_id='$branch_id' order by rname asc");
			return $query->result();
		}
		
		public function getCustomer($branch_id) {
	        $query = $this->db->query("SELECT * FROM customer where out_branch_id='$branch_id'");
			return $query->result();
		}
		
		public function getUserAccess($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_outlet_phno() {
    	    $outlet='CEO';
    		$query = $this->db->query("SELECT * from user where username='$outlet'");
    	    return $query->result_array();
    	}
    	
    	public function get_uom($keyword){
			$this->db->distinct();
			$this->db->select('unit_measure,unit_id');
			$this->db->from('unit_of_measure');
			$this->db->like('unit_measure', $keyword);
			$query = $this->db->get();
			foreach($query->result_array() as $row){
				//$data[$row['friendly_name']];
				$data[] = $row;
			}
			return $query;
		}
		
		public function get_auoto_item($keyword){
			$this->db->distinct();
			$this->db->select('item_name,itm_id');
			$this->db->from('item');
			$this->db->like('item_name', $keyword);
			$query = $this->db->get();
			foreach($query->result_array() as $row){
				//$data[$row['friendly_name']];
				$data[] = $row;
			}
			return $query;
		}
		
		public function get_item($pname){
		    $query = $this->db->query("SELECT count(*) as num FROM item_creation where item_name='$pname'");
		    $row=$query->row_array();
		   
			if($row)
    		{
    			$data=array("counts" => $row['num']);
    			echo json_encode($data);
    		}
    		else
    		{
    			$data = array("counts" => '0');
    			echo json_encode($data);
    		}
		}
		
		public function get_product($pname){
		    $query = $this->db->query("SELECT count(*) as num FROM recipe where rname='$pname'");
		    $row=$query->row_array();
		   
			if($row)
    		{
    			$data=array("counts" => $row['num']);
    			echo json_encode($data);
    		}
    		else
    		{
    			$data = array("counts" => '0');
    			echo json_encode($data);
    		}
		}
		
	public function get_Item_Details($iname,$br_id)
	{
		$query = $this->db->query("select item_name,uom,item_price,item_tax,stock_item,item_id,category_id,itm_id from item_creation where item_name='$iname' and branch_id='$br_id'");
		$row=$query->row_array();
		if($row)
		{
			$price = $row['item_price'];
			$tax = $row['item_tax'];
			$tax_a = ($price * $tax)/100;
			$tot_price = $price + $tax_a;
			$data=array("item_name" => $row['item_name'], "uom" => $row['uom'], "item_price" => $tot_price, "item_tax" => $row['item_tax'], "item_id" => $row['item_id'], "stock_item"=> $row['stock_item'], "category_id" => $row['category_id'], "itm_id" => $row['itm_id']);
			echo json_encode($data);
		}
		else
		{
			$data = array("uom" => '0');
			echo json_encode($data);
		}
	}
	
	public function get_item_all() {
							
		$query = $this->db->query("SELECT * FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW' order by T.item_id");
		return $query->result();
	}
	
	
	public function get_item_history1($item_name,$branchname){
	    $query = $this->db->query("select max(`stock_id`) as stid,opening_stock,item_name,DATE_FORMAT(transaction_date,'%M') as month1,DATE_FORMAT(transaction_date,'%m') as month2,sum(in_qty)as inward,sum(out_qty) as outward,branchname,max(last_modified) as maxdate1 from stock_detail where branchname='$branchname' and item_name='$item_name' and (transaction_date >= CURDATE() - INTERVAL 3 MONTH )and trans_ref_no like 'G%' group by month1 order by month2 asc");
		return $query->result();
	}
	
	public function get_item_history2($item_name,$branchname){
	    $query = $this->db->query("select max(`stock_id`) as stid1,opening_stock,item_name,DATE_FORMAT(transaction_date,'%M') as month,DATE_FORMAT(transaction_date,'%m') as month2,sum(in_qty)as inward,sum(out_qty) as outward,branchname,max(last_modified) as maxdate1 from stock_detail where branchname='$branchname' and item_name='$item_name' and (transaction_date >= CURDATE() - INTERVAL 3 MONTH )group by month order by month2 asc");
		return $query->result();
	}
	
	public function getAllCategory(){
		$query = $this->db->query("select * from category");
		return $query->result();
	}
	
	public function getCatname($cat_id)
	{
		$query = $this->db->query("SELECT `category_id`, `category_name`, `status` FROM `category` where category_id='$cat_id'");
		$row=$query->row_array();
		if($row)
		{
			
			$data=array("category_id" => $row['category_id'], "category_name" => $row['category_name'], "status" => $row['status']);
			echo json_encode($data);
		}
		else
		{
			$data = array("category_id" => '0');
			echo json_encode($data);
		}
	}
	
	public function searchAccCat($data)
	{
		$to_date =date('Y-m-d', strtotime($data['to_date']));
		$branch_name = $data['branch'];
		$category_name = $data['category_name'];
		if($to_date!="" && $branch_name!="" && $category_name==""){
    		$query = $this->db->query("select c.category_name,i.item_name,i.uom,i.item_price,i.item_tax,i.avg_price,i.vendor_name,st.closing_stock,(i.item_price*st.closing_stock) as total from (select * from category) c inner join (select item_name,uom,item_price,item_tax,avg_price,vendor_name,category_id from item_creation where branchname='$branch_name' and status='Enabled') i on c.category_id=i.category_id inner join (select * from stock_detail) st on i.item_name=st.item_name inner join (select item_name,transaction_date,closing_stock,MAX(last_modified) as maxdate from stock_detail where branchname='$branch_name' and transaction_date <= '$to_date' group by item_name) stk on st.item_name=stk.item_name and st.last_modified=stk.maxdate");
		}
		else if($to_date!="" && $branch_name!="" && $category_name!=""){
		    $query = $this->db->query("select c.category_name,i.item_name,i.uom,i.item_price,i.item_tax,i.avg_price,i.vendor_name,st.closing_stock,(i.item_price*st.closing_stock) as total from (select * from category) c inner join (select item_name,uom,item_price,item_tax,avg_price,vendor_name,category_id from item_creation where branchname='$branch_name' and category_id='$category_name' and status='Enabled') i on c.category_id=i.category_id inner join (select * from stock_detail) st on i.item_name=st.item_name inner join (select item_name,transaction_date,closing_stock,MAX(last_modified) as maxdate from stock_detail where branchname='$branch_name' and transaction_date <= '$to_date' group by item_name) stk on st.item_name=stk.item_name and st.last_modified=stk.maxdate");
		}
		return $query->result_array();	
	}
	
	public function get_vendordata($company_name){
	    $branch_name = $_SESSION['branch_name'];
	    $cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("select DISTINCT(item_name) from item_creation where vendor_name='$company_name'");
        }
        else{
            $query = $this->db->query("select DISTINCT(item_name) from item_creation where vendor_name='$company_name' and branchname='$cw_ceo'");
        }
        /*if($branch_name=='CW4_CEO')
        {
    		$query = $this->db->query("select DISTINCT(item_name) from item_creation where vendor_name='$company_name' and branchname='CW4'");
        }
        else if($branch_name=='CW5_CEO')
        {
    		$query = $this->db->query("select DISTINCT(item_name) from item_creation where vendor_name='$company_name' and branchname='CW5'");
        }
        else if($branch_name=='CW6_CEO')
        {
    		$query = $this->db->query("select DISTINCT(item_name) from item_creation where vendor_name='$company_name' and branchname='CW6'");
        }
        else
	    {
    		$query = $this->db->query("select DISTINCT(item_name) from item_creation where vendor_name='$company_name'");
	    }*/
		return $query->result();
	}
	
	public function get_pCategory()
	{
		$query = $this->db->query("SELECT `pr_cat_id`, `pr_cat_name`, `effective_from`, `applicable_for`, `status`, `common_status`, `combination`, `combo_qty_limit`, `social_order` from product_category");
		return $query->result();
	}
	
	public function searchAdmCat($data)
	{
		$to_date =date('Y-m-d', strtotime($data['to_date']));
		//$branch_name = $data['branch'];
		$category_name = $data['category_name'];
		
		    $query = $this->db->query("select c.category_name,i.item_id,i.item_name,i.uom,i.item_price,i.item_tax,i.avg_price,i.vendor_name,i.branchname,st.closing_stock,(i.item_price*st.closing_stock) as total from (select * from category) c inner join (select item_name,item_id,uom,item_price,item_tax,avg_price,vendor_name,category_id,branchname from item_creation where branchname='CW4' and category_id='$category_name' and status='Enabled') i on c.category_id=i.category_id inner join (select * from stock_detail) st on i.item_name=st.item_name inner join (select item_name,transaction_date,closing_stock,MAX(last_modified) as maxdate from stock_detail where branchname='CW4' and transaction_date <= '$to_date' group by item_name) stk on st.item_name=stk.item_name and st.last_modified=stk.maxdate");
	
		return $query->result_array();	
	}
		
	public function get_itemall() {
		$branch_name = $_SESSION['branch_name'];
		$cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW' order by T.item_name");
        }
        else{
            $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='$cw_ceo' order by T.item_name");
        }
        /*if($branch_name=='CW4_CEO')
        {
		    $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW4' order by T.item_name");
        }
        else if($branch_name=='CW5_CEO')
        {
		    $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW5' order by T.item_name");
        }
        else if($branch_name=='CW6_CEO')
        {
		    $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW6' order by T.item_name");
        }
        else{
             $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW' order by T.item_name");
        }*/
		return $query->result();
	}
	
	public function get_CWitemall() {
		$branch_name = $_SESSION['branch_name'];
		$cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
             $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='$branch_name' and T.status='Enabled' order by T.item_name");
        }
        if($cw_ceo=='CEO')
        {
              $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW' and T.status='Enabled' order by T.item_name");
        }
        else{
            $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='$cw_ceo' order by T.item_name");
        }
        /*if($branch_name=='CW4_CEO')
        {
		    $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW4' order by T.item_name");
        }
        else if($branch_name=='CW5_CEO')
        {
		    $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW5' order by T.item_name");
        }
        else if($branch_name=='CW6_CEO')
        {
		    $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW6' order by T.item_name");
        }
        else if($branch_name=='CEO')
        {
		    $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='CW' and T.status='Enabled' order by T.item_name");
        }
        else{
             $query = $this->db->query("SELECT *,T.branchname as br_name FROM item_creation T inner join category C on C.category_id=T.category_id where T.branchname='$branch_name' and T.status='Enabled' order by T.item_name");
        }*/
		return $query->result();
	}
	
	public function get_Products1($branch_id)
	{
		$query = $this->db->query("SELECT rname from recipe where branch_id='$branch_id' and status='Enabled'");
		return $query->result();
	}
	
	
	public function getCounter()
	{
		$query = $this->db->query("SELECT * FROM `user` WHERE `branch_name` like '%CTR'");
		return $query->result();
	}
	
	public function searchCWCat($data)
	{
		$to_date =date('Y-m-d', strtotime($data['to_date']));
		$branch_name = $_SESSION['branch_name'];
		$category_name = $data['category_name'];
		if($category_name == 'All')
		{
			 $query = $this->db->query("select c.category_name,i.item_name,i.uom,i.item_price,i.item_tax,i.avg_price,st.closing_stock,(i.item_price*st.closing_stock) as total from (select * from add_category) c inner join (select item_name,uom,item_price,item_tax,avg_price,category_id from item_creation where branchname='$branch_name' and status='Enabled') i on c.category_id=i.category_id inner join (select * from stock_detail) st on i.item_name=st.item_name inner join (select item_name,transaction_date,closing_stock,MAX(last_modified) as maxdate from stock_detail where branchname='$branch_name' and transaction_date <= '$to_date' group by item_name) stk on st.item_name=stk.item_name and st.last_modified=stk.maxdate");
		}
		else
		{
			 $query = $this->db->query("select c.category_name,i.item_name,i.uom,i.item_price,i.item_tax,i.avg_price,st.closing_stock,(i.item_price*st.closing_stock) as total from (select * from add_category) c inner join (select item_name,uom,item_price,item_tax,avg_price,category_id from item_creation where branchname='$branch_name' and category_id='$category_name') i on c.category_id=i.category_id inner join (select * from stock_detail) st on i.item_name=st.item_name inner join (select item_name,transaction_date,closing_stock,MAX(last_modified) as maxdate from stock_detail where branchname='$branch_name' and transaction_date <= '$to_date' group by item_name) stk on st.item_name=stk.item_name and st.last_modified=stk.maxdate");
		}
		
		return $query->result_array();	
	}
	
	public function fetchCategory($smd_id)
	{
		$query=$this->db->query("SELECT * FROM user_access where module_id='$smd_id' and access_category!=''");
		$result = $query->result();
		return $result;
	}
	
	public function fetchProduct($cat,$branch_id )
	{
		$query=$this->db->query("select * from recipe where pr_cat_id='$cat' and branch_id='$branch_id' and status='Enabled'");
		$result = $query->result();
		return $result;
	}
	
	public function addMapping($postdata)
	{
		extract($postdata);
		$count = count($pr_name);
		//echo $count;
		for($i=0;$i<$count;$i++)
		{
			$q1 = $this->db->query("select * from recipe where rname='$pr_name[$i]' and branch_id='$branch_id'");
			$row = $q1->row_array();
			if($pr_name[$i]!=''){
			    //echo $row['recipe_id'];
    			$map_recipe_id[$i] = $row['recipe_id'];
        		$map_recipe_no[$i] = $row['recipe_no'];
    			$data2 = array(
    				'map_recipe_id' => $map_recipe_id[$i],
    				'map_recipe_no' => $map_recipe_no[$i]
    				);
    			$this->db->where('recipe_no',$recipe_no[$i]);
    			$this->db->update('recipe', $data2);
			}
		}
	}
	
	public function get_Recipe($branch_id){
		$query = $this->db->query("select * from recipe R where pr_cat_id NOt in(16,17,20,21,24,25) and  R.branch_id='$branch_id'");
		return $query->result();
	}
	
	public function getProductDetail($branch_id){
		$query = $this->db->query("select R.recipe_id,R.recipe_no,R.pr_cat_id,C.pr_cat_name,R.rname as product_name,R.map_recipe_no as map_recipe_no from product_category C inner join recipe R on C.pr_cat_id=R.pr_cat_id where  R.pr_cat_id in(16,17,18,19,20,21,22,23,24,25) and R.branch_id='$branch_id'  and R.status='Enabled' order by C.pr_cat_name,R.rname");
		return $query->result_array();
	}
	
	public function prMappingList($branch_id){
		$query = $this->db->query("select rname,map_recipe_no from recipe  where pr_cat_id in(16,17,20,21,24,25) and  branch_id='$branch_id'");
		return $query->result_array();
	}
	
	public function getrecipe($branch_id){
	    $branch_id = $_SESSION['branch_id'];
	    $cur_date = date('Y-m-d');
	    $query = $this->db->query("select R.recipe_id,R.recipe_no,R.pr_cat_id,C.pr_cat_name,R.rname as product_name,COALESCE(RP.total_price, 0 ) as total_price,COALESCE(RP.tax, 0 ) as tax,R.food_delivery,R.ttotal_price,R.ttax,'$cur_date' as effective_date,R.branchname,R.branch_id from product_category C inner join recipe R on C.pr_cat_id=R.pr_cat_id left join recipe_sales_price RP on R.recipe_no=RP.recipe_no and R.branch_id=RP.branch_id where R.branchname in('CW-CTR','CW2-CTR','CW3-CTR','CW4-CTR','CW5-CTR','CW6-CTR','CWC-1') and R.status='Enabled' order by C.pr_cat_name,R.rname,R.branch_id");
	  	return $query->result_array();
	}
	
	public function getPriceUpdateRecipe($up_date){
	    $branch_id = $_SESSION['branch_id'];
	    $query = $this->db->query("select R.recipe_id,R.recipe_no,R.pr_cat_id,C.pr_cat_name,R.rname as product_name,R.price,R.tax,R.tax_amount,R.total_price,R.food_delivery,R.tprice,R.ttax,R.ttax_amount,R.ttotal_price,R.branchname,R.branch_id from product_category C inner join recipe R on C.pr_cat_id=R.pr_cat_id where R.branchname in('CW-CTR','CW2-CTR','CW3-CTR','CW4-CTR','CW5-CTR','CWC-1','CW6-CTR') and R.sls_price_up_date='$up_date' order by C.pr_cat_name,R.rname,R.branch_id");
	  	return $query->result_array();
	}
	
	public function getPriceUpdateRecipeList(){
	    $branch_id = $_SESSION['branch_id'];
	    $up_date = date("Y-m-d");
	    $query = $this->db->query("SELECT DISTINCT(sls_price_up_date) as up_date FROM `recipe` WHERE sls_price_up_date!=''");
	  	return $query->result_array();
	}
	
	public function getdcrecipe(){
	    $branch_id = $_SESSION['branch_id'];
	    $cur_date = date('Y-m-d');
	    $query = $this->db->query("select R.recipe_id,R.recipe_no,R.pr_cat_id,C.pr_cat_name,R.rname as product_name,R.cw_total_price,R.cw_tax,R.cw4_total_price,R.cw4_tax,R.cw5_total_price,R.cw5_tax,U.username,R.branchname,R.branch_id from product_category C inner join recipe R on C.pr_cat_id=R.pr_cat_id inner join user U on R.branch_id=U.branch_id where R.branchname in('CW-SVR','CW-SK','CW-FFK','CMS','CW-DCK','CW3-CK','CW2-FFK','CW3-FFK','CW4-SK','CW4-CLD','CW4-SPL','CW5-HK') and R.status='Enabled' order by C.pr_cat_name,R.rname,R.branch_id");
	  	return $query->result_array();
	}
	
	public function getDCPriceUpdateRecipe($up_date){
	    $branch_id = $_SESSION['branch_id'];
	    $query = $this->db->query("select R.recipe_id,R.recipe_no,R.pr_cat_id,C.pr_cat_name,R.rname as product_name,R.cw_price,R.cw_tax_amount,R.cw_total_price,R.cw_tax,R.cw4_price,R.cw4_tax_amount,R.cw4_total_price,R.cw4_tax,R.cw5_price,R.cw5_tax_amount,R.cw5_total_price,R.cw5_tax,U.username,R.branchname,R.branch_id from product_category C inner join recipe R on C.pr_cat_id=R.pr_cat_id inner join user U on R.branch_id=U.branch_id where R.branchname in('CW-SVR','CW-SK','CW-FFK','CMS','CW-DCK','CW3-CK','CW2-FFK','CW3-FFK','CW4-SK','CW4-CLD','CW4-SPL','CW5-HK') and R.dc_price_up_date='$up_date' order by C.pr_cat_name,R.rname,R.branch_id");
	  	return $query->result_array();
	}
	
	public function getDcPriceUpdateRecipeList(){
	    $branch_id = $_SESSION['branch_id'];
	    $up_date = date("Y-m-d");
	    $query = $this->db->query("SELECT DISTINCT(dc_price_up_date) as up_date FROM `recipe` WHERE dc_price_up_date!=''");
	  	return $query->result_array();
	}
	
	public function getSupplierPending(){
	    $branch_name = $_SESSION['branch_name'];
	    $cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_name,bank_branch,bank_acc_no,address,address1,aprv_status,benificiary_name,cr_prd,branchname,branch_id from vendor_creation where vendor_status='Pending' group by vender_ref_no order by vender_id desc");
        }
        else{
            $query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='$cw_ceo' and vendor_status='Pending'");
        }
        /*if($branch_name=='CW4_CEO')
        {
    		$query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='CW4' and vendor_status='Pending'");
        }
        else if($branch_name=='CW5_CEO')
        {
    		$query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='CW5' and vendor_status='Pending'");
        }
        else if($branch_name=='CW6_CEO')
        {
    		$query = $this->db->query("SELECT * FROM `vendor_creation` where branchname='CW6' and vendor_status='Pending'");
        }
        else
	    {
    		$query = $this->db->query("select vender_id,vender_ref_no,company_name,contact_person,contact_no,email_id,ifsc_code,bank_name,bank_branch,bank_acc_no,address,address1,aprv_status,benificiary_name,cr_prd,branchname,branch_id from vendor_creation where vendor_status='Pending' group by vender_ref_no order by vender_id desc");
	    }*/
		return $query->result();
	}
	
	public function approvalSupplier($postdata)
	{
		extract($postdata);
		
			$data = array(
			'company_name' => $sname,
			'contact_person	' => $contact_person,
			'designation' => $designation,
			'department' => $dept,
			'contact_no' => $contact_no,
			'email_id' => $email_id,
			'fax_no' => $fax_no,
			'bank_name' => $bank_name,
			'benificiary_name' => $benificiary_name,
			'ifsc_code' => $ifsc_code,
			'bank_branch' => $branch_addr,
			'bank_acc_no' => $bank_acc_no,
			'pan_no	' => $pan_no,
			'gstin_no' => $gstin_no,
			'mop' => $mop,
			'address' => $address,
			'address1' => $city,
			'postal_code' => $postal_code,
			'state' => $state,
			'country' => $country,
			'area_code_phone' => $area_code_phone,
			'land_phone' => $landline,
			'cr_prd' => $crt_period,
			'rgst_msme' => $rgst_msme,
			'rgst_msme_no' => $msme_rgst_no,
			'vendor_status'=>'Approved'
		);
		$this->db->where('vender_ref_no', $vref);
		$this->db->update('vendor_creation', $data);
	}
	
	public function addBranch($postdata)
	{
		extract($postdata);
		$this->db->select('MAX(b_no) as b_no',false);
		$result = $this->db->get('branch')->row();
		$b_no = $result->b_no;
		if($b_no == 0){
			$b_no = 1;
		}
		else{
			$b_no = $b_no+1;
		}
		$bcode='B-'.$b_no;
		$branch_name='BY2C'.$b_no;
		$d1 = array(
    	'branch_name' => $branch_name,
    	'b_no'=>$b_no,
    	'branch_code'=>$bcode,
    	'user_type'=>$user_type,
    	'user_group'=>$user_grp
    	);
    	$this->db->insert('branch', $d1);
		$branch_id = $this->db->insert_id();
		if($user_type == 'Tax' && $user_grp='customer'){
		    
		    
    		$this->db->select('MAX(out_ref_no) as vno',false);
    		$result = $this->db->get('outlet_customer')->row();
    		$vno = $result->vno;
    		if($vno == 0){
    			$vno = 1;
    		}
    		else{
    			$vno = $vno+1;
    		}
    		$d2 = array(
    		'out_ref_no' => $vno,
    		'company_name' => $sname,
    		'code'=>$bcode,
    		'title'=>$title,
    		'description'=>$description,
    		'contact_no' => $contact_no,
    		'email_id' => $email_id,
    		'gstin_no' => $gstin_no,
    		'address' => $address,
    		'branch_name'=>$branch_name,
    		'branch_id'=>$branch_id
    		);
    	    $this->db->insert('outlet_customer', $d2);
    	    
    	    $user_grp_id='7';
    	    $data = array(
    		'outlet' => $sname,
    		'org_name' => $description,
    		'code'=>$bcode,
    		'title'=>$title,
    		'description'=>$description,
    		'gstin_no' => $gstin_no,
    		'address1' => $address,
    		'phone_no'=>$contact_no,
    		'email_id'=>$email_id,
    		'user_type'=>$user_type,
    		'user_group'=>$user_grp
    		);
		    $this->db->insert('address', $data);
		    $address_id = $this->db->insert_id();
		    
		    $hash_password = password_hash($pwd,PASSWORD_DEFAULT);
			$data2 = array(
				'username' => $username,
				'full_name' => $title,
				'password' => $hash_password,
				'user_group' => $user_grp_id,
				'branch_id' => $branch_id,
				'branch_name' => $branch_name,
				'ph_no' => $contact_no,
				'address_id' => $address_id,
				'br_id' => $pwd,
				'branch_title' => $title,
			);
			$this->db->insert('user', $data2);
		}
		else if($user_type == 'No Tax' && $user_grp='outlet'){
		    $user_grp_id='6';
		    
		    $data = array(
    		'outlet' => $sname,
    		'org_name' => $description,
    		'code'=>$bcode,
    		'title'=>$title,
    		'description'=>$description,
    		'gstin_no' => $gstin_no,
    		'address1' => $address,
    		'phone_no'=>$contact_no,
    		'email_id'=>$email_id,
    		'user_type'=>$user_type,
    		'user_group'=>$user_grp
    		);
		    $this->db->insert('address', $data);
		    $address_id = $this->db->insert_id();
		    
		    $hash_password = password_hash($pwd,PASSWORD_DEFAULT);
			$data2 = array(
				'username' => $username,
				'full_name' => $title,
				'password' => $hash_password,
				'user_group' => $user_grp_id,
				'branch_id' => $branch_id,
				'branch_name' => $branch_name,
				'ph_no' => $contact_no,
				'address_id' => $address_id,
				'br_id' => $pwd,
				'branch_title' => $title,
			);
			$this->db->insert('user', $data2);
		    $user_id = $this->db->insert_id();
		    
		    $q1 = $this->db->query("select * from item_creation where branchname='STR'");
    		foreach($q1->result_array() as $row){
        		$item_name = $row['item_name'];
    		    $hsn_code = $row['hsn_code'];
    		    $uom = $row['uom'];
    		    $item_price =$row['item_price'];
    		    $item_tax=$row['item_tax'];
    		    $cat_id=$row['category_id'];
    		    $itm_id=$row['itm_id'];
    			$q1 = $this->db->query("insert into item_creation(item_name,hsn_code,uom,item_price,item_tax,category_id,itm_id,branch_id,branchname)values('$item_name','$hsn_code','$uom','$item_price','$item_tax','$cat_id','$itm_id','$branch_id','$branch_name')");
    		}
    		
    		$q2 = $this->db->query("select * from recipe where branchname='STR'");
    		foreach($q2->result_array() as $rw){
        		$item_name = $rw['rname'];
    		    $hsn_code = $rw['hsn_code'];
    		    $uom = $rw['uom'];
    		    $price =$rw['price'];
    		    $tax=$rw['tax'];
    		    $cat_id=$rw['pr_cat_id'];
    		    $recipe_no=$rw['recipe_no'];
    		    $production_stock=$rw['production_stock'];
    			
    	        $q8 = $this->db->query("INSERT INTO `recipe`( `recipe_no`, `hsn_code`, `rname`, `production_stock`,  `yeild`, `uom`,  `price`, `tax`,  `pr_cat_id`, `branch_id`, `branchname`) VALUES ('$recipe_no','$hsn_code','$item_name','$production_stock','1','$uom','$price','$tax','$cat_id','$branch_id','$branch_name')");
    		}
		}
		else{
		    $data = array(
    		'outlet' => $sname,
    		'org_name' => $description,
    		'code'=>$bcode,
    		'title'=>$title,
    		'description'=>$description,
    		'gstin_no' => $gstin_no,
    		'address1' => $address,
    		'phone_no'=>$contact_no,
    		'email_id'=>$email_id
    		);
		    $this->db->insert('address', $data);
		    $address_id = $this->db->insert_id();
		    
		    $hash_password = password_hash($pwd,PASSWORD_DEFAULT);
			$data2 = array(
				'username' => $username,
				'full_name' => $title,
				'password' => $hash_password,
				'user_group' => $user_grp,
				'branch_id' => $branch_id,
				'branch_name' => $branch_name,
				'ph_no' => $contact_no,
				'address_id' => $address_id,
				'br_id' => $pwd,
				'branch_title' => $title,
			);
			$this->db->insert('user', $data2);
		    $user_id = $this->db->insert_id();
		    
		    $q1 = $this->db->query("select * from item_creation where branchname='STR'");
    		foreach($q1->result_array() as $row){
    		$item_name = $row['item_name'];
		    $hsn_code = $row['hsn_code'];
		    $uom = $row['uom'];
		    $item_price =$row['item_price'];
		    $item_tax=$row['item_tax'];
		    $cat_id=$row['category_id'];
		    $itm_id=$row['itm_id'];
			$q1 = $this->db->query("insert into item_creation(item_name,hsn_code,uom,item_price,item_tax,category_id,itm_id,branch_id,branchname)values('$item_name','$hsn_code','$uom','$item_price','$item_tax','$cat_id','$itm_id','$branch_id','$branch_name')");
    		}
    		
    		$q2 = $this->db->query("select * from recipe where branchname='STR'");
    		foreach($q2->result_array() as $rw){
        		$item_name = $rw['rname'];
    		    $hsn_code = $rw['hsn_code'];
    		    $uom = $rw['uom'];
    		    $price =$rw['price'];
    		    $tax=$rw['tax'];
    		    $cat_id=$rw['pr_cat_id'];
    		    $recipe_no=$rw['recipe_no'];
    		    $production_stock=$rw['production_stock'];
    			
    	        $q8 = $this->db->query("INSERT INTO `recipe`( `recipe_no`, `hsn_code`, `rname`, `production_stock`,  `yeild`, `uom`,  `price`, `tax`,  `pr_cat_id`, `branch_id`, `branchname`) VALUES ('$recipe_no','$hsn_code','$item_name','$production_stock','1','$uom','$price','$tax','$cat_id','$branch_id','$branch_name')");
    		}
		}
		
	}
	
	
	public function getbranchList(){
	    $query = $this->db->query("select * from outlet_customer");
        return $query->result();
    }
    
    public function getNobranchList(){
	    $query = $this->db->query("select * from address");
        
        return $query->result();
    }
    
    public function addFProduct($postdata)
		{
			extract($postdata);
			$pname1 = preg_replace('/[^a-zA-Z0-9_ \[\]\.\(\)-]/s','',$product_name);
			$pname1 = preg_replace("/'/", "", $pname1);
            $pname = trim($pname1);
            
			$cq = $this->db->query("SELECT rname FROM recipe where rname='$pname'");
			if($cq->num_rows() > 0)
			{
			    return $cq;
			}
			else
			{
    			
    			//product image upload
    			if(!empty($_FILES['pd_upload']['name'])){
    				$config['upload_path'] = 'product_image/';
    				$config['allowed_types'] = 'gif|jpg|png|jpeg';
    				$filename = $_FILES['pd_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('pd_upload')){
    					$uploadData = $this->upload->data();
    					//$pd_upload = $uploadData['file_name'];
    					$pd_upload = 'product_image/'.$uploadData['file_name'];
    					//$file_name = 'upload/'.$_FILES['in_upload']['name'];
    				}else{
    					$pd_upload = '';
    				}
    			}else{
    				$pd_upload = '';
    			}
    			$q1 = $this->db->query("select branch_id,branch_name from branch where branch_name not in ('CEO') and user_type='No Tax'");
        		foreach($q1->result_array() as $row){
        		//$login_id = $row['login_id'];
        		$branch_id = $row['branch_id'];
        		$branch_name = $row['branch_name'];
    			//recipe number
        			$this->db->select('MAX(recipe_no) as recipe_no',false);
        			$result = $this->db->get('recipe')->row();
        			$recipe_no = $result->recipe_no;
        			if($recipe_no == 0){
        				$recipe_no = 1;
        			}
        			else{
        				$recipe_no = $recipe_no+1;
        			}
        			
        			$cur_date = date('Y-m-d');
        	//		$upd_in = implode(',',$updated_in);
        			$data = array(
        				'recipe_no' => $recipe_no,
        				'hsn_code' => $hsn_code,
        				'barcode_no'=>$bar_code,
        				'raised_date' => $cur_date,
        				'rname' => $pname,
        				'yeild' => $yield,
        				'uom' => $uom,
        				'price' => $base_price,
        				'tax' => $gst_rate,
        				'tax_amount' => $gst,
        				'total_price' => $tprice,
        				'product_image' => $pd_upload,
        				'status'=>'Enabled',
        				'aprvl_status' => 'Pending',
        				'pr_cat_id' => $category,
        				'branchname' => $branch_name,
        				'branch_id' => $branch_id
        			);
        			$this->db->insert('recipe', $data);
        			$rc_id = $this->db->insert_id();
        			
        			/*$adata = array(
        				'recipe_id' => $rc_id,
        				'recipe_no' => $recipe_no,
        				'hsn_code' => $hsn_code,
        				'rname' => $pname,
        				'yeild' => $yield,
        				'uom' => $uom,
        				'pr_cat_id' => $category,
        				'branchname' => $branch_name,
        				'branch_id' => $branch_id
        			);
        			$this->db->insert('recipe_avg_cost', $adata);*/
        		}
    			
			}
		}  
		
		
		
		public function updateFProduct($postdata)
		{
			extract($postdata);
			$updated_date = date("Y-m-d");
			$updated_time = date("h:i A");
			$pname1 = preg_replace('/[^a-zA-Z0-9_ \[\]\.\(\)-]/s','',$product_name);
			$pname1 = preg_replace("/'/", "", $pname1);
            $pname = trim($pname1);
            
            //recipe cost
            if($yield == '0' || $yield == '')
            {
                $rc_cost = '0';
            }
            
			
			//product image upload
			if(!empty($_FILES['pd_upload']['name'])){
			    //removing previous file
			    $ImagePathAndName = $pd_upload1;
    			$do = unlink($ImagePathAndName);
              /*  if($do == "1")
                {
                    echo "The file was deleted successfully.";
                }
                else
                {
                    echo "There was an error trying to delete the file.";
                } */
				$config['upload_path'] = 'product_image/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				$filename = $_FILES['pd_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('pd_upload')){
					$uploadData = $this->upload->data();
					$pd_upload = 'product_image/'.$uploadData['file_name'];
					$pdimage = $fpath.$pd_upload;
				}else{
					$pd_upload = $pd_upload1;
					if($pd_upload1 == '')
				    {
				        $pdimage = '';
				    }
				    else
				    {
				        $pdimage = $fpath.$pd_upload1;
				    }
				}
			}else{
				$pd_upload = $pd_upload1;
				if($pd_upload1 == '')
			    {
			        $pdimage = '';
			    }
			    else
			    {
			        $pdimage = $fpath.$pd_upload1;
			    }
			}
									
			$cur_date = date('Y-m-d');
			//$upd_in = implode(',',$updated_in);
			$data = array(
				'hsn_code' => $hsn_code,
				'product_code' => $product_code,
				'rname' => $pname,
				'yeild' => $yield,
				'uom' => $uom,
				'price' => $base_price,
				'tax' => $gst_rate,
				'tax_amount' => $gst,
				'total_price' => $tprice,
				'product_image' => $pd_upload,
				'status' => 'Enabled',
				'aprvl_status' => 'Approved',
				'pr_cat_id' => $category,
				'updated_date' => $updated_date,
                'updated_time' => $updated_time
			);
			//$this->db->where('recipe_id',$recipe_id);
			$this->db->where('recipe_no',$recipe_no);
			$this->db->update('recipe', $data);
			
			/*$q1 = $this->db->query("select * from recipe where branchname='CHF'");
            	foreach($q1->result_array() as $row){
            	//$login_id = $row['login_id'];
            	$pr_cat_id = $row['pr_cat_id'];
            	$recipe_no = $row['recipe_no'];
            	$data = array(
            	'pr_cat_id' => $pr_cat_id,
            	);
            $this->db->where('recipe_no',$recipe_no);
            $this->db->update('recipe', $data);
            	}*/
		
		}
		
		
	public function getCustomerDetail($vid){
		$query = $this->db->query("select * from outlet_customer where out_cust_id ='$vid'");
		return $query->result();
	}
	
	public function getCustomerDetailBranch($vref){
		$query = $this->db->query("select * from outlet_customer where out_ref_no='$vref'");
		return $query->result();
	}
	
	public function getOutletDetail($vid){
		$query = $this->db->query("select * from address where address_id ='$vid'");
		return $query->result();
	}
	
	public function edtOutlet($postdata)
	{
		extract($postdata);
		    $data = array(
    		'outlet' => $sname,
    		'org_name' => $description,
    		'code'=>$bcode,
    		'title'=>$title,
    		'description'=>$description,
    		'gstin_no' => $gstin_no,
    		'address1' => $address1,
    		'phone_no'=>$phone_no,
    		'email_id'=>$email_id
    		);
		$this->db->where('address_id ', $address_id);
		$this->db->update('address', $data);
		
		
		if($user_type == 'No Tax' && $user_group == 'outlet'){
    		$data1 = array(
        		'username' => $username,
        		'branch_title'=>$title
        		);
    		$this->db->where('user_id ', $user_id);
    		$this->db->update('user', $data1);
		}
		else if($user_type == 'Tax' && $user_group == 'outlet'){
    		$data1 = array(
        		'username' => $username,
        		'branch_title'=>$title
        		);
    		$this->db->where('user_id ', $user_id);
    		$this->db->update('user', $data1);
		}  
	}
	
	public function edtCust($postdata)
	{
		extract($postdata);
		    $data = array(
    		'outlet' => $sname,
    		'org_name' => $description,
    		'code'=>$bcode,
    		'title'=>$title,
    		'description'=>$description,
    		'gstin_no' => $gstin_no,
    		'address1' => $address,
    		'phone_no'=>$phone_no,
    		'email_id'=>$email_id
    		);
		$this->db->where('address_id ', $address_id);
		$this->db->update('address', $data);

	}
	
	public function getUserDetail($vid){
		$query = $this->db->query("select * from user where address_id ='$vid'");
		return $query->result();
	}
}