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

$maindb = "kotmain";
$formname = "Daily Report";
$file = "dailyrep_fr.php";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$mtodate = trim($_POST["tdate"]);
$keycond = "`vouchdt` = '$mtodate' and billdone = 'Y'";

$comp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code = '1'")or die(mysqli_error($conn));


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

    $totsale = 0;

    if (empty($_POST["tdate"])) {
        browse("Date Cannot Be Empty");
    }
    $table = mysqli_query($conn, "SELECT SUM(cashamt) AS cashamt, SUM(ccamt) as cardamt, SUM(netamt) as totamt FROM `$maindb` WHERE $keycond and parcel = '' ")or die(mysqli_error($conn));
    $ftable = mysqli_fetch_array($table);

    $parcel = mysqli_query($conn, "SELECT SUM(cashamt) AS cashamt, SUM(ccamt) as cardamt, SUM(netamt) as totamt  FROM `$maindb` WHERE $keycond and parcel = 'Y'")or die(mysqli_error($conn));
    $fparcel = mysqli_fetch_array($parcel);

    $onlinesale = mysqli_query($conn, "SELECT  SUM(netamt) AS totamt FROM `$maindb` WHERE $keycond and delvcomp = '1'")or die(mysqli_error($conn));
    $fonlsl = mysqli_fetch_array($onlinesale);
    if ($fonlsl['totamt'] > 0) {
        $onlsl = $fonlsl['totamt'];
    } else {
        $onlsl = 0;
    }

    $gtable = mysqli_query($conn, "SELECT tableno FROM `$maindb` where $keycond and parcel = '' group by tableno order by tableno ")or die(mysqli_error($conn));

    $gtableno = mysqli_query($conn, "SELECT  tableno, SUM(cashamt) as totcash, SUM(ccamt) as totcard, 
                                   SUM(netamt) as totamt  FROM `$maindb` WHERE $keycond and parcel = '' GROUP BY tableno ORDER BY tableno")or die(mysqli_error($conn));

    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;
        }
        .bold{
            font-weight: bold; 
        }
        .f14{
            font-size: 14px;
        }
        .f15{
            font-size: 15px;
        }
        .f17{
            font-size: 17px;
        }
        .black{
            color: black;
        }
    </style>
    </head>
    <body>
        <div class="container fifty" >
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white; width: 100%;margin-bottom: 0px" class="table table-bordered table-condensed table-hover">

                        <tr class="mySearchTr">
                            <th style="width:25%" class="text-center bold"></th>
                            <th style="width:25%" class="text-right f15">Cash</th>
                            <th style="width:25%" class="text-right f15">Card</th>
                            <th style="width:25%" class="text-right f15">Total</th>
                        </tr>
                        <tr>
                            <td class="bold f15 black"><b>Table Sale</b></td>
                            <td class="text-right f14"><?= $ftable["cashamt"] ?></td>
                            <td class="text-right f14"><?= $ftable["cardamt"] ?></td>
                            <td class="text-right f14"><?= $ftable["totamt"] ?></td>
                        </tr>
                        <tr>
                            <td class="bold f15 black"><b>Parcel Sale</b></td>
                            <td class="text-right f14"><?= $fparcel["cashamt"] ?></td>
                            <td class="text-right f14"><?= $fparcel["cardamt"] ?></td>
                            <td class="text-right f14"><?= $fparcel["totamt"] ?></td>
                        </tr>
                        <tr>
                            <td class="bold f15 black"><b>Online Sale</b></td>
                            <td class="text-right f14" colspan="4">&nbsp;<?= $onlsl ?></td>
                        </tr>
                        <?php
                        $totsale = $ftable['totamt'] + $fparcel['totamt'] + $onlsl;
                        ?>
                        <tr>
                            <td class="bold f15 black"><b>Grand Total</b></td>
                            <td class="text-right bold f15" colspan="4">&nbsp;<?= $totsale; ?></td>
                        </tr>
                    </table>
                    <table class="table table-bordered" style="margin-bottom: 0px">
                        <tr class="mySearchTr">
                            <th class="bold f17" style="width:25%"><b>Table No.</b></th>
                            <th style="width:25%" class="text-right f17">Cash</th>
                            <th style="width:25%" class="text-right f17">Card</th>
                            <th style="width:25%" class="text-right f17">Total</th>
                        </tr>
                    </table>
                    <table class="table table-bordered" style="width:25%;float:left;">
                        <?php
                        while ($ftable = mysqli_fetch_object($gtable)) {
                            $tablename = mysqli_query($conn, "SELECT  name FROM `tablemast` where code = '$ftable->tableno'")or die(mysqli_error($conn));
                            $tablenm = mysqli_fetch_object($tablename);
                            ?>
                            <tr class="mySearchTr">
                                <td style="color:white" class="bold f14"><?= $tablenm->name; ?></std>
                            </tr>
                        <? } ?>
                        <tr class="mySearchTr">
                            <th>Total</th>
                        </tr>
                    </table>
                    <table class="table table-bordered" style="width:75%;float:right;background-color: white;">
                        <?php
                        $ctr = $totcash = $totcard = $totamt = 0;

                        while ($ftableno = mysqli_fetch_array($gtableno)) {
                            $ctr++;
                            ?>
                        
                            <tr>
                                <td class="text-right f14" style="width:25%"><?= $ftableno['totcash']; ?></td>
                                <td class="text-right f14" style="width:25%"><?= $ftableno['totcard']; ?></td>
                                <td class="text-right  f14" style="width:25%"><?= $ftableno['totamt']; ?></td>
                            </tr>
                            <?
                            $totcash = $totcash + $ftableno['totcash'];
                            $totcard = $totcard + $ftableno['totcard'];
                            $totamt = $totamt + $ftableno['totamt'];
                        }
                        ?>
                        <tr >
                            <td class="text-right bold f15 "><?= $totcash; ?></td>
                            <td class="text-right bold f15 "><?= $totcard; ?></td>
                            <td class="text-right bold f15 "><?= $totamt; ?></td>
                        </tr>
                    </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;
    }
}
?>