<?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 = 'Used Sale Return Register';
if ($admin == 1) {
    $maindb = 'outmain';
    $trandb = 'outtran';
} else {
    $maindb = 'outmain_d';
    $trandb = 'outtran_d';
}

$grid = 'usedsalreg.php';


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

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

    if (!empty($_POST["party"])) {
        $pname = trim($_POST["party"]);
        $pcond = "AND a.`party` = '$pname'";
    }

     if (!empty($_POST["staff"])) {
        $sname = trim($_POST["staff"]);
        $scond = "AND b.`staff` = '$sname'";
    }
    
    if (empty($_POST["frmdate"])) {
        browse("FROM DATE CANNOT BE EMPTY");
        header("refresh:1; usedsalreg.php");
    }
    if (empty($_POST["todate"])) {
        browse("TO DATE CANNOT BE EMPTY");
        header("refresh:1; usedsalreg.php");
    }

    $mfrmdate = trim($_POST["frmdate"]);
    $mtodate = trim($_POST["todate"]);

//    $get = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
//                       `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $pcond $scond $ptcond") or die(mysqli_error($conn));

    $get = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt FROM `$maindb` a, accmast b WHERE
                       a.name = b.accode AND a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' $pcond $scond ") or die(mysqli_error($conn));

    $getno = mysqli_num_rows($get);
    include("includes/files.php");
    ?>
    <!DOCTYPE HTML>
    <html lang="en-US">

        <head>
            <meta charset="UTF-8">
            <title><?php echo $flag . " " . $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/uniform/themes/default/css/uniform.default.css">
            <link rel="stylesheet" href="assets/lib/inputlimiter/jquery.inputlimiter.1.0.css">
            <link rel="stylesheet" href="assets/lib/chosen/chosen.min.css">
            <link rel="stylesheet" href="assets/lib/colorpicker/css/colorpicker.css">
            <link rel="stylesheet" href="assets/lib/tagsinput/jquery.tagsinput.css">
            <link rel="stylesheet" href="assets/lib/daterangepicker/daterangepicker-bs3.css">
            <link rel="stylesheet" href="assets/lib/datepicker/css/datepicker.css">
            <link rel="stylesheet" href="assets/lib/timepicker/css/bootstrap-timepicker.min.css">

            <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.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>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        </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>$formname &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size='3px'>Date : " . date("d-m-Y") . "</b><br />";
            echo "From Date - " . date("d-m-Y", strtotime($mfrmdate)) . "<br/>
                To Date &nbsp;&nbsp;&nbsp;&nbsp;-  " . date("d-m-Y", strtotime($mtodate)) . "<br/>";
            echo "<b>" . "Total Records - $getno</b><br />";
            echo "<br />";
            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 class="table table-bordered sortableTable responsive-table">
                                <thead>
                                    <tr>
                                        <th align="left" width='8%'>Voucher<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='9%'>Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="left" width='35%'>Customer<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        
                                        <th align="right" width='12%' style="text-align: right;">Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" width='12%' style="text-align: right;">Discount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" width='12%' style="text-align: right;">VAT<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                        <th align="right" width='12%' 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
                                        $totamt = 0;
                                        $billamt = 0;
                                        $tdisc = 0;
                                        $tvat = 0;
                                        
                                        $ctr = 0;
                                        while ($row = mysqli_fetch_object($get)) {
                                            mysqli_select_db($conn, $zdb);
                                            $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode`='$row->name'") or die(mysqli_error($conn));
                                            $party = mysqli_fetch_object($getparty);
                                            $ctr++;
                                            echo "<tr> ";
                                            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->subtotal</td>";
                                            echo "<td align='right'>$row->discount</td>";
                                            echo "<td align='right'>$row->vat</td>";
                                            echo "<td align='right'>$row->netamt</td>";
                                            
                                            $totamt = $totamt + $row->subtotal;
                                            $billamt = $billamt + $row->netamt;
                                            $tdisc = $tdisc + $row->discount;
                                            $tvat  = $tvat + $row->vat;                                            
                                            
                                            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.itemcode = 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='200'><font face=$style size='$size'>Item</font></th>";
                                                   echo "<th align='left' width='100'><font face=$style size='$size'>Colour</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>5</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>6</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>7</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>8</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>9</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>10</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>11</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>12</font></th>";
                                                   echo "<th  style='text-align: right;' width='30'><font face=$style size='$size'>Other</font></th>";
                                                   echo "<th  style='text-align: right;'><font face=$style size='$size'>Total Qty</font></th>";
                                                   echo "<th align='right' style='text-align: right;'><font face=$style size='$size'>Mrp</font></th>";
                                                   echo "<th align='right' style='text-align: right;'><font face=$style size='$size'>Rate</font></th>";
                                                   echo "<th align='right' style='text-align: right;'><font face=$style size='$size'>Amount</font></th>";
                                                 echo "<tr>";

                                                $ctrdtl = 0;
                                                while ($row2 = mysqli_fetch_object($gettran)){
                                                    $getitem = mysqli_query($conn, "SELECT * FROM `color` WHERE `code`='$row2->color'") or die(mysqli_error($conn));
                                                    $item = mysqli_fetch_array($getitem);
                                                    
                                                    $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:left;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'>". $item["name"]."</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'> $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->mrp</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->amount</font> </span> \n </td> \n";
                                                    echo "</tr>";
                                                }
                                                echo "</table>";
                                                echo "</td>";
                                            }
                                        }
                                            echo "<tr> ";
                                            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($totamt, 2, '.', '') . "</b></font></td>";
                                            echo "<td align='right'><font><b>" . number_format($tdisc, 2, '.', '') . "</b></font></td>";
                                            echo "<td align='right'><font><b>" . number_format($tvat, 2, '.', '') . "</b></font></td>";
                                            echo "<td align='right'><font color='green'><b>". number_format($billamt, 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
?>