<?php
require_once("dbconn.php");
require_once("phpFunctions.php");

$effectivity_date = isset($_POST["drsi_effectivity_date"]) ? $_POST["drsi_effectivity_date"] : null;
$customer_name = isset($_POST["drsi_customer_name"]) ? $_POST["drsi_customer_name"] : null;

$response = array(); 

$result = executeQuery($conn, "SELECT * FROM pricelist WHERE pl_effectivity_date = ? AND pl_customer_name = ? AND is_deleted = FALSE AND is_current = TRUE", "ss", [$effectivity_date, $customer_name]);

if ($result === false) { 
	throw new Exception("Error executing query: " . $conn->error);
} 

if ($result->num_rows > 0) 
{
	while($row = $result->fetch_assoc()) 
	{
		$response["pl_eggtray_charge"] = isset($row["pl_eggtray_charge"]) && $row["pl_eggtray_charge"] !== 0 ? number_format($row["pl_eggtray_charge"], 2) : "";
		
		$response["pl_shipping_charge"] = isset($row["pl_shipping_charge"]) && $row["pl_shipping_charge"] !== 0 ? number_format($row["pl_shipping_charge"], 2) : "";

		$response["pl_other_charges"] = isset($row["pl_other_charges"]) && $row["pl_other_charges"] !== 0 ? number_format($row["pl_other_charges"], 2) : "";
		
		$response["pl_JMB"] = isset($row["pl_JMB"]) && $row["pl_JMB"] !== 0 ? number_format($row["pl_JMB"], 2) : "";
		$response["pl_XLR"] = isset($row["pl_XLR"]) && $row["pl_XLR"] !== 0 ? number_format($row["pl_XLR"], 2) : "";
		$response["pl_LRG"] = isset($row["pl_LRG"]) && $row["pl_LRG"] !== 0 ? number_format($row["pl_LRG"], 2) : "";
		$response["pl_MED"] = isset($row["pl_MED"]) && $row["pl_MED"] !== 0 ? number_format($row["pl_MED"], 2) : "";
		$response["pl_SML"] = isset($row["pl_SML"]) && $row["pl_SML"] !== 0 ? number_format($row["pl_SML"], 2) : "";
		$response["pl_PUL"] = isset($row["pl_PUL"]) && $row["pl_PUL"] !== 0 ? number_format($row["pl_PUL"], 2) : "";
		$response["pl_PWE"] = isset($row["pl_PWE"]) && $row["pl_PWE"] !== 0 ? number_format($row["pl_PWE"], 2) : "";
		$response["pl_SPW"] = isset($row["pl_SPW"]) && $row["pl_SPW"] !== 0 ? number_format($row["pl_SPW"], 2) : "";
		$response["pl_B1"] = isset($row["pl_B1"]) && $row["pl_B1"] !== 0 ? number_format($row["pl_B1"], 2) : "";
		$response["pl_B2"] = isset($row["pl_B2"]) && $row["pl_B2"] !== 0 ? number_format($row["pl_B2"], 2) : "";
		$response["pl_B3"] = isset($row["pl_B3"]) && $row["pl_B3"] !== 0 ? number_format($row["pl_B3"], 2) : "";
		$response["pl_Dirty"] = isset($row["pl_Dirty"]) && $row["pl_Dirty"] !== 0 ? number_format($row["pl_Dirty"], 2) : "";
	}
}

if (empty($response)) {
	echo json_encode(["status" => "error", "message" => "pop_effectivity_date - An error occurred while processing your request."]);
} else {
    echo json_encode($response);
}
?>