<?php

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

$maindb = 'itemtran';

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

$model = $_REQUEST["model"];
$totqty = $_REQUEST["totqty"];

$qry = mysqli_query($conn, "SELECT srno,material,qty,unit,rate FROM `itemtran` where  `itemcode` =  $model  order by srno") or die(mysqli_error($conn));
$rows = array();
$ctr = 0;
$mctr = 0;
$pctr2 = 1;
while ($row = mysqli_fetch_array($qry)) {
    $mctr++;
    $getproduct = mysqli_query($conn, "SELECT  code,name,type from rawmat where code = '$row[material]' order by name");
    $getp = mysqli_fetch_array($getproduct);
    $gettpe = $getp['type'];

    


//    $getunit = mysqli_query($conn, "SELECT  name from unit where code = '$row[unit]' order by name");
//    $getu = mysqli_fetch_array($getunit);

    $qty = $row['qty'];
//    if ($totqty != "") {
//        $totqty1 = $qty * $totqty;
//    } else {
//        $totqty1 = $qty * 1;
//    }
    $totqty2 = 0;
    $totqty1 = 0;
//    if ($gettpe == 'F') {
//        if ($totqty != "") {
//            $totqty2 = $qty * $totqty;
//        } else {
//            $totqty2 = $qty * 1;
//        }
//    } else {
//        if ($totqty != "") {
//            $totqty1 = $qty * $totqty;
//        } else {
//            $totqty1 = $qty * 1;
//        }
//    }
     $q =  $y =  $m = "";

    if ($row['unit'] == '17') {
        $calq = "M";
        $calqn = "Meters";
        $met = $qty * $totqty;
        $yard = 0;
        $pcs = 0;
        $q = "readonly";
        $y = "readonly";
        $m = "";
    } elseif ($row['unit'] == '18') {
        $calq = "Y";
        $calqn = "Yard";
        $yard = $qty * $totqty;
        $met = 0;
        $pcs = 0;
        $q = "readonly";
        $y = "";
        $m = "readonly";
    } else {
        $calq = "Q";
        $calqn = "Qty.";
        $pcs = $qty * $totqty;
        $met = 0;
        $yard = 0;
        $q = "";
        $y = "readonly";
        $m = "readonly";
    }




    Echo "<tr>";
    Echo "<td><i class='fa fa-close remove' style=''></i></td>";
    Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$row[srno]'>
                                                      <input type='hidden' class='form-control form text-right purvouch' name='purvouch_$sctr' value='$fetquery[purvouch]'>
                                                      <input type='hidden' class='form-control form text-right pursrno' name='pursrno_$sctr' value='$fetquery[pursrno]'></td>";
    Echo "<td>";
    Echo "<select class='form-control form item select2' name='item_$ctr' style='width:100%'>";
    Echo "<option value='$getp[code]'>$getp[name]</option>";
//    $gitem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat`  where code = '$row[material]' order by name")or die(mysqli_error($conn));
//    while ($item = mysqli_fetch_array($gitem)) {
//        Echo "<option value='$item[code]'>$item[name]</option>";
//    }
    Echo "</select>";
//    Echo "<input type='hidden' class='msp' name='msp_$ctr' value='$tran[msp]' >";
//    Echo "<input type='hidden' class='otprowsucs' name='otprowsucs_$ctr' value='$tran[otprowsucs]' >";
    Echo "</td>";
    Echo "<td>";
    Echo "<select class='form-control form color select2' name='color_$ctr' style='width:100%'>";
    Echo "<option value=''></option>";
    $gcolor = mysqli_query($conn, 'SELECT `code`,`name` FROM `color` order by name')or die(mysqli_error($conn));
    while ($color = mysqli_fetch_array($gcolor)) {
        Echo "<option value='$color[code]'>$color[name]</option>";
    }
    Echo "</select>";
    echo "</td>";
//                                            Echo "<td><input type='text' class='form-control form hsncode'           name='hsncode_$ctr' value='$tran[hsncode]'></td>";
    Echo "<td><input type='text' class='form-control form text-right yard'   name='yard_$ctr'    value='$yard' $y ></td>";
    Echo "<td><input type='text' class='form-control form text-right meters' name='meters_$ctr'  value='$met'  $m></td>";
    Echo "<td><input type='text' class='form-control form text-right qty'    name='qty_$ctr'     value='$pcs'  $q></td>";
    Echo "<td><select class='form-control form calby ' name='calby_$ctr' style='width:100%'>";
    Echo "<option value='$calq'>$calqn</option>";
    Echo "<option value='Q'>Qty</option>";
    Echo "<option value='M'>Meters</option>";
    Echo "<option value='Y'>Yard</option>";
    Echo "</select>";
    Echo "</td>";

//    Echo "<td><input type='text' class='form-control form unit'    name='unit_$ctr'     value='$getu[name]' readonly>";
//                                            Echo "<select class='form-control form unit select2' name='unit_$ctr' style='width:100%'>";
//                                            Echo "<option value='$funit[code]'>$funit[name]</option>";
//                                            $gunit = mysqli_query($conn, 'SELECT `code`,`name` FROM `unit`')or die(mysqli_error($conn));
//                                            while ($unit = mysqli_fetch_array($gunit)) {
//                                                Echo "<option value='$unit[code]'>$unit[name]</option>";
//                                            }
//                                            Echo "</select>";
//    Echo "<input type='hidden' class='rawtype' name='rawtype_$ctr' value='$tran[rawtype]'>";
//    Echo "<input type='hidden' class='msp' name='msp_$ctr' >";
//    Echo "<input type='hidden' class='otpvalue' id='otpvalue_$ctr' name='otpvalue_$ctr' value='$tran[otpvalue]'>";
//                                            Echo "</td>";
//                                            Echo "<td><input type='text' class='form-control form text-right mrp'      name='mrp_$ctr'      value='$tran[mrp]'></td>";
//                                            echo "<td><input type='text' name='ratebfdisc_$ctr' class='ratebfdisc form-control text-right  form'  value='$tran[ratebfdisc]' ></td>";
//                                            Echo "<td><input type='text' name='discount2_$ctr' class='discount2 form-control text-right  form'  value='$tran[discount2]' ></td>";
    Echo "<td><input type='text' class='form-control form text-right rate'     name='rate_$ctr'     value='$row[rate]'></td>";
    Echo "<td><input type='text' class='form-control form text-right amount'     name='amount_$ctr'     value=''></td>";

    Echo "</tr>";
    $ctr++;
}
echo "<input type='hidden' id='items' name='items' value='$ctr'>";

//     header("Content-type: application/json");
//    echo json_encode($rows);
?>