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

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

$formname = 'Partywise ItemGroup Sale Register';
$maindb   = 'saletran';
$trandb   = 'item';
$grid     = 'itemgrp_sale.php';

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

    $icond = "";
    $scond = "";
    $pcond = "";

    if (!empty($_POST["name"])) {
        $iname = trim($_POST["name"]);
        $icond = "AND a.itemcode = '$iname'";
        $getitemname = mysqli_query($conn, "SELECT `name` FROM `item` WHERE code='$iname'")or die(mysqli_error($conn));
        $itname = mysqli_fetch_object($getitemname);
        $mitem = $itname->name;
    }
    
    if (!empty($_POST["itemgrp"])) {
        $pname = trim($_POST["itemgrp"]);
        $pcond = "AND `b.itemgrp` = '$pname'";
    }
    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; itemwise_sale.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; itemwise_sale.php");
    }

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

//    $get = mysqli_query($conn, "SELECT *, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
//                       `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'") or die(mysqli_error($conn));
//    $getno = mysqli_num_rows($get);
    $get = mysqli_query($conn, "SELECT a.vouchdt,  sum(a.qty) as totqty, sum(a.totamt) as total, b.itemgrp from `$maindb` a,
                        `$trandb` b where a.itemcode = b.code and a.vouchdt >= '$mfrmdate' and a.vouchdt <= '$mtodate' $icond $pcond group by b.itemgrp") or die(mysqli_error($conn));
    
    $getno = mysqli_num_rows($get);

    ?>
    <?php include("includes/files.php");
          include("includes/tabsuf.php");
    ?>

    <?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]</b><br />";
            echo "<font size='2px'>Date : " . date("d-m-Y") . "</b>&nbsp;&nbsp;";
            echo "( From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "
                To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . " )&nbsp;&nbsp;";
            echo "" . "Total Records - $getno";
            echo "</font>"; 
    ?>

            <div class="row">
                <div class="col-lg-6" style="width:100%;">
                    <div class="box">
                        <header>
                            <h5><?php echo $formname; ?></h5>
                            <div class="toolbar">
                                <div class="btn-group">		  
                                     <div class="btn-group">
                          
                        <a href="#sortableTable" data-toggle="collapse" class="btn btn-default btn-sm minimize-box">
                          <i class="fa fa-angle-up"></i>
                        </a>

                           <input type='button' name='submit' class='btn btn-sm btn-danger' onClick="window.location.href='<?php echo $grid;?>'" value="Exit"  value='Save'/> 
                      </div>
                                </div>
                            </div>
                        </header>
                        <div id="sortableTable" class="body collapse in">
                            <table id="t01" class="table table-bordered sortableTable responsive-table">
                                <thead>
                                    <tr>
                                        <th align="left" width=''>Item Group<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th style=text-align:right;" width=''>Qty.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th style=text-align:right;" width=''>Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    </tr>
                                </thead>
                                <tbody>
                                 <?php
                                       $tqty    =0;
                                       $trate   =0;
                                       $totamt  =0;
                                       
                                       
//                                            echo "<label>Item Name - $mitem</label>";
                                        $ctr = 0;
                                        while ($row = mysqli_fetch_object($get)) {
                                            mysqli_select_db($conn, $zdb);
                                            $getitemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE  `code`='$row->itemgrp' ORDER BY `name`") or die(mysqli_error($conn));
                                            $itemgrp = mysqli_fetch_object($getitemgrp);
                                            
                                            $ctr++;
                                            echo "<tr> ";
                                            #echo "<td align='left'>$ctr</td>";
                                            echo "<td align='left'>$itemgrp->name</td>";
                                            echo "<td align='right'>$row->totqty</td>";
                                            echo "<td align='right'>$row->total</td>";
                                            
                                            
                                            $tqty    = $tqty   + $row->totqty; 
                                            $totamt  = $totamt + $row->total;
                                            
                                            echo "</tr>";
                                           
                                       }
                                         echo "<tr style='background-color:white;'> ";
                                            echo "<td align='right'><font><b>Total&nbsp;&nbsp;</font></b></td>";
                                            echo "<td align='right'><font><b>" . number_format($tqty, 2, '.', '') . "</b></font></td>";
                                            echo "<td align='right'><font color='green'><b>". number_format($totamt, 2, '.', '')."</b></font></td>";
                                            echo "</tr>";
                                        ?>


                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <!-- /.col-lg-6 -->
            </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
}// Submit Tag Close
?>