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

$maindb     = "salmain";
$trandb     = "saltran";
$formname   = "Sale Register";
$file       = "salereg.php";

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

if(isset($_POST["view"])){
    $party   = $_POST[party];
    $broker  = $_POST[broker];
    $transport  = $_POST[transport];
    $item = $_POST[item];
    $godown = $_POST[godown];
    $mtype = $_POST[mtype];    
    
    $partycond = "";
    $brokercond  = "";
    $transportcond  = "";
    $itemcond  = "";
    $godowncond  = "";
    $mtypecond = "";    
    
    if(!empty($party)){
        $partycond = " AND `party`='$party' ";
    }
    
    if(!empty($broker)){
        $brokercond = " AND `broker`='$broker' ";
    }
    
    if(!empty($transport)){
        $transportcond = " AND `transport`='$transport' ";
    }
    
    if(!empty($item)){
        $itemcond = " AND `name`=%$item% ";
    }
    
    if(!empty($godown)){
        $godowncond = " AND `godown`='$godown' ";
    }
    
    if(!empty($mtype)){
        $mtypecond = " AND `mtype`='$mtype' ";
    }
        
}

$getpmain   = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `vouchno`!='' $party $broker $transpor $item $godown $mtype  ")or die(mysqli_error($conn));

include("includes/linkshead.php");
?>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <label class="label-control marg">Date - </label>
                <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
            </div>
            <div class="col-lg-5">
                <label class="label-control marg">Print - </label>
                <label class="label-control marg"><?php Echo $formname; ?></label>
            </div>
            <div class="col-lg-1">
                <a onclick="window.close()" class="btn btn-primary">GO Back</a>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 pad">
                <table style="width:100%;background: white" class="table table-condensed table-hover">
                    <thead>
                        <tr style="height:10px;background-color: #1ABC9C">
                            <th>Sr</th>
                            <th>Type</th>
                            <th>Invoice No</th>
                            <th>Date</th>
                            <th>Customer</th>
                            <th>Amount</th>
                            <th>Discount</th>
                            <th>Freight</th>
                            <th>Qty</th>
                            <th>Net Amount</th>
                            <th class='numeric'>Qty.</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getmtype = mysqli_query($conn, "SELECT * FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
                            $mtype = mysqli_fetch_array($getmtype);
                            $getmqty = mysqli_query($conn, "SELECT SUM(qty)as totqty FROM `$trandb` WHERE `code`='$pmain[code]'")or die(mysqli_error($conn));
                            $mqty = mysqli_fetch_array($getmqty);
                            $ctr++;
                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$mtype[name]</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[unit]</td>";
                            Echo "<td class='numeric'>$mqty[totqty]</td>";
                            Echo "</tr>";
                            Echo "<tr>";
                            
                            if ($_POST["dtype"] == 2) {
                                
                                Echo "<tr style='height:10px;'>";
                                Echo "<td colspan='25'>";
                                Echo "<table style='width:100%;' class='table table-condensed table-hover'>";
                                Echo "<th style='background-color: #cccccc'>Sr</th>";
                                Echo "<th style='background-color: #cccccc'>Godown</th>";
                                Echo "<th style='background-color: #cccccc' class='numeric'>Taka</th>";
                                Echo "<th style='background-color: #cccccc'>Batch</th>";
                                Echo "<th style='background-color: #cccccc' class='numeric'>Qty.</th>";
                                Echo "<th style='background-color: #cccccc' class='numeric'>Tot. Qty.</th>";
                                Echo "<th style='background-color: #cccccc' class='numeric'>Flag Qty.</th>";
                                Echo "<th style='background-color: #cccccc' class='numeric'>Act Qty.</th>";
                                Echo "<tr>";


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

                                    $ctr2++;
                                    Echo "<tr>";
                                    Echo "<td>$ctr2</td>";
                                    Echo "<td>$tran[godown]</td>";
                                    Echo "<td class='numeric'>$tran[taka]</td>";
                                    Echo "<td>$tran[batch]</td>";
                                    Echo "<td class='numeric'>$tran[qty]</td>";
                                    Echo "<td class='numeric'>$tran[totqty]</td>";
                                    Echo "<td class='numeric'>$tran[flagqty]</td>";
                                    Echo "<td class='numeric'>$tran[actqty]</td>";
                                    Echo "</tr>";
                                }
                                Echo "</table>";
                                Echo "</td>";
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
