<?php
$dbtype = "root";
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "user";

$addflag = '';

$zdb = "abusuf_bluebird_1_2425";
mysqli_select_db($conn, $zdb);

$port = $_REQUEST["port"];
$vouchdt = $_REQUEST["currdt"];
// $vouchdt = "2024-12-30";

$getdata2 = mysqli_query($conn, "SELECT vouchno,boat FROM `entmain` WHERE `port`='$port' AND `vouchdt`='$vouchdt' ") or die(mysqli_error($conn));
$row = mysqli_fetch_object($getdata2);

$metricton = "-";
$boatno = $row->boat; // Get the boat number

// Check if boat number exists
$boatnoExists = !empty($boatno); // Check if boat number is not empty

// Fetch transaction data based on voucher number
$gettrandata = mysqli_query($conn, "SELECT a.*, b.code, b.name FROM `enttran` a 
                                    LEFT JOIN item b ON a.item = b.code WHERE a.vouchno='$row->vouchno'
                                    ORDER BY a.metricton desc") or die(mysqli_error($conn));

// Prepare the table data to be returned
$tableData = "";

while ($tdrow = mysqli_fetch_array($gettrandata)){

    if ($tdrow['metricton'] > 0 ){
        $metricton = $tdrow['metricton'];
    }else{
        $metricton = "-";
    }


    $tableData .= "<tr>";
    $tableData .= "<td>$tdrow[name]</td>";
    $tableData .= "<td style='text-align:right'>$metricton</td>";
    $tableData .= "<td></td>";
    $tableData .= "<td>$tdrow[rate]</td>";
    $tableData .= "</tr>";
}

// Return boat number, flag, and table data in the response
echo json_encode([
    'boatno' => $boatno,
    'boatnoExists' => $boatnoExists,
    'tableData' => $tableData
]);
?>