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

$maindb = "pgstmain";
$trandb = "pgsttran";
$formname = "PURCHASE REGISTER";
$file = "purchreg.php";

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

$invno = $_POST["invno"];
$party = $_POST["party"];
$broker = $_POST["broker"];
$item = $_POST["item"];
$prodtype = $_POST["prodtype"];
$dtype = $_POST["dtype"];
$scope = $_POST["scope"];
$printnar = $_POST["prna"];
$gstperc = $_POST["gstperc"];
$broker = $_POST["broker"];

$dtypecond = "";
$mscopecond = "";
$itemcond = "";
$gstcond = "";
$prodtypecond = "";

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

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

if (!empty($salesman)) {
    $salemcond = " AND `salesman`='$salesman' ";
}
if (!empty($item)) {
    $itemcond = " AND `item`='$item' ";
}
if (!empty($gstperc)) {
    $gstcond = " AND `gstperc`='$gstperc' ";
}
if (!empty($prodtype)) {
    $prodtypecond = " AND `prodtype`='$prodtype' ";
}

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`='$dtype' ";
}

if (!empty($scope)) {
    $mscopecond = " AND `type`='$scope' ";
}

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

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` where
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond  $itemcond $brokercond
                            $salemcond $prodtypecond $gstcond ") or die(mysqli_error($conn));

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));
if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .dtype {
            background-color: #cccccc;
        }

        @media print {

            .table-bordered>thead>tr>th {
                border: 1px solid black !important;
            }

            .table-bordered>tbody>tr>td {
                border: 1px solid black !important;
            }

            .table-bordered>tbody>tr>th {
                border: 1px solid black !important;
            }

            .table-condensed>tbody>tr>td {
                padding: 3px !important;
            }

            .container {
                margin-left: 30px !important;
                margin-top: 10px !important;    
            }
        }
    </style>
    </head>

    <body>
        <div class="container">
            <?php
            $zroot = $_SESSION["zdir"];
            $zcomp = $_SESSION["comp_" . $dbname];
            $getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` where code = '$zcomp' ") or die(mysqli_error($conn));
            $comp = mysqli_fetch_array($getcomp);
            ?>
            <style>
                @media print {
                    .noPrint {
                        display: none;
                    }
                }

                .sktable {
                    border-collapse: collapse;
                    width: 100%;
                    /* margin: 20px auto; */
                    /* Centers the table horizontally */
                    /* border: 1px solid #ddd; */
                }
            </style>
            <table class="sktable" width="100%" id="head" align="center">
                <tr style="text-align:center">
                    <td style="font-size: 18px;text-align:center">
                        <label style='font-size:20px;'><? echo $comp['pname']; ?></label>
                    </td>
                    <!-- <td style="text-align:right" class="noPrint">
                        <a onclick="window.close()" class="btn btn-primary noPrint">GO Back</a>
                        </button>
                    </td> -->
                </tr>
                <tr>
                    <td style="font-size: 13px;text-align:center">
                        <?php echo "<label style='font-size:13px;'>$comp[add1]$comp[add2]$comp[add3]</label>" ?>
                    </td>
                </tr>
                <tr>
                    <td style="font-size: 14px;text-align:center">
                        <label class="label-control marg" style="font-size:14px"><?php echo $formname; ?> &nbsp;&nbsp; -
                            &nbsp;&nbsp;
                            <?php if ($mfdt != '') { ?>
                                From Date :
                                <?php echo $mfdt; ?>&nbsp;&nbsp;&nbsp;
                            <?php } ?>
                            <?php if ($mtdt != '') { ?>
                                To Date :
                                <?php echo $mtdt; ?>
                            <?php } ?> </label>
                    </td>
                    <!-- <td style="font-size: 13px;width:164px" align="right">
                        <label class="label-control marg" style="font-size:14px">Report Date -
                            <?php echo Date("d-m-Y"); ?></label>
                    </td> -->
                </tr>

                <?php
                if (!empty($party)) {
                    $getsuppnm = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$party'") or die(mysqli_error($conn));
                    $suppnamesk = mysqli_fetch_object($getsuppnm);
                    ?>

                    <tr>
                        <td style="text-align:left">
                            <?php echo "<label style='font-size:17px;'>Party :  $suppnamesk->name</label>" ?>
                        </td>
                    </tr>
                <?php } ?>

            </table>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class=" table-condensed table-bordered ">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='5%'>Invoice</th>
                                <th width='15%'>Date</th>
                                <!-- <th width='25%'>Broker</th> -->
                                <th width='25%'>Party</th>
                                <!--<th width='5%' class="text-right">Qty</th>-->
                                <th width='7%' class="text-right">Amount</th>
                                <th width='5%' class="text-right">Disc. %</th>
                                <th width='5%' class="text-right">Discount</th>
                                <th width='5%' class="text-right">GT Amount</th>
                                <!-- <th width='5%' class="text-right">Packing</th> -->
                                <th width='7%' class="text-right">Total Amount</th>
                                <!-- <th width='5%' class="text-right">Round Off</th> -->
                                <th width='8%' class="text-right">Bill.Amt</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totalq = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;
                            $totamt = 0;
                            $totalamount = 0;
                            $totround = 0;
                            $totpack = 0;
                            $totvat = 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);
                                $getbr = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[broker]'") or die(mysqli_error($conn));
                                $febr = mysqli_fetch_array($getbr);

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[invno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                // echo "<td>$febr[name]</td>";
                                echo "<td>$supp[name]</td>";
                                //                                Echo "<td class='text-right'>$pmain[totqty]</td>";
                                echo "<td class='text-right'>$pmain[subtotal]</td>";
                                echo "<td class='text-right'>$pmain[discperc]</td>";
                                echo "<td class='text-right'>$pmain[discount]</td>";
                                echo "<td class='text-right'>$pmain[vatamt]</td>";
                                // echo "<td class='text-right'>$pmain[packing]</td>";
                                echo "<td class='text-right'>$pmain[totalamount]</td>";
                                // echo "<td class='text-right'>$pmain[roundoff]</td>";
                                echo "<td class='text-right'>$pmain[netamt]</td>";
                                echo "</tr>";

                                $ttotamt = $ttotamt + $pmain['subtotal'];
                                $totitmpr = $totitmpr + $pmain['discount'];
                                $totbill = $totbill + $pmain['netamt'];
                                $totalq = $totalq + $pmain['totqty'];
                                $totround = $totround + $pmain['roundoff'];
                                $totalamount = $totalamount + $pmain['totalamount'];
                                $totpack = $totpack + $pmain['packing'];
                                $totvat = $totvat + $pmain['vatamt'];

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

                                    echo "<tr style='height:10px;'>";
                                    echo "<td colspan='25'>";
                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    echo "<th width='5%;'class='dtype'>Sr</th>";
                                    //                                    Echo "<th width='5%' class='dtype''>Barcode</th>";
                                    echo "<th width='20%' class='dtype'>Itemname</th>";
                                    echo "<th width='10%' class='dtype'>Size</th>";
                                    echo "<th width='6%' class='numeric dtype'>Qty</th>";
                                    echo "<th width='6%' class='numeric dtype'>Rate</th>";
                                    echo "<th width='6%' class='numeric dtype'>Amount</th>";
                                    echo "<tr>";

                                    $ctr2 = 0;
                                    $totqty = $totmtrs = $totyard = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 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[itemcode]'") or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($getitem);

                                        //                                        $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'")or die(mysqli_error($conn));
                                        //                                        $color = mysqli_fetch_array($getcolor);
                        
                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$item[name]</td>";
                                        echo "<td>$tran[size]</td>";
                                        echo "<td class='numeric'>$tran[qty]</td>";
                                        echo "<td class='numeric'>$tran[rate]</td>";
                                        echo "<td class='numeric'>$tran[amount]</td>";
                                        echo "</tr>";

                                        $totamt = $totamt + $tran['amount'];
                                        $totqty = $totqty + $tran['qty'];
                                    }
                                    echo "<tr> ";
                                    $colr1 = "blue";

                                    echo "<td style='color:$colr1'</td>";
                                    echo "<td style='color:$colr1'</td>";
                                    //                                    echo "<td style='color:$colr1'</td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>Total</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totqty</b></h5></td>";
                                    //                                    echo "<td style='color:$colr1' id='font1'></td>";
                                    //                                    echo "<td style='color:$colr1' id='font1'></td>";
                                    echo "<td style='color:$colr1' id='font1'></td>";
                                    echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totamt</b></h5></td>";
                                    echo "</tr>";

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

                            $colr = "green";
                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td align='right' style='color:$colr;' colspan='2'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totitmpr</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totvat</b></h4></td>";
                            // echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totpack</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totalamount</b></h4></td>";
                            // echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totround</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "</tr>";


                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b>Bill Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' colspan='2'><h4><b>$ttotamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            // echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            // echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font' colspan='2'><h4><b>$totbill</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
    <?php
}
if (isset($_POST["excel"])) {

    include("includes/head_excel_rep.php");

    $contents .= "Sr, Invoice, Date, Broker, Party, Amount, Discount, GT Amount, Packing, Total Amount, Round Off, Bill.Amt \n";

    $ctr = 0;
    $totalq = 0;
    $totbill = 0;
    $totitmpr = 0;
    $totdisc = 0;
    $tottaxamt = 0;
    $totcgst = 0;
    $totsgst = 0;
    $totigst = 0;
    $totamt = 0;
    $totalamount = 0;
    $totround = 0;
    $totpack = 0;
    $totvat = 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);

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

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[invno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$febr[name]" . ",";
        $contents .= "$supp[name]" . ",";
        $contents .= "$pmain[subtotal]" . ",";
        $contents .= "$pmain[discount]" . ",";
        $contents .= "$pmain[vatamt]" . ",";
        $contents .= "$pmain[packing]" . ",";
        $contents .= "$pmain[totalamount]" . ",";
        $contents .= "$pmain[roundoff]" . ",";
        $contents .= "$pmain[netamt]" . "\n";

        $ttotamt = $ttotamt + $pmain['subtotal'];
        $totitmpr = $totitmpr + $pmain['discount'];
        $totbill = $totbill + $pmain['netamt'];
        $totalq = $totalq + $pmain['totqty'];
        $totround = $totround + $pmain['roundoff'];
        $totalamount = $totalamount + $pmain['totalamount'];
        $totpack = $totpack + $pmain['packing'];
        $totvat = $totvat + $pmain['vatamt'];

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

            $contents .= "Sr, Itemname, Size, Qty, Rate, Amount\n";

            $ctr2 = $totqty = $totmtrs = $totyard = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 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[itemcode]'") or die(mysqli_error($conn));
                $item = mysqli_fetch_array($getitem);

                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$item[name]" . ",";
                $contents .= "$tran[size]" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[rate]" . ",";
                $contents .= "$tran[amount]" . "\n";

                $totamt = $totamt + $tran['amount'];
                $totqty = $totqty + $tran['qty'];
            }

            $contents .= "" . ",,";
            $contents .= "Total" . ",";
            $contents .= "$totqty" . ",,";
            $contents .= "$totamt" . "\n";
        }
    }

    $contents .= "" . ",,,,";
    $contents .= "Grand Total" . ",";
    $contents .= "$ttotamt" . ",";
    $contents .= "$totitmpr" . ",";
    $contents .= "$totvat" . ",";
    $contents .= "$totpack" . ",";
    $contents .= "$totalamount" . ",";
    $contents .= "$totround" . ",";
    $contents .= "$totbill" . "\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;
    }
}
?>