<?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 = "slgstreg_fr.php";

$zroot = '';

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

if (isset($_POST["view"])) {
    $party = $_POST["party"];
//    $salesman = $_POST["salesman"];
//    $item = $_POST["item"];
//    $prodtype = $_POST["prodtype"];
    $dtype = $_POST["dtype"];
    $scope = $_POST["scope"];
    $printnar = $_POST["prna"];
//    $gstperc = $_POST["gstperc"];

    $dtypecond = "";
    $mscopecond = "";
    $itemcond = "";
    $gstcond = "";
    $prodtypecond = "";

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




//    if (!empty($invno)) {
//        $invno = " AND a.`invno`='$party' ";
//    }
    if (!empty($party)) {
        $partycond = " AND a.`party`='$party' ";
    }
//    if (!empty($salesman)) {
//        $salemcond = " AND a.`salesman`='$salesman' ";
//    }
    if (!empty($item)) {
        $itemcond = " AND b.`item`='$item' ";
    }
//    if (!empty($gstperc)) {
//        $gstcond = " AND a.`gstperc`='$gstperc' ";
//    }
//    if (!empty($prodtype)) {
//        $prodtypecond = " AND a.`prodtype`='$prodtype' ";
//    }

//    if (!empty($frmdt)) {
//        $frmdtcond = " AND `vouchdt`='$frmdt' ";
//    }
//
//    if (!empty($todt)) {
//        $todtcond = " AND `todt`='$todt' ";
//    }

    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`='$dtype' ";
    }

    if (!empty($scope)) {
        $mscopecond = " AND `type`='$scope' ";
    }

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

//    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
//                            AND `vouchdt` <= '$mtodate'  $partycond $salemcond $mscopecond $prodtypecond")or die(mysqli_error($conn));
    $getpmain = mysqli_query($conn, "SELECT a.*, b.barcode, b.item, b.color, b.hsncode, b.unit, b.mrp, b.vouchno,
                           DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `$trandb` b  WHERE a.vouchno = b.vouchno and 
                           a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'  $partycond $itemcond $mscopecond group by a.vouchno")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    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; 
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </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-condensed table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Sr</th>
                                <th style="width:7%">Inv.No</th>
                                <th style="width:10%">Date</th>
                                <th style="width:20%">Customer</th>
                                <!--<th style="width:15%">Salesman</th>-->
                                <th style="width:10%" class="text-right">Amount</th>
                                <th style="width:10%" class="text-right">Discount</th>
                                <th style="width:10%" class="text-right">Packing</th>
                                <th style="width:8%" class="text-right">Taxable Value</th>
                                <th style="width:10%" class="text-right">SGST Rate</th>
                                <th style="width:10%" class="text-right">SGST Amount</th>
                                <th style="width:10%" class="text-right">CGST Rate</th>
                                <th style="width:10%" class="text-right">CGST Amount</th>
                                <th style="width:10%" class="text-right">IGST Rate</th>
                                <th style="width:10%" class="text-right">IGST Amount</th>
                                <th style="width:10%" class="text-right">Bill.Amt</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $mtotdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;


                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                $supp = mysqli_fetch_array($getsupp);
                                
                                $getsale = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[salesman]'")or die(mysqli_error($conn));
                                $sale = mysqli_fetch_array($getsale);

                                $ctr++;

                                $mtotdisc = $pmain[discount] + $pmain[cdiscamt];
                                
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[invno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$supp[name]</td>";
//                                Echo "<td>$sale[name]</td>";
                                Echo "<td class='text-right'>$pmain[subtotal]</td>";
//                                Echo "<td class='text-right'>$pmain[litemdisc]</td>";
                                Echo "<td class='text-right'>$mtotdisc</td>";
                                Echo "<td class='text-right'>$pmain[couamt]</td>";
                                Echo "<td class='text-right'>$pmain[tottaxamt]</td>";
                                Echo "<td class='text-right'>$pmain[sgstperc]</td>";
                                Echo "<td class='text-right'>$pmain[totsgst]</td>";
                                Echo "<td class='text-right'>$pmain[cgstperc]</td>";
                                Echo "<td class='text-right'>$pmain[totcgst]</td>";
                                Echo "<td class='text-right'>$pmain[igstperc]</td>";
                                Echo "<td class='text-right'>$pmain[totigst]</td>";
                                Echo "<td class='text-right'>$pmain[netamt]</td>";
                                Echo "</tr>";
                                if ($printnar == 1) {
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td></td>";
//                                    Echo "<td></td>";
//                                    Echo "<td></td>";
                                    Echo "<td>$pmain[remark1] $pmain[remark2]</td>";
                                    Echo "<td></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "</tr>";
                                }
                                $totpack = $totpack + $pmain[couamt];
                                $totbill = $totbill + $pmain[netamt];
                                $ttotamt = $ttotamt + $pmain[subtotal];
                                $totitmpr = $totitmpr + $mtotdisc;
                                $totdisc = $totdisc + $mtotdisc;
                                $tottaxamt = $tottaxamt + $pmain[tottaxamt];
                                $totsgst = $totsgst + $pmain[totsgst];
                                $totcgst = $totcgst + $pmain[totcgst];
                                $totigst = $totigst + $pmain[totigst];

                                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 style='background-color: #cccccc;width:5%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc;width:10%; class='numeric''>Barcode</th>";
                                    Echo "<th style ='background-color: #cccccc';width:6%; >Itemname</th>";
                                    Echo "<th style ='background-color: #cccccc';width:6%; >Color</th>";
                                    Echo "<th style='background-color: #cccccc';width:6%; >HSN Code</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Qty</th>";
//                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Unit</th>";
//                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Mrp</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Rate</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Amount</th>";
                                    Echo "<tr>";

                                    $ctr2 = 0;
                                    $totamt2 = 0;
                                    $totqty2 = 0;

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

                                        $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$pmain[item]'")or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($getitem);

                                        $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$pmain[color]'")or die(mysqli_error($conn));
                                        $color = mysqli_fetch_array($getcolor);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td class='numeric'>$pmain[barcode]</td>";
                                        Echo "<td>$item[name]</td>";
                                        Echo "<td>$color[name]</td>";
                                        Echo "<td class=''>$pmain[hsncode]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
//                                        Echo "<td class='numeric'>$pmain[name]</td>";
//                                        Echo "<td class='numeric'>$pmain[mrp]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";
                                        $totamt2 = $totamt2 + $tran[amount];
                                        $totqty2 = $totqty2 + $tran[qty];
                                    }
                                    echo "<tr> ";
                                    $colr1 = "blue";

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

                                    echo "<td style='color:$colr1;'  id='font1'><h5><b>$totamt2</b></h5></td>";
                                    echo "</tr>";

                                    Echo "</table>";
                                    Echo "</td>";

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

                            $colr = "green";
                            echo "<tr> ";
                            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></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></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totdisc</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>$tottaxamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totsgst</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totsgst</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totigst</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"])) {
    $invno = $_POST["invno"];
    $party = $_POST["party"];
    $salesman = $_POST["salesman"];
    $item = $_POST["item"];
    $prodtype = $_POST["prodtype"];
    $dtype = $_POST["dtype"];
    $scope = $_POST["scope"];
    $printnar = $_POST["prna"];
    $gstperc = $_POST["gstperc"];

    $dtypecond = "";
    $mscopecond = "";
    $itemcond = "";
    $gstcond = "";
    $prodtypecond = "";

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




    if (!empty($invno)) {
        $invno = " AND a.`invno`='$party' ";
    }
    if (!empty($party)) {
        $partycond = " AND a.`party`='$party' ";
    }
    if (!empty($salesman)) {
        $salemcond = " AND a.`salesman`='$salesman' ";
    }
    if (!empty($item)) {
        $itemcond = " AND b.`item`='$item' ";
    }
    if (!empty($gstperc)) {
        $gstcond = " AND a.`gstperc`='$gstperc' ";
    }
    if (!empty($prodtype)) {
        $prodtypecond = " AND a.`prodtype`='$prodtype' ";
    }


//    if (!empty($frmdt)) {
//        $frmdtcond = " AND `vouchdt`='$frmdt' ";
//    }
//
//    if (!empty($todt)) {
//        $todtcond = " AND `todt`='$todt' ";
//    }

    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`='$dtype' ";
    }

    if (!empty($scope)) {
        $mscopecond = " AND `type`='$scope' ";
    }

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

    $getpmain = mysqli_query($conn, "SELECT a.*, b.barcode, b.item, b.hsncode, b.qty, b.unit, b.mrp, b.rate, b.amount, b.vouchno,
                           DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `$trandb` b  WHERE a.vouchno = b.vouchno and 
                           a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'  $partycond $itemcond $salemcond $mscopecond $prodtypecond $gstcond")or die(mysqli_error($conn));

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

    $contents.= "Sr.No.,Invno No.,Date,Supplier,Salesman,Amount,Item Disc%,Discount,"
            . "Packing,Taxable Value,SGST%,SGST Amount,CGST%,CGST Amount,IGST%,IGST Amount,Bill Amount\n";

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

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
        $supp = mysqli_fetch_array($getsupp);
        $getsale = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[salesman]'")or die(mysqli_error($conn));
        $sale = mysqli_fetch_array($getsale);

        $mtotdisc = $pmain[discount] + $pmain[cdiscamt];

        $pinvno = $pmain[invno];
        $pmydt = $pmain[mydt];
        $sname = $supp[name];
        $sinvno = $sale[invno];
        $psubtotal = $pmain[subtotal];
        $plitemdisc = $mtotdisc;
        $pdiscount = $pmain[discount];
        $pcouamt = $pmain[couamt];
        $ptaxamt = $pmain[taxamt];
        $psgstperc = $pmain[sgstperc];
        $ptotsgst = $pmain[totsgst];
        $pcgstperc = $pmain[cgstperc];
        $ptotcgst = $pmain[totcgst];
        $pigstperc = $pmain[igstperc];
        $ptotigst = $pmain[totigst];
        $pnetamt = $pmain[netamt];



        $contents.= $ctr . ",";
        $contents.= $pinvno . ",";
        $contents.= $pmydt . ",";
        $contents.= $sname . ",";
        $contents.= $sinvno . ",";
        $contents.= $psubtotal . ",";
        $contents.= $plitemdisc . ",";
        $contents.= $pdiscount . ",";
        $contents.= $pcouamt . ",";
        $contents.= $ptaxamt . ",";
        $contents.= $psgstperc . ",";
        $contents.= $totsgst . ",";
        $contents.= $pcgstperc . ",";
        $contents.= $ptotcgst . ",";
        $contents.= $pigstperc . ",";
        $contents.= $ptotigst . ",";
        $contents.= $pnetamt . "\n";
        $ctr++;

        $totpack = $totpack + $pmain[couamt];
        $totbill = $totbill + $pmain[netamt];
        $ttotamt = $ttotamt + $pmain[subtotal];
        $totitmpr = $totitmpr + $pmain[litemdisc];
        $totdisc = $totdisc + $pmain[discount];
        $tottaxamt = $tottaxamt + $pmain[taxamt];
        $totsgst = $totsgst + $pmain[totsgst];
        $totcgst = $totcgst + $pmain[totcgst];
        $totigst = $totigst + $pmain[totigst];

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


            $contents.= "Sr.No.,Barcode,Item Name,HSN Code,Qty,Unit,Mrp,Rate,Amount\n";





            $ctr2 = 1;
            $totamt = 0;
            $totqty = 0;

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

                $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$pmain[item]'")or die(mysqli_error($conn));
                $item = mysqli_fetch_array($getitem);

                $getunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit` WHERE `code`='$pmain[unit]'")or die(mysqli_error($conn));
                $unit = mysqli_fetch_array($getunit);


                $tbarcode = $pmain[barcode];
                $iname = $item[name];
                $thsncode = $pmain[hsncode];
                $tqty = $tran[qty];
                $uname = $unit[name];
                $tmrp = $pmain[mrp];
                $trate = $tran[rate];
                $tamount = $tran[amount];


                $contents.= $ctr2 . ",";
                $contents.= $tbarcode . ",";
                $contents.= $iname . ",";
                $contents.= $thsncode . ",";
                $contents.= $tqty . ",";
                $contents.= $uname . ",";
                $contents.= $tmrp . ",";
                $contents.= $trate . ",";
                $contents.= $tamount . "\n";
                $ctr2++;

                $totamt = $totamt + $tran[amount];
                $totqty = $totqty + $tran[qty];
            }

            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= "Total" . ",";
            $contents.= $totqty . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= $totamt . "\n";
        }
    }

    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= "Grand Total" . ",";
    $contents.= " " . ",";
    $contents.= $ttotamt . ",";
    $contents.= " " . ",";
    $contents.= $totdisc . ",";
    $contents.= $totpack . ",";
    $contents.= $tottaxamt . ",";
    $contents.= " " . ",";
    $contents.= $totsgst . ",";
    $contents.= " " . ",";
    $contents.= $totcgst . ",";
    $contents.= " " . ",";
    $contents.= $totigst . ",";
    $contents.= $totbill . "\n";

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
    }
    print $contents;
}
?>