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

$maindb = "purmain";
$trandb = "purtran";
$formname = "Purchase GST Register";
$file = "purreggst_fr.php";


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

if (isset($_POST["view"])) {
    $party = $_POST["party"];
    $dtype = $_POST["dtype"];
    $scope = $_POST["scope"];
    $gstperc = $_POST["gstperc"];
    $item = $_POST["item"];

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

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



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

    if (!empty($item)) {
        $itemcond = " AND b.`itemcode`='$item' ";
    }

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


//    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; purreggst.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purreggst.php");
    }

//    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
//                            AND `vouchdt` <= '$mtodate'  $partycond $mscopecond ")or die(mysqli_error($conn));
    $getpmain = mysqli_query($conn, "SELECT a.*, b.itemcode, 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 $mscopecond $itemcond $gstcond group by a.vouchno")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
//    browse($get);
    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-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:5%">Inv.No</th>
                                <th style="width:10%">Date</th>
                                <th style="width:10%">Supplier</th>
                                <th style="width:5%" class="text-right">Subtot</th>
                                <th style="width:5%"  class="text-right">Item Disc</th>
                                <th style="width:4%"  class="text-right">Cash Disc%</th>
                                <th style="width:5%"  class="text-right">Cash Disc Amt</th>
                                <th style="width:5%"  class="text-right">Less Disc</th>
                                <th style="width:5%"  class="text-right">Add Pack</th>
                                <th style="width:5%"  class="text-right">Taxable Value</th>
                                <th style="width:4%"  class="text-right">CGST %</th>
                                <th style="width:5%"  class="text-right">CGST Amt</th>
                                <th style="width:4%"  class="text-right">SGST %</th>
                                <th style="width:5%"  class="text-right">SGST Amt</th>
                                <th style="width:4%"  class="text-right">IGST %</th>
                                <th style="width:5%"  class="text-right">IGST Amt</th>
                                <th style="width:9%" class="text-right">Bill.Amt</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $totdisc = 0;
                            $totbill = 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);

                                $ctr++;
                                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 class='text-right'>$pmain[subtotal]</td>";
                                Echo "<td class='text-right'>$pmain[litemdisc]</td>";
                                Echo "<td class='text-right'>$pmain[cdiscperc]</td>";
                                Echo "<td class='text-right'>$pmain[cdiscamt]</td>";
                                Echo "<td class='text-right'>$pmain[lesother]</td>";
                                Echo "<td class='text-right'>$pmain[packing]</td>";
                                Echo "<td class='text-right'>$pmain[tottaxamt]</td>";
                                Echo "<td class='text-right'>$pmain[cgstperc]</td>";
                                Echo "<td class='text-right'>$pmain[totcgst]</td>";
                                Echo "<td class='text-right'>$pmain[sgstperc]</td>";
                                Echo "<td class='text-right'>$pmain[totsgst]</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>";
                                $totbill = $totbill + $pmain[netamt];
                                $totamt = $totamt + $pmain[subtotal];
                                $totdisc = $totdisc + $pmain[discount];

                                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='text-center''>Barcode</th>";
                                    Echo "<th style='background-color: #cccccc';width:6%; class='' >Itemname</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'>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 "<th style='background-color: #cccccc;width:6%;' class='numeric'>Dis%age</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Discount</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Total</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Msp</th>";

                                    Echo "</tr>";


                                    $ctr2 = 0;
                                    $tqty = 0;
                                    $tamt = 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[itemcode]'")or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($getitem);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td class='text-center'>$pmain[itemcode]</td>";
                                        Echo "<td>$item[name]</td>";
                                        Echo "<td class='numeric'>$pmain[hsncode]</td>";
//                                        browse($tran[barcode]);
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[mrp]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "<td class='numeric'>$tran[discperc]</td>";
                                        Echo "<td class='numeric'>$tran[discount]</td>";
                                        Echo "<td class='numeric'>$tran[total]</td>";
                                        Echo "<td class='numeric'>$tran[msp]</td>";
                                        Echo "</tr>";

                                        $tqty = $tran[qty] + $tqty;
                                        $tamt = $tran[total] + $tamt;

//                                        
                                    }
                                    Echo "<tr style=' '>";
                                    Echo "<td style=' '></td>";
                                    Echo "<td style=' '></td>";
                                    Echo "<td style=' '></td>";
                                    Echo "<th style='text-align:right;color:blue; '>Total</th>";
                                    Echo "<th style='text-align:right;color:blue; '>$tqty</th>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<th style='text-align:right;color:blue; '>$tamt</td>";
                                    Echo "<td style=' '></td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                    Echo "</td>";
                                }
