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

$maindb = "salemain";
$trandb = "saletran";
$formname = "Sale Register";
$file = "salereg.php";

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

$invno = $_POST["invno"];
$party = $_POST["party"];
$item = $_POST["item"];
$godown = $_POST["godown"];
$prodtype = $_POST["prodtype"];
$dtype = $_POST["dtype"];
$scope = $_POST["scope"];
$printnar = $_POST["prna"];
$gstperc = $_POST["gstperc"];
$city = $_POST['city'];
$state = $_POST['state'];
$broadcast = $_POST['broadcast'];


$dtypecond = "";
$mscopecond = "";
$itemcond = "";
$gstcond = "";
$prodtypecond = "";
$citycond = "";
$statecond = "";
$gdncond = "";
$broadcastcond = "";

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


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

if (!empty($godown)) {
    $gdncond = " AND a.`godown`='$godown' ";
}


if (!empty($broadcast)) {
    $broadcastcond = " AND b.`website` LIKE '%$broadcast%' ";
}
if (!empty($state)) {
    $statecond = " AND b.`state` = '$state' ";
}
if (!empty($city)) {
    $citycond = " AND b.`city` LIKE '%$city%' ";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}

$getpmain = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.name, c.name as gdnname 
                            FROM `$maindb` a LEFT JOIN accmast b ON b.accode=a.party
                            LEFT JOIN godown c ON c.code=a.godown WHERE
                            a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $partycond  $itemcond 
                            $salemcond $prodtypecond $gstcond $broadcastcond $statecond $citycond $gdncond") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .dtype {
            background-color: #cccccc;
        }
    </style>
    </head>

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

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-striped table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='5%'>Invoice</th>
                                <th width='12%'>Date</th>
                                <th width='30%'>Party</th>
                                <th width='10%'>Godown</th>
                                <th width='5%' class="text-right">Amount</th>
                                <th width='5%' class="text-right">Discount</th>
                                <th width='8%' class="text-right">Total Amount</th>
                                <th width='5%' class="text-right">Round Off</th>
                                <th width='5%' class="text-right">Packing</th>
                                <th width='11%' class="text-right">Bill.Amt</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;
                            $totroundoff = 0;


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

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[invno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$pmain[name]</td>";
                                echo "<td>$pmain[gdnname]</td>";
                                echo "<td class='text-right'>$pmain[subtotal]</td>";
                                echo "<td class='text-right'>$pmain[lesother]</td>";
                                echo "<td class='text-right'>$pmain[totalamount]</td>";
                                echo "<td class='text-right'>$pmain[roundoff]</td>";
                                echo "<td class='text-right'>$pmain[packamt]</td>";
                                echo "<td class='text-right'>$pmain[netamt]</td>";
                                echo "</tr>";

                                $ttotamt = $ttotamt + $pmain['subtotal'];
                                $totitmpr = $totitmpr + $pmain['lesother'];
                                $totpack = $totpack + $pmain['packamt'];
                                $tottaxamt = $tottaxamt + $pmain['totalamount'];
                                $totroundoff = $totroundoff + $pmain['roundoff'];
                                $totbill = $totbill + $pmain['netamt'];

                                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 "<th width='5%;'class='dtype'>Sr</th>";
                                    echo "<th width='8%' class='dtype''>Barcode</th>";
                                    echo "<th width='8%' class='dtype'>Itemname</th>";
                                    echo "<th width='8%' class='dtype'>Color</th>";
                                    echo "<th width='6%' class='numeric dtype'>Qty</th>";
                                    //                                    Echo "<th width='6%' class='numeric dtype'>Unit</th>";
                                    //                                    Echo "<th width='6%' class='numeric dtype'>Mrp</th>";
                                    echo "<th width='6%' class='numeric dtype'>Rate</th>";
                                    echo "<th width='6%' class='numeric dtype'>Amount</th>";
                                    echo "<tr>";

                                    $ctr2 = 0;
                                    $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;

                                    $gettran = mysqli_query($conn, "SELECT a. *,b.name as itemname, c.name as colorname  FROM `$trandb` a
                                     LEFT JOIN item b ON b.code=a.item
                                     LEFT JOIN color c ON c.code=a.color
                                     WHERE a.`vouchno`='$pmain[vouchno]'") or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {


                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$tran[barcode]</td>";
                                        echo "<td>$tran[itemname]</td>";
                                        echo "<td>$tran[colorname]</td>";
                                        echo "<td class='numeric'>$tran[qty]</td>";
                                        echo "<td class='numeric'>$tran[rate]</td>";
                                        echo "<td class='numeric'>$tran[amount]</td>";
                                        echo "</tr>";

                                        $totamt = $totamt + $tran['amount'];
                                        $totqty = $totqty + $tran['qty'];
                                    }
                                    echo "<tr> ";
                                    $colr1 = "blue";

                                    echo "<td style='color:$colr1'</td>";
                                    echo "<td style='color:$colr1'</td>";
                                    echo "<td style='color:$colr1'</td>";
                                    echo "<td style='color:$colr1' id='font1'><h5><b>Total</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totqty</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1'></td>";
                                    echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totamt</b></h5></td>";
                                    echo "</tr>";

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

                            $colr = "green";
                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$ttotamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totitmpr</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$tottaxamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totroundoff</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totpack</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totbill</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}


