<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
///////////////////////////////////////////////////////
$maindb = "invmain";
$trandb = "invtran";
// $trandb = "saletran";
$formname = "Invoice Register";
$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);
///////////////////////////////////////////////////////
$file = 'invreg_fr.php';
$entfile = 'invoice.php';

$party = $_POST["party"];

$dtypecond = "";
$partycond = "";
$vehcond = "";

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

$zroot = $_SESSION["zdir"];

$dtsqry = mysqli_query($conn, "SELECT * FROM  $zroot.compyear WHERE finyear != '1001' and ('$mfrmdate' between `startdate` and `lastdate`)") or die(mysqli_error($conn));
$dtsfet = mysqli_fetch_array($dtsqry);
$stdate = $dtsfet['startdate'];
$lstdate = $dtsfet['lastdate'];

$dtlqry = mysqli_query($conn, "SELECT * FROM  $zroot.compyear WHERE finyear != '1001' and ('$mtodate' between `startdate` and `lastdate`)") or die(mysqli_error($conn));
$dtlfet = mysqli_fetch_array($dtlqry);
$lsdate = $dtlfet['lastdate'];

$dateqry = mysqli_query($conn, "SELECT * FROM  $zroot.compyear WHERE finyear != '1001' and (`startdate` >= '$stdate'  and `lastdate` <= '$lsdate') 
                        order by lastdate ") or die(mysqli_error($conn));
$yrnos = mysqli_num_rows($dateqry);

// clubbing data from all year
$salpurstring = '';
$yctr = 1;

while ($getyr = mysqli_fetch_array($dateqry)) {
    $mfinyear = $getyr["finyear"];
    $dbfyear = $zdbpath . $mfinyear;
    if ($yctr == 1) {
        $firstyear = $dbfyear;
    }

    $salpurstring .= "select * from $dbfyear.$maindb where party != '' $partycond";

    if ($yctr < $yrnos) {
        $salpurstring .= " union all ";
    }
    $yctr++;
}
mysqli_query($conn, "create temporary table saldbf $salpurstring order by vouchdt") or die(mysqli_error($conn));

$getpmain2 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, d.name as partyname,v.name as vname FROM saldbf a 
                                LEFT JOIN accmast d ON d.accode=a.party
                                LEFT JOIN vessel v ON v.code=a.vessel
                                WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                                order by a.maninvno DESC") 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>
                                    <th width="6%;">Sr</th>
                                    <th width="6%;">Invoice No.</th>
                                    <th width="6%;">Man. Inv. No.</th>
                                    <th width="8%;">Date</th>
                                    <th width="20%;">Party</th>
                                    <th width="20%;">Vessel</th>
                                    <th width="15%;" class="text-right">Amount</th>
                                    <th width="25%;">Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $ptotamt = 0;

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

                                    $voucher = $pmain['vouchno'];
                                    $maninvno = $pmain['maninvno'];
                                    $mydt = $pmain['mydt'];
                                    $vname = $pmain['vname'];
                                    $total = $pmain['amount'];
                                    $remark1 = $pmain['rem1'];
                                    $remark2 = $pmain['rem2'];
                                    $remark2 = $pmain['rem2'];
                                    $pname = $pmain['partyname'];

                                    $pctr++;
                                    echo "<tr data-href='$entfile?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                    echo "<td>$pctr</td>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$maninvno</td>";
                                    echo "<td>$mydt</td>";
                                    echo "<td>$pname</td>";
                                    echo "<td>$vname</td>";
                                    echo "<td class='text-right'>$total</td>";
                                    echo "<td>$remark1 $remark2</td>";
                                    echo "</tr>";

                                    $pgrtotal += $total;

                                    if ($_POST["dtype"] == 2) {
                                        echo "<tr style='height:10px;'>";
                                        echo "<td colspan='10'>";
                                        echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                        echo "<thead>";
                                        echo "<tr class='' style='background-color:#BABFC2'>";
                                        echo "<th style='width:8%;'>Sr</th>";
                                        echo "<th style='width:8%;' class=''>Challan No.</th>";
                                        echo "<th style='width:8%;'>Date</th>";
                                        echo "<th style='width:20%;'>Launch</th>";
                                        echo "<th style='width:20%;'>Vessel</th>";
                                        echo "<th style='width:20%;'>Anchor</th>";
                                        echo "<th style='width:16%;'>Hrs</th>";
                                        echo "<th style='width:20%;' class='text-right'>Amount</th>";
                                        echo "</tr>";
                                        echo "<thead>";
                                        echo "<tr>";

                                        $ctr2 = 0;
                                        $pctr2 = 0;
                                        $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

                                        $gettran = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`chaldate`,'%d-%m-%Y') as challandt, b.name as lname, 
                                                            c.name as aname FROM `$trandb` a LEFT JOIN launch b ON b.code=a.launch LEFT JOIN port c ON c.code=a.anchor
                                                             WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $chalno = $tran['chalno'];
                                            $chaldt = $tran['challandt'];
                                            $launch = $tran['lname'];
                                            $vessel = $tran['vessel'];
                                            $anchor = $tran['aname'];
                                            $nohrs = $tran['nohrs'];
                                            $nomin = $tran['nomin'];
                                            $amount = $tran['amount'];

                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$chalno</td>";
                                            echo "<td>$chaldt</td>";
                                            echo "<td>$launch</td>";
                                            echo "<td>$vessel</td>";
                                            echo "<td>$anchor</td>";
                                            ;
                                            echo "<td>$nohrs.$nomin</td>";
                                            echo "<td class='text-right'>$amount</td>";
                                            echo "</tr>";


                                            $ptotamount += $amount;
                                        }

                                        echo "<tr style='background-color:#d8e0e6'>";
                                        echo "<td colspan='7' class='text-right'>Total</td>";
                                        echo "<td class='text-right'>$ptotamount</td>";
                                        echo "</tr>";
                                        echo "</table>";
                                    }
                                }

                                echo "<tr style='background-color: #98F9F0'>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td class='text-right'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>" . number_format($pgrtotal, 2) . "</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"])) {

    $getno2 = mysqli_num_rows($getpmain2);


    if ($getno2 > 0) {
        $contents .= "Invoice Register\n";

        $contents .= "Total no. of record, $getno2\n";

        $contents .= "Sr, Invoice No., Man. Inv. No., Date, Party, Vessel, Amount, Remarks\n";

        $pctr = 0;
        $ptotamt = 0;

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

            $voucher = $pmain['vouchno'];
            $maninvno = $pmain['maninvno'];
            $mydt = $pmain['mydt'];
            $vname = $pmain['vname'];
            $total = $pmain['amount'];
            $remark1 = $pmain['rem1'];
            $remark2 = $pmain['rem2'];
            $remark2 = $pmain['rem2'];
            $pname = $pmain['partyname'];

            $pctr++;
            $contents .= "$pctr" . ",";
            $contents .= "$voucher" . ",";
            $contents .= "$maninvno" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$pname" . ",";
            $contents .= "$vname" . ",";
            $contents .= "$total" . ",";
            $contents .= "$remarkl $remark2" . "\n";

            $pgrtotal += $total;

            if ($_POST["dtype"] == 2) {

                $contents .= "Sr" . ",";
                $contents .= "Challan No." . ",";
                $contents .= "Date" . ",";
                $contents .= "Launch" . ",";
                $contents .= "Vessel" . ",";
                $contents .= "Anchor" . ",";
                $contents .= "Hrs" . ",";
                $contents .= "Amount" . "\n";

                $ctr2 = 0;
                $pctr2 = 0;
                $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

                $gettran = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`chaldate`,'%d-%m-%Y') as challandt, b.name as lname, 
                                                            c.name as aname FROM `$trandb` a LEFT JOIN launch b ON b.code=a.launch LEFT JOIN port c ON c.code=a.anchor
                                                             WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                while ($tran = mysqli_fetch_array($gettran)) {

                    $chalno = $tran['chalno'];
                    $chaldt = $tran['challandt'];
                    $launch = $tran['lname'];
                    $vessel = $tran['vessel'];
                    $anchor = $tran['aname'];
                    $nohrs = $tran['nohrs'];
                    $nomin = $tran['nomin'];
                    $amount = $tran['amount'];

                    $ctr2++;
                    $contents .= "$ctr2" . ",";
                    $contents .= "$chalno" . ",";
                    $contents .= "$chaldt" . ",";
                    $contents .= "$launch" . ",";
                    $contents .= "$vessel" . ",";
                    $contents .= "$anchor" . ",";
                    $contents .= "$nohrs . $nomin" . ",";
                    $contents .= "$amount" . "\n";

                    $ptotamount += $amount;
                }

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

            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$pgrtotal" . ",";
        $contents .= "" . "\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;
    }
}
?>