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

$maindb = "purmain";
$trandb = "purtran";
$formname = "Purchase Register";
$file = "purreg_fr.php";


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

if (isset($_POST["view"])) {
    $party = $_POST["party"];
    $dtype = $_POST["dtype"];
    $scope = $_POST["scope"];

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

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



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


//    if (!empty($frmdt)) {
//        $frmdtcond = " AND `vouchdt`='$frmdt' ";
//    }
//
//    if (!empty($todt)) {
//        $todtcond = " AND `todt`='$todt' ";
//    }

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

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

    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 $mscopecond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
//    browse($get);
    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">
            <?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-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:10%">Inv.No</th>
                                <th style="width:10%">Date</th>
                                <th style="width:30%">Supplier</th>
                                <th style="width:15%" class="text-right">Amount</th>
                                <th style="width:8%"  class="text-right">Discount</th>
                                <th style="width:6%"  class="text-right">V.A.T</th>
                                <th style="width:15%" class="text-right">Bill.Amt</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $totdisc = 0;
                            $totbill = 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);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[invno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$supp[name]</td>";
                                Echo "<td class='text-right'>$pmain[subtotal]</td>";
                                Echo "<td class='text-right'>$pmain[discount]</td>";
                                Echo "<td class='text-right'>$pmain[vatamt]</td>";
                                Echo "<td class='text-right'>$pmain[netamt]</td>";
                                Echo "</tr>";
                                $totbill = $totbill + $pmain[netamt];
                                $totamt = $totamt + $pmain[subtotal];
                                $totdisc = $totdisc + $pmain[discount];

                                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 style='background-color: #cccccc;width:5%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc;width:10%; class='numeric''>Barcode</th>";
                                    Echo "<th style='background-color: #cccccc';width:6%; >Itemname</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Qty</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Mrp</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Rate</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Amount</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Dis%age</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Discount</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Total</th>";
                                    Echo "<th style='background-color: #cccccc;width:6%;' class='numeric'>Msp</th>";

                                    Echo "<tr>";


                                    $ctr2 = 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[itemcode]'")or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($getitem);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td class='numeric'>$tran[barcode]</td>";
                                        Echo "<td>$item[name]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[mrp]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "<td class='numeric'>$tran[discperc]</td>";
                                        Echo "<td class='numeric'>$tran[discount]</td>";
                                        Echo "<td class='numeric'>$tran[total]</td>";
                                        Echo "<td class='numeric'>$tran[msp]</td>";
                                        Echo "</tr>";

                                        $tqty = $tran[qty] + $tqty;
                                        $tamt = $tran[total] + $tamt;

//                                        
                                    }
                                    Echo "<tr style=' '>";
                                    Echo "<td style=' '></td>";
                                    Echo "<td style=' '></td>";
                                    Echo "<th style='text-align:right;color:blue; '>Total</th>";
                                    Echo "<th style='text-align:right;color:blue; '>$tqty</th>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<td style=' 'class='numeric'></td>";
                                    Echo "<th style='text-align:right;color:blue; '>$tamt</td>";
                                    Echo "<td style=' '></td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                    Echo "</td>";
                                }
//                                if ($_POST["dtype"] == 2) {
//                                    echo "<tr> ";
//                                    $colr1 = "blue";
//                                    echo "<td style='color:$colr1;' align='right' id='font1' colspan='6' ><h5><b>Total :</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='right' id='font1'><h5><b>$topaytot1</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='center' id='font1'><h5><b>$totweight</b></h5></td>";
//                                    echo "<td style='color:$colr1;'  id='font1'><h5><b>$totpaid</b></h5></td>";
//                                    echo "</tr>";
//                                }
                            }
                            $colr = "red";
                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totdisc</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>$totbill</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

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

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




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


//    if (!empty($frmdt)) {
//        $frmdtcond = " AND `vouchdt`='$frmdt' ";
//    }
//
//    if (!empty($todt)) {
//        $todtcond = " AND `todt`='$todt' ";
//    }

    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; 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 $mscopecond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
//    browse($get);

    $contents.="Sr.,Inv. No.,Date,Supplier,Amount,Discount,V.A.T,Bill. Amt.\n";

    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 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);


        $ctr++;
        $pinvno = $pmain[invno];
        $pdate = $pmain[mydt];
        $pname = $supp[name];
        $psubtotal = $pmain[subtotal];
        $pdiscount = $pmain[discount];
        $pvatamt = $pmain[vatamt];
        $pnetamt = $pmain[netamt];

        $contents.=$ctr . ",";
        $contents.=$pinvno . ",";
        $contents.=$pdate . ",";
        $contents.=$pname . ",";
        $contents.=$psubtotal . ",";
        $contents.=$pdiscount . ",";
        $contents.=$pvatamt . ",";
        $contents.=$pnetamt . "\n";

        $totbill = $totbill + $pmain[netamt];
        $totamt = $totamt + $pmain[subtotal];
        $totdisc = $totdisc + $pmain[discount];

        if ($dtype == 2) {

            $ctr2 = 0;
            $tqty = 0;
            $tamt = 0;

            $contents.= "Sr.,Barcode,Itemname,Qty,Mrp,Rate,Amount,Dis%age,Discount,Total,Msp\n";

            $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++;
                $pbarcode = $tran[barcode];
                $pitemname = $item[name];
                $pqty = $tran[qty];
                $pmrp = $tran[mrp];
                $prate = $tran[rate];
                $pamount = $tran[amount];
                $pdiscperc = $tran[discperc];
                $pdiscount = $tran[discount];
                $ptotal = $tran[total];
                $pmsp = $tran[msp];

                $contents.=$ctr2 . ",";
                $contents.=$pbarcode . ",";
                $contents.=$pitemname . ",";
                $contents.=$pqty . ",";
                $contents.=$pmrp . ",";
                $contents.=$prate . ",";
                $contents.=$pamount . ",";
                $contents.=$pdiscperc . ",";
                $contents.=$pdiscount . ",";
                $contents.=$ptotal . ",";
                $contents.=$pmsp . "\n";
            }
            $tqty1 = $tran[qty] + $tqty;
            $tamt1 = $tran[total] + $tamt;

            $contents.="," . ",";
            $contents.="Total" . ",";
            $contents.="$tqty1" . ",";
            $contents.="," . ",";
            $contents.="," . ",";
            $contents.=" " . ",";
            $contents.="$tamt1" . "\n";
        }
    }
    $contents.="," . ",";
    $contents.=" " . ",";
    $contents.="Grand Total" . ",";
    $contents.=$totamt . ",";
    $contents.=$totdisc . ",";
    $contents.=" " . ",";
    $contents.=$totbill . "\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;
    }
}
?>