<?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);


$zroot = $_SESSION["zdbroot"];

$formname = 'Dept. Inward Outward Register';
$zroot = $_SESSION["zdir"];
$file = 'depinou_fr.php';


$code = $_POST["code"];
$prodname = $_POST["prodname"];


$codecond = "";
$prodnamecond = "";

    if (!empty($code)) {
        $codecond = " AND `dtype`='$code' ";
    }
    if (!empty($prodname)) {
        $prodnamecond = " AND `dtype`='$prodname' ";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        echo "<script>window.close();</script>";
        
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        echo "<script>window.close();</script>";
    }
    if (!empty($_POST["frmdt"]) && !empty($_POST["todt"])) {
        if ($_POST["todt"] < $_POST["frmdt"]) {
            browse("To Date Cannot Be Less Than From Date");
            echo "<script>window.close();</script>";
        }
    }
    
    $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 `stktran` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' order by vouchdt")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="">
                <div class="row">
                    <div class="col-lg-12 ">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%">Sr</th>
                                    <th style="width:10%">Date</th>
                                    <th style="width:10%">Shift</th>
                                    <th style="width:10%">Source</th>
                                    <th style="width:10%">Voucher</th>
                                    <th style="width:10%">Pump No.</th>
                                    <th style="width:10%; text-align:right;">Add</th>
                                    <th style="width:10%; text-align:right;">Less</th>
                                    <th style="width:30%; text-align:right;">Balance</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

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

                   

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$pmain[shift]</td>";
                                    Echo "<td>$pmain[source]</td>";
                                    Echo "<td>$pmain[vouchno]</td>";
                                    Echo "<td>$pmain[pumpno]</td>";
                                    Echo "<td>$godwn[gcode]</td>";
                                    Echo "<td>$godwn[gcode]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[totqty]</td>";
                                    Echo "</tr>";

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

                                }

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

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

                                Echo "<tr>";
                                $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;
    }
}
?>