<?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 a.`item` = '$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 b.`party` = '$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.`invdt`, '%d-%m-%Y') as mydt, b.vouchdt,  b.vouchno, b.party, b.netamt FROM `$trandb`a, `$maindb`b
                       WHERE b.vouchdt >= '$mfrmdate' AND b.vouchdt <= '$mtodate' AND a.`vouchno`=b.`vouchno` $icond $pcond order by a.vouchno") or die(mysqli_error($conn));

    $getno = mysqli_num_rows($get);
    if ($getno == 0) {

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

    include("includes/linkshead.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 col-lg-12">		  
                            <div class="btn-group col-lg-10">

                                <a href="#sortableTable" data-toggle="collapse" class="btn btn-default btn-sm minimize-box">
                                    <i class="fa fa-angle-up"></i>
                                </a>
                            </div>
                            <div class="col-lg-2" style="float: right;">
                                <a onclick="window.close()" class="btn btn-primary">GO Back</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 class="mySearchTr">
                                <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->party' ORDER BY `name`") or die(mysqli_error($conn));
                                $party = mysqli_fetch_object($getparty);

                                $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->netamt</td>";

                                $tqty = $tqty + $row->qty;
                                $trate = $trate + $row->rate;
                                $totamt = $totamt + $row->netamt;

                                echo "</tr>";
                            }
                            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>  



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


    </body>
    </html>
    <?php
}// Submit Tag Close
?>