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

$maindb = "debmain";
$trandb = "debtran";
$formname = "Debit Note Register";
$file = "debnotereg.php";

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

if (isset($_POST["view"])) {
    $name = $_POST[name];
    $gtype = $_POST[gtype];
    $mtype = $_POST[mtype];

    $namecond = "";
    $gtypecond = "";
    $mtypecond = "";

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

    if (!empty($gtype)) {
        $gtypecond = " AND `gtype`='$gtype' ";
    }

    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 `mtype`='$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'")or die(mysqli_error($conn));
    include("includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white" class="table table-condensed table-hover">
                        <thead>
                            <tr style="height:10px;background-color: #1ABC9C">
                                <th>Sr</th>
                                <th>Voucher No.</th>                            
                                <th>Date</th>
                                <th>Supplier</th>
                                <th>Type</th>
                                <th>Qty.</th>
                                <th>Amount</th>                                                        
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $subtot = 0;
                            $totqty1 = 0;
                            $totnet = 0;
                            
                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getgtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$pmain[gtype]'")or die(mysqli_error($conn));
                                $gtype = mysqli_fetch_array($getgtype);

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

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


                                $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);

                                if ($pmain[gtype] == "R") {
                                    $rttype = "Goods Return";
                                } else {
                                    $rttype = "Others";
                                }

                                $ctr++;
                                Echo "<tr>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[invno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$msupp[name]</td>";
                                Echo "<td>$rttype</td>";
                                Echo "<td>$pmain[totqty]</td>";
                                Echo "<td>$pmain[subtotal]</td>";
//                            Echo "<td>$pmain[netamt]</td>";
                                Echo "</tr>";
                                Echo "<tr>";

                                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 "<th style='background-color: #cccccc'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc'>Brand</th>";
                                    Echo "<th style='background-color: #cccccc'>item Name</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Taka</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Tot. Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Flag</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Flag Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Inv Qty.</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Discount</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Rate</th>";
                                    Echo "<th style='background-color: #cccccc' class='numeric'>Amount</th>";
                                    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]'")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++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$mbrand[name]]</td>";
                                        Echo "<td>$mitem[name]</td>";
                                        Echo "<td class='numeric'>$tran[taka]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[totqty]</td>";
                                        Echo "<td class='numeric'>$tran[flag]</td>";
                                        Echo "<td class='numeric'>$tran[flagqty]</td>";
                                        Echo "<td class='numeric'>$tran[invqty]</td>";
                                        Echo "<td class='numeric'>$tran[discount]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";
                                    }
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                              if ($_POST["dtype"] == 2) {
                                    echo "<tr> ";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td><h4><b>Total Qty.</b></h4></td>";
                                    echo "<td align='right' id='font'><h4><b>$totdtqty</b></h4></td>";
                                    echo "<td></td>";
                                    echo "<td align='right' id='font'><h4><b>$totdtinv</b></h4></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
//                                    echo "<td align='right' id='font'><h4><b>$totdtamt</b></h4></td>";
                                    echo "</tr>";
                                }
                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' id='font'><h4><b>$subtot</b></h4></td>";
//                        echo "<td align='right' id='font'><h4><b>$totdisc</b></h4></td>";
//                        echo "<td align='right' id='font'><h4><b>$totfr</b></h4></td>";
                            echo "<td align='right' id='font'><h4><b>$totqty1</b></h4></td>";
                            echo "<td align='right' id='font'><h4><b>$totnet</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    $namecond = "";
    $gtypecond = "";
    $mtypecond = "";

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

    if (!empty($gtype)) {
        $gtypecond = " AND `gtype`='$gtype' ";
    }

    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 `mtype`='$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'")or die(mysqli_error($conn));

    $contents.="Sr,Voucher No.,Date,Supplier,Type,Qty.,Amount\n";

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



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

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

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


        $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);

        if ($pmain[gtype] == "R") {
            $rttype = "Goods Return";
        } else {
            $rttype = "Others";
        }

        $ctr++;

        $mctr      = $ctr;
        $minvno    = $pmain[invno];
        $mmydt     = $pmain[mydt];
        $mname     = $msupp[name];
        $mrtype    = $rttype;
        $mtotqty   = $pmain[totqty];
        $msubtotal = $pmain[subtotal];

        $contents.=$mctr . ",";
        $contents.=$minvno . ",";
        $contents.=$mmydt . ",";
        $contents.=$mname . ",";
        $contents.=$mrtype . ",";
        $contents.=$mtotqty . ",";
        $contents.=$msubtotal . "\n";
        


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

            $contents.="Sr,Brand,Item Name.,Taka,Qty,Tot. Qty.,Flag,Flag Qty.,Inv Qty.,Discount,Rate,Amount\n";
            
            


            $ctr2 = 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++;
                
                 $mmctr      = $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];
                $mmdiscount = $tran[discount];
                $mmrate     = $tran[rate];
                $mmamount   = $tran[amount];


                $contents.=$mmctr . ",";
                $contents.=$mmbname . ",";
                $contents.=$mminame . ",";
                $contents.=$mmtaka . ",";
                $contents.=$mmqty . ",";
                $contents.=$mmtotqty . ",";
                $contents.=$mmflag . ",";
                $contents.=$mmflagqty . ",";
                $contents.=$mminvqty . ",";
                $contents.=$mmdiscount . ",";
                $contents.=$mmrate . ",";
                $contents.=$mmamount . "\n";
                
                
                $totdtqty = $totdtqty + $tran[totqty];
                $totdtinv = $totdtinv + $tran[invqty];
                $totdtamt = $totdtamt + $tran[amount];
                $totqty1 = $totqty1 + $tran[totqty];
            }
        }
    }

     $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.="Grand Total" . ",";
    $contents.=$subtot . ",";
    $contents.=$totqty1 . ",";
    $contents.=$totnet . "\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;
    }
}
?>