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

$formname = "Online Payment";
$file = "online_fr.php";

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

    $delvcomp = $_POST["delvcomp"];
    $delvcmpcond = "";

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

    if (!empty($delvcomp)) {
        $delvcmpcond = " AND `delvcomp`='$delvcomp' ";
    }

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

    $mainqry = mysqli_query($conn, "SELECT vouchno, SUM(netamt) as totamt, delvcomp FROM `kotmain`
                                    WHERE parcel = 'Y' and `vouchdt` >= '$mfrmdate' and 
                                   `vouchdt` <= '$mtodate' $delvcmpcond GROUP BY delvcomp 
                                    order by totamt desc ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getitemgrp);
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

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

    include("includes/linkshead.php");
    ?>
    </head>
    <body>
        <!--<div class="container" style="width:550px">-->
        <div class="container fifty" >
            <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:10%">Sr.</th>
                                <th style="width:75%">Company</th>
                                <th style="width:15%" class="text-right pad">Amont</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $grtotamt = 0;
                            while ($fmain = mysqli_fetch_object($mainqry)) {

                                $paymode = mysqli_query($conn, "SELECT name from delvcomp where code = '$fmain->delvcomp'")or die(mysqli_error($conn));
                                $fpaymode = mysqli_fetch_object($paymode);
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$fpaymode->name</td>";
                                Echo "<td class='text-right'>$fmain->totamt</td>";
                                Echo "</tr>";
                                $grtotamt = $grtotamt + $fmain->totamt;

                                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-bordered'>";
                                    Echo "<tr style='background-color: #cccccc;' >";
                                    Echo "<th style='width:7%;'>Sr</th>";
                                    Echo "<th style='width:13%;'>Date</th>";
                                    Echo "<th style='width:10%;'>Bill No.</th>";
                                    Echo "<th style='width:50%;'>Party</th>";
                                    Echo "<th style='width:20%;' class='text-right dth numeric'>Amount</th>";
                                    Echo "<tr>";
                                    $ctr2 = 0;
                                    $dtypeqry = mysqli_query($conn, "SELECT custname, DATE_FORMAT(`vouchdt` , '%d-%m-%Y') as vouchdt,
                                                                     vouchno, `netamt` from `kotmain` 
                                                                     where parcel = 'Y'  and `vouchdt` >= '$mfrmdate'
                                                                     and `vouchdt` <= '$mtodate' and delvcomp = '$fmain->delvcomp' ")or die(mysqli_error($conn));
                                    while ($fdetail = mysqli_fetch_object($dtypeqry)) {
                                        $ctr2++;

                                        if ($fdetail->custname == '') {
                                            $custname = "WALKIN CUSTOMER";
                                        } else {
                                            $custname = "$fdetail->custname";
                                        }

                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td >$fdetail->vouchdt</td>";
                                        Echo "<td >$fdetail->vouchno</td>";
                                        Echo "<td class=' dth' >$custname</td>";
                                        Echo "<td class='numeric text-right dth'>$fdetail->netamt</td>";
                                        Echo "</tr>";
                                    }
                                    Echo "<tr style='background-color: lightgray'>";
                                    Echo "<th style='text-align:right;color:blue;' colspan='4'>Total</th>";
                                    Echo "<th style='text-align:right;color:blue; '>" . round($fmain->totamt). "</td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }
                            ?>

                            <tr>
                                <td colspan="2" class="text-right" style="font-size:15px"><b>Total</b></td>
                                <td class="text-right" style="font-size:15px"><b><?= round($grtotamt); ?></b></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr.,Company, Amount\n";
    $ctr = 0;
    $grtotamt = 0;
    while ($fmain = mysqli_fetch_object($mainqry)) {

        $paymode = mysqli_query($conn, "SELECT name from delvcomp where code = '$fmain->delvcomp'")or die(mysqli_error($conn));
        $fpaymode = mysqli_fetch_object($paymode);
        $ctr++;


        $contents .= $ctr . ",";
        $contents .= $fpaymode ->name . ",";
        $contents .= $fmain->totamt . "\n";
        
        $grtotamt = $grtotamt + $fmain->totamt;

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

            $contents.= "Sr, Date,Bill No., Party, Amount\n";
            $ctr2 = 0;
          $dtypeqry = mysqli_query($conn, "SELECT custname, DATE_FORMAT(`vouchdt` , '%d-%m-%Y') as vouchdt,
                                                                     vouchno, `netamt` from `kotmain` 
                                                                     where parcel = 'Y'  and `vouchdt` >= '$mfrmdate'
                                                                     and `vouchdt` <= '$mtodate' and delvcomp = '$fmain->delvcomp' ")or die(mysqli_error($conn));
            while ($fdetail = mysqli_fetch_object($dtypeqry)) {
                $ctr2++;

                if ($fdetail->custname == '') {
                    $custname = "WALKIN CUSTOMER";
                } else {
                    $custname = "$fdetail->custname";
                }

                $contents.= $ctr2 . "," ;
                $contents.= $fdetail->vouchdt . "," ;
                $contents.= $fdetail->vouchno . "," ;
                $contents.= $custname . "," ;
                $contents.= $fdetail->netamt . "\n" ;
            }
            $contents.= "," . "," ;
            $contents.= "Total" . "," ;
            $contents.= (round($fmain->totamt)) . "\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;
}
}
?>