<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
$maindb = "kotmain";
$trandb = "kottran";
$formname = "Sale Register";
$file = "salereg_fr.php";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
if (isset($_POST["view"])) {
    $table = $_POST["table"];
    $staff = $_POST["staff"];
    $dtype = $_POST["dtype"];
    $dtypecond = "";
    $tablecond = "";
    $staffcond = "";
    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);
    if (!empty($table)) {
        $tablecond = " AND `tableno`='$table' ";
    } if (!empty($staff)) {
        $staffcond = " AND `user`='$staff' ";
    } if (!empty($dtype)) {
        $dtypecond = " AND `dtype`='$dtype' ";
    } if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
    } if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
    } $getkot = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate'                             AND `vouchdt` <= '$mtodate' $tablecond $staffcond")or die(mysqli_error($conn));
    $fetch = mysqli_fetch_array($getkot);
    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>    </head>    <body>        <div class="container" style="width:90%">            <div class="row" style="background-color: #b9408f;">    <?php include("includes/head_rep.php"); ?>            </div>            <div class="row">                <div class="col-lg-12 pad">                    <table style="background: white; width: 100%;" class="table table-bordered table-condensed table-hover">                        <thead class="mySearchTr">                            <tr  style="background-color: #b9408f;">                                <th style="width:5%">Voucher</th>                                <th style="width:8%">Date</th>                                <th style="width:25%">Staff</th>                                <th style="width:10%">Table</th>                                <th style="width:5%" class="text-right dth">Subtotal</th>                                <th style="width:4%" class="text-right dth">Disc%</th>                                <th style="width:4%" class="text-right dth">DiscAmt</th>                                <th style="width:6%" class="text-right dth">Taxable</th>                                <th style="width:5%" class="text-right dth">CGST%</th>                                <th style="width:9%" class="text-right dth">CGST Amt</th>                                <th style="width:5%" class="text-right dth">SGST%</th>                                <th style="width:9%" class="text-right dth">SGST Amt</th>                                <th style="width:8%" class="text-right dth">Net.Amt</th>                            </tr>                        </thead>                        <tbody>    <?php
    $ctr = 0;
    $totsub = 0;
    $totdisc = 0;
    $tdiscamt = 0;
    $tottax = 0;
    $tcgstamt = 0;
    $tsgstamt = 0;
    $totnet = 0;
    while ($kot = mysqli_fetch_array($getkot)) {
        $gettable = mysqli_query($conn, "SELECT `code`,`name` FROM `tablemast` WHERE `code`='$kot[tableno]'")or die(mysqli_error($conn));
        $ftable = mysqli_fetch_array($gettable);
        $tablename = $ftable[name];
//        browse($kot[vouchno]);
        $ctr++;
        Echo "<tr id='boldfont'>";
        Echo "<td>$kot[vouchno]</td>";
        Echo "<td>$kot[mydt]</td>";
        Echo "<td>$kot[user]</td>";
        Echo "<td>$tablename</td>";
        Echo "<td class='text-right dth'>$kot[subtotal]</td>";
        Echo "<td class='text-right dth'>$kot[discperc]</td>";
        Echo "<td class='text-right dth'>$kot[discamt]</td>";
        Echo "<td class='text-right dth'>$kot[taxable]</td>";
        Echo "<td class='text-right dth'>$kot[cgstperc]</td>";
        Echo "<td class='text-right dth'>$kot[cgstamt]</td>";
        Echo "<td class='text-right dth'>$kot[sgstperc]</td>";
        Echo "<td class='text-right dth'>$kot[sgstamt]</td>";
        Echo "<td class='text-right dth'>$kot[netamt]</td>";
        Echo "</tr>";
        $totsub = $totsub + $kot[subtotal];
        $totdisc = $totdisc + $kot[discperc];
        $tdiscamt = $tdiscamt + $kot[discamt];
        $tottax = $tottax + $kot[taxable];
        $totnet = $totnet + $kot[netamt];
        $tcgstamt = $tcgstamt + $kot[cgstamt];
        $tsgstamt = $tsgstamt + $kot[sgstamt];
        if ($_POST["dtype"] == 2) {
            Echo "<tr style='height:10px;'>";
            Echo "<td colspan='25' style=' padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;'>";
            Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-bordered'>";
            Echo "<th style='background-color: #cccccc;width:6%;'>Sr</th>";
            Echo "<th style='background-color: #cccccc';width:5%; >Itemname</th>";
            Echo "<th style='background-color: #cccccc;width:8%;' class='text-right dth numeric'>Qty</th>";
            Echo "<th style='background-color: #cccccc;width:8%;' class='text-right dth numeric'>Rate</th>";
            Echo "<th style='background-color: #cccccc;width:12%;' class='text-right dth numeric'>Amount</th>";
            Echo "<tr>";
            $ctr2 = 0;
            $tqty = 0;
            $trate = 0;
            $tamt = 0;
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$kot[vouchno]'")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {
                $itemqry = mysqli_query($conn, "SELECT `name` FROM item WHERE code = '$tran[item]'")or die(mysqli_error());
                $fitem = mysqli_fetch_array($itemqry);
                $itemname = $fitem[name];
                $ctr2++;
                Echo "<tr>";
                Echo "<td>$ctr2</td>";
                Echo "<td >$itemname</td>";
                Echo "<td class='numeric text-right dth' width='3%'>$tran[qty]</td>";
                Echo "<td class='numeric text-right dth width='3%'>$tran[rate]</td>";
                Echo "<td class='numeric text-right dth'>$tran[amount]</td>";
                Echo "</tr>";
                $tqty = $tran[qty] + $tqty;
                $tamt = $tran[amount] + $tamt;
                $trate = $tran[rate] + $trate;
            } Echo "<tr style='background-color: lightgray'>";
            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 "<th style='text-align:right;color:blue; '></th>";
            Echo "<th style='text-align:right;color:blue; '>$tamt</td>";
            Echo "</tr>";
            Echo "</table>";
            Echo "</td>";
        }
    } $colr = "red";
    echo "<tr>";
    echo "<td colspan='4' align='right' style='color:$colr;'><h4><b>Total</b></h4></td>";
    echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totsub</b></h4></td>";
    echo "<td align='right' style='color:$colr;' id='font'><h4><b>$tdiscamt</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>$tottax</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>$tcgstamt</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>$tsgstamt</b></h4></td>";
    echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totnet</b></h4></td>";
    echo "</tr>";
    ?>                        </tbody>                    </table>                </div>            </div>        </div>    <?php include("includes/linksfoot.php"); ?>    </body>    </html>    <?php
}if (isset($_POST["excel"])) {
    $contents.="Sr.,Voucher.,Date,Staff,Table,Subtotal,Disc%,DisAmt,Taxable,CGST%,CGSTAmt%,SGST%,SGSTAmt%,NetAmt.\n";
    $ctr = 0;
    $totsub = 0;
    $totdisc = 0;
    $tdiscamt = 0;
    $tottax = 0;
    $tcgstamt = 0;
    $tsgstamt = 0;
    $totnet = 0;
    while ($kot = mysqli_fetch_array($getkot)) {
        $gettable = mysqli_query($conn, "SELECT `code`,`name` FROM `tablemast` WHERE `code`='$kot[tableno]'")or die(mysqli_error($conn));
        $ftable = mysqli_fetch_array($gettable);
        $tablename = $ftable[name];
        $ctr++;
        $pvoucher = $kot[voucher];
        $pdate = $kot[mydt];
        $puser = $kot[user];
        $ptable = $tablename;
        $psubtot = $kot[subtotal];
        $pdisc = $kot[discperc];
        $pdamt = $kot[discamt];
        $ptax = $kot[taxable];
        $pcgst = $kot[cgstperc];
        $pcamt = $kot[cgstamt];
        $psgst = $kot[sgstperc];
        $psamt = $kot[sgstamt];
        $pnet = $kot[netmat];
        $contents.=$ctr . ",";
        $contents.=$pvoucher . ",";
        $contents.=$pdate . ",";
        $contents.=$puser . ",";
        $contents.=$ptable . ",";
        $contents.=$psubtot . ",";
        $contents.=$pdisc . ",";
        $contents.=$pdamt . ",";
        $contents.=$ptax . ",";
        $contents.=$pcgst . ",";
        $contents.=$pcamt . ",";
        $contents.=$psgst . ",";
        $contents.=$psamt . ",";
        $contents.=$pnet . "\n";
        $totsub = $totsub + $kot[subtotal];
        $totdisc = $totdisc + $kot[discperc];
        $tdiscamt = $tdiscamt + $kot[discamt];
        $tottax = $tottax + $kot[taxable];
        $totnet = $totnet + $kot[netamt];
        $tcgstamt = $tcgstamt + $kot[cgstamt];
        $tsgstamt = $tsgstamt + $kot[sgstamt];
        if ($_POST["dtype"] == 2) {
            $ctr2 = 0;
            $tqty = 0;
            $trate = 0;
            $tamt = 0;
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$kot[vouchno]'")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {
                $itemqry = mysqli_query($conn, "SELECT `name` FROM item WHERE code = '$tran[item]'")or die(mysqli_error());
                $fitem = mysqli_fetch_array($itemqry);
                $itemname = $fitem[name];
                $ctr2++;
                $pitemname = $tran[itemname];
                $pqty = $tran[qty];
                $prate = $tran[rate];
                $pamount = $tran[amount];
                $contents.=$ctr2 . ",";
                $contents.=$pitemname . ",";
                $contents.=$pqty . ",";
                $contents.=$prate . ",";
                $contents.=$pamount . "\n";
                $tqty = $tran[qty] + $tqty;
                $tamt = $tran[amount] + $tamt;
                $trate = $tran[rate] + $trate;
            } $contents.=" " . ",";
            $contents.="Total" . ",";
            $contents.="$tqty" . ",";
            $contents.=" " . ",";
            $contents.="$tamt" . "\n";
        }
    } $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.="Total" . ",";
    $contents.=$totsub . ",";
    $contents.=" " . ",";
    $contents.=$tdiscamt . ",";
    $contents.=$tottax . ",";
    $contents.=" " . ",";
    $contents.=$tcgstamt . ",";
    $contents.=" " . ",";
    $contents.="$tsgstamt" . ",";
    $contents.=$totnet . "\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;
    }
}?>