<?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 = 'Itemwise Sale Register';
   $maindb   = 'salemain';
   $trandb   = 'saletran';
$grid     = 'itemwise_sale.php';

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

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

    if (!empty($_POST["name"])) {
        $iname = trim($_POST["name"]);
        $icond = "AND `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["pname"])) {
        $pname = trim($_POST["pname"]);
        $pcond = "AND `accode` = '$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.*, DATE_FORMAT(b.`billdt`, '%d-%m-%Y') as mydt, b.vouchdt,  b.vouchno, b.accode, b.billamt FROM `$trandb`a, `$maindb`b
                       WHERE b.vouchdt >= '$mfrmdate' AND b.vouchdt <= '$mtodate' AND a.`vouchno`=b.`vouchno` $icond $pcond") 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 "$zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><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='7%'>Voucher No.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='7%'>Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='24%'>Party Name<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" width='10%' style="text-align: right;">Qty.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" width='9%' style="text-align: right;">Rate<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" width='11%' style="text-align: right;">Bill 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);
                                            $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode`='$row->accode' ORDER BY `name`") or die(mysqli_error($conn));
                                            $party = mysqli_fetch_object($getparty);
                                            /*$getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE  `code`='$row->item' ORDER BY `name`") or die(mysqli_error($conn));
                                            $item = mysqli_fetch_object($getitem);*/
                                            
                                            $ctr++;
                                            echo "<tr> ";
                                            #echo "<td align='left'>$ctr</td>";
                                            echo "<td align='left'>$row->vouchno</td>";
                                            echo "<td align='left'>$row->mydt</td>";
                                            echo "<td align='left'>$party->name</td>";
                                            echo "<td align='right'>$row->qty</td>";
                                            echo "<td align='right'>$row->rate</td>";
                                            echo "<td align='right'>$row->billamt</td>";
                                            
                                            $tqty    = $tqty   + $row->qty; 
                                            $trate   = $trate  + $row->rate;                    
                                            $totamt  = $totamt + $row->billamt;
                                            
                                            echo "</tr>";
                                            /*if ($_POST["dtype"] == 2) {
//                                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$row->vouchno' ORDER BY vouchno") or die(mysqli_error($conn));
                                                $gettran = mysqli_query($conn, "SELECT a.*, b.name as item FROM `$trandb` a, item b 
                                                           WHERE a.vouchno='$row->vouchno' AND a.item = b.code ORDER BY vouchno, srno") or die(mysqli_error($conn));;
                                                
                                                $style = "calibri";
                                                $size = '2px';
                                                echo "<tr>";
                                                 echo "<td colspan='25'>";
                                                  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='250'><font face=$style size='$size'>Item</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Packing</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Unit</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Qty.</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Free Qty.</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Free Pcs.</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Sub Total</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Rate</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Amount</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>MRP</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Disc %</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Discount</font></th>";
                                                   echo "<th align='right' style='text-align: right;'><font face=$style size='$size'>Total</font></th>";
                                                 echo "<tr>";
                                                $ctrdtl = 0;
                                                while ($row2 = mysqli_fetch_object($gettran)){
                                                    $getcolr = mysqli_query($conn, "SELECT * FROM `color` WHERE `code`='$row2->color'") or die(mysqli_error($conn));
                                                    $colr1 = mysqli_fetch_array($getcolr);
                                                    
                                                    $ctrdtl++;
                                                    
                                                    echo "<tr>";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $ctrdtl</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:left;\" valign='top'> \n <span><font face=$style size='$size'> $row2->item</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'>$row2->packing</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'>$row2->unit </font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->qty</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->freeqty</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->freepcs</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->total</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->rate</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->amt</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->mrp</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->discperc</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->discamt</font> </span> \n </td> \n";
                                                    echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->final</font> </span> \n </td> \n";
                                                    echo "</tr>";
                                                }
                                                echo "</table>";
                                                echo "</td>";
                                            }*/
                                        }
                                         echo "<tr style='background-color:white;'> ";
                                            echo "<td align='left'></td>";
                                            
                                            echo "<td align='left'></td>";

                                            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><b>" . number_format($trate, 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
?>