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

$maindb = "discgive";
//$trandb = "samptran";
$formname = "Discount Given Register";
$file = "discgvreg_fr.php";

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

$name = $_POST["pname"];
$dtypecond = "";
$namecond = "";

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

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

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 vouchno != '' 
                         AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $namecond ")or die(mysqli_error($conn));
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>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container"style="width:800px">  

            <div class="row">
                            <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%;color:black">Sr</th>
                                <th style="width:15%;color:black">Date</th>
                                <th style="width:10%;color:black">Voucher</th>
                                <th style="width:50%;color:black">Given To</th>
                                <th style="width:15%;color:black" class="text-right">Amount</th>
                                <!--<th style="width:25%;color:black" class="">Remark</th>-->
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $total = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getname = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[name]'")or die(mysqli_error($conn));
                                $gname = mysqli_fetch_array($getname);
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td class=''>$gname[name]</td>";
                                Echo "<td class='text-right'>$pmain[amount]</td>";
//                        Echo "<td class=''>$pmain[narr1] $pmain[narr2]</td>";
                                Echo "</tr>";
//                                $totqty = $totqty + $pmain[qty];
                                $total = $total + $pmain[amount];
                            }
                            $colr = "red";
                            echo "<tr> ";
                            echo "<td> </td>";

                            echo "<td align='right' style='color:$colr;' id='font' colspan='3'><h4><b>Grand Total</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totqty</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$total</b></h4></td>";
//                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>

    = 
    <?php
    include("includes/head_rep_excel.php");
    $contents.="Sr,Voucher,Date,Name,Amount,Remark\n";
    ?>

    <?php
    $ctr = 0;
    $totqty = 0;
    $total = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getname = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[name]'")or die(mysqli_error($conn));
        $gname = mysqli_fetch_array($getname);
        $ctr++;
        $contents.="$ctr" . ",";
        $contents.="$pmain[mydt]" . ",";
        $contents.="$pmain[vouchno]" . ",";
        $contents.="$gname[name]" . ",";
        $contents.="$pmain[amount]" . ",";
        $contents.="$pmain[narr1]" . "\n";
        $total = $total + $pmain[amount];
    }
    $contents.="" . ",";
    $contents.="Grand Total" . ",";
    $contents.="$total" . ",";

    $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;
    }
}