if (isset($_POST["excel"])) {
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    $contents .= "Sr., Invoice, Date, Party, Godown, Amount, Discount, Total Amount, Round Off, Packing, Bill Amount \n";

    $ctr = 0;
    $ttotamt = 0;
    $totpack = 0;
    $totbill = 0;
    $totitmpr = 0;
    $totdisc = 0;
    $tottaxamt = 0;
    $totcgst = 0;
    $totsgst = 0;
    $totigst = 0;
    $totroundoff = 0;


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

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[invno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[name]" . ",";
        $contents .= "$pmain[gdnname]" . ",";
        $contents .= "$pmain[subtotal]" . ",";
        $contents .= "$pmain[lesother]" . ",";
        $contents .= "$pmain[totalamount]" . ",";
        $contents .= "$pmain[roundoff]" . ",";
        $contents .= "$pmain[packamt]" . ",";
        $contents .= "$pmain[netamt]" . "\n";

        $ttotamt = $ttotamt + $pmain['subtotal'];
        $totitmpr = $totitmpr + $pmain['lesother'];
        $totpack = $totpack + $pmain['packamt'];
        $tottaxamt = $tottaxamt + $pmain['totalamount'];
        $totroundoff = $totroundoff + $pmain['roundoff'];
        $totbill = $totbill + $pmain['netamt'];

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

            $contents .= "Sr., Barcode, Itemname, Color, Qty, Rate, Amount \n";

            $ctr2 = 0;
            $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;

            $gettran = mysqli_query($conn, "SELECT a. *,b.name as itemname, c.name as colorname  FROM `$trandb` a
                                     LEFT JOIN item b ON b.code=a.item
                                     LEFT JOIN color c ON c.code=a.color
                                     WHERE a.`vouchno`='$pmain[vouchno]'") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$tran[barcode]" . ",";
                $contents .= "$tran[itemname]" . ",";
                $contents .= "$tran[colorname]" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[rate]" . ",";
                $contents .= "$tran[amount]" . "\n";

                $totamt = $totamt + $tran['amount'];
                $totqty = $totqty + $tran['qty'];
            }
            $colr1 = "blue";
            $contents .= "" . ",,,";
            $contents .= "Total" . ",";
            $contents .= "$totqty" . ",";
            $contents .= "" . ",";
            $contents .= "$totamt" . "\n";
        }
    }

    $colr = "green";
    $contents .= "" . ",,,,";
    $contents .= "Total" . ",";
    $contents .= "$ttotamt" . ",";
    $contents .= "$totitmpr" . ",";
    $contents .= "$tottaxamt" . ",";
    $contents .= "$totroundoff" . ",";
    $contents .= "$totpack" . ",";
    $contents .= "$totbill" . "\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;
    }
}
?>