<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
///////////////////////////////////////////////////////
$maindb = "challan";
$trandb = "invmain";

$formname = "Pending Bill";
$file = "chalreg_fr.php";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdir"];
$zdbpath = substr($zroot, 0, -4);

///////////////////////////////////////////////////////

$party = $_POST["party"];
$vessel = $_POST["vessel"];

$dtypecond = "";
$partycond = "";
$vesselcond = "";

$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);

if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; salereg.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; salereg.php");
}
$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));


if (!empty($party)) {
    $partycond = " AND `party`='$party' ";
}
if (!empty($vessel)) {
    $vesselcond = " AND a.`vessel`='$vessel' ";
}

$zroot = $_SESSION["zdir"];

$qry = "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, c.name as anchname, d.name as partyname FROM chalmain a 
                                LEFT JOIN port c ON c.code=a.anchor
                                LEFT JOIN accmast d ON d.accode=a.party
                                 LEFT JOIN invtran i ON a.vouchno = i.chalno WHERE i.chalno IS NULL AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $vesselcond $partycond order by a.vouchdt";
// print table($qry);
// die();
$getpmain2 = mysqli_query($conn, $qry) or die(mysqli_error($conn));

if (isset($_POST["view"]) || ($_GET["view"])) {
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
    </style>
    </head>

    <body>


        <div class="container-fluid">
            <?php
            include("includes/head_rep.php");

            $getno2 = mysqli_num_rows($getpmain2);


            if ($getno2 > 0) {
                ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <th width="100%" class="h4" style="color:white"><b>Invoice Register</b></th>
                            </tr>
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>
                        <table style="background: white;width: 100%"
                            class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr class="mySearchTr">
                                    <th style="width:5%">Sr</th>
                                    <th style="width:5%">Voucher</th>
                                    <th style="width:7%">Date</th>
                                    <th style="width:11%">Party</th>
                                    <th style="width:11%">Vessel</th>
                                    <th style="width:5%">Order By</th>
                                    <th style="width:5%">Anchor</th>
                                    <th style="width:5%">Purpose</th>
                                    <th style="width:5%" class="text-right dth">Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $ptotamt = 0;

                                while ($pmain = mysqli_fetch_array($getpmain2)) {

                                    $vouchno = $pmain['vouchno'];
                                    $date = $pmain['mydt'];
                                    $partyname = $pmain['partyname'];
                                    $ordby = $pmain['ordby'];
                                    $vessel = $pmain['vessel'];
                                    $anchor = $pmain['anchname'];
                                    $launch = $pmain['lname'];
                                    $purpose = $pmain['purpose'];
                                    $amount = $pmain['amount'];
                                    if ($type == 'H') {
                                        $bgrow = "style='background-color: #fee5d9'";
                                    } else {
                                        $bgrow = "";
                                    }

                                    $ctr++;
                                    echo "<tr class='clickable-row2' data-href='$entfile?mode=edit&vouchno=$sale[vouchno]&cancel=1' [target]='_blank' $bgrow>";
                                    echo "<td>$ctr</td>";
                                    echo "<td>$vouchno</td>";
                                    echo "<td>$date</td>";
                                    echo "<td class='font-22'>$partyname</td>";
                                    echo "<td class='font-22'>$vessel</td>";
                                    echo "<td class='font-22'>$ordby</td>";
                                    echo "<td class='font-22'>$anchor</td>";
                                    echo "<td class='font-22'>$purpose</td>";
                                    echo "<td class='text-right dth amount'>$amount</td>";
                                    echo "</tr>";

                                    $pgrtotal += $amount;
                                }

                                echo "<tr style='background-color: #98F9F0'>";
                                echo "<td colspan='8' class='text-right'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>" . number_format($pgrtotal, 2) . "</b></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <?php
            }
            ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
    <?php
}

if (isset($_POST["excel"]) || ($_GET["excel"])) {

    $getno2 = mysqli_num_rows($getpmain2);


    if ($getno2 > 0) {
        $contents .= "Invoice Register\n";
        $contents .= "Total No. of Record:, $getno2\n";

        $contents .= "Sr, Voucher, Date, Party, Vessel, Order By, Anchor, Purpose, Amount\n";

        $pctr = 0;
        $ptotamt = 0;

        while ($pmain = mysqli_fetch_array($getpmain2)) {

            $vouchno = $pmain['vouchno'];
            $date = $pmain['mydt'];
            $partyname = $pmain['partyname'];
            $ordby = $pmain['ordby'];
            $vessel = $pmain['vessel'];
            $anchor = $pmain['anchname'];
            $launch = $pmain['lname'];
            $purpose = $pmain['purpose'];
            $amount = $pmain['amount'];
            if ($type == 'H') {
                $bgrow = "style='background-color: #fee5d9'";
            } else {
                $bgrow = "";
            }

            $ctr++;
            $contents .= "$ctr" . ",";
            $contents .= "$vouchno" . ",";
            $contents .= "$date" . ",";
            $contents .= "$partyname" . ",";
            $contents .= "$vessel" . ",";
            $contents .= "$ordby" . ",";
            $contents .= "$anchor" . ",";
            $contents .= "$purpose" . ",";
            $contents .= "$amount" . "\n";

            $pgrtotal += $amount;
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$pgrtotal" . "\n";

    }

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        header("Content-Disposition: attachment;filename = $formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment;filename = $formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>