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

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

$reptype = $_POST["reptype"];

$qtycond = "" ;
if ($reptype == "I"){
   $qtycond = " and qty > 0 " ;    
} else {
    if ($reptype == "O"){
        $qtycond = " and qty < 0 " ;    
    }
}


if (empty($_POST["fromdate"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>"
    . "window.close();"
            . "</script>";
}

if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>"
    . "window.close();"
            . "</script>";
}

$mfrmdate = trim($_POST["fromdate"]);
$mtodate = trim($_POST["todate"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$mtodate = $_POST["todate"];
$mcode = $_POST["code"];
$mprodcode = $_POST["prodname"];

$codecond = '';
$pcodecond = '';

if ($mcode != '') {
    $codecond = " and prodcode = '$mcode'  ";
}

if ($mprodcode != '') {
    $pcodecond = " and prodcode = '$mprodcode'  ";
}

if (empty($mcode) && empty($mprodcode) ) {
    browse("Product Code / Name Cannot Be Empty");
    echo "<script>"
    . "window.close();"
            . "</script>";
    
}

$orderby = " order by vouchdt, source, vouchno ";

$getp = mysqli_query($conn, "SELECT prodcode, name, unit FROM `$maindb` WHERE name != '' $codecond $pcodecond ")or die(mysqli_error($conn));
$fetp = mysqli_fetch_array($getp) ;
$pname = $fetp['name'] ;
$pcode = $fetp['prodcode'] ;
$punit = $fetp['unit'] ;

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$trandb`  
                                        WHERE `vouchdt` >= '$mfrmdate' and `vouchdt` <= '$mtodate'  
                                        $codecond $pcodecond $qtycond $orderby ")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-3 col-md-3 col-sm-3 col-xs-3 text-left pad">
                        <label class="label-control marg"><b>Product : <?php echo $pcode . '  -  ' .  $pname ?></b></label>
                    </div>
                    <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 text-right pad">
                        <label class="label-control marg"><b>Unit : <?php echo $punit ?></b></label>
                    </div>

                    <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:5%">Sr</th>
                                    <th style="width:15%">Date</th>
                                    <th style="width:15%">Voucher</th>
                                    <th style="width:10%; text-align:right;">Inward</th>
                                    <th style="width:10%; text-align:right;">Outward</th>
                                    <th style="width:10%; text-align:right;">Rate</th>
                                    <th style="width:15%; text-align:right;">Amount</th>
                                    <th style="width:15%; text-align:right;">Balance</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $bal = $qty = $tinw = $tout = 0;

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    
                                    $mydt = $pmain['mydt'] ;
                                    $source = $pmain['source'] ;
                                    $vouchno = $pmain['vouchno'] ;
                                    $vouch = $source . ' ' . $vouchno ;
                                    
                                    $rate = $pmain['rate'] ;
                                    
                                    $inward = $outward = 0 ;
                                    
                                    $qty = $pmain['qty'] ;
                                    if ($qty > 0) {
                                      $inward = $qty ;
                                      $tinw = $tinw + abs($qty) ;
                                    } else {
                                      $outward = $qty ;
                                      $tout = $tout + abs($qty) ;
                                    }
                                    $bal = $bal + $qty ;
                                    
                                    $amt = $rate * abs($qty) ;
                                    
                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$mydt</td>";
                                    Echo "<td>$vouch</td>";
                                    Echo "<td style='text-align:right;'>$inward</td>";
                                    Echo "<td style='text-align:right;'>$outward</td>";
                                    Echo "<td style='text-align:right;'>$rate</td>";
                                    Echo "<td style='text-align:right;'>$amt</td>";
                                    Echo "<td style='text-align:right;'>$bal</td>";
                                    Echo "</tr>";

                                }

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

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

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