<?php
	class Maintain_model extends CI_model
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
			
		}
		
		
	public function addMaintainance($postdata){
		extract($postdata);	
		$branch_id = $_SESSION['branch_id'];
		$grn_date = date("Y-m-d", strtotime($grn_date));
		$order_time = date("h:i A");
		$lm=date("Y-m-dH:i:s",time());
		$this->db->select('MAX(m_gn_no) as m_gn_no',false);
		$this->db->where('branch_id',$branch_id);
		$result = $this->db->get('maintainance_grn')->row();
		$m_gn_no = $result->m_gn_no;
		if($m_gn_no == 0){
			$gn_no = 1;
		}
		else{
			$gn_no = $m_gn_no+1;
		}
		$grn_no = "MNT/GRN"."/".$branch_name."/".$gn_no;
		
		$k=0;
		for($j=0;$j<count($quantity);$j++)
		{
			if($quantity[$j]!='0' && $quantity[$j]!='')
			{
				$k=$k+1;
			}
		}
		if($k>0)
		{			
		    $this->db->query("insert into maintainance_grn (m_gn_no,m_grn_no,invoice_no,vendor_name,grn_date,grn_status,branchname,rjn_status,user_name,grn_time,branch_id) values ('$gn_no','$grn_no','$grn_invoice','$vender_name','$grn_date','Pending','$branch_name','Pending','$uname','$order_time','$branch_id')");
		    $grn_id = $this->db->insert_id();
		}
		if($grn_id)
		{
		for($i=0;$i<count($item_name),$i<count($quantity),$i<count($price),$i<count($basic_amount),$i<count($tax),$i<count($tax_amount),$i<count($total_amount),$i<count($remarks);$i++) {
			
			
			$this->db->query("insert into maintainance_grn_item (m_item_name,m_item_qty,m_item_price,m_total_price,m_tax,m_tax_amount,m_total_amount,m_remarks,m_rejected_qty,m_grn_id,rjn_status) values ('$item_name[$i]','$quantity[$i]','$price[$i]','$basic_amount[$i]','$tax[$i]','$tax_amount[$i]','$total_amount[$i]','$remarks[$i]','0','$grn_id','Pending')");
			
			$last_id = $this->db->insert_id();
			
			if (empty($quantity[$i])) {
				$this->db->query("DELETE FROM maintainance_grn_item WHERE m_grn_item_id='$last_id'");	
			}
							
			$sql4=$this->db->query("select stock from maintainance_product where item_name='$item_name[$i]' and branch_id='$branch_id'");
			
			if($sql4->num_rows()>0)
			{
				//$res = mysqli_fetch_assoc($sql4);
				foreach($sql4->result_array() as $row4)
				$stock[$i] = $row4['stock'];
				$stk[$i] = $stock[$i] + $quantity[$i];
				
				$sql5 =  $this->db->query("UPDATE maintainance_product set price='$price[$i]',tax='$tax[$i]',stock='$stk[$i]' WHERE  item_name='$item_name[$i]' and branchname='$branch_name'");
				$sql6 = $this->db->query("insert into stock_detail(item_name,trans_type,trans_ref_no,transaction_date,opening_stock,in_qty,closing_stock,last_modified,branchname,branch_id)values('$item_name[$i]','MGRN','$grn_no','$grn_date','$stock[$i]','$quantity[$i]','$stk[$i]','$lm','$branch_name','$branch_id')");
			}
			else{
				$sql5 =  $this->db->query("insert into maintainance_product(item_name,price,tax,stock,branchname,login_id)values('$item_name[$i]','$price[$i]','$tax[$i]','$quantity[$i]','$branch_name','$login_id')");
				$sql6 = $this->db->query("insert into stock_detail(item_name,trans_type,trans_ref_no,transaction_date,opening_stock,in_qty,closing_stock,last_modified,branchname,branch_id)values('$item_name[$i]','MGRN','$grn_no','$grn_date','0','$quantity[$i]','$quantity[$i]','$lm','$branch_name','$branch_id')");
			
			}									
		}
	}
		//$grn_id	
		//echo $grn_id;
		return $grn_id;			
	}
		
	public function get_mnt_grn($gid) {
		$query = $this->db->query("SELECT *,DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where m_grn_id='$gid'");
		return $query->result();
	}
	
	public function get_mnt_grn_item($gid) {
		$query = $this->db->query("SELECT *,ROUND(m_tax_amount,2) as m_tax_amount,ROUND(m_total_amount,2) as m_total_amount FROM maintainance_grn_item where m_grn_id='$gid' and m_item_qty !=''");
		return $query->result();
	}
	public function mnt_grn_total_sum($gid) {
		$query = $this->db->query("SELECT ROUND(SUM(m_total_amount),2) as m_total_amount,ROUND(SUM(m_tax_amount),2) as m_tax_amount FROM  maintainance_grn_item where m_grn_id='$gid'");
		return $query->result();
	}	
	public function mnt_grn_list($branch_id) {
		$query = $this->db->query("SELECT *, DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where branch_id='$branch_id' ORDER BY m_grn_id desc");
		return $query->result();
	}
	
	public function get_vendor() {
		$query = $this->db->query("SELECT DISTINCT(vendor_name) as vendor_name from maintainance_grn");
		return $query->result();
	}
	
	public function addDGrn($postdata){
			extract($postdata);
			$k=0;
			for($j=0;$j<count($quantity);$j++)
			{
				if($quantity[$j]!='0' && $quantity[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
				
				$branch_id = $_SESSION['branch_id'];							 
				$grn_date = date("Y-m-d", strtotime($grn_date));
				$invoice_date = date("Y-m-d", strtotime($grn_inv_date));
				$order_time = date("h:i A");
				$lm=date("Y-m-dH:i:s",time());
				$this->db->select('MAX(gn_no) as gn_no',false);
				$this->db->where('branch_id',$branch_id);
				$result = $this->db->get('gnr_creation')->row();
				$gn_no = $result->gn_no;
				if($gn_no == 0){
					$grnno = 1;
				}
				else{
					$grnno = $gn_no+1;
				}
				$grn_no = "GRN"."/".$branchname."/".$grnno;
				$randno=rand(10000, 990000);
				if(!empty($_FILES['in_upload']['name'])){
					$config['upload_path'] = 'upload/';
					$config['allowed_types'] = 'jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF';
					$extension = pathinfo($_FILES['in_upload']['name'], PATHINFO_EXTENSION);
					$filename = 'upload/'.$randno. "_". time().".".$extension;
					//$filename = 'upload/'.$_FILES['in_upload']['name'];
				}	
				if(!empty($_FILES['in_upload']['name'])){
					$data =array(
					'gn_no'=>$grnno,	
					'grn_no'=>$grn_no,
					'invoice_no'=>$grn_invoice,
					'grn_date'=>$grn_date,
					'invoice_date'=>$invoice_date,
					'gnr_status'=>'Pending',
					'branchname'=>$branchname,
					'vendor_name'=>$vender_name,
					'rjn_status'=>'Pending',
					'invoice_upload'=>$filename,
					'courier'=>$courier,
					'transport'=>$transport,
					'insurance'=>$insurance,
					'total_amount'=>$total_amt,
					'reject_amt'=>$total_amt,
					'user_name'=>$user_name,
					'tot_amt'=>$tamt,
					'round_off'=>$roundoff,
					'grn_time'=>$order_time,
					'branch_id'=>$branch_id);
					$this->db->insert('gnr_creation', $data);
					}
					else
					{
						$data1 =array('gn_no'=>$grnno,
						'grn_no'=>$grn_no,
						'invoice_no'=>$grn_invoice,
						'grn_date'=>$grn_date,
						'invoice_date'=>$invoice_date,
						'gnr_status'=>'Pending',
						'branchname'=>$branchname,
						'vendor_name'=>$vender_name,
						'rjn_status'=>'Pending',
						'courier'=>$courier,
						'transport'=>$transport,
						'insurance'=>$insurance,
						'total_amount'=>$total_amt,
						'reject_amt'=>$total_amt,
						'user_name'=>$user_name,
						'tot_amt'=>$tamt,
						'round_off'=>$roundoff,
						'grn_time'=>$order_time,
						'branch_id'=>$branch_id);
						$this->db->insert('gnr_creation', $data1);
					}
					$gid = $this->db->insert_id();
					if(!empty($_FILES['in_upload']['name'])){
						$config['upload_path'] = 'upload/';
						$config['allowed_types'] = 'jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF';
						$filename = $_FILES['in_upload']['name'];
						
						$extension = pathinfo($_FILES['in_upload']['name'], PATHINFO_EXTENSION);
					    $file_name = 'upload/'.$randno. "_". time().".".$extension;
					    $file_name1 = $randno. "_". time().".".$extension;
						//$file_name = 'upload/'.$_FILES['in_upload']['name'];
						$config['file_name'] = $file_name1;
						//Load upload library and initialize configuration
						$this->load->library('upload',$config);
						$this->upload->initialize($config);
						
						if($this->upload->do_upload('in_upload')){
							
							$data = $this->upload->data();  
							 $config['image_library'] = 'gd2';  
							 $config['source_image'] = 'upload/'.$data["file_name"];  
							 $config['create_thumb'] = FALSE;  
							 $config['maintain_ratio'] = FALSE;  
							 $config['quality'] = '80%';  
							 $config['width'] = 750;  
							 $config['height'] = 750;  
							 $config['new_image'] = 'upload/'.$data["file_name"];  
							 $this->load->library('image_lib', $config);  
							 $this->image_lib->resize();  
							 $image_data = array(  
								  'file_name' => $file_name,
								  'uploaded_on'=>'NOW()',
								   'invoice_ref_id'=>$gid,
								   'gstatus'=>'D_Grn'
								  );  
							  $this->db->insert("uploaded_invoice", $image_data); 
							}else{
							$in_upload = '';
						}
					}else{
						$in_upload = '';
					}
					
			}
			if($gid)
			{
					$gtotal=0;	
					//echo count($quantity);
					for($i=0;$i<count($quantity);$i++) {
						
						$q1 = $this->db->query("select * from item_creation where item_id='$item_sid[$i]'");
						
						if($q1->num_rows() > 0){
						{
							foreach($q1->result_array() as $row)
							$stock_item[$i] = $row['stock_item'];
							$itm_id[$i] = $row['itm_id'];
							$cat_id[$i] = $row['category_id'];
							
							$opeing_stock[$i] = $quantity[$i] + $stock_item[$i];
        		            $closing_stock[$i] = $quantity[$i] + $stock_item[$i];
						
							
							$uname = $_SESSION['username'];
							$login_id = $_SESSION['user_id'];
						
						$data2 =  array('grn_item' => $item_name[$i],
							'grn_item_qty' =>$quantity[$i],
							'grn_item_price' =>$price[$i],
							'uom'=>$unit_measurements[$i],
							'grn_tax' =>$tax[$i],
							'grn_tax_amount' =>$tax_amount[$i],
							'grn_total_amount' =>$basic_amount[$i],
							'grn_grand' =>$total_amount[$i],
							'grn_remarks' =>$remarks[$i],
							'gnr_id' =>$gid,
							'purchase_order_id' =>0,
							'rjn_status' =>'Pending',
							'item_id' =>$item_sid[$i],
							'itm_id' =>$itm_id[$i],
							'category_id' =>$cat_id[$i]);
						$this->db->insert('purchase_order_grn', $data2);
						$last_id = $this->db->insert_id();
						
						if($remarks[$i] != '')
						{
							$data3 =  array('last_remark'=>$lm,
								'frm_user'=>$uname,
								'remark'=>$remarks[$i],
								'item_id'=>$item_sid[$i],
								'gnr_id'=>$gid,
								'purchase_grn_id'=>$last_id,
								'login_id'=>$login_id,
								'status'=>'UnSeen');
							$this->db->insert('remarks', $data3);
						}
						$grn_month = date('Y-m',strtotime($grn_date));	
						$data4 =  array('item_name'=>$item_name[$i],
							'uom'=>$unit_measurements[$i],
							'grn_price'=>$price[$i],
							'grn_tax'=>$tax[$i],
							'grn_date'=>$grn_date,
							'grn_month'=>$grn_month,
							'purchase_grn_id'=>$last_id,
							'branchname'=>$branchname,
							'item_id'=>$item_sid[$i],
							'itm_id'=>$itm_id[$i],
							'category_id'=>$cat_id[$i],
							'branch_id'=>$branch_id);
						$this->db->insert('grn_price', $data4);	
                        
                        //$this->db->query("update item_creation set item_price='$price[$i]',item_tax='$tax[$i]',stock_item='55',vendor_name='$vender_name' WHERE item_id='$item_sid[$i]'");
					/*	$data5 =  array('grn_balance_qty'=>$qty[$i],
							'grn_last_price'=>$inward_price[$i],
							'grn_last_tax'=>$grn_tax[$i],
							'grn_last_amount'=>$inward_total_amount[$i],
							'grn_last_remarks'=>$remarks[$i]);
							$this->db->where('purchase_order_id', $item_id[$i]);
							$this->db->update('purchase_order_item', $data5);*/	

						if($quantity[$i]!='')
						{
							$data6 = array('item_price'=>$price[$i],
								'item_tax'=>$tax[$i],
								'stock_item'=>$opeing_stock[$i],
								'vendor_name'=>$vender_name); 
							$this->db->where('item_id', $item_sid[$i]);
							$this->db->update('item_creation', $data6);
						}
						
						
						$q3 = $this->db->query("select * from purchase_order_grn where grn_item_qty='0' and gnr_id='$gid'");
						if($q3->num_rows() > 0)
						{
							//if (empty($inward_quantity[$i])) {
							$this->db->query("DELETE FROM grn_price WHERE purchase_grn_id='$last_id'");
							$this->db->query("DELETE FROM purchase_order_grn WHERE purchase_grn_id='$last_id' and grn_item_qty='0' and gnr_id='$gid'");
							
						}
						if($quantity[$i]!='0' && $quantity[$i]!='')
						{
							 $data7 = array('item_name'=>$item_name[$i],
								'trans_ref_no'=>$grn_no,
								'trans_type'=>'GRN',
								'transaction_date'=>$grn_date,
								'opening_stock'=>$stock_item[$i],
								'in_qty'=>$quantity[$i],
								'closing_stock'=>$closing_stock[$i],
								'last_modified'=>$lm,
								'branchname'=>$branchname,
								'item_id'=>$item_sid[$i],
								'itm_id'=>$itm_id[$i],
								'category_id'=>$cat_id[$i],
								'branch_id'=>$branch_id);
							 $this->db->insert('stock_detail', $data7);	

						}
					}
				}
			}
	}
		//echo $gid;
		return $gid;
	}
	
	public function get_dgrn($mid) {
		$query = $this->db->query("SELECT *,ROUND(total_amount,2) as total_amount,DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM gnr_creation where gnr_id='$mid'");
		return $query->result();
	}
	
	public function get_dgrn_details($mid) {
		$query = $this->db->query("SELECT *,ROUND(grn_tax_amount,2) as grn_tax_amount,ROUND(grn_grand,2) as grn_grand FROM purchase_order_grn where gnr_id='$mid' and grn_item_qty !=''");
		return $query->result();
	}

	public function grn_total_amount($gid) {
		$query = $this->db->query("SELECT ROUND(SUM(O.grn_grand),2) as sum_amount,ROUND(SUM(O.grn_tax_amount),2) as sum_tax_amount FROM  gnr_creation G inner join purchase_order_grn O on G.gnr_id=O.gnr_id where G.gnr_id='$gid'");
		return $query->result();
	}
	public function get_uploaded_invoice($gid)
	{
		$query = $this->db->query("select * from uploaded_invoice where invoice_ref_id='$gid'");
		return $query->result();
	}
	
	public function d_grn_list($branch_id){
	    $query = $this->db->query("SELECT *,DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM gnr_creation where branch_id='$branch_id' order by  gnr_id desc");
		return $query->result();
	}
	
	public function get_drjection_grn($mid) {
		$query = $this->db->query("SELECT *,DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM gnr_creation where gnr_id='$mid'");
		return $query->result();
	}
	
	public function get_drejection_item($mid) {
		$query = $this->db->query("SELECT *,(grn_item_qty-rejected_qty) as rejection_bal_qty FROM purchase_order_grn where gnr_id='$mid'");
		return $query->result();
	}
	
	public function addRejection($postdata){
			extract($postdata);
			$branch_id = $_SESSION['branch_id'];
	
			$rejection_date = date("Y-m-d", strtotime($rejection_date));
			$order_time = date("h:i A");
			$lm=date("Y-m-dH:i:s",time());
			
			$this->db->select('MAX(rn_no) as rn_no',false);
			$this->db->where('branch_id',$branch_id);
			$result = $this->db->get('rejection_creation')->row();
			/*if($result->num_rows()>0)
			{*/
				$rnno = $result->rn_no;
				if($rnno == 0){
					$rn_no = 1;
				}
				else{
					$rn_no = $rnno+1;
				}
				$rejection_no = "RJN"."/".$branchname."/".$rn_no;
			//}
			$k=0;
			for($j=0;$j<count($rjn_quantity);$j++)
			{
				if($rjn_quantity[$j]!='0' && $rjn_quantity[$j]!='')
				{
					$k=$k+1;
				}
			}
			if($k>0)
			{
			$this->db->query("insert into rejection_creation (rn_no,rejection_no,rejection_date,branchname,gnr_id,user_name,rej_amt,rtime,branch_id) values ('$rn_no','$rejection_no','$rejection_date','$branchname','$gnr_id','$uname','$rej_amt','$order_time','$branch_id')");
			$rid = $this->db->insert_id();
			}
			if($rid)
			{
			for($i=0;$i<count($item_name),$i<count($rjn_quantity),$i<count($rejected_qty),$i<count($rjn_price),$i<count($rjn_tax),$i<count($rjn_tax_amount),$i<count($rjn_inward_grand_amount),$i<count($rjn_remarks),$i<count($gnr_po_id);$i++) {
				
				$rejctd_qty[$i] = (float)$rjn_quantity[$i] + (float)$rejected_qty[$i];//rejected qty
								
				
				$this->db->query("update purchase_order_grn set rejected_qty='$rejctd_qty[$i]' where purchase_grn_id='$gnr_po_id[$i]'");
				
				$sql3 = $this->db->query("select * from item_creation where itm_id='$itm_no[$i]' and branchname='$branchname'");
				if($sql3->num_rows() > 0){
					//foreach($sql3->result_array() as $res)
					$res = $sql3->row_array();		
					$qty[$i] = $res['stock_item'];//opening stock
					$qty1[$i] = (float)$qty[$i] - (float)$rjn_quantity[$i];//closing stock
				    $item_id[$i] = $res['item_id'];
				    $itm_id[$i] = $res['itm_id'];
				    $cat_id[$i] = $res['category_id'];
					
					$this->db->query("UPDATE item_creation set stock_item='$qty1[$i]' WHERE item_name='$itm_no[$i]' and branchname='$branchname'");
					//$sql5 = mysqli_query($this->con,"insert into stock_detail(item_name,trans_ref_no,transaction_date,opening_stock,out_qty,closing_stock)values('$item_name[$i]','$rejection_no','$rejection_date','$qty[$i]','$rjn_quantity[$i]','$qty1[$i]')");
				}
				$this->db->query("insert into  purchase_order_rejection(rejection_qty,rejection_price,rejection_tax,rejection_tax_amount,rejection_total_amount,rejection_remarks,rejection_id,purchase_grn_id,item_id,itm_id,category_id) values ('$rjn_quantity[$i]','$rjn_price[$i]','$rjn_tax[$i]','$rjn_tax_amount[$i]','$rjn_inward_grand_amount[$i]','$rjn_remarks[$i]','$rid','$gnr_po_id[$i]','$item_id[$i]','$itm_id[$i]','$cat_id[$i]')");
				$last_id = $this->db->insert_id();
				if($rjn_quantity[$i]!='0' && $rjn_quantity[$i]!='')
				{
					$this->db->query("insert into stock_detail(item_name,trans_ref_no,transaction_date,opening_stock,out_qty,closing_stock,last_modified,branchname,item_id,itm_id,category_id,branch_id)values('$item_name[$i]','$rejection_no','$rejection_date','$qty[$i]','$rjn_quantity[$i]','$qty1[$i]','$lm','$branchname','$item_id[$i]','$itm_id[$i]','$cat_id[$i]','$branch_id')");
				}
				
				$sql6 = $this->db->query("select grn_item_qty,rejected_qty from purchase_order_grn where purchase_grn_id='$gnr_po_id[$i]'");
				if($sql6->num_rows() > 0){
					foreach($sql6->result_array() as $res)
					$grn_qty[$i] = $res['grn_item_qty'];
					$rej_qty[$i] = $res['rejected_qty'];
					
					if($grn_qty[$i] == $rej_qty[$i] && $rjn_quantity[$i]!='') {
						$status = "Closed";
						$this->db->query("update purchase_order_grn set rjn_status='$status' where purchase_grn_id='$gnr_po_id[$i]'");
					}
					else {
						//echo "failure|0";
					}
				}
				
				$sql8 = $this->db->query("SELECT count(*) as `num` FROM purchase_order_grn where gnr_id='$gnr_id'");
				
				foreach($sql8->result_array() as $row)
				$count1 = $row['num'];
				
				$sql9 = $this->db->query("SELECT count(*) as `num1` FROM purchase_order_grn where rjn_status='Closed' and gnr_id='$gnr_id'");
				
				foreach($sql9->result_array() as $row1)
				$count2 = $row1['num1'];
				
				
				if($count1 == $count2 ) {
				
					$status = "Closed";
					$sql10 = $this->db->query("update gnr_creation set rjn_status='$status' where gnr_id='$gnr_id'");
					
				}
			
			}
			$sql11 = $this->db->query("select * from gnr_creation where gnr_id='$gnr_id'");
			if($sql11->num_rows()>0)
			{
				foreach($sql11->result_array() as $res12)
				$total = $res12['reject_amt'];
				$update_total = $total - $rej_amt;
				$this->db->query("update gnr_creation set reject_amt='$update_total' where gnr_id='$gnr_id'");
			}
	}
		//echo $rid;
		return $rid;
	}
	
	public function get_drejection_receipt_details($rejection_id) {
		$query = $this->db->query("SELECT *,DATE_FORMAT(G.grn_date, '%d-%m-%Y') as grn_date, DATE_FORMAT(C.rejection_date, '%d-%m-%Y') as rejection_date FROM gnr_creation G inner join rejection_creation C on G.gnr_id=C.gnr_id  where C.rejection_id='$rejection_id'");
		return $query->result();
	}
	
	public function get_dreceipt_rejection_print_list($rejection_id) {
		$query = $this->db->query("SELECT *,ROUND(J.rejection_total_amount,2) as rejection_total_amount FROM purchase_order_grn O inner join  purchase_order_rejection J on O.purchase_grn_id=J.purchase_grn_id where J.rejection_id='$rejection_id' and J.rejection_qty !=''");
		return $query->result();
	}
	
	public function total_sum_drejection_amount($rejection_id) {
		$query = $this->db->query("SELECT ROUND(SUM(D.rejection_total_amount),2) as sum_amount FROM gnr_creation G inner join rejection_creation C on G.gnr_id=C.gnr_id inner join purchase_order_rejection D on C.rejection_id=D.rejection_id where C.rejection_id='$rejection_id'");
		return $query->result();
	}
		

	
	public function get_rejection_list($branch_id) {
		$query = $this->db->query("SELECT *,DATE_FORMAT(G.grn_date, '%d-%m-%Y') as grn_date,C.rejection_id as mid, DATE_FORMAT(C.rejection_date, '%d-%m-%Y') as rejection_date FROM gnr_creation G inner join rejection_creation C on G.gnr_id=C.gnr_id where C.branch_id='$branch_id'  ORDER BY C.rejection_id desc");
		//SELECT *,DATE_FORMAT(G.grn_date, '%d-%m-%Y') as grn_date,C.rejection_id as mid, DATE_FORMAT(C.rejection_date, '%d-%m-%Y') as rejection_date FROM gnr_creation G inner join rejection_creation C on G.gnr_id=C.gnr_id where C.branch_id='$branch_id' and G.purchase_id='0' ORDER BY C.rejection_id desc
		return $query->result();
	}
	
	public function cmnt_grn_list() {
	    $branch_name = $_SESSION['branch_name'];
	    $cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("SELECT *, DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where branchname!='CW4' or branchname!='CW5' or branchname!='CW6' ORDER BY m_grn_id desc"); 
        }
        else{
            $query = $this->db->query("SELECT *, DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where branchname='CW4' ORDER BY m_grn_id desc"); 
        }
        /*if($branch_name=='CW4_CEO')
        {
            $query = $this->db->query("SELECT *, DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where branchname='CW4' ORDER BY m_grn_id desc"); 
        }
        else if($branch_name=='CW5_CEO')
        {
            $query = $this->db->query("SELECT *, DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where branchname='CW5' ORDER BY m_grn_id desc"); 
        }
        else if($branch_name=='CW6_CEO')
        {
            $query = $this->db->query("SELECT *, DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where branchname='CW6' ORDER BY m_grn_id desc"); 
        }
        else{
           $query = $this->db->query("SELECT *, DATE_FORMAT(grn_date, '%d-%m-%Y') as grn_date FROM maintainance_grn where branchname!='CW4' or branchname!='CW5' ORDER BY m_grn_id desc"); 
        }*/
		return $query->result();
	}
	
	public function mnt_stock($branch_id) {
	    $branch_name = $_SESSION['branch_name'];
	    $cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='$branch_name'  ORDER BY mnt_item_id desc");
        }
        else{
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='$cw_ceo' ORDER BY mnt_item_id desc");
        }
        /*if($branch_name=='CW4_CEO')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='CW4' ORDER BY mnt_item_id desc");
        }
        else if($branch_name=='CW5_CEO')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='CW5' ORDER BY mnt_item_id desc");
        }
        else if($branch_name=='CW6_CEO')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='CW6' ORDER BY mnt_item_id desc");
        }
        else{
		    $query = $this->db->query("SELECT * FROM maintainance_product where branchname='$branch_name'  ORDER BY mnt_item_id desc");
        }*/
		return $query->result();
	}
	
	public function cmnt_stock() {
	    $branch_name = $_SESSION['branch_name'];
	    $cw_ceo = $_SESSION['cw_ceo'];
		if($cw_ceo=='CW')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product ORDER BY mnt_item_id desc");
        }
        else{
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='$cw_ceo' ORDER BY mnt_item_id desc");
        }
        /*if($branch_name=='CW4_CEO')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='CW4' ORDER BY mnt_item_id desc");
        }
        else if($branch_name=='CW5_CEO')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='CW5' ORDER BY mnt_item_id desc");
        }
        else if($branch_name=='CW6_CEO')
        {
            $query = $this->db->query("SELECT * FROM maintainance_product where branchname='CW6' ORDER BY mnt_item_id desc");
        }
        else{
		    $query = $this->db->query("SELECT * FROM maintainance_product ORDER BY mnt_item_id desc");
        }*/
		return $query->result();
	}
	
	public function cmnt_stockBranch($data1) {
	    $branch = $data1['branch'];
		$query = $this->db->query("SELECT * FROM maintainance_product where branchname='$branch' ORDER BY mnt_item_id desc");
		return $query->result_array();
	}
}
?>