<?php

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

$maindb = 'inwdtran';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
if (isset($_REQUEST["rows"])) {
    $qry = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `vouchno`='$_REQUEST[code]' ORDER BY `srno`") or die(mysqli_error($conn));
    $ctr2 = 0;
//    $pctr = 0;
    while ($row = mysqli_fetch_array($qry)) {
        $getitem = mysqli_query($conn, "SELECT * FROM `item` WHERE code='$row[itemcode]'") or die(mysqli_error($conn));
        $gitem = mysqli_fetch_array($getitem);

        $getcolr = mysqli_query($conn, "SELECT * FROM `color` WHERE code='$row[color]' ORDER BY name") or die(mysqli_error($conn));
        $colr = mysqli_fetch_object($getcolr);

        echo "<tr>";
        echo '<td align="center" valign="top"><input type="image" src="delbut.png" name="remove"  value="remove" class="remove"/></td>';
        echo "<td align='left' valign='top'><input type='text' size='6' maxlength='6' name='srno_$ctr2' class='sno form-control' style='height:33px;' value='$row[srno]'/></td>";

        echo "<td align='left'>";
        echo "<input list='item_$ctr2' name='item_$ctr2' class='item form-control'  value='$gitem[name]' style='height:33px;'>";
        echo "<datalist id='item_$ctr2'>";
        echo "<option value='$gitem->name'>";
        $mitem = mysqli_query($conn, "SELECT `name` FROM `item` ORDER BY `name`") or die(mysqli_error($conn));
        while ($trow1 = mysqli_fetch_array($mitem)) {
            echo "<option value='$trow1[name]'>";
        }
        echo "</datalist>";
        echo "</td>";


        echo "<td align='center' valign='top'>";
        echo "<select name='color_$ctr2' style='height:34px;' class='form-control'>";
        echo "<option value='$colr->code'>$colr->name</option>";
        $getcolors = mysqli_query($conn, "SELECT `code`, `name` FROM `color` ORDER BY name") or die(mysqli_error($conn));
        while ($wcol = mysqli_fetch_array($getcolors)) {
            echo "<option value='$wcol[code]' >$wcol[name]</option>";
        }
        echo "</select>";
        echo "</td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4' name='s5_$ctr2' class='s5 form-control' style='text-align:right;height:33px;' value='$row[s5]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s6_$ctr2' id='cost' class='s6 form-control' style='text-align:right;height:33px;' value='$row[s6]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s7_$ctr2' class='s7 form-control'  style='text-align:right;height:33px;' value='$row[s7]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s8_$ctr2' class='s8 form-control'  style='text-align:right;height:33px;' value='$row[s8]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s9_$ctr2' class='s9 form-control'  style='text-align:right;height:33px;' value='$row[s9]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s10_$ctr2' class='s10 form-control'  style='text-align:right;height:33px;' value='$row[s10]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s11_$ctr2' class='s11 form-control'  style='text-align:right;height:33px;' value='$row[s11]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='s12_$ctr2' class='s12 form-control'  style='text-align:right;height:33px;' value='$row[s12]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' maxlength='4'  name='soth_$ctr2' class='soth form-control'  style='text-align:right;height:33px;' value='$row[soth]'/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4'  name='qty_$ctr2' class='qty form-control'  style='text-align:right;height:33px;'  value='$row[qty]' readonly/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' name='mrp_$ctr2' class='mrp form-control'  style='text-align:right;height:33px;'   value='$row[mrp]' readonly//></td>";
        echo "<td align='center' valign='top'><input type='text' size='4' name='rate_$ctr2' class='rate form-control'  style='text-align:right;height:33px;' value='$row[selling]' readonly/></td>";
        echo "<td align='center' valign='top'><input type='text' size='4'  name='amt_$ctr2' class='amt form-control'  style='text-align:right;height:33px;'  value='$row[amount]' readonly/></td>";
        echo "<td><input type='hidden' name='mrpperc_$ctr2' class='mrpperc form-control'  style='text-align:right;height:33px;'  value='$row[mrpperc]' /></td>";
        echo "</tr>";
        $ctr2++;
    }
}
if(isset($_REQUEST["count"])){
    $qry = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `vouchno`='$_REQUEST[code]' ORDER BY `srno`") or die(mysqli_error($conn));
    $counter = mysqli_num_rows($qry);
   
    header("Content-type: application/json");
    $data = array("code" => $counter);
    echo json_encode($data);
}
?>