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

$maindb = "amtrqstmn";
$trandb = "amtrqsttn";
$formname = "Amount Request Register";
$file = "amtreqreg_fr.php";

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

    $acname = $_POST["acname"];
    $karigar = $_POST["karigar"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $scope = $_POST["scope"];

    $dtypecond = "";
    $mscopecond = "";

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

    $scpcond = "";

    if (!empty($scope)) {
        if ($scope == 'P') {
            $scpcond = " and `recddate`='0000-00-00'";
        } else {
            $scpcond = " and `recddate`!='0000-00-00'";
        }
    }




    if (!empty($karigar)) {
        $karcond = " AND `karigar`='$karigar' ";
        $karcond1 = " AND b.`karigar`='$karigar' ";
    }
    if (!empty($acname)) {
        $acnamecond = " AND `party`='$acname' ";
        $acnamecond1 = " AND b.`party`='$acname' ";
    }
//    if (!empty($itemname)) {
//        $itemnamecond = " AND `itemname `='$itemname ' ";
//    }

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

//    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate' 
//                            AND a.`vouchdt` <= '$mtodate' and a.vouchno = b.vouchno $acnamecond1 $karcond1 $scpcond")or die(mysqli_error($conn));
    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, DATE_FORMAT(`recddate`, '%d-%m-%Y') as myrecd
                             FROM `$trandb` WHERE `vouchdt` >= '$mfrmdate' 
                             AND `vouchdt` <= '$mtodate' $acnamecond $karcond $scpcond")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-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style='background-color: #cccccc;width:5%;'>Sr</th>
                                <th style='background-color: #cccccc;width:10%;'>Req. Date</th>
                                <th style='background-color: #cccccc;width:25%;'>Customer</th>
                                <th style='background-color: #cccccc;width:6%;' class='numeric'>Amount</th>
                                <th style='background-color: #cccccc';width:25%; >Karigar</th>
                                <th style='background-color: #cccccc;width:15%;' >A/c No.</th>
                                <th style='background-color: #cccccc;width:10%;'>Recd. Date</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

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

                                $ctr++;
                                $getitem = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                $item = mysqli_fetch_array($getitem);

                                $getcolor = mysqli_query($conn, "SELECT `accode`,`name` from `accmast` WHERE `accode`='$pmain[karigar]' ")or die();
                                $color = mysqli_fetch_array($getcolor);
                                $ctr2++;
                                Echo "<tr>";
                                Echo "<td>$ctr2</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$item[name]</td>";
                                Echo "<td class='numeric'>$pmain[amount]</td>";
                                Echo "<td>$color[name]</td>";
                                Echo "<td >$pmain[acno]</td>";
                                Echo "<td >$pmain[myrecd]</td>";
                                Echo "</tr>";

                                $tamt = $pmain[amount] + $tamt;
                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<th class='text-right' style='color: red;'>Grand Total</td>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($tamt,2) ."</td>";
                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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