<?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 = 'dismantle';
$zroot = $_SESSION["zdbroot"];
$formname = 'Dismantle';
$file = 'tidisreg_fr.php';



 $godown = $_POST["godown"];
    $dtype = $_POST["dtype"];

    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; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.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 ")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getpmain);
    
if (isset($_POST["view"])) {
   


    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </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-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%;">Sr</th>
                                <th style="width:5%;">Voucher</th>
                                <th style="width:7%;">Date</th>
                                <th style="width:8%;">Godown</th>
                                <th style="width:38%;" class="text-center" colspan="4">Add</th>
                                <th style="width:37%;" class="text-center" colspan="4">Less</th>
                            </tr>
                            <tr>
                                <th style="width:5%;"></th>
                                <th style="width:6%;"></th>
                                <th style="width:6%;"></th>
                                <th style="width:10%;"></th>
                                <th style="width:10%;">Item Code</th>
                                <th style="width:9%;">Gram</th>
                                <th style="width:9%;">Unit</th>
                                <th style="width:10%;" class="text-right">Qty</th>
                                <th style="width:10%;">Item Code</th>
                                <th style="width:9%;">Gram</th>
                                <th style="width:8%;">Unit</th>
                                <th style="width:10%;" class="text-right">Qty</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totaddqty = 0;
                            $totlessqty = 0;

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

                                
                                $voucher = $pmain['voucher'];
                                $mydt = $pmain['mydt'];
                                $gcode = $godwn['gcode'];
                                $itemcode = $pmain['itemcode'];
                                $gram = $pmain['gram'];
                                $unit = $pmain['unit'];
                                $qty = $pmain['qty'];
                                $litemcode = $pmain['litemcode'];
                                $lgram = $pmain['lgram'];
                                $lunit = $pmain['lunit'];
                                $lqty = $pmain['lqty'];
                                
                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$voucher</td>";
                                Echo"<td>$mydt</td>";
                                Echo"<td>$gcode</td>";
                                Echo"<td>$itemcode</td>";
                                Echo"<td>$gram</td>";
                                Echo"<td>$unit</td>";
                                Echo"<td class='text-right'>$qty</td>";
                                Echo"<td>$litemcode</td>";
                                Echo"<td>$lgram</td>";
                                Echo"<td>$lunit</td>";
                                Echo"<td class='text-right'>$lqty</td>";
                                Echo"</tr>";

                                $totaddqty = $totaddqty + $qty;
                                $totlessqty = $totlessqty + $lqty;
                            }
                            Echo "<tr class='mySearchTr'>";
                            Echo "<td class='text-right h4' colspan='7'><b>Total</b></td>";
                            Echo "<td class='text-right h4'><b>$totaddqty</b></td>";
                            Echo "<td class='text-right h4' colspan='3'><b>Total</b></td>";
                            Echo "<td class='text-right h4'><b>$totlessqty</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $godown = $_POST["godown"];
    $dtype = $_POST["dtype"];

    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; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.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 ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);



    $contents .= "Sr., Date, Godown, Add,,,,Less,,,,\n";
    $contents .= ",,,Item Code,Gram,Unit,Qty,Item Code,Gram,Unit,Qty\n";




    $ctr = 0;
    $totaddqty = 0;
    $totlessqty = 0;

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

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

        $ctr++;


        $contents .= $ctr . ",";
        $contents .= $pmain['voucher'] . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $godwn['gcode'] . ",";
        $contents .= $pmain['itemcode'] . ",";
        $contents .= $pmain['gram'] . ",";
        $contents .= $pmain['unit'] . ",";
        $contents .= $pmain['qty'] . ",";
        $contents .= $pmain['litemcode'] . ",";
        $contents .= $pmain['lgram'] . ",";
        $contents .= $pmain['lunit'] . ",";
        $contents .= $pmain['lqty'] . "\n";

        $totaddqty = $totaddqty + $pmain['qty'];
        $totlessqty = $totlessqty + $pmain['lqty'];
    }

    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $totaddqty . ",";
    $contents .= " " . ", ";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= $totlessqty . "\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;
    }
}
?>