<?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";
$invdb = "invmain";
$chaltrandb = "chaltran";
$admin = $_SESSION["admin_" . $dbname];
if ($_REQUEST["party"]) {
    $trandb = 'invtran';
    $party = $_REQUEST["party"];
    $vessel = $_REQUEST["vessel"];
    $voucher = $_REQUEST["vouchno"];

    $pcond = "AND e.`name`='$party'";

    $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 = "";
    if (!empty($vessel)){
        $vcond = "AND a.`vessel` = '$vesselname'";
    }

    $mctr = 0;
    $tnomin = 0;
    $tnohrs = 0;
    $trate = 0;
    $tamount = 0;
        $myquery = mysqli_query($conn, "SELECT a.vouchno as newchal,a.anchor,a.party,a.vessel as vesselname,a.vouchdt as ndt, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') AS newdt,
        b.*,b.amount as chalamt,b.srno as chalsrno, c.code as launchcode, 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.invno='$voucher' OR b.chalmark!='1') $pcond $vcond ORDER BY a.vouchdt") or die(mysqli_error($conn));
    //   WHERE b.invno=$voucher OR b.chalmark=1 $pcond $vcond  ORDER BY a.vouchdt
        

    //     $myquery = mysqli_query($conn, "SELECT a.vouchno as newchal,a.anchor,a.party,a.vessel as vesselname,a.vouchdt as ndt, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') AS newdt,
    // b.*,b.amount as chalamt,b.srno as chalsrno, c.code as launchcode, 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.invno=$voucher $pcond $vcond  ORDER BY a.vouchdt") or die(mysqli_error($conn));

    $ctr = 0;
    $mctr = 0;
    $rows = mysqli_num_rows($myquery);
    if ($rows > 0) {
        while ($fetdet = mysqli_fetch_array($myquery)) {
            $mctr++;

                $newvouch = $fetdet['newchal'];
                $newdt = $fetdet['newdt'];
                $srno = $fetdet['chalsrno'];
            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='$fetdet[ndt]' class='form chaldate'>$newdt</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[vesselname]' class='form vessel' >$fetdet[vesselname]</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[chalamt]' class='form amount' >
            <input type='hidden' name='chalsrno_$ctr' value='$fetdet[chalsrno]' class='form chalsrno' >$fetdet[chalamt]</td>";
                echo "<td class='text-left'><input type='checkbox' name='mark_$ctr' value='$fetdet[vouchno]' class='form selall' data-amount='$fetdet[chalamt]'></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'>" . $tamount . "</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 1</td>";
    }
} else {
    echo "<td colspan='5' class='text-left'>No Record for this Party 2</td>";
}
