<?php
session_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$chaldb = "chalmain";
$chaltrandb = "chaltran";
$admin = $_SESSION["admin_" . $dbname];
$salent = $_SESSION["salent"];
if ($_REQUEST["party"]) {
    $trandb = 'invtran';
    $party = $_REQUEST["party"];
    $vessel = $_REQUEST["vessel"];
    $voucher = $_REQUEST["vouchno"];
    $addflag = $_REQUEST["addflag"];
    $pcond = "AND e.`name`='$party'";

    
    $vcond = '';

    if (!empty($vessel)) {
        $getvessel = mysqli_query($conn, "SELECT `code`,`name` FROM vessel Where code=$vessel")or die(mysqli_error($conn));
        $fetvessel = mysqli_fetch_array($getvessel);
        $vesselname = $fetvessel['name'];
        $vcond = "AND `vessel` = '$vesselname'";
    }
    $mctr = 0;
    $tnomin = 0;
    $tnohrs = 0;
    $trate = 0;
    $tamount = 0;
    
    if ($addflag == 1) {

    $myquery = mysqli_query($conn, "SELECT a.*, a.vouchdt AS chaldt, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') AS newdt,
        b.*,b.srno as chalsrno, c.name AS launchname, d.name AS anchname, e.name AS partyname
        FROM chalmain a
        LEFT JOIN chaltran b ON b.vouchno = a.vouchno
        LEFT JOIN launch c ON c.code = b.launch
        LEFT JOIN port d ON d.code = a.anchor
        LEFT JOIN accmast e ON e.accode = a.party WHERE b.chalmark=0 $pcond $vcond ORDER BY a.vouchdt") or die(mysqli_error($conn));
    } else {
        $myquery = mysqli_query($conn, "SELECT a.*,a.srno as chalsrno , DATE_FORMAT(a.`chaldate`, '%d-%m-%Y') as newdt, b.name as launchname, c.name as anchname 
FROM $trandb a
LEFT JOIN `launch` b ON b.code = a.launch
LEFT JOIN `port` c ON c.code = a.anchor
WHERE a.`vouchno` = $voucher ORDER BY a.chaldate,a.chalno ") or die(mysqli_error($conn));
    }
    $ctr = 0;
    $mctr = 0;
    $rows = mysqli_num_rows($myquery);
    if ($rows > 0) {
        
        while ($fetdet = mysqli_fetch_array($myquery)) {
            $mctr++;
            if ($addflag == 1) {
                $newvouch = $fetdet['vouchno'];
                $newdate = $fetdet['chaldt'];
                $newdt = $fetdet['newdt'];
            } else {
                $newvouch = $fetdet['chalno'];
                $newdate = $fetdet['chaldate'];
                $newdt = $fetdet['newdt'];
            }
            $srno = $fetdet['chalsrno'];
            // echo "<tr class='tabital clickable-row' data-href='$file?mode=edit&vouchno=$fetdet[vouchno]'>";
            echo "<tr style='height:30px'>";
            echo "<td><input type='hidden' name='srno_$ctr' value='$srno' class='form srno' >$mctr</td>";
            echo "<td><input type='hidden' name='chalno_$ctr' value='$newvouch' class='form chalno'>$newvouch</td>";
            echo "<td><input type='hidden' name='chaldate_$ctr' value='$newdate' class='form chaldate'>$newdt</td>";
            // echo "<td><input type='hidden' name='chaldate_$ctr' value='$newdate' class='form chaldate' >$newdate</td>";
            echo "<td class='text-center'><input type='hidden' name='launch_$ctr' value='$fetdet[launchname]' class='form launch' >$fetdet[launchname]</td>";
            echo "<td class='text-center'><input type='hidden' name='anchor_$ctr' value='$fetdet[anchname]' class='form anchor' >$fetdet[anchname]</td>";
            echo "<td class='text-center'><input type='hidden' name='vessel_$ctr' value='$fetdet[vessel]' class='form vessel' >$fetdet[vessel]</td>";
            echo "<td class='text-right'><input type='hidden' name='nohrs_$ctr' value='$fetdet[nohrs]' class='form nohrs' ><input type='hidden' name='nomin_$ctr' value='$fetdet[nomin]' class='form nomin' >$fetdet[nohrs].$fetdet[nomin]</td>";
            echo "<td class='text-right'><input type='hidden' name='amount_$ctr' value='$fetdet[amount]' class='form amount' >$fetdet[amount]</td>";
            if ($addflag == 1) {
                echo "<td class='text-left'><input type='checkbox' name='mark_$ctr' value='$fetdet[vouchno]' class='form selall' data-amount='$fetdet[amount]'></td>";
            } else {
                echo "<td class='text-left'><input type='hidden' name='mark_$ctr' value='$fetdet[vouchno]'></td>";
            }
            echo "</tr>";

            $ctr++;
            $tamount += $fetdet['amount'];
        }
        echo "<tr style='background-color:white'>";
        echo "<td class='text-right' style='color:#561585;font-weight:bolder' colspan='7'>Total</td>";
        echo "<td class='text-right' style='color:#561585;font-weight:bolder'>" . rajnumformat($tamount, 2) . "</td>";
        echo "<td class='text-right' style='color:#561585;font-weight:bolder'><input type='hidden' name='newitem' value='$ctr'></td>";
        echo "</tr>";
    } else {

        echo "<td colspan='5' class='text-left'>No Record for this Party</td>";
    }
} else {
    echo "<td colspan='5' class='text-left'>No Record for this Party</td>";
}
