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

$data_id = isset($_GET["data_id"]) ? $_GET["data_id"] : null;

$result = executeQuery($conn, "SELECT * FROM transactions WHERE id = ? AND is_deleted = FALSE AND is_current = TRUE", "i", [$data_id]);

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

if ($result->num_rows > 0) {
	while($row = $result->fetch_assoc()) 
	{
		$response["dr_id"] = $row["id"];
		$response["dr_date_created"] = $row["dr_date_created"];
		$response["dr_customer_name"] = $row["dr_customer_name"];
		$response["dr_effectivity_date"] = $row["dr_effectivity_date"];
		$response["dr_type"] = $row["dr_type"];
		$response["dr_number"] = $row["dr_number"];
		$response["receiver"] = $row["receiver"];
		$response["received_date"] = $row["received_date"];
		
		$response["dr_eggtray_pcs"] = isset($row["dr_eggtray_pcs"]) && $row["dr_eggtray_pcs"] !== 0 ? $row["dr_eggtray_pcs"] : "";
		$response["dr_eggtray_charge"] = isset($row["dr_eggtray_charge"]) && $row["dr_eggtray_charge"] !== 0 ? $row["dr_eggtray_charge"] : "";
		
		$response["dr_jmb_crates"] = isset($row["dr_jmb_crates"]) && $row["dr_jmb_crates"] !== 0 ? $row["dr_jmb_crates"] : "";
		$response["dr_jmb_trays"] = isset($row["dr_jmb_trays"]) && $row["dr_jmb_trays"] !== 0 ? $row["dr_jmb_trays"] : "";

		$response["dr_xlr_crates"] = isset($row["dr_xlr_crates"]) && $row["dr_xlr_crates"] !== 0 ? $row["dr_xlr_crates"] : "";
		$response["dr_xlr_trays"] = isset($row["dr_xlr_trays"]) && $row["dr_xlr_trays"] !== 0 ? $row["dr_xlr_trays"] : "";

		$response["dr_lrg_crates"] = isset($row["dr_lrg_crates"]) && $row["dr_lrg_crates"] !== 0 ? $row["dr_lrg_crates"] : "";
		$response["dr_lrg_trays"] = isset($row["dr_lrg_trays"]) && $row["dr_lrg_trays"] !== 0 ? $row["dr_lrg_trays"] : "";
		
		$response["dr_med_crates"] = isset($row["dr_med_crates"]) && $row["dr_med_crates"] !== 0 ? $row["dr_med_crates"] : "";
		$response["dr_med_trays"] = isset($row["dr_med_trays"]) && $row["dr_med_trays"] !== 0 ? $row["dr_med_trays"] : "";

		$response["dr_sml_crates"] = isset($row["dr_sml_crates"]) && $row["dr_sml_crates"] !== 0 ? $row["dr_sml_crates"] : "";
		$response["dr_sml_trays"] = isset($row["dr_sml_trays"]) && $row["dr_sml_trays"] !== 0 ? $row["dr_sml_trays"] : "";

		$response["dr_pul_crates"] = isset($row["dr_pul_crates"]) && $row["dr_pul_crates"] !== 0 ? $row["dr_pul_crates"] : "";
		$response["dr_pul_trays"] = isset($row["dr_pul_trays"]) && $row["dr_pul_trays"] !== 0 ? $row["dr_pul_trays"] : "";
		
		$response["dr_pwe_crates"] = isset($row["dr_pwe_crates"]) && $row["dr_pwe_crates"] !== 0 ? $row["dr_pwe_crates"] : "";
		$response["dr_pwe_trays"] = isset($row["dr_pwe_trays"]) && $row["dr_pwe_trays"] !== 0 ? $row["dr_pwe_trays"] : "";

		$response["dr_spw_crates"] = isset($row["dr_spw_crates"]) && $row["dr_spw_crates"] !== 0 ? $row["dr_spw_crates"] : "";
		$response["dr_spw_trays"] = isset($row["dr_spw_trays"]) && $row["dr_spw_trays"] !== 0 ? $row["dr_spw_trays"] : "";
		
		$response["dr_b1_crates"] = isset($row["dr_b1_crates"]) && $row["dr_b1_crates"] !== 0 ? $row["dr_b1_crates"] : "";
		$response["dr_b1_trays"] = isset($row["dr_b1_trays"]) && $row["dr_b1_trays"] !== 0 ? $row["dr_b1_trays"] : "";

		$response["dr_b2_crates"] = isset($row["dr_b2_crates"]) && $row["dr_b2_crates"] !== 0 ? $row["dr_b2_crates"] : "";
		$response["dr_b2_trays"] = isset($row["dr_b2_trays"]) && $row["dr_b2_trays"] !== 0 ? $row["dr_b2_trays"] : "";

		$response["dr_b3_crates"] = isset($row["dr_b3_crates"]) && $row["dr_b3_crates"] !== 0 ? $row["dr_b3_crates"] : "";
		$response["dr_b3_trays"] = isset($row["dr_b3_trays"]) && $row["dr_b3_trays"] !== 0 ? $row["dr_b3_trays"] : "";

		$response["dr_dirty_crates"] = isset($row["dr_dirty_crates"]) && $row["dr_dirty_crates"] !== 0 ? $row["dr_dirty_crates"] : "";
		$response["dr_dirty_trays"] = isset($row["dr_dirty_trays"]) && $row["dr_dirty_trays"] !== 0 ? $row["dr_dirty_trays"] : "";

		$response["dr_assorted"] = isset($row["dr_assorted"]) && $row["dr_assorted"] !== 0 ? $row["dr_assorted"] : "";
		$response["dr_others"] = isset($row["dr_others"]) && $row["dr_others"] !== 0 ? $row["dr_others"] : "";
		$response["dr_ass_jmb_bd"] = isset($row["dr_ass_jmb_bd"]) && $row["dr_ass_jmb_bd"] !== 0 ? $row["dr_ass_jmb_bd"] : "";
		$response["dr_ass_xlr_bd"] = isset($row["dr_ass_xlr_bd"]) && $row["dr_ass_xlr_bd"] !== 0 ? $row["dr_ass_xlr_bd"] : "";
		$response["dr_ass_lrg_bd"] = isset($row["dr_ass_lrg_bd"]) && $row["dr_ass_lrg_bd"] !== 0 ? $row["dr_ass_lrg_bd"] : "";
		$response["dr_ass_med_bd"] = isset($row["dr_ass_med_bd"]) && $row["dr_ass_med_bd"] !== 0 ? $row["dr_ass_med_bd"] : "";
		$response["dr_ass_sml_bd"] = isset($row["dr_ass_sml_bd"]) && $row["dr_ass_sml_bd"] !== 0 ? $row["dr_ass_sml_bd"] : "";
		$response["dr_ass_pul_bd"] = isset($row["dr_ass_pul_bd"]) && $row["dr_ass_pul_bd"] !== 0 ? $row["dr_ass_pul_bd"] : "";
		$response["dr_ass_pwe_bd"] = isset($row["dr_ass_pwe_bd"]) && $row["dr_ass_pwe_bd"] !== 0 ? $row["dr_ass_pwe_bd"] : "";
		$response["dr_ass_spw_bd"] = isset($row["dr_ass_spw_bd"]) && $row["dr_ass_spw_bd"] !== 0 ? $row["dr_ass_spw_bd"] : "";
		$response["dr_ass_b1_bd"] = isset($row["dr_ass_b1_bd"]) && $row["dr_ass_b1_bd"] !== 0 ? $row["dr_ass_b1_bd"] : "";
		$response["dr_ass_b2_bd"] = isset($row["dr_ass_b2_bd"]) && $row["dr_ass_b2_bd"] !== 0 ? $row["dr_ass_b2_bd"] : "";
		$response["dr_ass_b3_bd"] = isset($row["dr_ass_b3_bd"]) && $row["dr_ass_b3_bd"] !== 0 ? $row["dr_ass_b3_bd"] : "";
		$response["dr_ass_dirty_bd"] = isset($row["dr_ass_dirty_bd"]) && $row["dr_ass_dirty_bd"] !== 0 ? $row["dr_ass_dirty_bd"] : "";
		$response["dr_oth_jmb_bd"] = isset($row["dr_oth_jmb_bd"]) && $row["dr_oth_jmb_bd"] !== 0 ? $row["dr_oth_jmb_bd"] : "";
		$response["dr_oth_xlr_bd"] = isset($row["dr_oth_xlr_bd"]) && $row["dr_oth_xlr_bd"] !== 0 ? $row["dr_oth_xlr_bd"] : "";
		$response["dr_oth_lrg_bd"] = isset($row["dr_oth_lrg_bd"]) && $row["dr_oth_lrg_bd"] !== 0 ? $row["dr_oth_lrg_bd"] : "";
		$response["dr_oth_med_bd"] = isset($row["dr_oth_med_bd"]) && $row["dr_oth_med_bd"] !== 0 ? $row["dr_oth_med_bd"] : "";
		$response["dr_oth_sml_bd"] = isset($row["dr_oth_sml_bd"]) && $row["dr_oth_sml_bd"] !== 0 ? $row["dr_oth_sml_bd"] : "";
		$response["dr_oth_pul_bd"] = isset($row["dr_oth_pul_bd"]) && $row["dr_oth_pul_bd"] !== 0 ? $row["dr_oth_pul_bd"] : "";
		$response["dr_oth_pwe_bd"] = isset($row["dr_oth_pwe_bd"]) && $row["dr_oth_pwe_bd"] !== 0 ? $row["dr_oth_pwe_bd"] : "";
		$response["dr_oth_spw_bd"] = isset($row["dr_oth_spw_bd"]) && $row["dr_oth_spw_bd"] !== 0 ? $row["dr_oth_spw_bd"] : "";
		$response["dr_oth_b1_bd"] = isset($row["dr_oth_b1_bd"]) && $row["dr_oth_b1_bd"] !== 0 ? $row["dr_oth_b1_bd"] : "";
		$response["dr_oth_b2_bd"] = isset($row["dr_oth_b2_bd"]) && $row["dr_oth_b2_bd"] !== 0 ? $row["dr_oth_b2_bd"] : "";
		$response["dr_oth_b3_bd"] = isset($row["dr_oth_b3_bd"]) && $row["dr_oth_b3_bd"] !== 0 ? $row["dr_oth_b3_bd"] : "";
		$response["dr_oth_dirty_bd"] = isset($row["dr_oth_dirty_bd"]) && $row["dr_oth_dirty_bd"] !== 0 ? $row["dr_oth_dirty_bd"] : "";
	}
} 

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