<?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 = "purchreg.php";

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

$party = $_POST['name'];
$brand = $_POST['brand'];
$godown = $_POST['godown'];
$mtype = $_POST['mtype'];

$partycond = "";
$gdncond = "";
$brandcond = "";
$brandcond2 = "";


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

if (!empty($brand)) {
    $brandcond = " AND b.`brand`='$brand' ";
}
if (!empty($brand)) {
    $brandcond2 = " AND `brand`='$brand' ";
}

if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; purchreg.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; purchreg.php");
}
$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);



if ($_POST["dtype"] != 2) {
    $getpmain = mysqli_query($conn, "SELECT a.*,b.*,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 $partycond $gdncond group by a.vouchno order by a.vouchno,b.srno")or die(mysqli_error($conn));
} else {
    $getpmain = mysqli_query($conn, "SELECT a.*,b.*,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 $partycond $gdncond $brandcond group by a.vouchno order by a.vouchno,b.srno")or die(mysqli_error($conn));
}
//$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
//                                  AND `vouchdt` <= '$mtodate' $partycond $itemcond $godowncond $mtypecond")or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%;border-radius: 5px" class="table table-condensed table-hover ">
                            <tr>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="4%;">Sr</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="5%;">Invoice</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;">Date</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="20%;">Party</th>   
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="5%;" class="text-right">Qty</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="5%;" class="text-right">Totkg</th>                             
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;" class="text-right">Amount</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;" class="text-right">Discount</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;" class="text-right">Freight</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;" class="text-right">Other Charges</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;" class="text-right">Bill Amt.</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="4%;" class="text-right">ROE</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;" class="text-right">Total Expense</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="7%;" class="text-right">Net Amt.</th>
                                <th style="color:white !important;background-color: #1ABC9C !important;-webkit-print-color-adjust: exact;font-size:12px" width="25%;" >Remarks</th>
                            </tr>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $subtot = 0;
                            $totdisc = 0;
                            $totfr = 0;
                            $totqty1 = 0;
                            $totnet = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getmtype = mysqli_query($conn, "SELECT name FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
                                $mtype = mysqli_fetch_array($getmtype);

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

                                $getmqty = mysqli_query($conn, "SELECT SUM(invqty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                $mqty = mysqli_fetch_array($getmqty);

                                $ctr++;
                                Echo "<tr class='clickable-row2' data-href='purchase.php?mode=edit&vouchno=$pmain[vouchno]' data-target='_blank'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[invno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$mcust[name]</td>";
                                Echo "<td class='numeric'>$pmain[tqty]</td>";
                                Echo "<td class='numeric'>$pmain[tweight]</td>";
                                Echo "<td class='numeric'>$pmain[subtotal]</td>";
                                Echo "<td class='numeric'>$pmain[discount]</td>";
                                Echo "<td class='numeric'>$pmain[freight]</td>";
                                Echo "<td class='numeric'>$pmain[othchgs]</td>";
                                Echo "<td class='numeric'>$pmain[billamt]</td>";
                                Echo "<td class='numeric'>$pmain[roe]</td>";
                                Echo "<td class='numeric'>$pmain[totexpamt]</td>";
                                Echo "<td class='numeric'>$pmain[netamt]</td>";
                                Echo "<td style='font-size:12px'>$pmain[rem1]  $pmain[rem2] </td>";
                                Echo "</tr>";
                                Echo "<tr>";

                                $subtot = $subtot + $pmain['subtotal'];
                                $totdisc = $totdisc + $pmain['discount'];
                                $totfr = $totfr + $pmain['freight'];
                                $totothchgs = $totothchgs + $pmain['othchgs'];
                                $tottotexpamt = $tottotexpamt + $pmain['totexpamt'];
                                $totnet = $totnet + $pmain['netamt'];
                                $totbillamt = $totbillamt + $pmain['billamt'];
                                $tottqty = $tottqty + $pmain['tqty'];
                                $tottweight = $tottweight + $pmain['tweight'];

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

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;'class='table table-condensed table-hover'>";
                                    Echo "<tr>";
                                    Echo "<th style='width:2%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Sr</th>";
                                    Echo "<th style='width:7%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Godown</th>";
                                    Echo "<th style='width:7%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Brand/Logo</th>";
                                    Echo "<th style='width:5%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Code</th>";
                                    Echo "<th style='width:10%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;'>Description</th>";
                                    Echo "<th style='width:6%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Qty.</th>";
                                    Echo "<th style='width:6%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Bale Qty.(Kgs)</th>";
                                    Echo "<th style='width:6%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Tot. Qty.(Kgs)</th>";
                                    Echo "<th style='width:5%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Rate</th>";
                                    Echo "<th style='width:5%;color: white !important; background-color:#BABFC2 !important;-webkit-print-color-adjust: exact;' class='text-right'>Amount</th>";
                                    Echo "</tr>";
                                    Echo "<tr>";

                                    $ctr2 = 0;
                                    $totqty = 0;
                                    $totamt = 0;
                                    $totinv = 0;
                                    $totdtqty = 0;
                                    $totdtinv = 0;
                                    $totdtamt = 0;

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

                                        $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                                        $mbrand = mysqli_fetch_array($getbrand);

                                        $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `barcode`='$tran[itemcode]'") or die(mysqli_error($conn));
                                        $fitem = mysqli_fetch_array($finditem);
                                        
                                        $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`='$tran[godown]'") or die(mysqli_error($conn));
                                        $fetgodown = mysqli_fetch_array($getgodown);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$fetgodown[gocode]</td>";
                                        Echo "<td>$mbrand[name]</td>";
                                        Echo "<td>$fitem[code]</td>";
                                        Echo "<td>$fitem[name]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[baleqty]</td>";
                                        Echo "<td class='numeric'>$tran[totqty]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";
                                        $totqty = $totqty + $tran['qty'];
                                        $totdtqty = $totdtqty + $tran['totqty'];
                                        $totdtinv = $totdtinv + $tran['baleqty'];
                                        $totdtamt = $totdtamt + $tran['amount'];
                                    }

                                        Echo "<tr style='background-color:#d8e0e6;'>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;'></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right' ><b>Total </b></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totqty </b></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtinv </b></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtqty </b></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right' ></td>";
                                        Echo "<td style='color: black !important; background-color:#d8e0e6 !important;-webkit-print-color-adjust: exact;' class='text-right'><b> $totdtamt </b> </td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                            }
                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'><b>Total</b></td>";
                            Echo "<td class='numeric text-right'><b>$tottqty</b></td>";
                            Echo "<td class='numeric text-right'><b>$tottweight</b></td>";
                            Echo "<td class='numeric text-right'><b>$subtot</b></td>";
                            Echo "<td class='numeric text-right'><b>$totdisc</b></td>";
                            Echo "<td class='numeric text-right'><b>$totfr</b></td>";
                            Echo "<td class='numeric text-right'><b>$totothchgs</b></td>";
                            Echo "<td class='numeric text-right'><b>$totbillamt</b></td>";
                            Echo "<td class='numeric text-right'><b></b></td>";
                            Echo "<td class='numeric text-right'><b>$tottotexpamt</b></td>";
                            Echo "<td class='numeric text-right'><b>$totnet</b></td>";
                            Echo "<td></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $party = $_POST['party'];
    $item = $_POST['item'];
    $godown = $_POST['godown'];
    $mtype = $_POST['mtype'];
    $frmdt = $_POST['frmdt'];
    $todt = $_POST['todt'];

    $partycond = "";
    $brokercond = "";
    $transportcond = "";
    $itemcond = "";
    $godowncond = "";
    $mtypecond = "";
    if (!empty($party)) {
        $partycond = " AND `party`='$party' ";
    }

    if (!empty($item)) {
        $itemcond = " AND `name`='$item' ";
    }

    if (!empty($godown)) {
        $godowncond = " AND `godown`='$godown' ";
    }


    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (!empty($mtype)) {
        $mtypecond = " AND `type`='$mtype' ";
    }
// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
             $partycond $itemcond $godowncond $mtypecond")or die(mysqli_error($conn));

    $contents .= "Sr,Type,Invoice No.,Date,Customer,Amount,Discount,Freight,Qty.,Net Amount\n";

    $ctr = 0;
    $subtot = 0;
    $totdisc = 0;
    $totfr = 0;
    $totqty1 = 0;
    $totnet = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getmtype = mysqli_query($conn, "SELECT name FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
        $mtype = mysqli_fetch_array($getmtype);

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

        $getmqty = mysqli_query($conn, "SELECT SUM(qty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[VOUCHNO]'")or die(mysqli_error($conn));
        $mqty = mysqli_fetch_array($getmqty);
        $ctr++;

        $mctr = $ctr;
        $mtype = $mtype['name'];
        $minvno = $pmain['invno'];
        $mmydt = $pmain['mydt'];
        $mname = $mcust['name'];
        $msubtotal = $pmain['subtotal'];
        $mdiscount = $pmain['discount'];
        $mfreight = $pmain['freight'];
        $mtotqty = $pmain['totqty'];
        $mnetamt = $pmain['netamt'];

        $contents .= $mctr . ",";
        $contents .= $mtype . ",";
        $contents .= $minvno . ",";
        $contents .= $mmydt . ",";
        $contents .= $mname . ",";
        $contents .= $msubtotal . ",";
        $contents .= $mdiscount . ",";
        $contents .= $mfreight . ",";
        $contents .= $mtotqty . ",";
        $contents .= $mnetamt . "\n";

        $subtot = $subtot + $pmain['subtotal'];
        $totdisc = $totdisc + $pmain['discount'];
        $totfr = $totfr + $pmain['freight'];

        $totnet = $totnet + $pmain['netamt'];

        if ($_POST["dtype"] == 2) {
            $contents .= "Sr,Brand,Item Name,Taka,Qty.,Tot. Qty.,Flag,Flag Qty.,Inv. Qty.,Rate,Amount\n";

            $ctr2 = 0;
            $totqty = 0;
            $totamt = 0;
            $totinv = 0;
            $totdtqty = 0;
            $totdtinv = 0;
            $totdtamt = 0;
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $getbrand = mysqli_query($conn, "SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                $mbrand = mysqli_fetch_array($getbrand);

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

                $ctr2++;

                $ctr2;
                $mmbname = $mbrand['name'];
                $mminame = $mitem['name'];
                $mmtaka = $tran['taka'];
                $mmqty = $tran['qty'];
                $mmtotqty = $tran['totqty'];
                $mmflag = $tran['flag'];
                $mmflagqty = $tran['flagqty'];
                $mminvqty = $tran['invqty'];
                $mmrate = $tran['rate'];
                $mmamount = $tran['amount'];

                $contents .= $ctr2 . ",";
                $contents .= $mmbname . ",";
                $contents .= $mminame . ",";
                $contents .= $mmtaka . ",";
                $contents .= $mmqty . ",";
                $contents .= $mmtotqty . ",";
                $contents .= $mmflag . ",";
                $contents .= $mmflagqty . ",";
                $contents .= $mminvqty . ",";
                $contents .= $mmrate . ",";
                $contents .= $mmamount . "\n";

                $totdtqty = $totdtqty + $tran['totqty'];
                $totdtinv = $totdtinv + $tran['invqty'];
                $totdtamt = $totdtamt + $tran['amount'];
                $totqty1 = $totqty1 + $tran['totqty'];
            }
        }
    }



    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . "Grand Total" . ",";
    $contents .= number_format($subtot, 2, '.', '') . ",";
    $contents .= number_format($totdisc, 2, '.', '') . ",";
    $contents .= number_format($totqty1, 2, '.', '') . ",";
    $contents .= number_format($totnet, 2, '.', '') . "\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;
    }
}
?>