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

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

$maindb = 'dammain_mi';
$trandb = 'damtran_mi';
$zroot = $_SESSION["zdbroot"];

$formname = "Damaged Register";
$file = 'midamreg_fr.php';
$keyfld = "vouchno";
$keycond = "";

if (isset($_POST["view"])) {
    $pchtype = $_POST["pchtype"];
    $godown = $_POST["godown"];
    $dtype = $_POST["dtype"];

    if (!empty($pchtype)) {
        $pchtypecond = " AND `type`= '$pchtype'";
    }
    if (!empty($godown)) {
        $godowncond = " AND `godown` = '$godown' ";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`= '$dtype' ";
    }

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

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));



    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $godowncond $pchtypecond ")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #26A69A;
        }
       
        .colr{
            background: #26A69A;
            font-size: 17px;
        }

    </style>
    </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-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width="15%;">Sr</th>
                                <th width="15%;">Voucher</th>
                                <th width="15%;">Date</th>
                                <th width="20%;">Godown</th>
                                <th width="20%;">Type</th>
                                <th width="15%;" class="text-right">Total Quantity</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                
                                if ($pmain[type] == "R") {
                                    $pchtype = "Raw Material";
                                } else {
                                    $pchtype = "Packing Material";
                                }

                                $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                $sgodw = mysqli_fetch_array($ggodw);

                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$pmain[voucher]</td>";
                                Echo"<td>$pmain[mydt]</td>";
                                Echo"<td>$sgodw[gcode]</td>";
                                Echo"<td>$pchtype</td>";
                                Echo"<td class='text-right'>$pmain[totqty]</td>";
                                Echo"</tr>";


                                $totamt = $totamt + $pmain[totqty];

                                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 "<thead>";
                                    Echo "<tr class='mySearchTr2'>";
                                    Echo "<th style='width:5%;'>Sr</th>";
                                    Echo "<th style='width:15%;'>Item Name</th>";
                                    Echo "<th style='width:7%;'>HSN Code</th>";
                                    Echo "<th style='width:5%;'>Unit</th>";
                                    Echo "<th style='width:4%;' class='text-right '>Qty.</th>";
                                    Echo "<th style='width:5%;' class='text-right '>Rate</th>";
                                    Echo "</tr>";
                                    Echo "<thead>";
                                    Echo "<tr>";
//
//
                                    $ctr2 = 0;
                                    $totqty = 0;
                                    $totrate = 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));
                                        $showitem = mysqli_fetch_array($getitem);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[itemname]</td>";
                                        Echo "<td>$tran[hsncode]</td>";
                                        Echo "<td>$tran[unit]</td>";
                                        Echo "<td class='text-right'>$tran[qty]</td>";
                                        Echo "<td class='text-right'>$tran[rate]</td>";
                                        Echo "</tr>";

                                        $totqty = $totqty + $tran[qty];
                                        $totrate = $totrate + $tran[rate];
                                    }

                                    Echo "<tr>";
                                    Echo "<td class='dth'></td>";
                                    Echo "<td class='dth'></td>";
                                    Echo "<td class='dth'></td>";
                                    Echo "<td class='dth'>Total</td>";
                                    Echo "<td class='text-right dth'>$totqty</td>";
                                    Echo "<td class='text-right dth'>$totrate</td>";
                                    Echo "</tr>";
                                    Echo "</table>";
                                }
                            }
                            Echo "<tr>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='colr'></td>";
                            Echo "<td class='text-right colr'><b>Total</b></td>";
                            Echo "<td class='numeric text-right colr'><b>$totamt</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

    if (isset($_POST["excel"])) {
    $pchtype = $_POST["pchtype"];
    $godown = $_POST["godown"];
    $dtype = $_POST["dtype"];

    if (!empty($pchtype)) {
        $pchtypecond = " AND `type`= '$pchtype'";
    }
    if (!empty($godown)) {
        $godowncond = " AND `godown` = '$godown' ";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`= '$dtype' ";
    }

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

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));



    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $godowncond $pchtypecond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    
    $contents.= "Sr, Voucher, Date, Godown, Type, Total Quantity \n";

                            $ctr = 0;
                            $totamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                
                                if ($pmain[type] == "R") {
                                    $pchtype = "Raw Material";
                                } else {
                                    $pchtype = "Packing Material";
                                }

                                $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                $sgodw = mysqli_fetch_array($ggodw);

                                $ctr++;
                                $contents.= $ctr . ",";
                                $contents.= $pmain[voucher] . ",";
                                $contents.= $pmain[mydt] . ",";
                                $contents.= $sgodw[gcode] . ",";
                                $contents.= $pchtype . ",";
                                $contents.= $pmain[totqty] . "\n";

                                $totamt = $totamt + $pmain[totqty];

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

                                    $contents.="Sr, Item Name, HSN Code, Unit, Qty, Rate \n";

                                    $ctr2 = 0;
                                    $totqty = 0;
                                    $totrate = 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));
                                        $showitem = mysqli_fetch_array($getitem);

                                        $ctr2++;
                                        $contents.= $ctr2 . ",";
                                        $contents.= $tran[itemname] . ",";
                                        $contents.= $tran[hsncode] . ",";
                                        $contents.= $tran[unit] . ",";
                                        $contents.= $tran[qty] . ",";
                                        $contents.= $tran[rate] . "\n";

                                        $totqty = $totqty + $tran[qty];
                                        $totrate = $totrate + $tran[rate];
                                    }

                                    $contents.= "" . ",";
                                    $contents.= "" . ",";
                                    $contents.= "" . ",";
                                    $contents.= "Total" . ",";
                                    $contents.= $totqty . ",";
                                    $contents.= $totrate . "\n";
                                }
                            }
                            $contents.= "" . ",";
                            $contents.= "" . ",";
                            $contents.= "" . ",";
                            $contents.= "" . ",";
                            $contents.= "Total" . ",";
                            $contents.= $totamt . "\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;
    }
}
?>