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

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

$maindb = 'purfuelmain';
$trandb = 'purfueltran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Purchase Normal Register';
$zroot = $_SESSION["zdir"];
$file = 'purreg_fr.php';


$account = $_POST["account"];
$dtype = $_POST["dtype"];
$type = $_POST["type"];
$purtype = $_POST["purtype"];


$accond = "";
if (!empty($account)) {
$accond = "AND `party` = '$account' " ;    
}
$$typecond = "";
if (!empty($type)) {
$typecond = "AND `type` = '$type' " ;    
}
$purtcond = "";
if (!empty($purtype)) {
$purtcond = "AND `purtype` = '$purtype' " ;    
}

if (empty($_POST["frmdt"])) {
    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>";
}

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

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


    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $accond $typecond $purtcond ORDER BY invno")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    
if (isset($_POST["view"])) {
    
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #6082BF;
        }

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php");   ?>
            <div class="container">
                <div class="row">
                    <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%">Invoice</th>
                                    <th style="width:5%">Date</th>
                                    <th style="width:20%">Supplier</th>
                                    <th style="width:5%; text-align:right;">Amount</th>
                                    <th style="width:5%; text-align:right;">Discount</th>
                                    <th style="width:5%; text-align:right;">V.A.T</th>
                                    <th style="width:5%; text-align:right;">V.A.T ADDL</th>
                                    <th style="width:10%; text-align:right;">Bill Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $msubtot = 0;
                                $mtotvat = 0;
                                $mtotvatadd = 0;
                                $totamt = 0;

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

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

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$pmain[billno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$supplier[name]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[subtot]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[discount]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[totvat]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[totvatadd]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[billamt]</td>";
                                    Echo "</tr>";

                                    $msubtot = $msubtot + $pmain['subtot'];
                                    $mtotvat = $mtotvat + $pmain['totvat'];
                                    $mtotvatadd = $mtotvatadd + $pmain['totvatadd'];
                                    $totamt = $totamt + $pmain['billamt'];

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

                                        Echo "<tr style='height:10px; background-color:#BABFC2'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-bordered'>";
                                        Echo "<th style='width:2%;'>Sr</th>";
                                        Echo "<th style='width:20%;'>Product Name</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Qty</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Amount</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Free Qty</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Total Qty</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Disc %</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Disc Amt</th>";
                                        Echo "<th style='width:5%;text-align:right;' >V.A.T %</th>";
                                        Echo "<th style='width:5%;text-align:right;' >V.A.T</th>";
                                        Echo "<th style='width:10%;text-align:right;'>Total</th>";
                                        Echo "</tr>";


                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' ORDER BY srno")or die(mysqli_error($conn));
                                        $ctr2 = 0;
                                        $trtotqty = 0;
                                        $tramount = 0;
                                        $trtotal = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {
//                                            $getitem = mysqli_query($conn, "SELECT code, name FROM `product` WHERE code = '$tran[item]'") or die(mysqli_error($conn));
//                                            $geti = mysqli_fetch_array($getitem);
                                            

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[particulars]</td>";
                                            Echo "<td style='text-align:right;'>$tran[qty]</td>";
                                            Echo "<td style='text-align:right;'>$tran[amount]</td>";
                                            Echo "<td style='text-align:right;'>$tran[freeqty]</td>";
                                            Echo "<td style='text-align:right;'>$tran[totalqty]</td>";
                                            Echo "<td style='text-align:right;'>$tran[discperc]</td>";
                                            Echo "<td style='text-align:right;'>$tran[discount]</td>";
                                            Echo "<td style='text-align:right;'>$tran[vat]</td>";
                                            Echo "<td style='text-align:right;'>$tran[vatadd]</td>";
                                            Echo "<td style='text-align:right;'>$tran[total]</td>";
                                            Echo "</tr>";

                                            $trtotqty = $trtotqty + $tran[qty];
                                            $tramount = $tramount + $tran[amount];
                                            $trtotal = $trtotal + $tran[total];
                                        }
                                        
                                        Echo "<tr>";
                                        Echo "<td style='text-align:right' colspan='2'>Total</td>";
                                        Echo "<td style='text-align:right;'>" . number_format($trtotqty, 2, '.', '') . "</td>";
                                        Echo "<td style='text-align:right;'>" . number_format($tramount, 2, '.', '') . "</td>";
                                        Echo "<td colspan='7' style='text-align:right;'>" . number_format($trtotal, 2, '.', '') . "</td>";
                                        Echo "</tr>";

                                        Echo "</table>";
                                        Echo "</td>";
                                    }
                                }

                                Echo "<tr>";
                                Echo "<td style='text-align:right' colspan= '3'><b>Total</b></td>";
                                Echo "<td style='text-align:right'><b>" . number_format($msubtot, 2, '.', '') . "</b></td>";
                                Echo "<td style='text-align:right'><b></b></td>";
                                Echo "<td style='text-align:right'><b>" . number_format($mtotvat, 2, '.', '') . "</b></td>";
                                Echo "<td style='text-align:right'><b>" . number_format($mtotvatadd, 2, '.', '') . "</b></td>";
                                Echo "<td style='text-align:right'><b>" . number_format($totamt, 2, '.', '') . "</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

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

                                    $contents .= "Invoice,Date,Supplier,Amount,Discount,V.A.T,V.A.T ADDL,Bill Amount\n"; 
                                   
                                $ctr = 0;
                                $msubtot = 0;
                                $mtotvat = 0;
                                $mtotvatadd = 0;
                                $totamt = 0;

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

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

                                    $ctr++;
                                    $contents .= "$pmain[billno]" . ",";
                                    $contents .= "$pmain[mydt]" . ",";
                                    $contents .= "$supplier[name]" . ",";
                                    $contents .= "$pmain[subtot]" . ",";
                                    $contents .= "$pmain[discount]" . ",";
                                    $contents .= "$pmain[totvat]" . ",";
                                    $contents .= "$pmain[totvatadd]" . ",";
                                    $contents .= "$pmain[billamt]" . "\n";
                                    
                                    $msubtot = $msubtot + $pmain['subtot'];
                                    $mtotvat = $mtotvat + $pmain['totvat'];
                                    $mtotvatadd = $mtotvatadd + $pmain['totvatadd'];
                                    $totamt = $totamt + $pmain['billamt'];

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


                                        $contents .= "Sr,Product Name,Qty,Amount,Free Qty,Total Qty,Disc %,Disc Amt,V.A.T %,V.A.T,Total\n";

                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' ORDER BY srno")or die(mysqli_error($conn));
                                        $ctr2 = 0;
                                        $trtotqty = 0;
                                        $tramount = 0;
                                        $trtotal = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            

                                            $ctr2++;
                                            $contents .= "$ctr2" . ",";
                                            $contents .= "$tran[particulars]" . ",";
                                            $contents .= "$tran[qty]" . ",";
                                            $contents .= "$tran[amount]" . ",";
                                            $contents .= "$tran[freeqty]" . ",";
                                            $contents .= "$tran[totalqty]" . ",";
                                            $contents .= "$tran[discperc]" . ",";
                                            $contents .= "$tran[discount]" . ",";
                                            $contents .= "$tran[vat]" . ",";
                                            $contents .= "$tran[vatadd]" . ",";
                                            $contents .= "$tran[total]" . "\n";

                                            $trtotqty = $trtotqty + $tran[qty];
                                            $tramount = $tramount + $tran[amount];
                                            $trtotal = $trtotal + $tran[total];
                                        }
                                        

                                        $contents .= "Total" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "$trtotqty" . ",";
                                        $contents .= "$tramount" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "$trtotal" . "\n";
                                    }
                                }

                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "Total" . ",";
                                        $contents .= "$msubtot" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "" . ",";
                                        $contents .= "$mtotvat" . ",";
                                        $contents .= "$mtotvatadd" . ",";
                                        $contents .= "$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;
    }
}
?>