<?php
	class Issue_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
			$this->db2 = $this->load->database('otherdb', TRUE);
	
		}
		
		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());
			$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 = "ISSU"."/".$branch_name."/".$is_no;
			
			$k=0;
			$gtotal=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
					$totalprice[$j] = round(($price[$j] * $quantity[$j]),2);
    				$taxamt[$j] = round((($totalprice[$j] * $tax[$j])/100),2);
    				$totalamount[$j] = round(($totalprice[$j] + $taxamt[$j]),2);
			        $gtotal=$gtotal+$totalamount[$j];
				}
			}
			if($k>0)
			{
			$data = array(
			'is_no'=> $is_no,
			'issue_no'=> $issue_no,
			'issue_date'=> $issue_date,
			'issue_time'=> $issue_time,
			'outlet_name'=> $outlet,
			'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'];
					$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(($price[$i] * $quantity[$i]),2);
				$taxamt[$i] = round((($totalprice[$i] * $tax[$i])/100),2);
				$totalamount[$i] = round(($totalprice[$i] + $taxamt[$i]),2);
				$data2 = array(
					'indent_item_name' => $item_name[$i],
					'indent_uom' => $uom[$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',
					'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 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 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' ORDER BY issue_id desc");
			return $query->result();	
		}
		
		public function get_outlet_by_branch($branch,$sub_mod_id) {
			if($branch == 'CW2')
			{
				$query = $this->db->query("SELECT * FROM user WHERE assignedto='cakewala2'");
			}
			else if($branch == 'CW3')
			{
				$query = $this->db->query("SELECT * FROM user WHERE assignedto='cakewala3'");
			}
			else if($branch == 'CW4')
			{
				$query = $this->db->query("SELECT * FROM user WHERE assignedto='cakewala4'");
			}
			else if($branch == 'CW6')
			{
				$query = $this->db->query("SELECT * FROM user WHERE assignedto='cakewala6'");
			}
			else if($branch == 'CMS')
			{
				$query = $this->db->query("SELECT * FROM user WHERE assignedto='commissary'");
			}
			else if($branch == 'CW5')
			{
				$query = $this->db->query("SELECT * FROM user WHERE assignedto='cakewala5'");
			}
			else if($branch == 'CMS-STR'){
			    $query = $this->db->query("SELECT * FROM user WHERE branch_name='CMS'");
			}
			else if($branch == 'ESH-STR2') {
			    $query = $this->db->query("SELECT access_username as username FROM user_access WHERE branch_name='$branch' and module_id='$sub_mod_id'");
			}
			else if($branch == 'ESH'){
			    $query = $this->db->query("SELECT * FROM user WHERE assignedto='eshanya'");
			}
			else{
			    $query = $this->db->query("SELECT * FROM user where branch_name='KTN'");
			}
			return $query->result();
		}
	}
?>