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

$maindb = "dammain";
$trandb = "damtran";
$formname = "Damaged Register";
$file = "damreg_fr.php";

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

    $mtype = $_POST['mtype'];
    $godown = $_POST['godown'];

    $mtypecond = "";
    $gdncond = "";

    if (empty($godown)) {
        $gdncond = "AND godown = '$godown'";
    }
    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 $gdncond")or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    
    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="background: white;width: 100%;border-radius: 5px" class="table table-responsive table-condensed table-hover ">
                            <thead >
                                <tr style="background-color: #1ABC9C">
                                    <th width="4%;">Sr</th>
                                    <th width="10%;">Date</th>
                                    <th width="10%;" class="text-right">Subtotal</th>
                                    <th width="38%;" >Remarks</th>
                                    <th width="5%;"></th>
                                    <th width="12%;"></th>                                
                                    <th width="7%;" class="text-right"></th>
                                    <th width="7%;" class="text-right"></th>
                                    <th width="7%;" class="text-right"></th>
                                </tr>
                            </thead>
                            <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='damaged.php?mode=edit&vouchno=$pmain[vouchno]' data-target='_blank'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td class='numeric'>$pmain[subtotal]</td>";
                                    Echo "<td >$pmain[remark1]  $pmain[remark2]</td>";
                                    Echo "</tr>";
                                    Echo "<tr>";

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

                                    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 "<thead>";
                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                        Echo "<th style='width:2%;'>Sr</th>";
                                        Echo "<th style='width:7%;'>Brand/Logo</th>";
                                        Echo "<th style='width:5%;'>Code</th>";
                                        Echo "<th style='width:10%;'>Description</th>";
                                        Echo "<th style='width:6%;' class='text-right'>Qty.</th>";
                                        Echo "<th style='width:6%;' class='text-right'>Bale Qty.(Kgs)</th>";
                                        Echo "<th style='width:6%;' class='text-right'>Tot. Qty.(Kgs)</th>";
                                        Echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                        Echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                        Echo "</tr>";
//                                        Echo "<thead>";
                                        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]' $itemcond ")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);

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</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></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right' ><b>Total </b></td>";
                                        Echo "<td class='text-right'><b> $totqty </b></td>";
                                        Echo "<td class='text-right'><b> $totdtinv </b></td>";
                                        Echo "<td class='text-right'><b> $totdtqty </b></td>";
                                        Echo "<td class='text-right' ></td>";
                                        Echo "<td class='text-right'><b> $totdtamt </b> </td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                            
                             }

                                Echo "<tr>";
                                Echo "<td></td>";
                                Echo "<td class='numeric text-right'><b>Total</b></td>";
                                Echo "<td class='numeric text-right'><b>$subtot</b></td>";
                                Echo "<td class='numeric text-right'><b></b></td>";
                                Echo "<td></td>";
                                Echo "</tr>";
                            ?>
                        </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;
    }
}
?>