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

$maindb = "ordoff";
$trandb = "ordofftran";

$formname = "OFFLINE REGISTER";
$file = "offlinereg.php";

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

if (isset($_POST["view"])) {
    $name = $_POST['name'];

    $namecond = "";

    if (!empty($name)) {
        $namecond = " AND `username`='$name' ";
    }
    if (!empty($frmdt)) {
        $frmdtcond = " AND `frmdt`='$frmdt' ";
    }

    if (!empty($todt)) {
        $todtcond = " AND `todt`='$todt' ";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; offlinereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; offlinereg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);
    
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `orderdt` >= '$mfrmdate' AND `orderdt` <= '$mtodate' $namecond ")or die(mysqli_error($conn));

    include("../includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container" style="width:90%">
            <div class="row">
                <?php include("../includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered">
                        <tr class="mySearchTr">
                            <th style="background-color: #B9408F;">Sr</th>
                            <th style="background-color: #B9408F;">Voucher</th>
                            <th style="background-color: #B9408F;">Date</th>
                            <th style="background-color: #B9408F;">Party Name</th>
                            <th style="background-color: #B9408F;">Sale Type</th>
                            <th style="background-color: #B9408F;">Address</th>
                            <th style="background-color: #B9408F;">Pin</th>
                            <th style="background-color: #B9408F;">Contact</th>
                            <th style="background-color: #B9408F;">Email</th>
                            <th style="background-color: #B9408F;">Total</th>
                            <th style="background-color: #B9408F;">Remark</th>
                        </tr>
                        <?php
                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $ctr++;

                            Echo "<tr style='background-color:white'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[vouchno]</td>";
                            Echo "<td>$pmain[orderdt]</td>";
                            Echo "<td>$pmain[username]</td>";
                            Echo "<td>$pmain[saletype]</td>";
                            Echo "<td>$pmain[add1] $pmain[add2]</td>";
                            Echo "<td>$pmain[pi]</td>";
                            Echo "<td>$pmain[contact]</td>";
                            Echo "<td>$pmain[email]</td>";
                            Echo "<td>$pmain[total]</td>";
                            Echo "<td>$pmain[remark]</td>";
                            Echo "</tr>";

                            if ($_POST["dtype"] == 2) {

                                Echo "<tr style='height:10px;'>";
                                Echo "<td colspan='25'>";
                                Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                Echo "<th style='background-color: #cccccc;width:5%;'>Sr</th>";
                                Echo "<th style='background-color: #cccccc;width:51%;'>Itemname</th>";
                                Echo "<th style='background-color: #cccccc';width:11%; >Size</th>";
                                Echo "<th style='background-color: #cccccc;width:11%;' class='numeric'>Qty</th>";
                                Echo "<th style='background-color: #cccccc;width:11%;' class='numeric'>Rate</th>";
                                Echo "<th style='background-color: #cccccc;width:11%;' class='numeric'>Amount</th>";

                                Echo "<tr>";


                                $ctr2 = 0;
//                                    $totamt = 0;
//                                    $totdisc = 0;

                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                while ($tran = mysqli_fetch_array($gettran)) {

                                    $ctr2++;
                                    Echo "<tr>";
                                    Echo "<td>$ctr2</td>";
                                    Echo "<td>$tran[prodname]</td>";
                                    Echo "<td>$tran[size]</td>";
                                    Echo "<td>$tran[qty]</td>";
                                    Echo "<td>$tran[mrp]</td>";
                                    Echo "<td>$tran[total]</td>";
                                    Echo "</tr>";
//                                        $totamt = $totamt + $tran[amount];
//                                        $totdisc = $totdisc + $tran[discount];
                                }
                                Echo "</table>";
                                Echo "</td>";
                            }
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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