<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

$admin = $_SESSION["admin_" . $dbname];
$grid     = 'stksize.php?para="U"';

$formname = 'Used Stock';

if (isset($_POST["view"])) {
    $bcond = "";
    $ncond = "";
    $gcond = "";
    $subcond = "";

    $maindb = 'item';
    $trandb = 'usedsacn';

/*    if (!empty($_POST["barcode"])) {
        $bname = trim($_POST["barcode"]);
        $bcond = "AND barcode = $bname ";
    } */

    if (!empty($_POST["name"])) {
        $nname = trim($_POST["name"]);
        $ncond = " and b.`name` LIKE '$nname%' ";
    }

    if (!empty($_POST["itemgrp"])) {
        $gname = trim($_POST["itemgrp"]);
        $gcond = " and b.`group` = '$gname' ";
    }

    if (!empty($_POST["subitem"])) {
        $subitem = trim($_POST["subitem"]);
        $subcond = " and b.`subitem` = '$subitem' ";
    }

    $stkcond = '';
    if ($_POST["zerostk"] == 0) {
        $stkcond = " and a.`qty` > 0 ";
    }

    if ($admin == 1) {
       $prnamt = $_POST["prnamt"] ;
    }   

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

    $sizeqry1 = mysqli_query($conn, "SELECT a.`itemcode`, b.name as itemname, a.`color`, b.`group`, 
                             SUM(a.s5) as s5, SUM(a.s6) as s6, SUM(a.s7) as s7, SUM(a.s8) as s8, SUM(a.s9) as s9, 
                             SUM(a.s10) as s10, SUM(a.s11) as s11, SUM(a.s12) as s12, SUM(a.soth) as soth, 
                             sum(a.qty) as qty, b.`mrp`, b.`rate`  FROM `$trandb` a, item b 
                             WHERE a.vouchdt <= '$todt' AND a.itemcode = b.code $ncond $stkcond
                             $gcond $subcond GROUP BY a.itemcode, a.color ORDER BY b.name, a.color ") or die(mysqli_error($conn));
    
    $getno = mysqli_num_rows($sizeqry1);
    
    ?>    
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title><?php echo $formname; ?></title>
            <meta name="msapplication-TileColor" content="#5bc0de"/>
            <meta name="msapplication-TileImage" content="assets/img/metis-tile.png"/>

            <link rel="stylesheet" href="assets/lib/bootstrap/css/bootstrap.css">
            <link rel="stylesheet" href="assets/css/main.css"/>
            <link rel="stylesheet" href="assets/lib/Font-Awesome/css/font-awesome.css"/>

            <link rel="stylesheet" href="assets/css/theme.css">


            <link rel="stylesheet" href="assets/lib/datatables/css/demo_page.css">
            <link rel="stylesheet" href="assets/lib/datatables/css/DT_bootstrap.css">


            <script>
                (function(i, s, o, g, r, a, m) {
                    i['GoogleAnalyticsObject'] = r;
                    i[r] = i[r] || function() {
                        (i[r].q = i[r].q || []).push(arguments)
                    }, i[r].l = 1 * new Date();
                    a = s.createElement(o),
                            m = s.getElementsByTagName(o)[0];
                    a.async = 1;
                    a.src = g;
                    m.parentNode.insertBefore(a, m)
                })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

                ga('create', 'UA-1669764-16', 'onokumus.com');
                ga('send', 'pageview');

            </script>

            <script src="assets/lib/modernizr-build.min.js"></script>
        </head>
        <body >

            <?php
            require("includes/dbconnect.php");
            $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
            $zcomp = mysqli_fetch_assoc($comp);
            echo "<font size='4px'><b>$zcomp[compname]</font></b></font><br />";
            echo "<font size='2px'><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br /><br />";
            echo "<b><font size='3px'>" .$formname . " - As On Date : " . date('d-m-Y', strtotime($mtodate)) . "</b><br/>";
            echo "<b><font size='3px'>Report Date : " . date("d-m-Y") . "</b>";
            echo "&nbsp;&nbsp;&nbsp;&nbsp;<b>" . "Total Records - $getno</b><br /> ";
            echo "<br />";
            echo "</font>";
            ?>

            <div class="col-lg-6" style="width:100%;">
<!--                <div class="box">-->
                    <header>
<!--                        <h5><?php echo $formname; ?></h5>-->
<!--                        <h5><?php echo $formname . " - As On Date : " . date("d-m-Y", strtotime($mtodate)) . "<br/>"; ?></h5>
                        
                        <div class="toolbar">
                            <div class="btn-group">		  
                            </div>
                        </div>-->
                    </header>

                    <div id="sortableTable" class="body collapse in">
                        <table class="table table-bordered sortableTable responsive-table">
                            <thead>

                                <tr>
                                    <?php
                                    echo "<table class='table table-bordered sortableTable responsive-table'>";
                                    echo "<th align='left'><font face=$style size='$size'>Srno</font></th>";
                                    echo "<th align='left' width='370'><font face=$style size='$size'>Item</font></th>";
                                    echo "<th align='left' width='110'><font face=$style size='$size'>Colour</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>5</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>6</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>7</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>8</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>9</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>10</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>11</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>12</font></th>";
                                    echo "<th  style='text-align: right;' width='60'><font face=$style size='$size'>Other</font></th>";
                                    echo "<th  style='text-align: right;' width='80'><font face=$style size='$size'>Total Qty</font></th>";
                                    echo "<th align='right' width='100' style='text-align: right;'><font face=$style size='$size'>Mrp</font></th>";
                                    echo "<th style='text-align:right;'>Cost <i class='icon-sort'></i><i class='icon-sort-down'></i> <i class='icon-sort-up'></i></th>";                                                             

                                    $totval = $totqty * $cost;
                                    $tqty = $tqty + $totqty;

                                    if ($admin == 1) {
                                        if ($prnamt == 1) {
                                        ?>
                                        <th style="text-align:right;">Total Value<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                                             
                                        <?
                                      }  
                                    }

                                    if ($_POST["img"] == 1) {
                                        ?>
                                        <th style="text-align:center;">Image<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>                                                             
                                                <?php
                                            }
                                        ?>
                                </tr>
                            </thead>
                            <tbody>

                                <?php
                                mysqli_select_db($conn, $zdb);
                                $tval = 0;
                                $tqty = 0;
                                $ctr = 0;
                                while ($row2 = mysqli_fetch_assoc($sizeqry1)) {
                                       $ctr++;
                                       $itemcode = $row2["itemcode"];
                                       $itemname = $row2["itemname"];
                                       $colrcode = $row2["color"];
                                       $totqty = $row2["qty"]; 

                                       $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$itemcode'") or die(mysqli_error($conn));
                                       $itemmast = mysqli_fetch_array($getitem);
                                       $cost = $itemmast["rate"];
                                       $mrp  = $itemmast["mrp"];
                                       $imgpath = $itemmast["imgpath"];

                                       $getcolr = mysqli_query($conn, "SELECT * FROM `color` WHERE `code`='$colrcode'") or die(mysqli_error($conn));
                                       $colrmast = mysqli_fetch_array($getcolr);
                                       $colrname = $colrmast["name"];

                                        echo "<tr>";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $ctr</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:left;\" valign='top'> \n <span><font face=$style size='$size'>  $itemname  </font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:left;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'>" . $colrname . "</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s5]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s6]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s7]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s8]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s9]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s10]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s11]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[s12]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2[soth]</font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $totqty </font> </span> \n </td> \n";
                                        echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $mrp</font> </span> \n </td> \n";
                                        echo "<td style='text-align:right;'> $cost</td>";

                                        $totval = $totqty * $cost;
                                        $tqty = $tqty + $totqty;
                                        if ($admin == 1) {
                                           echo "<td style='text-align:right;'> $totval</td>";
                                        }
                                    
                                        if ($_POST["img"] == 1) {
                                           echo "<td style='text-align:right;'>";
                                           echo "<img src='$imgpath' alt='$name'  style='width:150px;height:auto;'>";
                                           echo "</td>";
                                        }
                                        $tval = $tval + $totval;
                                        echo "</tr>";
                                    }

                                    echo "<tr> ";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'>Total</td>";
                                            
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='right'><font><b>". number_format($tqty, 0, '.', '')."</b></font></td>";
                                            echo "<td align='left'></td>";
                                            echo "<td align='left'></td>";
                                            if ($admin == 1) {
                                                if ($prnamt == 1) {
                                                    echo "<td align='right'><font color='green'><b>". number_format($tval, 2, '.', '')."</b></font></td>";
                                               }
                                            }
                                     echo "</tr>";
                                ?>  
                            </tbody>
                        </table>
                    </div>
<!--                </div>-->
            </div>            

            <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
            <script>window.jQuery || document.write('<script src="assets/lib/jquery.min.js"><\/script>')</script> 
            <script src="assets/lib/bootstrap/js/bootstrap.js"></script>
            <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
            <script src="assets/lib/datatables/jquery.dataTables.js"></script>
            <script src="assets/lib/datatables/DT_bootstrap.js"></script>
            <script src="assets/lib/tablesorter/js/jquery.tablesorter.min.js"></script>
            <script src="assets/lib/touch-punch/jquery.ui.touch-punch.min.js"></script>
            <script src="assets/js/main.js"></script>
            <script>
                $(function() {
                    metisTable();
                    metisSortable();
                });
            </script>
        </body>
    </html>
 <?php
}
?>

