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

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

if (isset($_REQUEST['prodcode'])) {
    $prodcode = $_REQUEST['prodcode'];
    $jobgiven = $_REQUEST['jobgiven'];
    // browse($jobgiven);
    $gettran = mysqli_query($conn, "SELECT * FROM `itemtran` WHERE itemcode = '$prodcode' ORDER BY srno") or die(mysqli_error($conn));
    $count = mysqli_num_rows($gettran);
    $ctr = 0;

   

    if ($count > 0) {
        $meters = 0;
        $yard = 0;
        $qty = 0;
        while ($tran = mysqli_fetch_array($gettran)) {
            $finditem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` WHERE `code`='$tran[material]'") or die(mysqli_error($conn));
            $fitem = mysqli_fetch_array($finditem);

            $findcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'") or die(mysqli_error($conn));
            $fcolor = mysqli_fetch_array($findcolor);

            $getunit = mysqli_query($conn, "SELECT `qty`,`meters` , `yard` FROM `jobgivetran` WHERE `vouchno`='$jobgiven' and item ='$tran[material]'") or die(mysqli_error($conn));
            $unit = mysqli_fetch_array($getunit);

            if($unit['meters'] > 0){
                $meters = $unit['meters'];
            }
            if($unit['yard'] > 0){
                $yard = $unit['yard'];
            }
            if($unit['qty'] > 0){
                $qty = $unit['qty'];
            }

            echo "<tr>";
            echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
            echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
            echo "<td>";
            echo "<select class='form-control form item select2' name='item_$ctr' style='width:100%'>";
            echo "<option value='$fitem[code]'>$fitem[name]</option>";
            echo "</select>";
            echo "</td>";
            echo "<td>";
            echo "<select class='form-control form color select2' name='color_$ctr' style='width:100%'>";
            echo "<option value='$fcolor[code]'>$fcolor[name]</option>";
            echo "</select>";
            echo "</td>";
            echo "<td><input type='text' style='color:red;' class='form-control form text-right meters'      name='meters_$ctr'  value='$meters'></td>";
            echo "<td><input type='text' style='color:blue;' class='form-control form text-right used_meters' name='used_meters_$ctr'  value='0'></td>";
            echo "<td><input type='text' style='color:green;' class='form-control form text-right bal_meters'  name='bal_meters_$ctr'  value='0'></td>";
            echo "<td><input type='text' style='color:red;' class='form-control form text-right yard'        name='yard_$ctr'    value='$yard'></td>";
            echo "<td><input type='text' style='color:blue;' class='form-control form text-right used_yard'   name='used_yard_$ctr'    value='0'></td>";
            echo "<td><input type='text' style='color:green;' class='form-control form text-right bal_yard'    name='bal_yard_$ctr'    value='0'></td>";
            echo "<td><input type='text' style='color:red;' class='form-control form text-right tr_qty'      name='qty_$ctr'     value='$qty'></td>";
            echo "<td><input type='text' style='color:blue;' class='form-control form text-right used_tr_qty' name='used_qty_$ctr'     value='0'></td>";
            echo "<td><input type='text' style='color:green;' class='form-control form text-right bal_tr_qty'  name='bal_qty_$ctr'     value='0'></td>";
            echo "</tr>";
            $ctr++;
        }

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

<?php
}
