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

$maindb     = "excmain";
$trandb     = "exctran";
$formname   = "Excess Register";
$file       = "excreg.php";

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

if(isset($_POST["view"])){    
    $mtype = $_POST[mtype];    
        
    $mtypecond = "";    
            
    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; excreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; excreg.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'            $mtypecond")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>Invoice No</th>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Qty</th>
                            <th>Remark</th>                                                        
<!--                            <th class='numeric'>Qty</th>-->                                                        
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getmtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
                            $mtype = mysqli_fetch_array($getmtype);
                                                                                    
                            $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++;
                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[invno]</td>";
                            Echo "<td>$pmain[mydt]</td>";
                            Echo "<td>$mtype[name]</td>";
                            Echo "<td>$mqty[totqty]</td>";
                            Echo "<td>$pmain[remark1]</td>";                                                                                    
//                            Echo "<td class='numeric'>$mqty[totqty]</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 "<tr>";


                                $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++;
                                    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 "</tr>";
                                }
                                Echo "</table>";
                                Echo "</td>";
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
   <?php
}
if (isset($_POST["excel"])) {
    $mtype = $_POST[mtype];

    $mtypecond = "";

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; damreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; damreg.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' $mtypecond")or die(mysqli_error($conn));

    $contents.="Sr No.,Invoice No ,Date,Type,Qty,Remark\n";

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

        $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;
        $minvno = $pmain[invno];
        $mmydt = $pmain[mydt];
        $mtypname = $mtype[name];
        $mtotqty = $mqty[totqty];
        $mremark1 = $pmain[remark1];

        $contents.=$mctr . ",";
        $contents.=$minvno . ",";
        $contents.=$mmydt . ",";
        $contents.=$mtypname . ",";
        $contents.=$mtotqty . ",";
        $contents.=$mremark1 . "\n";

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

            $contents.="Sr,Brand,Item Name.,Taka,Qty,Tot. Qty.,Flag,Flag Qty.,Inv Qty.\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];

                $contents.=$mmctr . ",";
                $contents.=$mmbname . ",";
                $contents.=$mminame . ",";
                $contents.=$mmtaka . ",";
                $contents.=$mmqty . ",";
                $contents.=$mmtotqty . ",";
                $contents.=$mmflag . ",";
                $contents.=$mmflagqty . ",";
                $contents.=$mminvqty . "\n";
            }
        }
    }


//    $contents.=" " . ",";
//    $contents.=" " . ",";
//    $contents.=" " . ",";
//    $contents.=" " . ",";
//    $contents.="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;
    }
}
?>