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

$formname = "Discount Sale";
$file = "discsale_fr.php";

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


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

    $itemgrp = $_POST["itemgrp"];
    $item = $_POST["item"];

    $itemgrpcond = "";
    $itemcond = "";

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

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

    if (!empty($itemgrp)) {
        $itemgrpcond = " AND `itemgrp`='$itemgrp' ";
    }

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

//    $getdisc = mysqli_query($conn, "SELECT vouchno, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as vouchdt, item, qty, rate, amount, cdiscperc, cdiscamt, total  FROM `kottran`
//                                       WHERE `vouchdt`>='$mfrmdate' and `vouchdt`<='$mtodate' $itemcond order by vouchno")or die(mysqli_error($conn));
    $getdisc = mysqli_query($conn, "SELECT a.vouchno, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt, a.item, a.qty, a.rate, a.amount,
                                       a.cdiscperc, a.cdiscamt, a.total, b.code, b.name as itemname, b.itemgrp FROM `kottran` a, item b
                                       WHERE a.cdiscperc!='' and a.`vouchdt`>='$mfrmdate' and a.`vouchdt`<='$mtodate' and a.item = b.code $itemcond $itemgrpcond order by a.vouchno")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getdisc);

    include("includes/linkshead.php");

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    </head>
    <body>
        <div class="container sixty">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr.</th>
                                <th style="width:10%">Date</th>
                                <th style="width:5%">Voucher</th>
                                <th style="width:28%">Item Name</th>
                                <th style="width:8%" class="text-right pad">Qty.</th>
                                <th style="width:12%" class="text-right pad">Act. Rate</th>
                                <th style="width:8%" class="text-right pad">Disc %</th>
                                <th style="width:10%" class="text-right pad">Disc Amt.</th>
                                <th style="width:8%" class="text-right pad">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 1;
                            $tqty = 0;
                            $tcdiscamt = 0;
                            $ttotal = 0;
                            while ($get = mysqli_fetch_array($getdisc)) {

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

                                $total = $get[rate] - $get[cdiscamt];
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$get[vouchdt]</td>";
                                Echo "<td>$get[vouchno]</td>";
                                Echo "<td>$get[itemname]</td>";
                                Echo "<td style='text-align:right'>$get[qty]</td>";
                                Echo "<td style='text-align:right'>$get[rate]</td>";
                                Echo "<td style='text-align:right'>$get[cdiscperc]</td>";
                                Echo "<td style='text-align:right'>$get[cdiscamt]</td>";
                                Echo "<td style='text-align:right'>$get[total]</td>";
                                Echo "</tr>";
                                $ctr++;

                                $tqty = $tqty + $get[qty];
                                $tcdiscamt = $tdiscamt + $get[cdiscamt];
                                $ttotal = $ttotal + $get[total];
                            }
                            echo "<tr>";
                            echo "<td colspan = '4' style='text-align:right'><b>Total</b></td>";
                            echo "<td style='text-align:right'><b>$tqty</b></td>";
                            echo "<td style='text-align:right'></td>";
                            echo "<td style='text-align:right'></td>";
                            echo "<td style='text-align:right'><b>$tcdiscamt</b></td>";
                            echo "<td style='text-align:right'><b>$ttotal</b></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
//if (isset($_POST["excel"])) {
//    $party = $_POST["party"];
//    $dtype = $_POST["dtype"];
//    $scope = $_POST["scope"];
//
//    $dtypecond = "";
//    $mscopecond = "";
//
//    $mfrmdate = trim($_POST["fdate"]);
//    $mtodate = trim($_POST["tdate"]);
//
//
//
//    if (!empty($party)) {
//        $partycond = " AND `party`='$party' ";
//    }
//
//    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 *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
//                            AND `vouchdt` <= '$mtodate'  $partycond $mscopecond ")or die(mysqli_error($conn));
//    $get = mysqli_num_rows($getpmain);
//
//    $contents.="Sr.,Voucher.,Bill No,Date,Party,GST No,Subtotal,Disc%,Discount,Taxable,GST3%,GST5%,GST12%,GST28%,Roundoff,NetAmt.\n";
//
//    $ctr = 0;
//    $totsub = 0;
//    $totdisc = 0;
//    $totbill = 0;
//    $tottax = 0;
//    $totround = 0;
//    $totbill = 0;
//
//    while ($pmain = mysqli_fetch_array($getpmain)) {
//        $getparty = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
//        $fparty = mysqli_fetch_array($getparty);
//
//
//        $gst3 = $pmain["totgst3"];
//        $gst5 = $pmain["totgst5"];
//        $gst12 = $pmain["totgst12"];
//        $gst18 = $pmain["totgst18"];
//        $gst28 = $pmain["totgst28"];
//        $party = $fparty["name"];
//        $subtot = $pmain["subtotal"];
//        $discamt = $pmain["discamt"];
//        $taxable = $pmain["taxableamt"];
//        $roundoff = $pmain["roundoff"];
//        $billamt = $pmain["billamt"];
//
//
//        $ctr++;
//        $pvoucher = $pmain[voucher];
//        $pinvo = $pmain[invno];
//        $pdate = $pmain[mydt];
//        $pname = $party;
//        $pgst = $pmain[gstno];
//        $psubtot = $subtot;
//        $pdiscamt = $discamt;
//        $ptaxable = $taxable;
//        $proundoff = $roundoff;
//        $pbill = $billamt;
//
//
//        $contents.=$ctr . ",";
//        $contents.=$pvoucher . ",";
//        $contents.=$pinvno . ",";
//        $contents.=$pdate . ",";
//        $contents.=$pname . ",";
//        $contents.=$pgst . ",";
//        $contents.=$psubtot . ",";
//        $contents.=$pdiscamt . ",";
//        $contents.=$ptaxable . ",";
//        $contents.=$gst3 . ",";
//        $contents.=$gst5 . ",";
//        $contents.=$gst12 . ",";
//        $contents.=$gst18 . ",";
//        $contents.=$gst28 . ",";
//        $contents.=$proundoff . ",";
//        $contents.=$pbill . "\n";
//
//
//        $totsub = $totsub + $subtot;
//        $totdisc = $totdisc + $pmain[discamt];
//        $tottax = $tottax + $taxable;
//        $totround = $totround + $roundoff;
//        $totbill = $totbill + $billamt;
//        $grtotgst3 = $grtotgst3 + $gst3;
//        $grtotgst5 = $grtotgst5 + $gst5;
//        $grtotgst12 = $grtotgst12 + $gst12;
//        $grtotgst18 = $grtotgst18 + $gst18;
//        $grtotgst28 = $grtotgst28 + $gst28;
//
//
//        if ($dtype == 2) {
//
//            $ctr2 = 0;
//            $tqty = 0;
//            $tamt = 0;
//
//            $contents.= "Sr.,Itemname,Hsncode,Unit,Qty,Rate,Amount,Dis%,DiscAmt,TaxableValue,CGSTRate,CGSTAmt,SGSTRate,SGSTAmt,IGSTRate,IGSTAmt,Total\n";
//
//            $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`='$tran[itemcode]'")or die(mysqli_error($conn));
//                $item = mysqli_fetch_array($getitem);
//
//                $ctr2++;
//                $pitemname = $tran[itemname];
//                $phsncode = $tran[hsncode];
//                $punit = $tran[unit];
//                $pqty = $tran[qty];
//                $prate = $tran[rate];
//                $pamount = $tran[amount];
//                $pdiscperc = $tran[discperc];
//                $pdiscount = $tran[discount];
//                $psubtot = $tran[subtot];
//                $pcgstrate = $tran[cgstrate];
//                $pcgstamt = $tran[cgstamt];
//                $psgstrate = $tran[sgstrate];
//                $psgstamt = $tran[sgstamt];
//                $pigstrate = $tran[igstrate];
//                $pigstamt = $tran[igstamt];
//                $ptotal = $tran[total];
//
//                $contents.=$ctr2 . ",";
//                $contents.=$pitemname . ",";
//                $contents.=$phsncode . ",";
//                $contents.=$punit . ",";
//                $contents.=$pqty . ",";
//                $contents.=$prate . ",";
//                $contents.=$pamount . ",";
//                $contents.=$pdiscperc . ",";
//                $contents.=$pdiscount . ",";
//                $contents.=$psubtot . ",";
//                $contents.=$pcgstrate . ",";
//                $contents.=$pcgstamt . ",";
//                $contents.=$psgstrate . ",";
//                $contents.=$psgstamt . ",";
//                $contents.=$pigstrate . ",";
//                $contents.=$pigstamt . ",";
//                $contents.=$ptotal . "\n";
//
//                $tqty = $tran[qty] + $tqty;
//                $tamt = $tran[amount] + $tamt;
//                $tdisc = $tran[discount] + $tdisc;
//                $ttax = $tran[subtot] + $ttax;
//                $tcgst = $tran[cgstamt] + $tcgst;
//                $tsgst = $tran[sgstamt] + $tsgst;
//                $tigst = $tran[igstamt] + $tigst;
//                $ttot = $tran[total] + $ttot;
//            }
//
//            $contents.=" " . ",";
//            $contents.=" " . ",";
//            $contents.=" " . ",";
//            $contents.="Total" . ",";
//            $contents.="$tqty" . ",";
//            $contents.=" " . ",";
//            $contents.="$tamt" . ",";
//            $contents.=" " . ",";
//            $contents.="$tdisc" . ",";
//            $contents.="$ttax" . ",";
//            $contents.=" " . ",";
//            $contents.="$tcgst" . ",";
//            $contents.=" " . ",";
//            $contents.="$tsgst" . ",";
//            $contents.=" " . ",";
//            $contents.="$tigst" . ",";
//            $contents.="$ttot" . "\n";
//        }
//    }
//    $contents.=" " . ",";
//    $contents.=" " . ",";
//    $contents.=" " . ",";
//    $contents.=" " . ",";
//    $contents.=" " . ",";
//    $contents.="Total" . ",";
//    $contents.=$totsub . ",";
//    $contents.=$totdisc . ",";
//    $contents.=$tottax . ",";
//    $contents.=$grtotgst3 . ",";
//    $contents.=$grtotgst5 . ",";
//    $contents.=$grtotgst12 . ",";
//    $contents.=$grtotgst18 . ",";
//    $contents.=$grtotgst28 . ",";
//    $contents.=$totround . ",";
//    $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;
//    }
//}
?>