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

$maindb = 'billmain';
$trandb = 'billtran';
$formname = "Bill Register";
$file = "billreg.php";
$zroot = $dbname;


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


$party = $_POST["party"];
$einv = $_POST["einv"];
$partycond = "";
$einvcond = "";

if (!empty($_POST["party"])) {
    $partycond = "AND `party` = '$party'";
}

if ($einv == 1){
    $einvcond = "AND `irn` != '' ";
}elseif ($einv == 2){
    $einvcond = "AND `irn` = '' ";
}else{
    $einvcond = "";
}


if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; salereg.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; salereg.php");
}


$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$getdata = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
                                `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond $einvcond  ORDER BY vouchno ") or die(mysqli_error($conn));

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

    include("includes/linkshead.php");
?>
    <link href="MyStyle/rep_format.css" type="text/css">
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .mtotclr {
            color: #0098DA;
        }

        .ttotclr {
            color: #ed3237;
        }
    </style>
    </head>

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

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:2%">Sr.No.</th>
                                <th style="width:7%">Date</th>
                                <th style="width:2%">E.Inv.</th>
                                <th style="width:7%">Bill No.</th>
                                <th style="width:23%">Party Name</th>
                                <th style="width:10%">Vessel</th>
                                <th style="width:10%">Location of Vessel</th>
                                <th style="width:10%">Delivery Address</th>
                                <th style="width:10%" class="text-right">Bill Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $totamt = 0;
                            $totdiscamt = 0;
                            $totvatp = 0;
                            $totvatamt = 0;
                            $totbamt = 0;

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

                                $gsparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'  ") or die(mysqli_error($conn));
                                $party = mysqli_fetch_array($gsparty);

                                $getport = mysqli_query($conn, "SELECT `name` FROM `port` where `code` = '$pmain[port]'  ") or die(mysqli_error($conn));
                                $port = mysqli_fetch_array($getport);

                                $getdelv = mysqli_query($conn, "SELECT `name` FROM `location` where `code` = '$pmain[delvadd]'  ") or die(mysqli_error($conn));
                                $delvadd = mysqli_fetch_array($getdelv);

                                $gettran2 = mysqli_query($conn, "SELECT irno,qrcode FROM `gsteinvoice` WHERE invno='$pmain[vouchno]'") or die(mysqli_error($conn));
                                $fettran2 = mysqli_fetch_array($gettran2);
// 
                                if ($fettran2['irno'] != '') {
                                    $inv = "<i class='fa fa-qrcode' aria-hidden='true' style='font-size:25px'></i>";
                                } else {
                                    $inv = "";
                                }


                                $ctr++;
                                echo "<tr class='clickable-row2' data-href='billentry.php?mode=edit&vouchno=$pmain[vouchno]'>";
                                echo "<td> $ctr</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$inv</td>";
                                echo "<td>$pmain[billno]</td>";
                                echo "<td>$party[name]</td>";
                                echo "<td>$pmain[vessel]</td>";
                                echo "<td>$port[name]</td>";
                                echo "<td>$delvadd[name]</td>";
                                echo "<td class='text-right'>$pmain[billamt]</td>";
                                echo "</tr>";
                                $totamt = $totamt + $pmain['amount'];
                                $totdiscamt = $totdiscamt + $totdisc;
                                $totvatp = $totvatp + $pmain['vatperc'];
                                $totvatamt = $totvatamt + $pmain['vat'];
                                $totbamt = $totbamt + $pmain['billamt'];



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

                                    echo "<tr style='height:10px;'>";
                                    echo "<td colspan='25'>";
                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";

                                    echo "<thead>";
                                    echo "<tr style='background:grey'>";

                                    echo "<th style='width:4%;'>Sr</th>";
                                    echo "<th style='width:5%;'>M/S</th>";
                                    echo "<th style='width:5%;'>Hsncode	</th>";
                                    echo "<th style='width:10%;'>Description1 </th>";
                                    echo "<th style='width:10%;'>Description2</th>";
                                    echo "<th style='width:7%;' class='text-right'>Qty.</th>";
                                    echo "<th style='width:7%;' class='text-right'>Rate</th>";
                                    echo "<th style='width:7%;' class='text-right'>Taxable value</th>";
                                    echo "<th style='width:5%;' class='text-right'>CGST%</th>";
                                    echo "<th style='width:7%;' class='text-right'>CGST Amt.</th>";
                                    echo "<th style='width:5%;' class='text-right'>SGST%</th>";
                                    echo "<th style='width:7%;' class='text-right'>SGST Amt.</th>";
                                    echo "<th style='width:5%;' class='text-right'>IGST%</th>";
                                    echo "<th style='width:7%;' class='text-right'>IGST Amt.</th>";
                                    echo "<th style='width:7%;' class='text-right'>Total</th>";


                                    echo "</tr>";
                                    echo "<thead>";

                                    echo "<tr>";


                                    $ctr2 = 0;
                                    $totqty = 0;
                                    $totrate = 0;
                                    $totamount = 0;

                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$tran[ms]</td>";
                                        echo "<td>$tran[hsncode]</td>";
                                        echo "<td>$tran[descrip1]</td>";
                                        echo "<td>$tran[descrip2]</td>";
                                        echo "<td class='text-right'>$tran[qty]</td>";
                                        echo "<td class='text-right'>$tran[rate]</td>";
                                        echo "<td class='text-right'>$tran[subtot]</td>";
                                        echo "<td class='text-right'>$tran[cgstrate]</td>";
                                        echo "<td class='text-right'>$tran[cgstamt]</td>";
                                        echo "<td class='text-right'>$tran[sgstrate]</td>";
                                        echo "<td class='text-right'>$tran[sgstamt]</td>";
                                        echo "<td class='text-right'>$tran[igstrate]</td>";
                                        echo "<td class='text-right'>$tran[igstamt]</td>";
                                        echo "<td class='text-right'>$tran[total]</td>";
                                        echo "</tr>";

                                        $totqty = $totqty + $tran['qty'];
                                        $totrate = $totrate + $tran['rate'];
                                        $totamount = $totamount + $tran['amount'];
                                        $totcgstamt = $totcgstamt + $tran['cgstamt'];
                                        $totsgstamt = $totsgstamt + $tran['sgstamt'];
                                        $totigstamt = $totigstamt + $tran['igstamt'];
                                        $tottotal = $tottotal + $tran['total'];
                                    }
                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<th class='ttotclr'>Total</th>";
                                    echo "<th class='ttotclr'>$totqty</th>";
                                    echo "<th class='ttotclr'>$totrate</th>";
                                    echo "<th class='ttotclr'>$totamount</th>";
                                    echo "<td></td>";
                                    echo "<th class='ttotclr text-right'>$totcgstamt</th>";
                                    echo "<td></td>";
                                    echo "<th class='ttotclr text-right'>$totsgstamt</th>";
                                    echo "<td></td>";
                                    echo "<th class='ttotclr text-right'>$totigstamt</th>";
                                    echo "<th class='ttotclr text-right'>$tottotal   </th>";


                                    echo "</tr>";
                                    echo "</table>";
                                }
                            }

                            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 class='text-right mtotclr' ><b> Total </b></td>";
                            echo "<td class='text-right mtotclr' ><b> $totbamt    </b></td>";
                            echo "</tr>";
                            ?>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr.No.,Date,E.Inv.,Bill No., Party Name,Vessel,Location of Vessel,Delivery Address,Bill Amount \n";

    $totamt = 0;
    $totdiscamt = 0;
    $totvatp = 0;
    $totvatamt = 0;
    $totbamt = 0;

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

        $gsparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'  ") or die(mysqli_error($conn));
        $party = mysqli_fetch_array($gsparty);

        $getport = mysqli_query($conn, "SELECT `name` FROM `port` where `code` = '$pmain[port]'  ") or die(mysqli_error($conn));
        $port = mysqli_fetch_array($getport);

        $getdelv = mysqli_query($conn, "SELECT `name` FROM `location` where `code` = '$pmain[delvadd]'  ") or die(mysqli_error($conn));
        $delvadd = mysqli_fetch_array($getdelv);

        $gettran2 = mysqli_query($conn, "SELECT irno,qrcode FROM `gsteinvoice` WHERE invno='$pmain[vouchno]'") or die(mysqli_error($conn));
        $fettran2 = mysqli_fetch_array($gettran2);

        if ($fettran2['irno'] != '') {
            $inv = "Y";
        } else {
            $inv = "";
        }

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$inv" . ",";
        $contents .= "$pmain[billno]" . ",";
        $contents .= "$party[name]" . ",";
        $contents .= "$pmain[vessel]" . ",";
        $contents .= "$port[name]" . ",";
        $contents .= "$delvadd[name]" . ",";
        $contents .= "$pmain[billamt]" . "\n";

        $totamt = $totamt + $pmain['amount'];
        $totdiscamt = $totdiscamt + $totdisc;
        $totvatp = $totvatp + $pmain['vatperc'];
        $totvatamt = $totvatamt + $pmain['vat'];
        $totbamt = $totbamt + $pmain['billamt'];



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

            $contents .= "Sr,M/S,Hsncode,Description 1,Description 2,Qty,Rate,Taxable Value,CGST %,CGST Amt.,SGST %,SGST Amt.,IGST %,IGST Amt.,Total\n";


            $ctr2 = 0;
            $totqty = 0;
            $totrate = 0;
            $totamount = 0;

            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $ctr2++;

                $contents .= "$ctr2" . ",";
                $contents .= "$tran[ms]" . ",";
                $contents .= "$tran[hsncode]" . ",";
                $contents .= "$tran[descrip1]" . ",";
                $contents .= "$tran[descrip2]" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[rate]" . ",";
                $contents .= "$tran[subtot]" . ",";
                $contents .= "$tran[cgstrate]" . ",";
                $contents .= "$tran[cgstamt]" . ",";
                $contents .= "$tran[sgstrate]" . ",";
                $contents .= "$tran[sgstamt]" . ",";
                $contents .= "$tran[igstrate]" . ",";
                $contents .= "$tran[igstamt]" . ",";
                $contents .= "$tran[total]" . "\n";


                $totqty = $totqty + $tran['qty'];
                $totrate = $totrate + $tran['rate'];
                $totamount = $totamount + $tran['amount'];
                $totcgstamt = $totcgstamt + $tran['cgstamt'];
                $totsgstamt = $totsgstamt + $tran['sgstamt'];
                $totigstamt = $totigstamt + $tran['igstamt'];
                $tottotal = $tottotal + $tran['total'];
            }
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "Total" . ",";
            $contents .= "$totqty" . ",";
            $contents .= "$totrate" . ",";
            $contents .= "$totamount" . ",";
            $contents .= "" . ",";
            $contents .= "$totcgstamt" . ",";
            $contents .= "" . ",";
            $contents .= "$totsgstamt" . ",";
            $contents .= "" . ",";
            $contents .= "$totigstamt" . ",";
            $contents .= "$tottotal" . "\n";
        }
    }

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


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        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;
    }
}
?>