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


$maindb = "expordmain";
$trandb = "";
$formname = "STOCK SUMMARY";
$grid = "menu.php";

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

$series = $_GET['series'];
$weeklyfrmdt = $_GET['fdate'];
$weeklytodt = $_GET['tdate'];
$month = $_REQUEST['month'];

if ($month != '') {
    $vdatecond = "and month(`date`)='$month'";
} else {
    $vdatecond = "and `date`>='$weeklyfrmdt' and `date`<='$weeklytodt'";
}

mysqli_query($conn, "create temporary table chalitemtran SELECT sum(`qty`) as iqty, size, itemcode FROM `chaltran` WHERE 
                                  chalno in (SELECT chalno FROM chalmain WHERE chalno != '' $vdatecond)
                                  group by itemcode") or die(mysqli_error($conn));

mysqli_query($conn, "create temporary table chalitemgrp SELECT a.`iqty`, a.`itemcode`,  a.`size`, b.`name` as itemname, b.`itemgrp` FROM `chalitemtran` a, `item` b 
                 WHERE a.`itemcode`=b.`code`") or die(mysqli_error($conn));

$gettopitemgrp = mysqli_query($conn, "SELECT sum(a.`iqty`) as `sqty`, b.`name` as `itemgrpname`,`size`, `itemname` FROM `chalitemgrp` a, `itemgrp` b 
                                  WHERE a.`itemgrp`=b.`code`group by a.`itemcode`, a.`size` order by sum(a.`iqty`) desc LIMIT 0,5") or die(mysqli_error($conn));


//$rows[] = ['series' => $series,
//    'minstk' => $minstk,
//    'maxstk' => $maxstk,
//    'zerostk' => $zerostk,];

include("includes/linkshead.php");
?>
<style> 
    h5{
        font-size: 18px;
        margin-top: 7px;
        margin-left: 6px;
    }
    .report{
        margin-top: 0px;
    }
    /*    @media print {
            thead {display: table-header-group;}
            table{font-size: 8px}
            @page {size: landscape}
        }*/
</style>    
</head>
<body>
    <div class="container">
        <?php include("includes/head_rep.php"); ?>
        <div class="col-lg-12 pad">
            <table style="width:100%;" class="table table-responsive table-condensed table-hover table-bordered">
                <thead>
                    <tr style="height:10px;">
                        <th colspan="5" class="text-center" style="background: #4285f4; color: white">Top Item Groups</th>
                    </tr>
                    <tr style="height:10px;">
                        <th>Sr</th>
                        <th>Item Group</th>
                        <th>Itemname</th>
                        <!--<th>Size</th>-->
                        <th>Qty</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $ctr = 0;
                    while ($ftopitemgrp = mysqli_fetch_array($gettopitemgrp)) {
                        $itemgrp = $ftopitemgrp['itemgrpname'];
                        $qty = $ftopitemgrp['sqty'];
                        $ctr++;
                        echo "<tr style='background: white;'> ";
                        echo "<td align='left'>$ctr</td>";
                        echo "<td align='left'>$itemgrp</td>";
                        echo "<td align='left'>$ftopitemgrp[itemname]</td>";
//                        echo "<td align='left'>$ftopitemgrp[size]</td>";
                        echo "<td align='left'>$qty</td>";
                        echo "</tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
        <div class="col-lg-12 text-center" style="height:20px;background-color: #EF5350;color: #ffffff;">
            End 
        </div> 
    </div>


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