<?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["vouchno"]) {
    $vouchno = $_REQUEST["vouchno"];
    $netamt = $_REQUEST["netamt"];
    $gstperc = $_REQUEST["gstperc"];
    $hide = $_REQUEST["hide"];
    $prodtype = $_REQUEST["prodtype"];
    $hide_var = "";

    if ($hide == 0) {
        $hide_var = 'style="display:none;"';
    }
    $mctr = $ctr;
    $mctr++;

    $gstat = mysqli_query($conn, "Select * from saletype where code = '$gstperc'") or die(mysqli_error($conn));
    $fstat = mysqli_fetch_array($gstat);
    $gst = $fstat[per];
    $mgstamt = ($gst * $netamt) / (100 + $gst);

    $mtaxable = $netamt - $mgstamt;

    $setamt = $mtaxable;


    $myquery = mysqli_query($conn, "Select *,sum(qty) as tqty  from pursacn where gst = '$gst' and `prodtype`='$prodtype' "
            . "             group by purvouch,pursrno having sum(qty) > 0 order by purvouch,pursrno") or die(mysqli_error($conn));
    $tctr = 0;
    $sctr = 0;
    $diffbal = 0;

    while ($fetquery = mysqli_fetch_array($myquery)) {
        $tctr++;


        $finditem = mysqli_query($conn, "SELECT `code`,`name`,hsncode FROM `item` WHERE `code`='$fetquery[itemcode]'") or die(mysqli_error($conn));
        $fitem = mysqli_fetch_array($finditem);
        $hsncode = $fitem[hsncode];
        

//        $findunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit` WHERE `code`='$fetquery[unit]'") or die(mysqli_error($conn));
//        $funit = mysqli_fetch_array($findunit);
//        if ($prodtype == 'O') {
            $mqty = round($fetquery[tqty],2);
//        } else {
//            $mqty = round($fetquery[tqty], 1);
//        }
        $approxrate = $fetquery[approxrate];
        $myamount = $mqty * $approxrate;

        if ($setamt > $myamount) {

            Echo "<tr>";
            Echo "<td><i class='fa fa-close remove'  ></i></td>";
            Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$sctr' value='$tctr'>
                    <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 "<input type='text' class='form-control form barcode' name='barcode_$sctr' value='$fetquery[barcode]'>";
            Echo "</td>";
            Echo "<td>";
            Echo "<select class='form-control form item fetchpart_$sctr' name='item_$sctr' style='width:100%'>";
            Echo "<option value='$fetquery[itemcode]'>$fitem[name]</option>";
            $gitem = mysqli_query($conn, 'SELECT `code`,`name` FROM `item` WHERE `prodtype`="' . $prodtype . '" 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_$sctr'  >";
            Echo "<input type='hidden' class='otprowsucs' name='otprowsucs_$sctr' >";
            Echo "<input type='hidden' class='otpvalue' id='otpvalue_$sctr' name='otpvalue_$sctr' >";
            Echo "</td>";
            Echo "<td><input type='text' class='form-control form hsncode'                name='hsncode_$sctr'       value='$hsncode'></td>";
            Echo "<td><input type='text' class='form-control form text-right qty'      name='qty_$sctr'      value='$mqty'></td>";
            Echo "<td><input type='text' class='form-control form unit'      name='unit_$sctr'      value='$fetquery[unit]'>";

            Echo "</td>";
            Echo "<td><input type='text' class='form-control form text-right mrp'      name='mrp_$sctr'      value='$fetquery[mrp]' readonly></td>";
            Echo "<td><input type='text' class='form-control form text-right rate'  id='rate_$sctr'   name='rate_$sctr'     value='$approxrate'></td>";
            Echo "<td><input type='text' class='form-control form text-right amount'   name='amount_$sctr'   value='$myamount' readonly>
                      <input type='hidden' class='form-control form text-right mspamount'   name='mspamount_$sctr'   value='0' readonly></td>";
            Echo "</tr >";

            $ctr++;
            $setamt = round($setamt - $myamount, 2);
        } else {
//            $itemqty = round($setamt / $approxrate, 2);
            $itemqty = round($setamt / $approxrate,2);

            Echo "<tr>";
            Echo "<td><i class='fa fa-close remove'  ></i></td>";
            Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$sctr' value='$tctr'>
                    <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 "<input type='text' class='form-control form barcode' name='barcode_$sctr' value='$fetquery[barcode]'>";
            Echo "</td>";
            Echo "<td>";
            Echo "<select class='form-control form item fetchpart_$sctr' name='item_$sctr' style='width:100%'>";
            Echo "<option value='$fetquery[itemcode]'>$fitem[name]</option>";
            $gitem = mysqli_query($conn, 'SELECT `code`,`name` FROM `item` WHERE `prodtype`="' . $prodtype . '" 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_$sctr'  >";
            Echo "<input type='hidden' class='otprowsucs' name='otprowsucs_$sctr' >";
            Echo "<input type='hidden' class='otpvalue' id='otpvalue_$sctr' name='otpvalue_$sctr' >";
            Echo "</td>";
            Echo "<td><input type='text' class='form-control form hsncode'                name='hsncode_$sctr'       value='$hsncode'></td>";
            Echo "<td><input type='text' class='form-control form text-right qty'      name='qty_$sctr'      value='$itemqty'></td>";
            Echo "<td><input type='text' class='form-control form unit'      name='unit_$sctr'      value='$fetquery[unit]'>";

            Echo "</td>";
            Echo "<td><input type='text' class='form-control form text-right mrp'      name='mrp_$sctr'      value='$fetquery[mrp]' readonly></td>";
            Echo "<td><input type='text' class='form-control form text-right rate'  id='rate_$sctr'   name='rate_$sctr'     value='$approxrate'></td>";
            Echo "<td><input type='text' class='form-control form text-right amount'   name='amount_$sctr'   value='$setamt' readonly>
                      <input type='hidden' class='form-control form text-right mspamount'   name='mspamount_$sctr'   value='0' readonly></td>";
            Echo "</tr >";
            $ctr++;
            break;
        }
        $sctr++;
    }
    if ($setamt > 0) {
        $diffbal = $mtaxable - $setamt;
    }
   
    echo "<tr><input type='hidden' id='diffbal' name='diffbal' value='$diffbal' /></tr>";
    echo "<tr><input type='hidden' id='setamt' name='setamt' value='$setamt' /></tr>";
    echo "<tr><input type='hidden' id='taxableamt' name='taxableamt' value='$mtaxable' /></tr>";
    echo "<input type='hidden' id='items' name='items' value='$sctr' />";
} else {
    echo "ERROR";
}
?>
 