<?php
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_return_item'){
	$returnArray = array();
	$return_item_mapping_id = $_REQUEST['return_item_mapping_id'];
	$delete_sql = "DELETE FROM returns_items_mapping WHERE id = '$return_item_mapping_id'";
	if(mysqli_query($conn,$delete_sql)){
		$returnArray["status"] = "success";
	}else{
		$returnArray["status"] = "failure";
	}
	
	echo json_encode($returnArray);
	die;
}else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'get_po_item_by_po_id') {
    $returnArray = array();
    $divSection = '';
    $purchase_order_id = $_REQUEST['purchase_order_id'];

    $final_total = 0;
    $grand_total = 0;

    $po_item_mapping_query = "SELECT * FROM purchase_order_items_mapping WHERE po_id='" . $purchase_order_id . "'";
    $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;

        while ($row_po_item_mapping = mysqli_fetch_array($po_item_mapping_sql)) {
            $is_returned = ($row_po_item_mapping['is_returned'] == 1) ? 'checked' : '';

            $quantity = $row_po_item_mapping['quantity'];
            $description = $row_po_item_mapping['description'];
            $item_type = $row_po_item_mapping['item_type'];
            $part_number = $row_po_item_mapping['part_number'];
            $unit_price = $row_po_item_mapping['unit_price'];
            $total = $quantity * $unit_price;

            $vat_amount = ($total * 20 / 100);
            $final_total += $total;
            $total_amount_with_vat = $vat_amount + $total;
            $grand_total += $total_amount_with_vat;

        $divSection .= "
				<div class='row'>
					<div class='col-sm-2'>
						<label>Quantity</label>
						<input type='text' name='quantity[$i]' class='form-control' value='$quantity'>
					</div>
					<div class='col-sm-2'>
						<label class='control-label' for='Stock Ref'>Item Type</label>
						" . $gsi->getItemTypeList("stock_type[$i]", $item_type, "100%", false, "form-control select2", "", "Return") . "
					</div>
					<div class='col-sm-2'>
						<label class='control-label' for='Stock Ref'>Item Ref</label>
						" . $gsi->getPartNumberListNew("stock_ref[$i]", $part_number, "100%", false, "form-control select2", "", "Return") . "
					</div>
					<div class='col-sm-2'>
						<label>Description</label>
						<input type='text' name='description[$i]' class='form-control' value='$description'>
					</div>
					<div class='col-sm-2'>
						<label>Unit Price</label>
						<input type='text' name='unit_price[$i]' class='form-control' value='$unit_price'>
					</div>
					<div class='col-sm-2'>
						<label>Total</label>
						<input type='text' name='total[$i]' class='form-control' value='$total' readonly>
					</div>
					<div class='col-sm-1'>
						<label>Returned</label>
						<input type='checkbox' name='is_returned[$i]' value='1' $is_returned>
					</div>
				</div>";
			$i++;

        }
        $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;
}
die;
?>