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

$maindb = "purchamt";
$trandb = "";
$formname = "Purchase Amt Register";
$file = "purchamtreg.php";


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

$party = $_POST["party"];
$dtype = $_POST["dtype"];



$dtypecond = "";
$partycond = "";

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

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


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
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'   $partycond ")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; 
        }
        .dtype{
            background-color: #cccccc;
        }
    </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-striped table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='12%'>Voucher No.</th>
                                <th width='12%'>Date</th>
                                <th width='5%'>Invoice No</th>
                                <th width='30%'>Party Name</th>
                                <th width='5%' class="text-right">Amount</th>
                                
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ttotqty = 0;
                            $tsubtotal = 0;
                            $tlitemdisc = 0;
                            $tpackchgs = 0;
                            $ttotgst5 = 0;
                            $ttotgst12 = 0;
                            $ttotgst18 = 0;
                            $ttotgst28 = 0;
                            $ttotamt = 0;
                            $ttotalamount = 0;
                            $ttcsamt = 0;
                            $ttottaxamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                $supp = mysqli_fetch_array($getsupp);
                                $partyname = $supp['name'];
                                $anetamt = $pmain['amount'];
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[recpno]</td>";
                                Echo "<td>$partyname</td>";
                                Echo "<td class='text-right'>$anetamt</td>";
                                Echo "</tr>";

                                
                                $ttotamt = $ttotamt + $anetamt;
                            }

                            $colr = "green";
                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td colspan='2' align='right' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
                            echo "<td style='color:$colr' class='numeric'><h5><b>$ttotamt</b></h5></td>";
                            echo "</tr>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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


if (isset($_POST["excel"])) {
    $contents .= "Sr,INV.No,Date,Party Name, Tot Qty,Amount,Discount,HandChgs Packgs,Taxable,GST 5%,GST 12%,GST 18%,GST 28%,Total,TCS,Bill.Amt\n";

    $ttotqty = 0;
    $tsubtotal = 0;
    $tlitemdisc = 0;
    $tpackchgs = 0;
    $ttotgst5 = 0;
    $ttotgst12 = 0;
    $ttotgst18 = 0;
    $ttotgst28 = 0;
    $ttotamt = 0;
    $ttotalamount = 0;
    $ttcsamt = 0;
    $ttottaxamt = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
        $supp = mysqli_fetch_array($getsupp);
        $partyname = $supp['name'];
        $anetamt = $pmain[netamt] - $pmain[tcsamt];
        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$partyname" . ",";
        $contents .= "$pmain[totqty]" . ",";
        $contents .= "$pmain[subtotal]" . ",";
        $contents .= "$pmain[litemdisc]" . ",";
        $contents .= "$pmain[packchgs]" . ",";
        $contents .= "$pmain[tottaxamt]" . ",";
        $contents .= "$pmain[totgst5]" . ",";
        $contents .= "$pmain[totgst12]" . ",";
        $contents .= "$pmain[totgst18]" . ",";
        $contents .= "$pmain[totgst28]" . ",";
        $contents .= "$pmain[totalamount]" . ",";
        $contents .= "$pmain[tcsamt]" . ",";
        $contents .= "$anetamt" . "\n";

        $ttotqty = $ttotqty + $pmain['totqty'];
        $tsubtotal = $tsubtotal + $pmain['subtotal'];
        $tlitemdisc = $tlitemdisc + $pmain['litemdisc'];
        $tpackchgs = $tpackchgs + $pmain['packchgs'];
        $ttottaxamt = $ttottaxamt + $pmain['tottaxamt'];
        $ttotgst5 = $ttotgst5 + $pmain['totgst5'];
        $ttotgst12 = $ttotgst12 + $pmain['totgst12'];
        $ttotgst18 = $ttotgst18 + $pmain['totgst18'];
        $ttotgst28 = $ttotgst28 + $pmain['totgst28'];
        $ttotalamount = $ttotalamount + $pmain['totalamount'];
        $ttcsamt = $ttcsamt + $pmain['tcsamt'];
        $ttotamt = $ttotamt + $anetamt;

        if ($_POST["dtype"] == 2) {
            $contents .= "Sr,Item,Co.,Hsncode,BS/24,S/28,M/32,L/36,XL/40,2XL/44,3XL/48,4XL/51,5XL/54,Qty,Amount\n";

            $ctr2 = 0;
            $ts24 = 0;
            $ts28 = 0;
            $ts32 = 0;
            $ts36 = 0;
            $ts40 = 0;
            $ts44 = 0;
            $ts48 = 0;
            $ts51 = 0;
            $ts54 = 0;
            $tqty = 0;
            $tamt = 0;

            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[item]'")or die(mysqli_error($conn));
                $item = mysqli_fetch_array($getitem);
                $itemname = $item['name'];


                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$itemname" . ",";
                $contents .= "$tran[co]" . ",";
                $contents .= "$tran[hsncode]" . ",";
                $contents .= "$tran[s24]" . ",";
                $contents .= "$tran[s28]" . ",";
                $contents .= "$tran[s32]" . ",";
                $contents .= "$tran[s36]" . ",";
                $contents .= "$tran[s40]" . ",";
                $contents .= "$tran[s44]" . ",";
                $contents .= "$tran[s48]" . ",";
                $contents .= "$tran[s51]" . ",";
                $contents .= "$tran[s54]" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[total]" . "\n";

                $ts24 = $ts24 + $tran['s24'];
                $ts28 = $ts28 + $tran['s28'];
                $ts32 = $ts32 + $tran['s32'];
                $ts36 = $ts36 + $tran['s36'];
                $ts40 = $ts40 + $tran['s40'];
                $ts44 = $ts44 + $tran['s44'];
                $ts48 = $ts48 + $tran['s48'];
                $ts51 = $ts51 + $tran['s51'];
                $ts54 = $ts54 + $tran['s54'];
                $tqty = $tqty + $tran['qty'];
                $tamt = $tamt + $tran['total'];
            }

            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "Total" . ",";
            $contents .= "$ts24" . ",";
            $contents .= "$ts28" . ",";
            $contents .= "$ts32" . ",";
            $contents .= "$ts36" . ",";
            $contents .= "$ts40" . ",";
            $contents .= "$ts44" . ",";
            $contents .= "$ts48" . ",";
            $contents .= "$ts51" . ",";
            $contents .= "$ts54" . ",";
            $contents .= "$tqty" . ",";
            $contents .= "$tamt" . "\n";
        }
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$ttotqty" . ",";
    $contents .= "$tsubtotal" . ",";
    $contents .= "$tlitemdisc" . ",";
    $contents .= "$tpackchgs" . ",";
    $contents .= "$ttottaxamt" . ",";
    $contents .= "$ttotgst5" . ",";
    $contents .= "$ttotgst12" . ",";
    $contents .= "$ttotgst18" . ",";
    $contents .= "$ttotgst28" . ",";
    $contents .= "$ttotalamount" . ",";
    $contents .= "$ttcsamt" . ",";
    $contents .= "$ttotamt" . "\n";


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