<?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 = 'indemain';
$trandb = 'indetran';

$formname = 'Deemed Inward Register';
$zroot = $_SESSION["zdir"];
$file = 'deeminwdreg_fr.php';


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

    $dtye = $_POST["dtype"];


    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 `indemain` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));
    $get = 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>
    <!--<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-hover table-bordered">
                            
                                <tr class="mySearchTr">
                                    <th>Sr</th>
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Godown</th>
                                    <th style="text-align:right">Total Qty.</th>
                                </tr>
                            
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 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++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$pmain[vouchno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$godwn[gcode]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[totqty]</td>";
                                    Echo "</tr>";

                                    $totqty = $totqty + $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-bordered'>";
                                        Echo "<tr class='mySearchTr2'>";
                                        Echo "<th style='width:5%;'>Sr</th>";
                                        Echo "<th style='width:10%;'>Barcode</th>";
                                        Echo "<th style='width:10%;' class=''>Item Code</th>";
                                        Echo "<th style='width:15%;' class=''>Item Name</th>";
                                        Echo "<th style='width:10%;' class=''>HSN Code</th>";
                                        Echo "<th style='width:8%;' class=''>Gram</th>";
                                        Echo "<th style='width:8%;' class=''>Unit</th>";
                                        Echo "<th style='width:8%;' class='text-right'>Batch</th>";
                                        Echo "<th style='width:8%;' class='text-right'>Qty.</th>";
                                        Echo "<th style='width:20%;' class=''>Remark</th>";
                                        Echo "</tr>";


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

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[barcode]</td>";
                                            Echo "<td>$tran[itemcode]</td>";
                                            Echo "<td>$tran[itemname]</td>";
                                            Echo "<td>$tran[hsncode]</td>";
                                            Echo "<td class=''>$tran[gram]</td>";
                                            Echo "<td>$tran[unit]</td>";
                                            Echo "<td>$tran[batch]</td>";
                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                            Echo "<td>$tran[remark]</td>";
                                            Echo "</tr>";
                                            
                                            $trtotqty = $trtotqty + $tran[qty];
                                        }
                                            Echo "<tr class='mySearchTr2'>";
                                            Echo "<td></td>";
                                            Echo "<td></td>";
                                            Echo "<td></td>";
                                            Echo "<td></td>";
                                            Echo "<td></td>";
                                            Echo "<td></td>";
                                            Echo "<td></td>";
                                            Echo "<td class='text-right'>Total</td>";
                                            Echo "<td class='text-right'>$trtotqty</td>";
                                            Echo "<td></td>";
                                            Echo "</tr>";
                                        
                                        Echo "</table>";
                                        Echo "</td>";
                                    }
                                }

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

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

    $dtye = $_POST["dtype"];


    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'")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

   $contents.= "Sr, Voucher, Date, Godown, Total Qty \n";
    
   
                                $ctr = 0;
                                $totqty = 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, Item Code, Item Name, HSN Code, Gram, Unit, Batch, Qty, Remark \n";
                                        
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                        $ctr2 = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $ctr2++;
                                            $contents.= $ctr2 . ",";
                                            $contents.= $tran[barcode] . ",";
                                            $contents.= $tran[itemcode] . ",";
                                            $contents.= $tran[itemname] . ",";
                                            $contents.= $tran[hsncode] . ",";
                                            $contents.= $tran[gram] . ",";
                                            $contents.= $tran[unit] . ",";
                                            $contents.= $tran[batch] . ",";
                                            $contents.= $tran[qty] . ",";
                                            $contents.= $tran[remark] . "\n";
                                  
                                            
                                            $trtotqty = $trtotqty + $tran[qty];
                                        }
                                            $contents.= " " . ",";
                                            $contents.= " " . ",";
                                            $contents.= " " . ",";
                                            $contents.= " " . ",";
                                            $contents.= " " . ",";
                                            $contents.= " " . ",";
                                            $contents.= " " . ",";
                                            $contents.= "Total" . ",";
                                            $contents.= $trtotqty . ",";
                                            $contents.= " " . "\n";
                                         
                                    }
                                }
                                
                                $contents.= " " . ",";
                                $contents.= " " . ",";
                                $contents.= " " . ",";
                                $contents.= "Total" . ",";
                                $contents.= $totqty . "\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;
    }
}

    
?>