<?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 = 'inwmain';
$trandb = 'inwtran';
$zroot = $_SESSION["zdbroot"];

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


//
//    $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);

if (isset($_POST["view"])) {
    
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #6082BF;
        }

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php");   ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:15%">Sr</th>
                                    <th style="width:15%">Voucher</th>
                                    <th style="width:20%">Date</th>
                                    <th style="width:20%">Godown</th>
                                    <th style="width:30%; text-align:right;">Total Qty.</th>
                                </tr>
                            </thead>
                            <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-hover table-bordered'>";
                                        Echo "<th style='width:5%;'>Sr</th>";
                                        Echo "<th style='width:10%;'>Barcode</th>";
                                        Echo "<th style='width:10%;' class='text-right'>Item Code</th>";
                                        Echo "<th style='width:15%;' class='text-right'>Item Name</th>";
                                        Echo "<th style='width:10%;' class='text-right'>HSN Code</th>";
                                        Echo "<th style='width:8%;' class='text-right'>Gram</th>";
                                        Echo "<th style='width:8%;' class='text-right'>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='text-right'>Remark</th>";
                                        Echo "<tr>";


                                        $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++;
                                            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>$tran[gram]</td>";
                                            Echo "<td>$tran[unit]</td>";
                                            Echo "<td>$tran[batch]</td>";
                                            Echo "<td>$tran[qty]</td>";
                                            Echo "<td>$tran[remark]</td>";
                                            Echo "</tr>";
                                            
                                            $trtotqty = $trtotqty + $tran[qty];
                                        }
                                            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>$trtotqty</td>";
                                            Echo "<td></td>";
                                            Echo "</tr>";
                                        
                                        Echo "</table>";
                                        Echo "</td>";
                                    }
                                }

                                Echo "<tr>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td><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"])) {

                            
    $contents.="Sr,Voucher,Date,Product,Slip No.,Qty.,Unit,Rate,Amount\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;
    }
}
?>