<?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 = 'Groupwise Sale Register';
$maindb = 'saletran';
$trandb = 'item';
$grid = 'partgrp_sale.php';

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

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

    if (!empty($_POST["name"])) {
        $iname = trim($_POST["name"]);

        $icond = "AND `party` = '$iname'";
    }

    if (!empty($_POST["itemgrp"])) {
        $pname = trim($_POST["itemgrp"]);
        $pcond = "AND `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(`vouchdt`, '%d-%m-%Y') as mydt FROM `salemain` WHERE
                       `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $icond group by party") or die(mysqli_error($conn));
//    $getno = mysqli_num_rows($get);
//    $get = mysqli_query($conn, "SELECT a.vouchdt, sum(a.qty) as totqty, sum(a.total) as total, b.itemgrp, b.name as itemname from `$maindb` a,
//                        `$trandb` b where a.itemcode = b.code and a.vouchdt >= '$mfrmdate' and a.vouchdt <= '$mtodate' $icond $pcond 
//                        group by b.itemgrp,b.name ") or die(mysqli_error($conn));
//    $get = mysqli_query($conn, "SELECT a.vouchdt, sum(a.qty) as totqty, sum(a.total) as total, a.item, b.party from `$maindb` a,
//                        `$trandb` b where a.vouchno = b.vouchno and a.vouchdt >= '$mfrmdate' and a.vouchdt <= '$mtodate' $icond group by b.party" ) or die(mysqli_error($conn));

    $getno = mysqli_num_rows($get);
//    print table browse($getno);
    

    if ($getno == 0) {

        browse("Record Not Found");
        echo "<script>
            window.close();
            </script>";
    }

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>

    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg">Date - </label>
                    <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
                </div>
                <div class="col-lg-5">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php Echo $formname; ?></label>
                </div>
                <div class="col-lg-1">
                    <a onclick="window.close()" class="btn btn-primary">GO Back</a>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed">
                        <!--<tbody>-->
    <!--                        <thead>
                            <tr>
                                
                            </tr>
                        </thead>-->
                        <tbody>
                            <?php
                            $tqty = 0;
                            $trate = 0;
                            $totamt = 0;
                            $a1 = 0;
                            $tname = '';

                            $ctr = 0;
                            while ($row = mysqli_fetch_object($get)) {
                                $getset = mysqli_query($conn, "SELECT sum(qty) as totqty, sum(total) as total, b.itemgrp FROM `saletran` a,
                                                        `item` b WHERE a.vouchno = '$row->vouchno' and a.item = b.code  ") or die(mysqli_error($conn));
                                $fetset = mysqli_fetch_object($getset);
                                
                                $itgrpname = $fetset->itemgrp;
                              
                                $getitemgrp1 = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE  `code`='$fetset->itemgrp' ORDER BY `name`") or die(mysqli_error($conn));
                                $itemgrp = mysqli_fetch_object($getitemgrp1);

                                $getaccmast = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode` = '$row->party' ") or die(mysqli_error($conn));
                                $namegrp = mysqli_fetch_object($getaccmast);
                                $partname = $namegrp->name;

                                if ($partname != $tname) {
                                    if ($a1 == 0) {
                                        ?>
                                        <tr>
                                            <td style=""><b>Party</b><?= $partname ?></td>
                                        </tr>

                                                                                                                            <!--<thead class="mySearchTr">-->
                                        <tr class="mySearchTr">
                                            <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>-->      
                                        <?php
                                    }
//                                    $ctr = 1;
//                                    echo "<tr style='background-color:white;height:30px;'><td><b>Party</b> $partname </td></tr>";
                                    $tname = $partname;
                                }
                                $ctr++;

                                echo "<tr> ";
                                #echo "<td align='left'>$ctr</td>";
                                echo "<td align='left'>$itemgrp->name</td>";
                                echo "<td align='right'>$fetset->totqty</td>";
                                echo "<td align='right'>$fetset->total</td>";


                                $tqty = $tqty + $fetset->totqty;
                                $totamt = $totamt + $fetset->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
?>