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

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

$formname = 'Container ETA Register';
$file = 'shipperreg_fr.php';

$maindb = 'shipmain';
// $trandb = 'inwtran';

$name = $_POST['name'];
$container = $_POST['container'];
$line = $_POST['line'];
$status = $_POST['status'];
$dtype = $_POST['dtype'];
$scope = $_POST['scope'];

$namecond = "";
$containercond = "";
$linecond = "";
$statuscond = "";


if ($scope == 1){
    $releasedt = " AND releasedt = '0000-00-00'";
}else if ($scope == 2){
    $releasedt = " AND releasedt != '0000-00-00'";
}else{
    $releasedt = "";
}



if (!empty($name)) {
    $namecond = " AND `name`='$name' ";
}
if (!empty($container)) {
    $containercond = " AND `container`='$container' ";
}
if (!empty($line)) {
    $linecond = " AND `live`='$line' ";
}
if (!empty($status)) {
    $statuscond = " AND `status`='$status' ";
}


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");
        echo "<script>window.close();</script>";
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        echo "<script>window.close();</script>";
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,DATE_FORMAT(`eta`, '%d-%m-%Y') as etadt,
                                        DATE_FORMAT(`delvdt`, '%d-%m-%Y')as delvdt  FROM `$maindb` WHERE `eta` >= '$mfrmdate' 
                                        AND `eta` <= '$mtodate' $namecond $containercond $linecond $statuscond $releasedt and vouchno != '' group by vouchno order by eta ") or die(mysqli_error($conn));


if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    </head>

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

            <div class="row">
                <div class="col-lg-12 ">

                    <table style="background: white;width: 100%;border-radius: 5px" class="table table-condensed table-bordered table-hover table">
                        <thead>
                            <tr style="background-color: #1ABC9C">
                                <th width="2%;" style="text-align: center;">Sr.</th>
                                <th width="14%;" style="text-align: center;">Shipper</th>
                                <th width="4%;" style="text-align: center;">Invoice</th>
                                <th width="3%;" style="text-align: center;">Logo</th>
                                <th width="8%;" style="text-align: center;">Container</th>
                                <th width="7%;" style="text-align: center;">Mundra</th>
                                <th width="7%;" style="text-align: center;">Patli</th>
                                <th width="6%;" style="text-align: center;">Status</th>
                                <th width="3%;" style="text-align: center;" class="text-right">Weight</th>
                                <th width="4%;" style="text-align: center;" class="text-right">Inv. Amt.</th>
                                <th width="4%;" style="text-align: center;" class='text-right'>Doc Value</th>
                                <th width="4%;" class='text-right'>INR Amt.</th>
                                <th width="8%;" style="text-align: center;">Line</th>
                                <th width="20%;" style="text-align: center;" colspan="2">Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $ttotwt = $tinvamt = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                $getshipper = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE accode='$pmain[name]' ") or die(mysqli_error($conn));
                                $fetshipper = mysqli_fetch_object($getshipper);
                                $shipper = $fetshipper->name;
                                $container = $pmain['container'];
                                $eta = $pmain['etadt'];
                                $live = $pmain['live'];
                                $status = $pmain['status'];
                                $docvalue = $pmain['docvalue'];

                                if($pmain['delvdt'] != "00-00-0000"){
                                    $delv = $pmain['delvdt'];
                                }else{
                                    $delv = "";
                                }

                                $ctr++;


                                $url = "shipperent.php?mode=edit&vouchno=$pmain[vouchno]";

                                echo "<tr class='clickable-row2' data-href='$url'  data-target='_blank'>";
                                echo "<td>$ctr</td>";
//                                 Echo "<td>$pmain[vouchno]</td>";
                                echo "<td style='font-size:12px'>$shipper</td>";
                                echo "<td>$pmain[invno]</td>";
                                echo "<td>$pmain[logo]</td>";
                                echo "<td>$container</td>";
                                echo "<td>$eta</td>";
                                echo "<td>$delv</td>";
                                echo "<td>$status</td>";
                                echo "<td class='text-right'>" . round($pmain['totwt'],0) . "</td>";
                                echo "<td class='text-right'>" . round($pmain['invamt'],0) . "</td>";
                                echo "<td class='text-right'>" . round($docvalue,0) . "</td>";
                                echo "<td class='text-right'>" . round($pmain['inramt'],0) . "</td>";
                                echo "<td style='font-size:12px'>$live</td>";
                                echo "<td colspan='2' style='font-size:10px'>$pmain[remark1] $pmain[remark2]</td>";
                                echo "</tr>";
                                $ttotwt = $ttotwt + $pmain['totwt'];
                                $tinvamt = $tinvamt + $pmain['invamt'];
                                $totdoc = $totdoc + $pmain['docvalue'];
                                $totinr = $totinr + $pmain['inramt'];
                            }
                            echo "<tr>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td style='font-size:14px;color:green'  class='text-right'>Total</td>";
                                echo "<td style='font-size:14px;color:green'  class='text-right'>$ttotwt</td>";
                                echo "<td style='font-size:14px;color:green' class='text-right'>$tinvamt</td>";
                                echo "<td style='font-size:14px;color:green' class='text-right'>$totdoc</td>";
                                echo "<td style='font-size:14px;color:green' class='text-right'>$totinr</td>";
                                echo "<td style='font-size:14px;color:green' class='text-right'></td>";
                                
                                echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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

        $contents .= "Sr.,Shipper,Inv No.,logo, Container,Mundra,Patli,Status,Weight,Inv.Amt.,DOC VALUE,INR Amt.,Line,Remarks\n";

            $ctr = 0;
            $ttotwt = $tinvamt = 0;
            while ($pmain = mysqli_fetch_array($getpmain)) {

                $getshipper = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE accode='$pmain[name]' ") or die(mysqli_error($conn));
                $fetshipper = mysqli_fetch_object($getshipper);
                $shipper = $fetshipper->name;
                $container = $pmain['container'];
                $eta = $pmain['etadt'];
                $live = $pmain['live'];
                $logo = $pmain['logo'];
                $status = $pmain['status'];
                $docvalue = $pmain['docvalue'];

                if($pmain['delvdt'] != "00-00-0000"){
                    $delv = $pmain['delvdt'];
                }else{
                    $delv = "";
                }

                $ctr++;
                $contents .= "$ctr" . ",";
                $contents .= "$shipper" . ",";
                $contents .= "$pmain[invno]" . ",";
                $contents .= "$logo" . ",";
                $contents .= "$container" . ",";
                $contents .= "$eta" . ",";
                $contents .= "$delv" . ",";
                $contents .= "$status" . ",";
                $contents .= "$pmain[totwt]" . ",";
                $contents .= "$pmain[invamt]" . ",";
                $contents .= "$docvalue" . ",";
                $contents .= "$pmain[inramt]" . ",";
                $contents .= "$live" . ",";
                $contents .= $pmain["remark1"] . $pmain["remark2"] . "\n";

                $tinvamt = $tinvamt + $pmain['invamt'];
                $ttotwt = $ttotwt + $pmain['totwt'];
                $totdoc = $totdoc + $pmain['docvalue'];
                $totinr = $totinr + $pmain['inramt'];
            }

            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "Total" . ",";
            $contents .= "$ttotwt" . ",";
            $contents .= "$tinvamt" . ",";
            $contents .= "$totdoc" . ",";
            $contents .= "$totinr" . ",";
            $contents .= "" . "\n";
        

    $contents = strip_tags($contents);

    header("Content-Disposition: attachment;filename = $formname" . "_Summary" . date('d-m-Y') . ".csv");
    print $contents;
}
?>