<?php
	class Barcode_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
			//$this->db2 = $this->load->database('otherdb', TRUE);
		}
		
		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 lastScanned($bcode)
		{
		    $branch_id = $_SESSION['branch_id'];
		    $query = $this->db->query("SELECT recipe_id,recipe_no,rname,barcode_no,scan_count,pr_cat_id,price,tax,production_stock FROM recipe where barcode_no='$bcode' and branch_id='$branch_id'");
			$row=$query->row_array();
			
			if($row)
			{
				$qty = $row['scan_count'];
				if($qty > 0)
				{
					$rep_prod = 1;
				}
				else
				{
					$rep_prod = 0;
				}
				$qty1 = $qty + 1;
				$q = $this->db->query("update recipe set scan_count='$qty1' where barcode_no='$bcode'");
				$sq = $this->db->query("SELECT sum(scan_count) as scan_count from recipe");
				$srow = $sq->row_array();
				$scount = $srow['scan_count'];
				$data=array("recipe_id" => $row['recipe_id'], "rname" => $row['rname'], "recipe_no" => $row['recipe_no'], "barcode_no" => $row['barcode_no'], "qty" => $qty1, "scount" => $scount, "rep_prod" => $rep_prod,"category_id"=>$row['pr_cat_id'],"price"=>$row['price'],"tax"=>$row['tax'],"close_stock"=>$row['production_stock']);
			}
			else
			{
				$eq = $this->db->query("SELECT scan_count from recipe where barcode_no='empty'");
				$erow = $eq->row_array();
				$qty = $erow['scan_count'];
				$qty1 = $qty + 1;
				$q = $this->db->query("update recipe set scan_count='$qty1' where barcode_no='empty'");
				$sq = $this->db->query("SELECT sum(scan_count) as scan_count from recipe");
				$srow = $sq->row_array();
				$scount = $srow['scan_count'];
				$data=array("recipe_id" => '0', "rname" => ' ', "recipe_no" => ' ', "barcode_no" => ' ', "qty" => '1', "scount" => $scount, "rep_prod" => '0',"category_id"=>'0',"price"=>'0',"tax"=>'0',"close_stock"=>'0');
			}
			echo json_encode($data);
		}
		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);
			$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','tax_invoice23');
			$result = $this->db->get('product_issues_creation')->row();
			$in_no = $result->in_no;
			if($in_no == 0){
				$in_no = 1;
			}
			else{
				$in_no = $in_no+1;
			}
			
			$number = sprintf('%03d',$in_no);
		    $dc_no = 'By2CNH-202324-'.$number;
			$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',
			'invoice_type'=>'tax_invoice23'
			);
			$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'];
    					$uom[$i]=$row['uom'];
    					$cat_id[$i]=$row['pr_cat_id'];
    					$barcode_no[$i]=$row['barcode_no'];
    					$qty1[$i] = $qty[$i] - $quantity[$i];
    					
    					$q2 = $this->db->query("UPDATE recipe set production_stock='$qty1[$i]',scan_count='0' 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' => $totalprice[$i], //$ttl_price[$i],
    					'tax' => $tax[$i],
    					'tax_amount' => $taxamt[$i], //$tax_amt
    					'total_price' => $totalamount[$i], //$total
    					'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);
    			}
    			$w1 = $this->db->query("select sum(total_price) as total from product_issues_item where issue_id='$isid'");
    				$rw = $w1->row_array();
    					$total_amt = $rw['total'];
    			$this->db->query("UPDATE product_issues_creation set total_amount='$total_amt' WHERE  issue_id='$isid'");		
    			/*$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";
                    $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);
                $output = curl_exec($ch);
            	 curl_close($ch); */
    		}
		
		
			return $isid;
		}
		
		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();	
		}
	}
?>