<?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];

if ($_REQUEST["code"]) {
    $code = $_REQUEST['code'];
    $relprod = $_REQUEST['prodcode'];
    $gettran4 = mysqli_query($conn, "SELECT * FROM `finprodpack` WHERE `code`='$code' and `relprod`='$relprod' ORDER BY `srno`")or die(mysqli_error($conn));
    $num = mysqli_num_rows($gettran4);
    $ctr4 = 0;

    if ($num > 0) {
        $ctr = 1;
        while ($tran4 = mysqli_fetch_array($gettran4)) {

            $getitem2 = mysqli_query($conn, "SELECT code, name FROM `packing` where code = '$tran4[packing]'")or die(mysqli_error($conn));
            $fitem2 = mysqli_fetch_array($getitem2);

            Echo "<tr class='curr'>";
            Echo "<td><i class='fa fa-close remove' ></i></td>";
            Echo "<td><input type='text' class='form-control form text-right srno4'    name='srno4'      value='$ctr' readonly></td>";
            Echo "<td>";
            Echo "<select class='form-control form packing select2' name='packing' style='width:100%' >";
            Echo "<option value='$fitem2[code]'>$fitem2[name]</option>";
            $getitm2 = mysqli_query($conn, "SELECT code, name FROM `packing` order by code")or die(mysqli_error($conn));
            while ($mitem2 = mysqli_fetch_array($getitm2)) {
                Echo "<option value='$mitem2[code]'>$mitem2[name]</option>";
            }
            Echo "</select>";
            Echo "</td>";
            Echo "<td>";
            Echo "<select class='form-control form unit select2' name='punit' style='width:100%' >";
            Echo "<option value='$tran4[unit]'>$tran4[unit]</option>";
            $gunit2 = mysqli_query($conn, "SELECT code, name FROM `unit` order by name")or die(mysqli_error($conn));
            while ($munit2 = mysqli_fetch_array($gunit2)) {
                Echo "<option value='$munit2[name]'>$munit2[name]</option>";
            }
            Echo "</select>";
            Echo "</td>";
            Echo "<td><input type='hidden' class='form-control form prodcode'   name='relprod'  value='$relprod'>
                      <input type='number' class='form-control form text-right pqty'   name='pqty'  step='any'   value='$tran4[qty]'></td>";
//                                                        Echo "<td><input type='number' class='form-control form text-right pcost'   name='pcost_$ctr4'  step='any'   value='$tran4[cost]'></td>";
//                                                        Echo "<td><input type='number' class='form-control form text-right pamount'   name='pamount_$ctr4'  step='any'   value='$tran4[amount]' readonly></td>";
            Echo "</tr>";
            $ctr++;
        }
    } else {
        $ctr = 0;
        $mctr = 1;
        Echo "<tr class='curr'>";
        Echo "<td><i class='fa fa-close remove' ></i></td>";
        Echo "<td><input type='text' class='form-control form text-right srno4'      name='srno4'        value='$mctr' readonly></td>";
        Echo "<td>";
        Echo "<select class='form-control form packing select2' name='packing' style='width:100%' >";
        Echo "<option value=''></option>";
        $getitm2 = mysqli_query($conn, "SELECT code, name FROM `packing` order by code")or die(mysqli_error($conn));
        while ($mitem2 = mysqli_fetch_array($getitm2)) {
            Echo "<option value='$mitem2[code]'>$mitem2[name]</option>";
        }
        Echo "</select>";
        Echo "</td>";
        Echo "<td>";
        Echo "<select class='form-control form unit select2' name='punit' style='width:100%' >";
        Echo "<option value=''></option>";
        $gunit2 = mysqli_query($conn, "SELECT code, name FROM `unit` order by code")or die(mysqli_error($conn));
        while ($munit2 = mysqli_fetch_array($gunit2)) {
            Echo "<option value='$munit2[name]'>$munit2[name]</option>";
        }
        Echo "</select>";
        Echo "</td>";
        Echo "<td><input type='hidden' class='form-control form prodcode'   name='relprod'  value='$relprod'>
                      <input type='number' class='form-control form text-right pqty'   name='pqty'  step='any'   value=''></td>";
//    Echo "<td><input type='number' class='form-control form text-right pcost'   name='pcost_$ctr'  step='any'   value='0'></td>";
//    Echo "<td><input type='number' class='form-control form text-right pamount'   name='pamount_$ctr'  step='any'   value='0'></td>";
        Echo "</tr>";
    }
}
?>
 