//                                if ($_POST["dtype"] == 2) {
//                                    echo "<tr> ";
//                                    $colr1 = "blue";
//                                    echo "<td style='color:$colr1;' align='right' id='font1' colspan='6' ><h5><b>Total :</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='right' id='font1'><h5><b>$topaytot1</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='center' id='font1'><h5><b>$totweight</b></h5></td>";
//                                    echo "<td style='color:$colr1;'  id='font1'><h5><b>$totpaid</b></h5></td>";
//                                    echo "</tr>";
//                                }
                            }
                            $colr = "red";
                            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></td>";
                            echo "<td></td>";
                            echo "<td align='right' colspan='2' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></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"])) {
    $party = $_POST["party"];
    $gstperc = $_POST["gstperc"];
    $item = $_POST["item"];
    $dtype = $_POST["dtype"];
    $scope = $_POST["scope"];

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

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



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


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

    if (!empty($item)) {
        $itemcond = " AND b.`item`='$item' ";
    }
    if (!empty($gstperc)) {
        $gstcond = " AND a.`gstperc`='$gstperc' ";
    }

    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; purreggst.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purreggst.php");
    }

    $getpmain = mysqli_query($conn, "SELECT a.*, b.itemcode, 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 $mscopecond $itemcond $gstcond")or die(mysqli_error($conn));
//    browse($get);
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));



    $contents .= "Sr.no,Invoice,Date,Supplier,Subtot,Item Disc%,Cash Disc%,Cash Disc Amt,Less Disc,Add Pack,"
            . "Taxable Value,CGST%,CGST Amt,SGST%,SGST Amt,IGST%,IGST Amt,Bill Amt,\n";

    $ctr = 1;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 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);


        $pinvno = $pmain[invno];
        $pmydt = $pmain[mydt];
        $ssname = $supp[name];
        $psubtotal = $pmain[subtotal];
        $plitemdisc = $pmain[litemdisc];
        $pcdiscperc = $pmain[cdiscperc];
        $pcdiscamt = $pmain[cdiscamt];
        $plesother = $pmain[lesother];
        $ppacking = $pmain[packing];
        $ptotaxamt = $pmain[tottaxamt];
        $pcgstperc = $pmain[cgstperc];
        $ptotcgst = $pmain[totcgst];
        $psgstper = $pmain[sgstperc];
        $ptotsgst = $pmain[totsgst];
        $pigstperc = $pmain[igstperc];
        $ptotigst = $pmain[totigst];
        $pnetamt = $pmain[netamt];

        $contents .= $ctr . ",";
        $contents .= $pinvno . ",";
        $contents .= $pmydt . ",";
        $contents .= $ssname . ",";
        $contents .= $psubtotal . ",";
        $contents .= $plitemdisc . ",";
        $contents .= $pcdiscperc . ",";
        $contents .= $pcdiscamt . ",";
        $contents .= $plesother . ",";
        $contents .= $ppacking . ",";
        $contents .= $ptotaxamt . ",";
        $contents .= $pcgstperc . ",";
        $contents .= $ptotcgst . ",";
        $contents .= $psgstper . ",";
        $contents .= $ptotsgst . ",";
        $contents .= $pigstperc . ",";
        $contents .= $ptotigst . ",";
        $contents .= $pnetamt . "\n";

        $ctr++;

        $totbill = $totbill + $pmain[netamt];
        $totamt = $totamt + $pmain[subtotal];
        $totdisc = $totdisc + $pmain[discount];

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


            $contents .= "Sr.No,Barcode,Item Name,HSN Code,Qty,Mrp,Rate,Amount,Disc%,Discount Amt,Total,Msp\n";



            $ctr2 = 1;
            $tdtqty = 0;
            $tamt = 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[itemcode]'")or die(mysqli_error($conn));
                $item = mysqli_fetch_array($getitem);

                $titemcode = $pmain[itemcode];
                $tname = $item[name];
                $thsncode = $tran[hsncode];
                $tqty = $tran[qty];
                $tmrp = $tran[mrp];
                $trate = $tran[rate];
                $tamount = $tran[amount];
                $tdiscperc = $tran[discperc];
                $tdiscount = $tran[discount];
                $ttotal = $tran[total];
                $tmsp = $tran[msp];

                $contents .= $ctr2 . ",";
                $contents .= $titemcode . ",";
                $contents .= $tname . ",";
                $contents .= $thsncode . ",";
                $contents .= $tqty . ",";
                $contents .= $tmrp . ",";
                $contents .= $trate . ",";
                $contents .= $tamount . ",";
                $contents .= $tdiscperc . ",";
                $contents .= $tdiscount . ",";
                $contents .= $ttotal . ",";
                $contents .= $tmsp . "\n";


                $ctr2++;


                $tdtqty = $tran[qty] + $tdtqty;
                $tdtamt = $tran[total] + $tdtamt;
            }
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= "Total" . ",";
            $contents .= $tdtqty . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= $tdtamt . "\n";
        }
//                                if ($_POST["dtype"] == 2) {
//                                    echo "<tr> ";
//                                    $colr1 = "blue";
//                                    echo "<td style='color:$colr1;' align='right' id='font1' colspan='6' ><h5><b>Total :</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='right' id='font1'><h5><b>$topaytot1</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='center' id='font1'><h5><b>$totweight</b></h5></td>";
//                                    echo "<td style='color:$colr1;'  id='font1'><h5><b>$totpaid</b></h5></td>";
//                                    echo "</tr>";
//                                }
    }
    $colr = "red";

    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $totbill . ",";



    $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;
    }
}
?>