<?php
	class Issue_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
			//$this->db2 = $this->load->database('otherdb', TRUE);
		}
		var $bill_year=bill_year;
		var $invoice_type = invoice_type;
		var $invoice_no = invoice_no;
		var $dc_invoice_type = dc_invoice_type;
		public function get_vehicle(){
    		$query=$this->db->query("select * from vehicle");
    		return $query->result();
    	}
    	
    	public function get_FPItems($branch_id)
    	{
    	
    		$query = $this->db->query("SELECT rname as item_name,uom,price as item_price from recipe where branch_id='$branch_id' and status='Enabled'");
    		return $query->result();
    	}
    	
    	
    	public function get_outletout(){
    		//$query=$this->db->query("select * from outlet_customer");
    		$query=$this->db->query("select * from address where user_type='Tax'and user_group='customer'");
    		return $query->result();
    	}
    	
    	public function get_outletin(){
    		//$query=$this->db->query("select * from outlet_customer");
    		$query=$this->db->query("select * from address where user_type!='Tax'and user_group!='customer'");
    		return $query->result();
    	}
    	
    	public function get_outletuser(){
    		//$query=$this->db->query("select * from outlet_customer");
    		$query=$this->db->query("select * from address");
    		return $query->result();
    	}
    	
    	public function get_useroutlet(){
    		//$query=$this->db->query("select * from outlet_customer");
    		$query=$this->db->query("select * from user where branch_name not in('CEO','CHF','KTN','')");
    		return $query->result();
    	}
    	
    	public function get_outlets(){
    		//$query=$this->db->query("select * from outlet_customer");
    		$query=$this->db->query("select * from address ");
    		return $query->result();
    	}
    	public function get_PItemDetails($iname,$br_id)
	    {
		$query = $this->db->query("select rname as item_name,uom,price as item_price,tax as item_tax,production_stock as stock_item,recipe_id as item_id,pr_cat_id  as category_id,recipe_no as itm_id from recipe where rname='$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 vehicle_fetch($keyword){
    		$query = $this->db->query("select * from vehicle where vehicle_id='$keyword'");
    		$row=$query->row_array();
    		if($row)
    		{
    			$data=array("vehicle_name" => $row['vehicle_name']);
    			echo json_encode($data);
    		}
    		else
    		{
    			$data = array("vehicle_name" => '0');
    			echo json_encode($data);
    		}
    	}
		public function addIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$trans_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$w1 = $this->db->query("select * from user where branch_id='$branch_id'");
		    $g1 = $w1->row_array();
			$branch_code = $g1['branch_code'];
			
			$w2 = $this->db->query("select * from user where username='$outlet'");
		    $g2 = $w2->row_array();
			$outlet_code = $g2['branch_code'];
			$outlet_branch_id = $g2['branch_id'];
			$outlet_name = $g2['username'];
			
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('issues_order_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "RMISSU"."/".$outlet."/".$is_no;
			
			
			//$issue_no = "IDC-".$branch_code.'-'.$outlet_code.'/'.$is_no;
			
			$this->db->select('MAX(doc_no) as doc_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('issues_order_creation')->row();
			$doc_no = $result->doc_no;
			if($doc_no == 0){
				$doc_no = 1;
			}
			else{
				$doc_no = $doc_no+1;
			}
			//$issue_no = "RMISSU"."/".$branch_name."/".$is_no;
			$dc_no = "IDC-".$branch_code.'-'.$outlet_code.'/'.$doc_no;
			$k=0;
			$gtotal=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
					$totalprice[$j] = round(((float)$price[$j] * (float)$quantity[$j]),2);
    				$taxamt[$j] = round((((float)$totalprice[$j] * (float)$tax[$j])/100),2);
    				$totalamount[$j] = round(((float)$totalprice[$j] + (float)$taxamt[$j]),2);
			        $gtotal=(float)$gtotal+(float)$totalamount[$j];
				}
			}
			if($k>0)
			{
			    $s1 = $this->db->query("select * from vehicle where vehicle_id='$vehicle_id'");
				$r1 = $s1->row_array();
				$v_name=$r1['vehicle_name'];
			$data = array(
			'is_no'=> $is_no,
			'issue_no'=> $issue_no,
			'doc_no'=>$doc_no,
			'dc_no'=>$dc_no,
			'issue_date'=> $issue_date,
			'issue_time'=> $issue_time,
			'outlet_name'=> $outlet_name,
			'vehicle_id' => $vehicle_id,
			'vehicle_name' => $v_name,
			'total'=> $gtotal,
			'branchname'=> $branch_name,
			'request_id'=> '0',
			'branch_id'=> $branch_id
			);
			$this->db->insert('issues_order_creation', $data);
			$isid = $this->db->insert_id();
			}
			
			if($isid)
			{
			$count = count($item_name);
			for($i=0;$i<$count;$i++)
			{
				$q1 = $this->db->query("select * from item_creation where item_id='$item_id[$i]'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$qty[$i] = $row['stock_item'];
					$hsn_code[$i] = $row['hsn_code'];
					$qty1[$i] = $qty[$i] - $quantity[$i];
					
					$q2 = $this->db->query("UPDATE item_creation set stock_item='$qty1[$i]' WHERE  item_id= '$item_id[$i]'");
				}
				$totalprice[$i] = round(((float)$price[$i] * (float)$quantity[$i]),2);
				$taxamt[$i] = round((((float)$totalprice[$i] * (float)$tax[$i])/100),2);
				$totalamount[$i] = round(((float)$totalprice[$i] + (float)$taxamt[$i]),2);
				$data2 = array(
					'indent_item_name' => $item_name[$i],
					'indent_uom' => $uom[$i],
					'hsn_code'=>$hsn_code[$i],
					'stock_transfer' => $qty[$i],
					'indent_qty' => $quantity[$i],
					'issues_price' => $price[$i],
					'basic_price' => $totalprice[$i],
					'tax' => $tax[$i],
					'tax_amount' => $taxamt[$i],
					'total_amount' => $totalamount[$i],
					'indent_remarks' => $remarks[$i],
					'issue_id' => $isid,
					'request_item_id' => '0',
					'item_id' => $item_id[$i],
					'cat_id' => $cat_id[$i],
					'itm_id' => $itm_id[$i]
				);
				$this->db->insert('issues_order_item', $data2);
				
				$data3 = array(
					'item_name' => $item_name[$i],
					'trans_ref_no' => $issue_no,
					'trans_type'=>'Issue',
					'hsn_code'=>$hsn_code[$i],
					'transaction_date' => $trans_date,
					'opening_stock' => $qty[$i],
					'out_qty' => $quantity[$i],
					'closing_stock' => $qty1[$i],
					'last_modified' => $lm,
					'branchname' => $branch_name,
					'item_id' => $item_id[$i],
					'category_id' => $cat_id[$i],
					'itm_id' => $itm_id[$i],
					'branch_id' => $branch_id
				);
				$this->db->insert('stock_detail', $data3);
				
				$q2 = $this->db->query("select * from item_creation where itm_id='$itm_id[$i]' and branch_id='$outlet_branch_id'");
				if($q2->num_rows() > 0)
				{
					$row2 = $q2->row_array();
					$qty2[$i] = $row2['stock_item'];
					$item_id1[$i] = $row2['item_id'];
					$cat_id1[$i] = $row2['category_id'];
					$qty12[$i] = $qty2[$i] - $quantity[$i];
					
					$q2 = $this->db->query("UPDATE item_creation set stock_item='$qty2[$i]' WHERE  itm_id='$itm_id[$i]' and branch_id='$outlet_branch_id'");
				}
				
				$data4 = array(
					'item_name' => $item_name[$i],
					'trans_ref_no' => $issue_no,
					'trans_type'=>'Issue',
					'hsn_code'=>$hsn_code[$i],
					'transaction_date' => $trans_date,
					'opening_stock' => $qty2[$i],
					'out_qty' => $quantity[$i],
					'closing_stock' => $qty12[$i],
					'last_modified' => $lm,
					'branchname' => $outlet,
					'item_id' => $item_id1[$i],
					'category_id' => $cat_id1[$i],
					'itm_id' => $itm_id[$i],
					'branch_id' => $branch_id
				);
				$this->db->insert('stock_detail', $data4);
			}
		}
			return $isid;
		}
		
		public function addTaxIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$trans_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			$w1 = $this->db->query("select * from user where branch_id='$branch_id'");
		    $g1 = $w1->row_array();
			$branch_code = $g1['branch_code'];
			
			$w2 = $this->db->query("select * from address where address_id ='$outlet'");
		    $g2 = $w2->row_array();
			$outlet_code = $g2['code'];
			$outlet_name = $g2['outlet'];
			
			
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('issues_order_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "TISSU"."/".$branch_name."/".$is_no;
			//$issue_no = "IDC-".$branch_code.'-'.$outlet_code.'/'.$is_no;
			
			$this->db->select('MAX(doc_no) as doc_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('issues_order_creation')->row();
			$doc_no = $result->doc_no;
			if($doc_no == 0){
				$doc_no = 1;
			}
			else{
				$doc_no = $doc_no+1;
			}
			//$issue_no = "RMISSU"."/".$branch_name."/".$is_no;
			$dc_no = "TIDC-".$branch_code.'-'.$outlet_code.'/'.$doc_no;
			$k=0;
			$gtotal=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
					$totalprice[$j] = round(((float)$price[$j] * (float)$quantity[$j]),2);
    				$taxamt[$j] = round((((float)$totalprice[$j] * (float)$tax[$j])/100),2);
    				$totalamount[$j] = round(((float)$totalprice[$j] + (float)$taxamt[$j]),2);
			        $gtotal=(float)$gtotal+(float)$totalamount[$j];
				}
			}
			if($k>0)
			{
			    $s1 = $this->db->query("select * from vehicle where vehicle_id='$vehicle_id'");
				$r1 = $s1->row_array();
				$v_name=$r1['vehicle_name'];
			$data = array(
			'is_no'=> $is_no,
			'issue_no'=> $issue_no,
			'doc_no'=>$doc_no,
			'dc_no'=>$dc_no,
			'issue_date'=> $issue_date,
			'issue_time'=> $issue_time,
			'outlet_name'=> $outlet_name,
			'vehicle_id' => $vehicle_id,
			'vehicle_name' => $v_name,
			'total'=> $gtotal,
			'branchname'=> $branch_name,
			'request_id'=> '0',
			'branch_id'=> $branch_id,
			'invoice_type'=>'tax_invoice24'
			);
			$this->db->insert('issues_order_creation', $data);
			$isid = $this->db->insert_id();
			}
			
			if($isid)
			{
			$count = count($item_name);
			for($i=0;$i<$count;$i++)
			{
				$q1 = $this->db->query("select * from item_creation where item_id='$item_id[$i]'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$qty[$i] = $row['stock_item'];
					$hsn_code[$i]=$row['hsn_code'];
					$qty1[$i] = $qty[$i] - $quantity[$i];
					
					$q2 = $this->db->query("UPDATE item_creation set stock_item='$qty1[$i]' WHERE  item_id= '$item_id[$i]'");
				}
				$totalprice[$i] = round(((float)$price[$i] * (float)$quantity[$i]),2);
				$taxamt[$i] = round((((float)$totalprice[$i] * (float)$tax[$i])/100),2);
				$totalamount[$i] = round(((float)$totalprice[$i] + (float)$taxamt[$i]),2);
				$data2 = array(
					'indent_item_name' => $item_name[$i],
					'indent_uom' => $uom[$i],
					'hsn_code'=>$hsn_code[$i],
					'stock_transfer' => $qty[$i],
					'indent_qty' => $quantity[$i],
					'issues_price' => $price[$i],
					'basic_price' => $totalprice[$i],
					'tax' => $tax[$i],
					'tax_amount' => $taxamt[$i],
					'total_amount' => $totalamount[$i],
					'indent_remarks' => $remarks[$i],
					'issue_id' => $isid,
					'request_item_id' => '0',
					'item_id' => $item_id[$i],
					'cat_id' => $cat_id[$i],
					'itm_id' => $itm_id[$i]
				);
				$this->db->insert('issues_order_item', $data2);
				
				$data3 = array(
					'item_name' => $item_name[$i],
					'trans_ref_no' => $issue_no,
					'trans_type'=>'Issue',
					'hsn_code'=>$hsn_code[$i],
					'transaction_date' => $trans_date,
					'opening_stock' => $qty[$i],
					'out_qty' => $quantity[$i],
					'closing_stock' => $qty1[$i],
					'last_modified' => $lm,
					'branchname' => $branch_name,
					'item_id' => $item_id[$i],
					'category_id' => $cat_id[$i],
					'itm_id' => $itm_id[$i],
					'branch_id' => $branch_id
				);
				$this->db->insert('stock_detail', $data3);
			}
		}
			return $isid;
		}
		
		public function addPrIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$entry_date = date("Y-m-d");
			$trans_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			
			$w2 = $this->db->query("select * from address where address_id='$outlet'");
		    $g2 = $w2->row_array();
			
			$outlet_name = $g2['outlet'];
			
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id);
			$this->db->where('invoice_type',$this->invoice_type);
			$this->db->where('bill_year',$this->bill_year);
			$result = $this->db->get('product_issues_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "TISSU"."/".$branch_name."/".$is_no;
			
			
			$this->db->select('MAX(in_no) as in_no',false);
			$this->db->where('branch_id',$branch_id);
			$this->db->where('invoice_type',$this->invoice_type);
			$this->db->where('bill_year',$this->bill_year);
			$result = $this->db->get('product_issues_creation')->row();
			$in_no = $result->in_no;
			if($in_no == 0){
				$in_no = 1;
			}
			else{
				$in_no = $in_no+1;
			}
			
			$number = sprintf('%03d',$in_no);
		    //$dc_no = 'By2CNH-202425-'.$number;
		    $dc_no = $this->invoice_no.$number;
			$k=0;
			$gtotal=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
					$totalprice1[$j] = round(((float)$price[$j] * (float)$quantity[$j]),2);
    				$taxamt1[$j] = round((((float)$totalprice1[$j] * (float)$tax[$j])/100),2);
    				$totalamount1[$j] = round(((float)$totalprice1[$j] + (float)$taxamt1[$j]),2);
			        $gtotal=(float)$gtotal+(float)$totalamount1[$j];
				}
			}
			if($k>0)
			{
			    $s1 = $this->db->query("select * from vehicle where vehicle_id='$vehicle_id'");
				$r1 = $s1->row_array();
				$v_name=$r1['vehicle_name'];
			$data = array(
			'is_no'=> $is_no,
			'issue_no'=> $issue_no,
			'in_no'=>$in_no,
			'invoice_no'=>$dc_no,
			'issue_date'=> $issue_date,
			'issue_time'=> $issue_time,
			'vehicle_id'=>$vehicle_id,
			'vehicle_name'=>$v_name,
			'outlet'=> $outlet_name,
			'total_amount'=> $total_amt,
			'branchname'=> $branch_name,
			'request_id'=> '0',
			'branch_id'=> $branch_id,
			'entry_date'=>$entry_date,
			'issue_status'=>'Issued',
			'bill_year'=>$this->bill_year,
			'invoice_type'=>$this->invoice_type
			);
			$this->db->insert('product_issues_creation', $data);
			$isid = $this->db->insert_id();
			}
			
			if($isid)
			{
			$count = count($item_name);
			for($i=0;$i<$count;$i++)
			{
				$q1 = $this->db->query("select * from recipe where recipe_id='$item_id[$i]'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$qty[$i] = $row['production_stock'];
					$hsn_code[$i]=$row['hsn_code'];
					$qty1[$i] = $qty[$i] - $quantity[$i];
					
					$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$i]' WHERE  recipe_id= '$item_id[$i]'");
				}
				//$totalprice[$i] = round(($price[$i] * $quantity[$i]),2);
				//$taxamt[$i] = round((($totalprice[$i] * $tax[$i])/100),2);
				//$totalamount[$i] = round(($totalprice[$i] + $taxamt[$i]),2);
				$data2 = array(
					'issue_item_name' => $item_name[$i],
					'issue_uom' => $uom[$i],
					'hsn_code'=>$hsn_code[$i],
					'issued_qty' => $quantity[$i],
					'price' => $price[$i],
					'basic_price' => $ttl_price[$i],
					'tax' => $tax[$i],
					'tax_amount' => $tax_amt[$i],
					'total_price' => $total[$i],
					'issue_id' => $isid,
					'request_item_id' => '0',
					'recipe_id' => $item_id[$i],
					'pr_cat_id' => $cat_id[$i],
					'recipe_no' => $itm_id[$i]
				);
				$this->db->insert('product_issues_item', $data2);
				
				$data3 = array(
					'item_name' => $item_name[$i],
					'trans_ref_no' => $issue_no,
					'trans_type'=>'Issue',
					'hsn_code'=>$hsn_code[$i],
					'transaction_date' => $trans_date,
					'opening_stock' => $qty[$i],
					'out_qty' => $quantity[$i],
					'closing_stock' => $qty1[$i],
					'last_modified' => $lm,
					'branchname' => $branch_name,
					'item_id' => $item_id[$i],
					'category_id' => $cat_id[$i],
					'recipe_no' => $itm_id[$i],
					'branch_id' => $branch_id
				);
				$this->db->insert('stock_detail', $data3);
			}
			
			$message = "Invoice $dc_no raised for $outlet_name on $entry_date for amount $total_amt - By2Coffee Kitchen";
			
		$message1 = urlencode($message);
		//9945414912
        //$url="https://portal.mobtexting.com/api/v2/sms/send?access_token=e1e4d07a26e56cb9d0b78066ba5cd3a2&to=9945414912&sender=ByToCf&service=T&template_id=1207164094938547316&message=$message1";
        $url="http://redirect.ds3.in/submitsms.jsp?user=by2coffee&key=5385593627XX&senderid=ByToCf&accusage=1&entityid=1201159092155152569&template_id=1207164094938547316&mobile=9945414912&message=$message1";
            $ch = curl_init();
            curl_setopt_array($ch, array(CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
           ));
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
         
            /*get response*/
         $output = curl_exec($ch);
    	 curl_close($ch); 
		}
		
		
			return $isid;
		}
		
		public function getIssue($isid)
		{
			$query = $this->db->query("SELECT *,DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation where issue_id='$isid'");
			return $query->result();	
		}
		
		public function getIssueItem($isid)
		{
			$query = $this->db->query("SELECT * FROM  issues_order_creation U inner join issues_order_item E on U.issue_id=E.issue_id where U.issue_id='$isid'");
			return $query->result();	
		}
		
		public function getPrIssue($isid)
		{
			$query = $this->db->query("SELECT *,DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation where issue_id='$isid'");
			return $query->result();	
		}
		
		public function getPrIssueItem($isid)
		{
			$query = $this->db->query("SELECT * FROM  product_issues_creation U inner join product_issues_item E on U.issue_id=E.issue_id where U.issue_id='$isid'");
			return $query->result();	
		}
		
		public function getPrIssueTotalItem($isid)
		{
			$query = $this->db->query("SELECT sum(total_price) as total FROM  product_issues_creation U inner join product_issues_item E on U.issue_id=E.issue_id where U.issue_id='$isid'");
			return $query->result();	
		}
		public function getIssueList($branch_id)
		{
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation where branch_id='$branch_id' and invoice_type='$this->dc_invoice_type' ORDER BY issue_id desc");
			$cur_date=date('Y-m-d');
			//$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation where branch_id='$branch_id' and issue_date='$cur_date' ORDER BY issue_id desc");
			
			return $query->result();	
		}
		
		public function getRMIssueList($branch_id)
		{
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation where branch_id='$branch_id' ORDER BY issue_id desc");
			return $query->result();	
		}
		
		public function get_outlet_by_branch($branch,$sub_mod_id) {
			
			$query = $this->db->query("SELECT * FROM user where user_group='6'");
		    return $query->result();
		}
		
		public function getCIssueList($row,$rowperpage){
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation ORDER BY issue_id desc limit $row,$rowperpage");
			return $query->result();
		}
           
        public function get_PRCIssueList(){
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation ORDER BY issue_id desc");
			return $query->result();
		}
		
        public function get_CIssueList(){
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation ORDER BY issue_id desc");
			return $query->result();
		}
		
		public function getIssueListReport($data)
		{
			$f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$branch = $data['branch'];
			//$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation where (issue_date between '$f_date' and '$t_date') and outlet_name='$branch' ORDER BY issue_id desc");
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation where (issue_date between '$f_date' and '$t_date') and outlet='$branch' ORDER BY issue_id desc");
			return $query->result();
		}
		
		public function getIssueTaxList($branch_id)
		{
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation where branch_id='$branch_id' and invoice_type='$this->invoice_type' ORDER BY issue_id desc");
			$cur_date=date('Y-m-d');
			return $query->result();	
		}
		
		public function getIssueFPTaxList($branch_id)
		{
			
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation where branch_id='$branch_id' and  invoice_type='$this->invoice_type' ORDER BY issue_id desc");
			
			return $query->result();	
		}
		
		public function get_CPIssueList(){
			$query = $this->db->query("SELECT i.invoice_type,i.issue_no,i.`invoice_no`,i.issue_date,i.`outlet`,sum(it.`basic_price`) as `basic`,sum(it.tax_amount) as `tax_amt`,sum(it.`total_price`) as tot_amt,i.issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation i inner join product_issues_item it on i.issue_id=it.issue_id where i.invoice_type='$this->invoice_type' group by i.issue_id ORDER BY i.issue_id desc");
			return $query->result();
		}
		
		public function getPIssueListReport($data)
		{
			$f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$branch = $data['branch'];
			if($f_date!='' && $t_date!='' && $branch=='All'){
			    $query = $this->db->query("SELECT i.invoice_type,i.issue_no,i.`invoice_no`,i.issue_date,i.`outlet`,sum(it.`basic_price`) as `basic`,sum(it.tax_amount) as `tax_amt`,sum(it.`total_price`) as tot_amt,i.issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation i inner join product_issues_item it on i.issue_id=it.issue_id where i.invoice_type='$this->invoice_type' and (i.issue_date between '$f_date' and '$t_date')  group by i.issue_id ORDER BY i.issue_id desc");
			}
			else{
			    $query = $this->db->query("SELECT i.invoice_type,i.issue_no,i.`invoice_no`,i.issue_date,i.`outlet`,sum(it.`basic_price`) as `basic`,sum(it.tax_amount) as `tax_amt`,sum(it.`total_price`) as tot_amt,i.issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation i inner join product_issues_item it on i.issue_id=it.issue_id where i.invoice_type='$this->invoice_type' and (i.issue_date between '$f_date' and '$t_date') and i.outlet='$branch' group by i.issue_id ORDER BY i.issue_id desc");
			}
			 
			return $query->result();
		}
		
		public function get_CTIssueList(){
			$query = $this->db->query("SELECT i.invoice_type,i.issue_no,i.dc_no,i.issue_date,i.outlet_name,sum(it.`basic_price`) as `basic`,sum(it.tax_amount) as `tax_amt`,sum(it.total_amount) as tot_amt,i.issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation i inner join issues_order_item it on i.issue_id=it.issue_id where i.invoice_type='$this->invoice_type' group by i.issue_id ORDER BY i.issue_id desc");
			return $query->result();
		}
		
		public function getTIssueListReport($data)
		{
			$f_date =date('Y-m-d', strtotime($data['from_date']));
			$t_date =date('Y-m-d', strtotime($data['to_date']));
			$branch = $data['branch'];
			$query = $this->db->query("SELECT i.invoice_type,i.issue_no,i.dc_no,i.issue_date,i.outlet_name,sum(it.`basic_price`) as `basic`,sum(it.tax_amount) as `tax_amt`,sum(it.total_amount) as tot_amt,i.issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM issues_order_creation i inner join issues_order_item it on i.issue_id=it.issue_id where i.invoice_type='$this->invoice_type' and (i.issue_date between '$f_date' and '$t_date') and i.outlet_name='$branch' group by i.issue_id ORDER BY i.issue_id desc");
		
			return $query->result();
		}
		
		public function addPrDCIssue($postdata)
		{
			extract($postdata);
			$issue_date = date("Y-m-d", strtotime($issue_date));
			$entry_date = date("Y-m-d");
			$trans_date = date("Y-m-d");
			$issue_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			
			$w2 = $this->db->query("select * from address where outlet='$outlet'");
		    $g2 = $w2->row_array();
			
			$outlet_name = $g2['outlet'];
			$code=$g2['code'];
			
			
			$this->db->select('MAX(is_no) as is_no',false);
			$this->db->where('branch_id',$branch_id);
			$this->db->where('invoice_type',$this->dc_invoice_type);
			$this->db->where('bill_year',$this->bill_year);
			//$this->db->where('invoice_type','DC_invoice24');
			$result = $this->db->get('product_issues_creation')->row();
			$is_no = $result->is_no;
			if($is_no == 0){
				$is_no = 1;
			}
			else{
				$is_no = $is_no+1;
			}
			$issue_no = "ISSU"."/".$branch_name."/".$is_no;
			//$issue_no = "ISSU"."/".$branch_name."/".$outlet_name."/".$is_no;
			//$outlet_branch
			
			$this->db->select('MAX(in_no) as in_no',false);
			$this->db->where('branch_id',$branch_id);
			$this->db->where('invoice_type',$this->dc_invoice_type);
			$this->db->where('bill_year',$this->bill_year);
			//$this->db->where('invoice_type','DC_invoice24');
			$result = $this->db->get('product_issues_creation')->row();
			$in_no = $result->in_no;
			if($in_no == 0){
				$in_no = 1;
			}
			else{
				$in_no = $in_no+1;
			}
			
			$number = sprintf('%03d',$in_no);
		    $dc_no = 'DC-STR-'.$code.'-'.$number;
		    //'DC-202324-'.$number;
			$k=0;
			$gtotal=0;
			/*for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
					$totalprice1[$j] = round(((float)$price[$j] * (float)$quantity[$j]),2);
    				$taxamt1[$j] = round((((float)$totalprice1[$j] * (float)$tax[$j])/100),2);
    				$totalamount1[$j] = round(((float)$totalprice1[$j] + (float)$taxamt1[$j]),2);
			        $gtotal=(float)$gtotal+(float)$totalamount1[$j];
				}
			}*
			if($k>0)
			{*/
			    $s1 = $this->db->query("select * from vehicle where vehicle_id='$vehicle_id'");
				$r1 = $s1->row_array();
				$v_name=$r1['vehicle_name'];
			$data = array(
			'is_no'=> $is_no,
			'issue_no'=> $issue_no,
			'in_no'=>$in_no,
			'invoice_no'=>$dc_no,
			'issue_date'=> $issue_date,
			'issue_time'=> $issue_time,
			'vehicle_id'=>$vehicle_id,
			'vehicle_name'=>$v_name,
			'outlet'=> $outlet_name,
			'total_amount'=> $total_amt,
			'branchname'=> $branch_name,
			'request_id'=> '0',
			'branch_id'=> $branch_id,
			'entry_date'=>$entry_date,
			'issue_status'=>'Issued',
			'bill_year'=>$this->bill_year,
			'invoice_type'=>$this->dc_invoice_type
			);
			$this->db->insert('product_issues_creation', $data);
			$isid = $this->db->insert_id();
			//}
			
			if($isid)
			{
			$count = count($item_name);
			for($i=0;$i<$count;$i++)
			{
				$q1 = $this->db->query("select * from recipe where recipe_id='$item_id[$i]'");
				if($q1->num_rows() > 0)
				{
					$row = $q1->row_array();
					$qty[$i] = $row['production_stock'];
					$hsn_code[$i]=$row['hsn_code'];
					$qty1[$i] = $qty[$i] - $quantity[$i];
					
					$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$i]' WHERE  recipe_id= '$item_id[$i]'");
				}
				//$totalprice[$i] = round(($price[$i] * $quantity[$i]),2);
				//$taxamt[$i] = round((($totalprice[$i] * $tax[$i])/100),2);
				//$totalamount[$i] = round(($totalprice[$i] + $taxamt[$i]),2);
				$data2 = array(
					'issue_item_name' => $item_name[$i],
					'issue_uom' => $uom[$i],
					'hsn_code'=>$hsn_code[$i],
					'issued_qty' => $quantity[$i],
					'price' => $price[$i],
					'basic_price' => $ttl_price[$i],
					//'tax' => $tax[$i],
					//'tax_amount' => $tax_amt[$i],
					'total_price' => $total[$i],
					'issue_id' => $isid,
					'request_item_id' => '0',
					'recipe_id' => $item_id[$i],
					'pr_cat_id' => $cat_id[$i],
					'recipe_no' => $itm_id[$i]
				);
				$this->db->insert('product_issues_item', $data2);
				
				$data3 = array(
					'item_name' => $item_name[$i],
					'trans_ref_no' => $issue_no,
					'trans_type'=>'Issue',
					'hsn_code'=>$hsn_code[$i],
					'transaction_date' => $trans_date,
					'opening_stock' => $qty[$i],
					'out_qty' => $quantity[$i],
					'closing_stock' => $qty1[$i],
					'last_modified' => $lm,
					'branchname' => $branch_name,
					'item_id' => $item_id[$i],
					'category_id' => $cat_id[$i],
					'recipe_no' => $itm_id[$i],
					'branch_id' => $branch_id
				);
				$this->db->insert('stock_detail', $data3);
			}
			$message = "Invoice $dc_no raised for $outlet_name on $entry_date for amount $total_amt - By2Coffee Kitchen";
			
			$message1 = urlencode($message);
			
            //$url="https://portal.mobtexting.com/api/v2/sms/send?access_token=e1e4d07a26e56cb9d0b78066ba5cd3a2&to=9945414912&sender=ByToCf&service=T&template_id=1207164094938547316&message=$message1";
            $url="http://redirect.ds3.in/submitsms.jsp?user=by2coffee&key=5385593627XX&senderid=ByToCf&accusage=1&entityid=1201159092155152569&template_id=1207164094938547316&mobile=9945414912&message=$message1";
                $ch = curl_init();
                curl_setopt_array($ch, array(CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true
               ));
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
             
                /*get response*/
             $output = curl_exec($ch);
        	 curl_close($ch);
		}
			return $isid;
		}
		
		public function getIssueFPDCList($branch_id)
		{
			
			$query = $this->db->query("SELECT *,issue_id as mid, DATE_FORMAT(issue_date, '%d-%m-%Y') as issue_date FROM product_issues_creation where branch_id='$branch_id' and  invoice_type='$this->dc_invoice_type' ORDER BY issue_id desc");
			
			return $query->result();	
		}
	}
?>