<?php
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_po_item_by_po_id'){
	$returnArray =  array();
	$divSection = '';
	$purchase_order_id = $_REQUEST['purchase_order_id'];
	$arolite_job_id = $_REQUEST['arolite_job_id'];
	
	$po_inv_exist_query = "SELECT id FROM po_invoices WHERE purchase_order_id='".$purchase_order_id."' AND arolite_job_id='".$arolite_job_id."' ORDER BY id";
	$po_inv_exist_sql=mysqli_query($conn,$po_inv_exist_query);
	$po_inv_exist_res=mysqli_num_rows($po_inv_exist_sql);
	if($po_inv_exist_res>0){
		$row_po_inv = mysqli_fetch_array($po_inv_exist_sql);
		$po_invoice_ID = $row_po_inv['id'];
	}else{
		$po_invoice_ID = "";
	}
	
	$final_total=0;
	$grand_total=0;
	$$quantity = "";
	$$unit_price = "";
	$po_item_mapping_query = "SELECT * FROM purchase_order_items_mapping WHERE po_id='".$purchase_order_id."' ORDER BY ordering ASC";
	$po_item_mapping_sql = mysqli_query($conn,$po_item_mapping_query);
	$po_item_mapping_result = mysqli_num_rows($po_item_mapping_sql);
	if($po_item_mapping_result>0){
		$i = 0;
		$j = 0;
		while($row_po_item_mapping = mysqli_fetch_array($po_item_mapping_sql)){
			/* if($row_po_item_mapping['vat_value']==0){
				$vat_valuee =20;
			}else{
				$vat_valuee =$row_po_item_mapping['vat_value'];
			} */
			$po_inv_item_exist_query = "SELECT id,quantity FROM po_invoices_items_mapping WHERE po_invoices_id='".$po_invoice_ID."' AND purchase_order_mapping_id='".$row_po_item_mapping['id']."' AND purchase_order_id='".$purchase_order_id."'ORDER BY id";
			$po_inv_item_exist_sql = mysqli_query($conn,$po_inv_item_exist_query);
			$po_inv_item_exist_res = mysqli_num_rows($po_inv_item_exist_sql);
			if($po_inv_item_exist_res>0){
				$row_po_inv_tem = mysqli_fetch_array($po_inv_item_exist_sql);
				$po_invoices_items_mapping_ID = $row_po_inv_tem['id'];
				$po_invoices_item_quantity = $row_po_item_mapping['quantity'] - $row_po_inv_tem['quantity'];
			}else{
				$po_invoices_items_mapping_ID = "";
				$po_invoices_item_quantity = $row_po_item_mapping['quantity'];
			}
			if($po_invoices_item_quantity > 0){
		$divSection .= '
				<div class="col-sm-12 col-lg-12 col-12 initial_row_for_po_invoices_items" id="new_po_inv_item_'.$row_po_item_mapping['id'].'" data-po_inv_item_count="'.$row_po_item_mapping['id'].'" alt="'.$j.'">
					<div class="row">
						<div class="col-sm-1"> 
							<label class="control-label" for="ordering">Sr No.<em></em></label><br/> 
							'.$row_po_item_mapping['ordering'].'
						</div>
						<div class="col-sm-1"> 
							<label class="control-label" for="Description">Description<em></em></label><br/>  
							'.$row_po_item_mapping['description'].'
						</div>
						<div class="col-sm-1"> 
							<label class="control-label" for="Quantity">Quantity<em></em></label> <br/> 
							'.$po_invoices_item_quantity.'
							
							<input type="hidden" id="po_item_quantity_hidden" class="po_item_quantity_hidden" name="po_item_quantity_hidden['.$i.']" value="'.$po_invoices_item_quantity.'" >
						</div>
						<div class="col-sm-1"> 
							<label class="control-label" for="Unit Price">Unit Price<em></em></label> <br/> 
							'.$row_po_item_mapping['unit_price'].'
							
							<input type="hidden" id="po_item_up_hidden" class="po_item_up_hidden" name="po_item_up_hidden['.$i.']" value="'.$row_po_item_mapping['unit_price'].'" >
						</div>
						<div class="col-sm-1" style="border-right:3px solid black;"> 
							<label class="control-label" for="Total">Total<em></em></label> <br/> 
							'.$row_po_item_mapping['total'].'
						</div>
						<div class="col-sm-1"> 
							 <label class="control-label" for="ordering">Sr No.<em></em></label> 
							 <input type="text" id="ordering" name="ordering['.$i.']" class="form-control ordering" value="'.$row_po_item_mapping['ordering'].'" placeholder="Sr No." readonly>
						</div>
						
						<div class="col-sm-2"> 
							 <label class="control-label" for="Description">Description<em></em></label> 
							 <input type="text" id="description" name="description['.$i.']" class="form-control description" value="'.$row_po_item_mapping['description'].'" placeholder="Description">
						</div>
						<div class="col-sm-1"> 
							<label class="control-label" for="Quantity">Quantity<em></em></label> 
							<div class=""> 
								<input type="text" id="quantity" name="quantity['.$i.']" class="form-control quantity check_number_value check_negative_value" value="" placeholder="Quantity">
							</div>
						</div>
						<div class="col-sm-1"> 
							 <label class="control-label" for="Unit Price">Unit Price<em></em></label> 
							 <div class=""> 
								<input type="text" id="unit_price" name="unit_price['.$i.']" class="form-control unit_price check_number_value check_negative_value" value="" placeholder="Unit Price">
								
								<input type="hidden" id="purchase_order_mapping_id" name="purchase_order_mapping_id['.$i.']" value="'.$row_po_item_mapping['id'].'" >
							</div>
						</div>
						<div class="col-sm-1"> 
							 <label class="control-label" for="Total">Total<em></em></label> 
							 <div class=""> 
								<input type="text" id="total" name="total['.$i.']" class="form-control total" value="" placeholder="Total" readonly>
							</div>
						</div>
						<div class="col-sm-1"> 
							 <label class="control-label" for="VAT Value">VAT Value<em></em></label> 
							  <input type="text" id="vat_value" name="vat_value['.$i.']" class="form-control vat_value check_number_value check_negative_value" value="'.$row_po_item_mapping['vat_value'].'" placeholder="VAT Value">
						</div>
						
						<div class="col-sm-1 hide"> 
							 <label class="control-label" for="Nominal Code">Nominal Code<em></em></label> 
							  <input type="text" id="nominal_code" name="nominal_code['.$i.']" class="form-control nominal_code" value="5000" placeholder="Nominal Code">
							  
							<div class="custom-trash">
								<label class="control-label" style="font-color:red;"></label>
								<span class="fa fa-trash delete_po_invoices_item" style="cursor:pointer;" alt="" ></span>
								
							</div>
						</div>
					</div>
				</div>
				
				';
			}
			$i++;
			$j++;
			
			$total = $row_po_item_mapping["quantity"] * $row_po_item_mapping["unit_price"];
			$vat_amount = ($total * 20 /100);
			$final_total = $final_total + $total;
			$total_amount_with_vat = $vat_amount + $total;
			$grand_total = $total_amount_with_vat+$grand_total;
		}
		$returnArray['status']  = "success";	
		$returnArray['divSection']  = $divSection;
		$returnArray['final_total']  = $final_total;
		$returnArray['grand_total']  = $grand_total;
	}else{
		$returnArray['status']  = "failure";
		$returnArray['divSection']  = "<div class='col-lg-10 text-center' style='padding: 7px 0px;font-style:italic;color: #F44336;'><b>There is no Item in this Purchase Order.</b></div>";	
	}
					
	echo json_encode($returnArray);
	die;

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_PO_by_arolite_job_id'){
	$arolite_job_id = $_REQUEST['arolite_job_id'];
    $sql = "SELECT id,order_ref,variation_no FROM purchase_order WHERE call_reference_id = '$arolite_job_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$gsi->getPurchaseOrderList("purchase_order_id","","100%",false,"form-control select2","",$arolite_job_id); 
	}else{
		echo "<div class='col-lg-10' style='padding: 7px 0px;font-style: italic;color: #F44336;'>There is no PO in this Order No.</div>";
	}
	die;	

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_po_invoices_item'){
	$returnArray = array();
	$po_invoices_mapping_id = $_REQUEST['po_invoices_mapping_id'];
	$delete_sql = "DELETE FROM po_invoices_items_mapping WHERE id = '$po_invoices_mapping_id'";
	if(mysqli_query($conn,$delete_sql)){
		$returnArray["status"] = "success";
	}else{
		$returnArray["status"] = "failure";
	}
	
	echo json_encode($returnArray);
	die;
}  
die;
?>