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

ini_set("display_errors", 1);
error_reporting(E_ERROR);

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdbroot"];

$file = 'containerreg_fr.php';
$maindb = "containerent";
$formname = "Container Register";

$vendor = $_POST["vendor"];
$customer = $_POST["customer"];
$product = $_POST["product"];
$importer = $_POST["importer"];
$mode = $_POST["mode"];

$vendorcond = "";
if (!empty($vendor)) {
    $vendorcond = " AND `vendor` = '$vendor' ";
}

$customercond = "";
if (!empty($customer)) {
    $customercond = " AND `customer` = '$customer' ";
}

$productcond = "";
if (!empty($product)) {
    $productcond = " AND `product` = '$product' ";
}

$importercond = "";
if (!empty($importer)) {
    $importercond = " AND `importer` = '$importer' ";
}

$modecond = "";
if (!empty($mode)) {
    $modecond = " AND `mode` = '$mode' ";
}

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

    $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));
}

$getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,DATE_FORMAT(`orderdt`, '%d-%m-%Y') as orderdt,DATE_FORMAT(`dispdt`, '%d-%m-%Y') as dispdt, DATE_FORMAT(`recddate`, '%d-%m-%Y') as recddate
                                              FROM $maindb WHERE `vouchdt` >= '$mfrmdate' 
                                              AND `vouchdt` <= '$mtodate' $vendorcond $customercond $productcond $importercond $modecond order by vouchdt,vouchno") or die(mysqli_error($conn));
