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

    $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));
    while ($fetsale = mysqli_fetch_array($getsale)) {
        $rate = $fetsale['rate'];
        $qty = $fetsale['qty'];
        $newrate = ( $rate * $ncutperc ) / 100;
        $newamt = $newrate * $qty;
        $mctr++;
        
        $getitem = mysqli_query($conn, "select * from item where code = '$fetsale[itemcode]'") or die(mysqli_error($conn));
        $fetitem = mysqli_fetch_array($getitem);
        $itemgrp = $fetitem['itemgrp'];
        $hsncode = $fetitem['hsncode'];
        
        $getitemgrp = mysqli_query($conn, "select * from itemgrp where code = '$itemgrp'") or die(mysqli_error($conn));
        $fetitemgrp = mysqli_fetch_array($getitemgrp);
        $itemgrpname = $fetitemgrp['name'];
        
        echo "<tr>";
        if ($hide == 0) {
            $hide_var = 'style="display:none;"';
        }
        echo "<td align='center' valign='top'><input type='image' src='delbut.png' name='remove' value='remove' class='remove' $hide_var/></td>";
        echo "<td align='left' valign='top'><input type='text' size='6' maxlength='6' name='srno_$ctr' class='sno form-control' style='height:33px;' value='$mctr'/></td>";
//        echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='barcode_$ctr' class='barcode form-control' style='height:29px;text-align:left;' value='$fetsale[barcode]' /></td>";
        
        echo "<td style='height:30px;' align='left' valign='center'>";
        echo "<select name='item_$ctr' style='height:29px;width:100%' class='item form-control form fetchpart_$ctr'>";
        echo "<option value='$fetitem[name]'>$fetitem[name]</option>";
        $mitem = mysqli_query($conn, "SELECT * FROM `item` ORDER BY `name`") or die(mysqli_error($conn));
        while ($trow1 = mysqli_fetch_array($mitem)) {
            echo "<option value='$trow1[name]'>$trow1[name]</option>";
        }
        echo "</select>";
        echo "</td>";
        echo "<td align='center' style='height:30px;' valign='center'><input type='text' size=''  name='descrip_$ctr' class='descrip form-control' style='height:29px;text-align:left;' value='$itemgrpname' /></td>";
        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='hsncode_$ctr' class='hsncode form-control' style='height:29px;' value='$hsncode'/></td>";
        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='qty_$ctr' class='qty form-control' style='height:29px;text-align:right;' value='$fetsale[qty]'/></td>";
        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='rate_$ctr' class='rate form-control' style='height:29px;text-align:right;' value='$newrate'/></td>";
//        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='amt_$ctr' class='amt form-control' style='height:29px;text-align:right;' value='$newamt'  readonly/></td>";
//        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='discperc_$ctr' class='discperc form-control' style='height:29px;text-align:right;' value='$fetsale[discperc]'  /></td>";
//        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='discamt_$ctr' class='discamt form-control' style='height:29px;text-align:right;' value='$fetsale[discount]'  readonly/></td>";
        echo "<td style='height:30px;' align='center' valign='center'><input type='text' size=''  name='taxval_$ctr' class='taxval form-control' style='height:29px;text-align:right;' value='$newamt'  readonly/></td>";
        echo "</tr>";
        $ctr++;
    }
    echo "<input type = 'hidden' id = 'items' name = 'items' value = '$ctr' />";
} else {
    echo "ERROR";
}
?>
