<?php

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

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

if ($_REQUEST["autovouch"]) {
    $autovouch = $_REQUEST["autovouch"];
    $cutperc = $_REQUEST["cutperc"];
    if ($cutperc > 0) {
        $ncutperc = $cutperc;
    } else {
        $ncutperc = 0;
    }
    $hide = $_REQUEST["hide"];
    $hide_var = "";
    $ctr = 0;
    $mctr = 0;
    $getsalemn = mysqli_query($conn, "Select party from salemain where vouchno = '$autovouch' ") or die(mysqli_error($conn));
    $fetsale = mysqli_fetch_array($getsalemn);

    $getpart = mysqli_query($conn, "Select oms from accmast where accode = '$fetsale[party]'") or die(mysqli_error($conn));
    $fetpart = mysqli_fetch_array($getpart);

    $oms = $fetpart['oms'];


//    $getsale = mysqli_query($conn, "Select itemcode, sum(qty) as qty, rate   from saletran where vouchno = '$autovouch' 
//                          group by rate order by srno") or die(mysqli_error($conn));
    $getsale = mysqli_query($conn, "Select * from saletran where vouchno = '$autovouch' order by srno") or die(mysqli_error($conn));
    $newrate = $newamt = $cgstrate = $sgstrate = $igstrate = $cgstamt = $sgstamt = $igstamt = $total = 0;
    while ($tran = mysqli_fetch_array($getsale)) {
        $rate = $tran['rate'];
        $qty = $tran['qty'];
        $newrate = ( $rate * $ncutperc ) / 100;
        $newamt = $newrate * $qty;
        $mctr++;


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

        if ($oms == '1') {
            $cgstrate = 0;
            $cgstamt = 0;
            $igstrate = $gstperc;
            $igstamt = $newamt * ($igstrate / 100);
        } else {
            $cgstrate = $gstperc / 2;
            $cgstamt = $newamt * ($cgstrate / 100);
            $igstrate = 0;
            $igstamt = 0;
        }
        $total = $newamt + $cgstamt + $cgstamt + $igstamt;

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

        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_$ctr' value='$tran[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 "<input type='text' class='form-control form barcode' name='barcode_$ctr' value='$tran[barcode]'>";
        Echo "</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>";
        $gitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` 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='$fcolor[code]'>$fcolor[name]</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='$hsncode'></td>";
        Echo "<td><input type='text' class='form-control form text-right qty'      name='qty_$ctr'      value='$tran[qty]'></td>";
//                                            Echo "<td><input type='text' class='form-control form  unit'      name='unit_$ctr'      value='$tran[unit]' 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='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='$newrate'></td>";
        Echo "<td><input type='text' class='form-control form text-right amount'   name='amount_$ctr'   value='$newamt' readonly>
                                            <input type='hidden' class='form-control form text-right mspamount'   name='mspamount_$ctr'   value='$tran[mspamount]' readonly></td>";
        Echo "<td><input type='text' name='discperc_$ctr' id='discperc_$ctr' class='discperc form-control text-right  form'  value='0' ></td>";
        Echo "<td><input type='text' name='discamt_$ctr' class='discamt form-control text-right  form' tabindex='-1' value='0' readonly></td>";
        Echo "<td><input type='text' name='subtot_$ctr' class='subtot form-control text-right  form' tabindex='-1' value='$newamt' readonly></td>";
        Echo "<td><input type='text' name='cgstrate_$ctr' class='cgstrate form-control text-right  form' tabindex='-1' value='$cgstrate' readonly></td>";
        Echo "<td><input type='text' name='cgstamt_$ctr' class='cgstamt form-control text-right  form' tabindex='-1' value='$cgstamt' readonly></td>";
        Echo "<td><input type='text' name='sgstrate_$ctr' class='sgstrate form-control text-right  form' tabindex='-1' value='$cgstrate' readonly></td>";
        Echo "<td><input type='text' name='sgstamt_$ctr' class='sgstamt form-control text-right  form' tabindex='-1' value='$cgstamt' readonly></td>";
        Echo "<td><input type='text' name='igstrate_$ctr' class='igstrate form-control text-right  form' tabindex='-1' value='$igstrate' readonly></td>";
        Echo "<td><input type='text' name='igstamt_$ctr' class='igstamt form-control text-right  form' tabindex='-1' value='$igstamt' readonly></td>";
        Echo "<td><input type='text' name='total_$ctr' class='total form-control text-right  form' tabindex='-1' value='$total' readonly></td>";
        Echo "</tr >";
        $ctr++;
    }
    echo "<input type = 'hidden' id = 'items' name = 'items' value = '$ctr' />";
} else {
    echo "ERROR";
}
?>