$getno2 = mysqli_num_rows($getpmain2);

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_rep2.php");

            if ($getno2 > 0) {
            ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b><?= $formname ?></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>
                                    <th width="7%;">Order No</th>
                                    <th width="8%;">Order Date</th>
                                    <th width="10%;">China Vendor</th>
                                    <th width="10%;">India Customer</th>
                                    <th width="10%;">Product</th>
                                    <th class="text-right" width="10%;">CBM</th>
                                    <th class="text-right" width="6%;">Amount</th>
                                    <th width="17%;">Importer</th>
                                    <th width="10%;">Mode</th>
                                    <th width="10%;">Item</th>
                                    <th width="14%;">Dispatch Date</th>
                                    <th width="10%;">30 Days True/False</th>
                                    <th width="10%;">Adv Payments</th>
                                    <th width="10%;">No Of Cartons</th>
                                    <th width="10%;">Received In India Date</th>
                                    <th width="10%;">Due Date</th>
                                    <th width="10%;">10 Days True/False</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $totamt = 0;

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

                                    $fvendor = mysqli_query($conn, "SELECT * FROM accmast WHERE accode = '$pmain[vendor]'") or die(mysqli_error($conn));
                                    $gvendor = mysqli_fetch_array($fvendor);

                                    $fcustomer = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE accode = '$pmain[customer]'") or die(mysqli_error($conn));
                                    $gcustomer = mysqli_fetch_array($fcustomer);

                                    $fproduct = mysqli_query($conn, "SELECT `code`,`name` FROM rawmat WHERE code = '$pmain[product]'") or die(mysqli_error($conn));
                                    $gproduct = mysqli_fetch_array($fproduct);

                                    $voucher  = $pmain['vouchno'];
                                    $mydt     = $pmain['mydt'];
                                    $orderdt     = $pmain['orderdt'];
                                    $vendor   = $gvendor['name'];
                                    $customer = $gcustomer['name'];
                                    $product  = $gproduct['name'];
                                    $cbm      = $pmain['cbm'];
                                    $amount   = $pmain['amount'];
                                    $importer = $pmain['importer'];

                                    if ($pmain['mode'] == 'Sea') {
                                        $mode = 'SEA';
                                    } else if ($pmain['mode'] == 'Air') {
                                        $mode = 'AIR';
                                    }

                                    $descrip  = $pmain['descrip'];
                                    $advance = $pmain['advance'];
                                    $dispdt = $pmain['dispdt'];
                                    $days30 = (strtotime($pmain['dispdt']) - strtotime($pmain['orderdt'])) / (60 * 60 * 24);
                                    $cartonsno = $pmain['cartonsno'];
                                    $recddate = $pmain['recddate'];
                                    $duedate = (strtotime($pmain['recddate']) - strtotime($pmain['dispdt'])) / (60 * 60 * 24);


                                    $pctr++;
                                    echo "<tr data-href='sale.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$orderdt</td>";
                                    echo "<td>$vendor</td>";
                                    echo "<td>$customer</td>";
                                    echo "<td>$product</td>";
                                    echo "<td class='text-right'>$cbm</td>";
                                    echo "<td class='text-right'>$amount</td>";
                                    echo "<td>$importer</td>";
                                    echo "<td>$mode</td>";
                                    echo "<td>$descrip</td>";
                                    echo "<td>$dispdt</td>";
                                    echo "<td>$days30</td>";
                                    echo "<td>$advance</td>";
                                    echo "<td>$cartonsno</td>";
                                    echo "<td>$recddate</td>";
                                    echo "<td>$duedate</td>";
                                    echo "<td></td>";
                                    echo "</tr>";

                                    $totamt += $amount;
                                }

                                // echo "<tr style='background-color: #98F9F0'>";
                                // echo "<td></td>";
                                // echo "<td></td>";
                                // echo "<td class='text-right' style='color: red; font-size:13px'><b>Total</b></td>";
                                // echo "<td class='numeric text-right' style='color: red; font-size:13px'><b>$totamt</b></td>";
                                // echo "<td></td>";
                                // echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            <?php
            }
            ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

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

    if ($getno2 > 0) {

        $contents .= "$formname \n";
        $contents .= " Total No. of Record : $getno2" . "\n";
        $contents .= "Order No, Order Date, China Vendor, India Customer, Product, CBM, Amount, Importer, Mode, Item, Dispatch Date, 30 Days True/False, Adv Payments, No Of Cartons, Received In India Date, Due Date, 10 Days True/False \n";

        $pctr = 0;
        $totamt = 0;

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

            $fvendor = mysqli_query($conn, "SELECT * FROM accmast WHERE accode = '$pmain[vendor]'") or die(mysqli_error($conn));
            $gvendor = mysqli_fetch_array($fvendor);

            $fcustomer = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE accode = '$pmain[customer]'") or die(mysqli_error($conn));
            $gcustomer = mysqli_fetch_array($fcustomer);

            $fproduct = mysqli_query($conn, "SELECT `code`,`name` FROM rawmat WHERE code = '$pmain[product]'") or die(mysqli_error($conn));
            $gproduct = mysqli_fetch_array($fproduct);

            $voucher  = $pmain['vouchno'];
            $mydt     = $pmain['mydt'];
            $orderdt     = $pmain['orderdt'];
            $vendor   = $gvendor['name'];
            $customer = $gcustomer['name'];
            $product  = $gproduct['name'];
            $cbm      = $pmain['cbm'];
            $amount   = $pmain['amount'];
            $importer = $pmain['importer'];

            if ($pmain['mode'] == 'Sea') {
                $mode = 'SEA';
            } else if ($pmain['mode'] == 'Air') {
                $mode = 'AIR';
            }

            $descrip  = $pmain['descrip'];
            $advance = $pmain['advance'];
            $dispdt = $pmain['dispdt'];
            $days30 = (strtotime($pmain['dispdt']) - strtotime($pmain['orderdt'])) / (60 * 60 * 24);
            $cartonsno = $pmain['cartonsno'];
            $recddate = $pmain['recddate'];
            $duedate = (strtotime($pmain['recddate']) - strtotime($pmain['dispdt'])) / (60 * 60 * 24);

            $pctr++;
            $contents .= "$voucher" . ",";
            $contents .= "$orderdt" . ",";
            $contents .= "$vendor" . ",";
            $contents .= "$customer" . ",";
            $contents .= "$product" . ",";
            $contents .= "$cbm" . ",";
            $contents .= "$amount" . ",";
            $contents .= "$importer" . ",";
            $contents .= "$mode" . ",";
            $contents .= "$descrip" . ",";
            $contents .= "$dispdt" . ",";
            $contents .= "$days30" . ",";
            $contents .= "$advance" . ",";
            $contents .= "$cartonsno" . ",";
            $contents .= "$recddate" . ",";
            $contents .= "$duedate" . "\n";

            $totamt += $amount;
        }
    }

    $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;
    }
}
