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

$maindb = "order";
$trandb = "ordertran";

$formname = "New Orders List";
$file = "ordreg.php";
$keyfld = "code";

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


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

    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; ordreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; ordreg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`orderdt`, '%d-%m-%Y') AS orderdt FROM `$maindb` WHERE `orderdt` >= '$mfrmdate' 
                            AND `orderdt` <= '$mtodate' ")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;width:8%">Sr</th>
                            <th style="background-color: #B9408F;width:10%">Order No.</th>
                            <th style="background-color: #B9408F;width:25%">Client Name</th>
                            <th style="background-color: #B9408F;width:25%">Contact</th>
                            <th style="background-color: #B9408F;width:10%">Date</th>
                            <!--<th style="background-color: #B9408F">Qty</th>-->
                            <th style="background-color: #B9408F;width:12%;text-align: right">Sub Total</th>
                            <th style="background-color: #B9408F;width:10%;text-align: right">Discount</th>
                            <th style="background-color: #B9408F;width:13%;text-align: right">Total</th>
                            <th style="background-color: #B9408F;width:12%;">Invoice Print</th>
                        </tr>
                        <?php
                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getsupp = mysqli_query($conn, "SELECT `user`,`srno` FROM `site_passmain` WHERE srno='$pmain[userid]'")or die(mysqli_error($conn));
                            $supp = mysqli_fetch_array($getsupp);

                            $ctr++;

                            Echo "<tr>";
                            Echo "<td style=background-color:lightyellow>$ctr</td>";
                            Echo "<td style=background-color:lightyellow>$pmain[vouchno]</td>";
                            Echo "<td style=background-color:lightyellow>$pmain[username]</td>";
                            Echo "<td style=background-color:lightyellow>$pmain[contact]</td>";
                            Echo "<td style=background-color:lightyellow>$pmain[orderdt]</td>";
                            Echo "<td style=background-color:lightyellow class=text-right>$pmain[subtotal]</td>";
                            Echo "<td style=background-color:lightyellow class=text-right>$pmain[discount]</td>";
                            Echo "<td style=background-color:lightyellow class=text-right>$pmain[total]</td>";
                            Echo "<td style='text-align: center;'><a href='invoice_print.php?vouchno=$pmain[vouchno]' target='_blank'<button type='button' class='btn btn-primary active'>Print</a></td>";
                            Echo "</tr>";

                            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:7%;'>Sr</th>";
                            Echo "<th style='background-color: #cccccc;width:24%;'>Product Name</th>";
                            Echo "<th style='background-color: #cccccc;width:7%;'class='numeric'>Qty</th>";
                            Echo "<th style='background-color: #cccccc;width:7%;' class='numeric'> Size </th>";
                            Echo "<th style='background-color: #cccccc;width:10%;' class='numeric'>M.R.P.</th>";
                            Echo "<th style='background-color: #cccccc;width:5%;' colspan='2'>Image</th>";
                            Echo "<tr>";


                            $ctr2 = 0;

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

                                $getimg = mysqli_query($conn, "SELECT * FROM `product` WHERE `code`='$tran[code]'")or die(mysqli_error($conn));
                                $img = mysqli_fetch_array($getimg);

                                $getsize = mysqli_query($conn, "SELECT `code`,`name` FROM `size` WHERE code='$tran[size]'")or die(mysqli_error($conn));
                                $size = mysqli_fetch_array($getsize);
                                $ctr2++;
                                Echo "<tr>";
                                Echo "<td>$ctr2</td>";
                                Echo "<td>$img[name]</td>";
                                Echo "<td class='numeric' 'text-right'>$tran[qty]</td>";
                                Echo "<td class='numeric' 'text-right'>$size[name]</td>";
                                Echo "<td class='numeric' 'text-right'>$tran[mrp]</td>";
                                Echo "<td> <img src='$img[smallimg]' alt='image'  colspan='2'></td>";

                                Echo "</tr>";
                            }
                            Echo "</table>";
                            Echo "</td>";
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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