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

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);

$trandb = "pordtran";
$stkdb  = "pordsacn";

if ($_REQUEST["code"]) {
    $code = $_REQUEST["code"];
    $vouchno = $_REQUEST["vouchno"];
    $accode = $_REQUEST["accode"];
    ?>

    <table id="data" border="1px" width="100%">

        <tr>
            <td></td>
            <td width="5%" align="center"> <label class="control-label">Sr.</label></td>
            <td width="50%" align="center"><label class="control-label">Description</label></td>
            <td width="10%" align="center"><label class="control-label">Qty.</label></td>
            <td width="10%" align="center"><label class="control-label">Unit</label></td>
            <td width="10%" align="center"><label class="control-label">Rate</label></td>
            <td width="17%" align="center"><label class="control-label">Amount</label></td>
        </tr>

        <?php
        $chkqry = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkqry) > 0) {
            $qry2 = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$vouchno' ORDER BY srno") or die(mysqli_error($conn));
        } else {
            $qry2 = mysqli_query($conn, "SELECT descrip, srno, unit, sum(qty) as qty, rate, amt FROM  `$stkdb` WHERE ordno='$code' 
                                 AND ordno != '' AND party = '$accode' GROUP BY ordno, descrip 
                                 having qty > 0 ORDER BY ordno") or die(mysqli_error($conn));
        }
        $ctr = 0;
        $srctr = 1;

        while ($row = mysqli_fetch_array($qry2)) {
            $descrip = mysqli_query($conn, "SELECT * FROM `item` where code = '$row[descrip]'")or die(mysqli_error($conn));
            $getdes = mysqli_fetch_assoc($descrip);
            
        echo "<tr>";
        echo '<td align="center" valign="top"><input type="image" src="images/delbut.png" name="remove" class="remove form" value="remove"></td>';
        echo "<td align='left' valign='top'><input type='text' size='6' maxlength='6' name='srno_$ctr' class='sno form-control form' style='height:33px;' value='$row[srno]'/></td>";
        echo "<td align='left' valign='top'><input type='text' size='6' name='descrip_$ctr' class='descrip form-control form' style='height:33px;' value='$getdes[name]'/></td>";
        echo "<td align='right' valign='top'><input type='text' size='4'  name='qty_$ctr' class='qty form-control form'  style='text-align:right;height:33px;' value='$row[qty]'></td>";
        echo "<td align='right' valign='top'><input type='text' size='4'  name='unit_$ctr' class='unit form-control form'  style='text-align:left;height:33px; ' value='$row[unit]'></td>";
        echo "<td align='right' valign='top'><input type='text' size='4'  name='rate_$ctr' class='rate form-control form'  style='text-align:right;height:33px; ' value='$row[rate]'></td>";
        echo "<td align='right' valign='top'><input type='text' size='4'  name='amt_$ctr' class='amt form-control form'  style='text-align:right;height:33px; ' value='$row[amt]'></td>";

        echo "</tr>";
        
        $ctr++;
            $srctr++;
        }
        //browse($ctr);
        ?>

    </table>
    <input type="hidden" id="items" name="items" value="<?php echo $ctr; ?>" />  

    <?php
} else {
    echo "ERROR";
}
?>
