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

$maindb = "dammain";
$trandb = "damtran";
$formname = "Damage Register";
$file = "tidamreg_fr.php";

$zroot = $_SESSION["zdir"];

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

if (isset($_POST["view"])) {

    $dtype = $_POST["dtype"];
    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);

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

    $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'")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; 
        }

        table tr td{
            font-size: 14px;
        }
    </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-hover table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:10%">Sr</th>
                                <th style="width:15%">Vouch No</th>
                                <th style="width:20%">Date</th>
                                <th style="width:25%">Godown</th>
                                <th style="width:25%" class="text-right">Total Qty.</th>
                            </tr>
                        </thead>
                        <tbody>
    <?php
    $ctr = 0;
    $totqty = 0;
    $total = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]'")or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($ggodwn);


        $vouchno = $pmain['vouchno'];
        $mydt = $pmain['mydt'];
        $gcode = $godwn['gcode'];
        $qty = $pmain['totqty'];





        $ctr++;
        Echo "<tr data-href='damaged.php?mode=edit&vouchno=$vouchno' class='clickable-row2' [target]='_blank'>";
        Echo "<td>$ctr</td>";
        Echo "<td>$vouchno</td>";
        Echo "<td>$mydt</td>";
        Echo "<td>$gcode</td>";
        Echo "<td class='text-right'>$qty</td>";
        Echo "</tr>";
        $totqty = $totqty + $qty;

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


            Echo "<tr style='height:10px;'>";
            Echo "<td colspan='25'>";
            Echo "<table style='width:100%;' class='table table-condensed table-bordered'>";

            Echo "<tr class='mySearchTr2'>";
            Echo "<td style='width:5%;'>Sr</td>";
            Echo "<td style='width:8%;'>Barcode</td>";
            Echo "<td style='width:9%;'>Product Code</td>";
            Echo "<td style='width:17%;'>Product Name</td>";
            Echo "<td style='width:8%;'>HSN Code</td>";
            Echo "<td style='width:5%;' class='text-right'>Gram</td>";
            Echo "<td style='width:5%;' class='text-right'>Unit</td>";
            Echo "<td style='width:5%;'>Batch</td>";
            Echo "<td style='width:5%;' class='text-right'>Qty.</td>";
            Echo "<td style='width:5%;' class='text-right'>Rate</td>";
            Echo "<td style='width:10%;' class='text-right'>Mrp</td>";
            Echo "</tr>";


            $ctr2 = 0;
            $tqty = 0;
            $tamt = 0;

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


                $barcode = $tran['barcode'];
                $itemcode = $tran['itemcode'];
                $hsncode = $tran['hsncode'];
                $gram = $tran['gram'];
                $unit = $tran['unit'];
                $batch = $tran['batch'];
                $tqty = $tran['qty'];
                $rate = $tran['rate'];
                $mrp = $tran['mrp'];

                $gitem = mysqli_query($conn, "SELECT name FROM `product` WHERE barcode ='$tran[itemcode]'") or die(mysqli_error($conn));
                $fitem = mysqli_fetch_array($gitem);
                $itemname = $fitem['name'];

                $ctr2++;
                Echo "<tr>";
                Echo "<td>$ctr2</td>";
                Echo "<td class='numeric'>$barcode</td>";
                Echo "<td>$itemcode</td>";
                Echo "<td>$itemname</td>";
                Echo "<td>$hsncode</td>";
                Echo "<td class='text-right'>$gram</td>";
                Echo "<td class='text-right'>$unit</td>";
                Echo "<td>$batch</td>";
                Echo "<td class='text-right'>$tqty</td>";
                Echo "<td class='text-right'>$rate</td>";
                Echo "<td class='text-right'>$mrp</td>";
                Echo "</tr>";
                $tqty = $tqty + $tran['qty'];
                $tamt = $tamt + $tran['amount'];
            }

            Echo "<tr>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td>Total</td>";
            Echo "<td class='text-right'>$totqty</td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "</tr>";

            Echo "</table>";
            Echo "</td>";
            Echo "</tr>";
        }
    }
    echo "<tr class='mySearchTr'> ";
    echo "<td align='right' colspan='4'><h4><b>Grand Total</b></h4></td>";
    echo "<td align='right' ><h4><b>$totqty</b></h4></td>";
    echo "</tr>";
    ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <?php include("includes/linksfoot.php");
    ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $itemcode = $_POST["itemcode"];
    $itemname = $_POST["itemname"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);

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

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; inweg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; inwreg.php");
    }

    $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));
    $get = mysqli_num_rows($getpmain);

    $contents .= "Sr.,Vouch No.,Date, Godown, Total Qty \n";

    $ctr = 0;
    $totqty = 0;
    $total = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]'")or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($ggodwn);

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['vouchno'] . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $godwn['gcode'] . ",";
        $contents .= $pmain['totqty'] . "\n";

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

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

            $contents .= "Sr.,Barcode, Product Code, Product Name,HSN Code, Gram, Unit, Batch,Qty., Rate,Mrp\n ";

            $ctr2 = 0;
            $tqty = 0;
            $tamt = 0;

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

                $gitem = mysqli_query($conn, "SELECT name FROM `product` WHERE barcode ='$tran[itemcode]'") or die(mysqli_error($conn));
                $fitem = mysqli_fetch_array($gitem);
                $itemname = $fitem['name'];


                $ctr2++;
                $contents .= $ctr2 . ",";
                $contents .= $tran['barcode'] . ",";
                $contents .= $tran['itemcode'] . ",";
                $contents .= $itemname . ",";
                $contents .= $tran['hsncode'] . ",";
                $contents .= $tran['gram'] . ",";
                $contents .= $tran['unit'] . ",";
                $contents .= $tran['batch'] . ",";
                $contents .= $tran['qty'] . ",";
                $contents .= $tran['rate'] . ",";
                $contents .= $tran['mrp'] . "\n";

                $tqty = $tqty + $tran['qty'];
                $tamt = $tamt + $tran['amount'];
            }

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