<?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 = 'Current Stock Register';
$zroot = $_SESSION["zdir"];
$file = 'stock_fr.php';

$order = $_POST["orderby"];
$prnzero = $_POST["prnzero"];


if (!$order == 'C') {
    $orderby = " order by a.prodcode ";
} else {
    $orderby = " order by a.name ";
}

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

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

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

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

if (empty($mtodate)) {
    browse("As On Date Cannot Be Empty");
    echo"<script>window.close();</script>";
}

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

if ($prnzero == 'Y') {
    $getpmain = mysqli_query($conn, "SELECT a.prodcode, a.name, sum(b.qty) as tqty, a.rate FROM `$maindb` a, `$trandb` b  
                                            WHERE b.`vouchdt` <= '$mtodate' and a.prodcode = b.prodcode $codecond $pcodecond 
                                            group by prodcode $orderby ")or die(mysqli_error($conn));
} else {
    $getpmain = mysqli_query($conn, "SELECT a.prodcode, a.name, sum(b.qty) as tqty, a.rate FROM `$maindb` a, `$trandb` b  
                                            WHERE b.`vouchdt` <= '$mtodate' and a.prodcode = b.prodcode $codecond $pcodecond 
                                            group by prodcode having sum(b.qty) > 0 $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: #4eb3c7;
        }
        .td1{
           text-align:right;
           /*color:black;*/
           font-size:14px;
           font-weight: bold;
        }
        .tr1{
           background: #98f9f0;
        }
    </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-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%">Sr</th>
                                    <th style="width:10%">Code</th>
                                    <th style="width:25%">Name</th>
                                    <th style="width:20%; text-align:right;">Balance Qty.</th>
                                    <th style="width:20%; text-align:right;">Rate</th>
                                    <th style="width:20%; text-align:right;">Total Value</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    $ctr++;
                                    $value = $pmain[tqty] * $pmain[rate] ;
                                    
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$pmain[prodcode]</td>";
                                    Echo "<td>$pmain[name]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[tqty]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[rate]</td>";
                                    Echo "<td style='text-align:right;'>$value</td>";
                                    Echo "</tr>";

                                    $totqty = $totqty + $pmain[tqty];
                                    $totval = $totval + $value;
                                }
                                
                                Echo "<tr class='tr1'>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td class='td1'><b>Grand Total</b></td>";
                                Echo "<td class='td1'><b>$totqty</b></td>";
                                Echo "<td></td>";
                                Echo "<td class='td1'><b>$totval</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}


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

                                $contents .= "Sr,Code,Name,Balance Qty.,Rate,Total Value\n";
                            
                                $ctr = 0;
                                $totqty = 0;

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    $ctr++;
                                    $value = $pmain[tqty] * $pmain[rate] ;
                                    
                                    $contents .= "$ctr" . ",";
                                    $contents .= "$pmain[prodcode]" . ",";
                                    $contents .= "$pmain[name]" . ",";
                                    $contents .= "$pmain[tqty]" . ",";
                                    $contents .= "$pmain[rate]" . ",";
                                    $contents .= "$value" . "\n";
                                    

                                    $totqty = $totqty + $pmain[tqty];
                                    $totval = $totval + $value;
                                }
                                
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "Grand Total" . ",";
                                $contents .= "$totqty" . ",";
                                $contents .= "" . ",";
                                $contents .= "$totval" . "\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;
    }
}
?>