<?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 = 'transacn';
$zroot = $_SESSION["zdbroot"];
$formname = 'Stock';
$file = 'stock.php';


$prodcode = $_POST["prodcode"];
$product = $_POST["product"];
$prodgrp = $_POST["prodgrp"];
$type = $_POST["type"];

$action = 'prodhist_fr.php';
$pcodecond = '';
$prodcond = '';
$grpcond = '';
$godwncond = '';
$locncond = '';

$godown = $_POST["godown"];
$location = $_POST["location"];
if (!empty($godown)) {
    $godwncond = " AND `godown` = '$godown' ";
}
if (!empty($location)) {
    $locncond = " AND `location` = '$location' ";
}

if (!empty($prodcode)) {
    $pcodecond = " AND `code` = '$prodcode' ";
}

if (!empty($prodgrp)) {
    $grpcond = " AND `itemgrp` = '$prodgrp' ";
}


$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);

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

$gprodct = mysqli_query($conn, "SELECT * FROM `item` where code != ''  $pcodecond $grpcond  order by name")or die(mysqli_error($conn));

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #7E827E;
        }
        .tsize{
            font-size:18px;
            padding : 6px;
            color   : white;
        }
        tr td{
            font-size:14px;
        }

    </style>
    </head>
    <body>
        <div class="col-lg-4"></div>
        
        <div class="col-lg-5 container">

            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-bordered">
                        <tr class="mySearchTr">
                            <td style="color: white"><b><?php echo $title; ?></b></td>
                        </tr>
                    </table>


                    <table style="background: white;" class="table table-condensed table-hover table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%;">Sr</th>
                                <th style="width:3%;">Code</th>
                                <th style="width:42%;">Product Name</th>
                                <th style="width:7%;" class='text-right'>Opening</th>
                                <th style="width:7%;" class="text-right">Add</th>
                                <th style="width:7%;" class="text-right">Less</th>
                                <th style="width:8%;" class="text-right">Balance</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $balance = $totinwd = 0;
                            while ($fproduct = mysqli_fetch_array($gprodct)) {

                                $barcode = $fproduct['barcode'];
                                $itemname = $fproduct['name'];
                                $icode = $fproduct['code'];
                                $irate = $fproduct['rate'];
                                $cost = $fproduct['cost'];


                                $getopen = mysqli_query($conn, "SELECT sum(`qty`) as sqty FROM `transacn` WHERE `itemcode`='$icode'
                                                        and vouchdt < '$mfrmdate' $godwncond $locncond")or die(mysqli_error($conn));
                                $fopen = mysqli_fetch_array($getopen);

                                $ginwd = mysqli_query($conn, "SELECT SUM(qty) AS `sqty`,itemcode FROM `transacn` WHERE `itemcode`='$icode'
                                                      and qty > 0 AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  $godwncond $locncond")or die(mysqli_error($conn));
                                $indward = mysqli_fetch_array($ginwd);

                                $goutwd = mysqli_query($conn, "SELECT SUM(`qty`) AS `sqty` FROM `transacn` WHERE `itemcode`='$icode'
                                                      and qty < 0  and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  $godwncond $locncond")or die(mysqli_error($conn));
                                $outwd = mysqli_fetch_array($goutwd);


                                if ($fopen["sqty"] != '') {
                                    $open = $fopen["sqty"];
                                } else {
                                    $open = 0;
                                }

                                if ($indward['sqty'] != '') {
                                    $inwd = $indward['sqty'];
                                } else {
                                    $inwd = 0;
                                }

                                if ($outwd["sqty"] != '') {
                                    $out = abs($outwd["sqty"]);
                                } else {
                                    $out = 0;
                                }


                                $balance = $open + $inwd - $out;
                                $ctr++;

                                Echo"<tr class='clickable-row2' data-href='$action?view=view&pcode=$prodcode&todate=$mtodate' [target]='_blank'>";
                                Echo"<td class=''>$ctr</td>";
                                Echo"<td class=''>$icode</td>";

                                Echo"<td class=''>$itemname</td>";
                                Echo"<td class='text-right'>$open</td>";
                                Echo"<td class='text-right'>$inwd</td>";
                                Echo"<td class='text-right'>$out</td>";
                                if ($balance >= 0) {
                                    Echo"<td class='text-right' style='color: blue;'><b>$balance</b></td>";
                                } else {
                                    Echo"<td class='text-right' style='color: red;'>$balance</td>";
                                }
                                Echo"</tr>";

                                $totopen = $totopen + $open;
                                $totinwd = $totinwd + $inwd;
                                $totout = $totout + $out;
                                $totbal = $totbal + $balance;
                            }

                            Echo "<tr style='background-color:#7E827E'>";
                            Echo "<td></td>";
                            Echo "<td></td>";
//                            Echo "<td></td>";
                            Echo "<td class='text-right tsize'>Total</td>";
                            Echo "<td class='text-right tsize'><b>$totopen</b></td>";
                            Echo "<td class='text-right tsize'><b>$totinwd</b></td>";
                            Echo "<td class='text-right tsize'><b>$totout</b></td>";
                            Echo "<td class='text-right tsize'><b>$totbal</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                    <? // } elseif($type == "L"){     ?>





                    <? // }     ?>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}


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

    $contents .= "Sr,Code,Product Name,Opening,Add,Less,Balance\n";
    
                            $ctr = 0;
                            $balance = $totinwd = 0;
                            while ($fproduct = mysqli_fetch_array($gprodct)) {

                                $barcode = $fproduct['barcode'];
                                $itemname = $fproduct['name'];
                                $icode = $fproduct['code'];
                                $irate = $fproduct['rate'];
                                $cost = $fproduct['cost'];


                                $getopen = mysqli_query($conn, "SELECT sum(`qty`) as sqty FROM `transacn` WHERE `itemcode`='$icode'
                                                        and vouchdt < '$mfrmdate'  $godwncond $locncond")or die(mysqli_error($conn));
                                $fopen = mysqli_fetch_array($getopen);

                                $ginwd = mysqli_query($conn, "SELECT SUM(qty) AS `sqty`,itemcode FROM `transacn` WHERE `itemcode`='$icode'
                                                      and qty > 0 AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  $godwncond $locncond")or die(mysqli_error($conn));
                                $indward = mysqli_fetch_array($ginwd);

                                $goutwd = mysqli_query($conn, "SELECT SUM(`qty`) AS `sqty` FROM `transacn` WHERE `itemcode`='$icode'
                                                      and qty < 0  and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  $godwncond $locncond")or die(mysqli_error($conn));
                                $outwd = mysqli_fetch_array($goutwd);


                                if ($fopen["sqty"] != '') {
                                    $open = $fopen["sqty"];
                                } else {
                                    $open = 0;
                                }

                                if ($indward['sqty'] != '') {
                                    $inwd = $indward['sqty'];
                                } else {
                                    $inwd = 0;
                                }


                                if ($outwd["sqty"] != '') {
                                    $out = abs($outwd["sqty"]);
                                } else {
                                    $out = 0;
                                }


                                $balance = $open + $inwd - $out;
                                $ctr++;

                                $contents .= "$ctr" . ",";
                                $contents .= "$icode" . ",";

                                $contents .= "$itemname" . ",";
                                $contents .= "$open" . ",";
                                $contents .= "$inwd" . ",";
                                $contents .= "$out" . ",";
                                $contents .= "$balance" . "\n";
                                
                                $totopen = $totopen + $open;
                                $totinwd = $totinwd + $inwd;
                                $totout = $totout + $out;
                                $totbal = $totbal + $balance;
                            }

                            $contents .= "" . ",";
                            $contents .= "Total" . ",";
                            $contents .= "$totopen" . ",";
                            $contents .= "$totinwd" . ",";
                            $contents .= "$totout" . ",";
                            $contents .= "$totbal" . "\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;
    }
}