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

$maindb = "hallbook";
$formname = "Pending Hall Amount Register";
$file = "pendhallbookreg_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];

$fseries = $_SESSION["series_" . $dbname];
$keycond23 = " `series`='$fseries'";

mysqli_select_db($conn, $zdb);

$eventdt = $_REQUEST["eventdt"];
$hallname = $_POST["hallname"];
$partyname = $_REQUEST["party"];
$staff = $_REQUEST["staffs"];

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

$datecond = "";
$eventdatecond = "";
$hallcond = "";
$partycond = "";
$staffcond = "";


if (empty($_POST["fromdt"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

if (!empty($vouchdt)) {
    $datecond = " AND `vouchdt`='$vouchdt' ";
}
if (!empty($eventdt)) {
    $eventdatecond = " AND `eventdt`<='$eventdt ' ";
}

if (!empty($hallname)) {
    $hallcond = " AND `hall`='$hallname' ";
}

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

if (!empty($staff)) {
    $staffcond = " AND `staff`='$staff' ";
}


$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt,DATE_FORMAT(`eventdt`, '%d-%m-%Y')as eventdt 
                                        FROM $maindb WHERE vouchno != '' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' AND balance > 0 
                                        AND $keycond23  $eventdatecond $hallcond $partycond $staffcond ")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);


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

    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">
            <?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">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:2%">Voucher</th>
                                <th style="width:8%">Date</th>
                                <th style="width:10%">Hall</th>
                                <th style="width:8%">Event Date</th>
                                <th style="width:27%;">Party/Address</th>
                                <th style="width:8%;">Mobile</th>
                                <th style="width:8%;" class="text-right">Amount</th>
                                <th style="width:8%;" class="text-right">Advance</th>
                                <th style="width:8%;" class="text-right">Balance</th>
                                <th style="width:15%;">Booked By</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {


                                $queryhall = mysqli_query($conn, "SELECT * FROM `hallname` WHERE code ='$pmain[hall]'") or die(mysqli_error($conn));
                                $fetqueryhall = mysqli_fetch_object($queryhall);
                                $hall = $fetqueryhall->name;

                                $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[vouchdt]</td>";
                                echo "<td>$hall</td>";
                                echo "<td>$pmain[eventdt]</td>";
                                echo "<td><span style='color:#B01542'>$pmain[name]</span><br>$address</td>";
                                echo "<td>$pmain[mobile]</td>";
                                echo "<td style='text-align:right'>$pmain[amount]</td>";
                                echo "<td style='text-align:right'>$pmain[advance]</td>";
                                echo "<td style='text-align:right'>$pmain[balance]</td>";
                                echo "<td>$pmain[staff]</td>";

                                $mtamt = $pmain['amount'] + $mtamt;
                                $mgtot = $pmain['advance'] + $mgtot;
                                $mgbal = $pmain['balance'] + $mgbal;

                                echo "</tr>";

                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td class='text-right' style='color: red;'>Grand Total</td>";
                            echo "<td class='text-right' style='color: red;'>$mtamt</td>";
                            echo "<td class='text-right' style='color: red;'>$mgtot</td>";
                            echo "<td class='text-right' style='color: red;'>$mgbal</td>";
                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}
if (isset($_POST["excel"])) {
    $itemcode = $_POST["itemcode"];
    $itemname = $_POST["itemname"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];

    $dtypecond = "";

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




    if (!empty($itemcode)) {
        $itemcond = " AND `itemcode`='$itemcode' ";
    }
    //    if (!empty($itemname)) {
    //        $itemnamecond = " AND `itemname `='$itemname ' ";
    //    }

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

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' ") or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

    $contents .= "Sr.,Voucher no.,Date,Qty,Total,Remark\n";

    $ctr = 0;
    $mtqty = 0;
    $mgtot = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $ctr++;
        $pvouchno = $pmain[vouchno];
        $pdate = $pmain[vouchdt];
        $pqty = $pmain[qty];
        $ptotal = $pmain[total];
        $premark = $pmain[remark] . $pmain[remark2];

        $contents .= $ctr . ",";
        $contents .= $pvouchno . ",";
        $contents .= $pdate . ",";
        $contents .= $pqty . ",";
        $contents .= $ptotal . ",";
        $contents .= $premark . "\n";

        $mtqty = $pmain[qty] + $mtqty;
        $mgtot = $pmain[total] + $mgtot;

        $contents .= "Sr.,Barcode,Itemname,Qty,Unit,Mrp,Rate,Amount,Msp\n";

        $ctr2 = 0;
        $tqty = 0;
        $tamt = 0;

        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $itemcond $itemnamecond  ") 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++;
            $pitemcode = $tran[itemcode];
            $pitemname = $item[name];
            $ptrqty = $tran[trqty];
            $punit = $tran[unit];
            $pmrp = $tran[mrp];
            $prate = $tran[rate];
            $pamount = $tran[amount];
            $pmsp = $tran[msp];

            $contents .= $ctr2 . ",";
            $contents .= $pitemcode . ",";
            $contents .= $pitemname . ",";
            $contents .= $ptrqty . ",";
            $contents .= $punit . ",";
            $contents .= $pmrp . ",";
            $contents .= $prate . ",";
            $contents .= $pamount . ",";
            $contents .= $pmsp . "\n";
        }
        $tqty = $tran[trqty] + $tqty;
        $tamt = $tran[amount] + $tamt;

        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= "$tqty" . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "$tamt" . ",";
        $contents .= " " . "\n";
    }
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$mtqty" . ",";
    $contents .= "$mgtot" . ",";
    $contents .= " " . "\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;
    }
}
?>