<?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];
$compcd = $_SESSION["comp_" . $dbname];
$newcmp = right($compcd, 1);
$zroot = $_SESSION["zdir"];
$zroot1 = left($zroot, -4);
$myyear = right($zdb, 4);
$maindb = "pgstmain";
$trandb = "pgsttran";

//$newdb = $zroot1 . $newcmp . "_" . $myyear;
$newdb = $zroot1 .  "1_" . $myyear;

mysqli_select_db($conn, $zdb);

if ($_REQUEST["autovouch"]) {
    $autovouch = $_REQUEST["autovouch"];
    $cutperc = $_REQUEST["cutperc"];
    $oms = $_REQUEST["oms"];
    if ($cutperc > 0) {
        $ncutperc = $cutperc;
    } else {
        $ncutperc = 0;
    }
    $hide = $_REQUEST["hide"];
    $hide_var = "";
    $ctr = 0;
    $mctr = 0;
    $success = false;

    $getsale = mysqli_query($conn, "Select * from $newdb.$trandb where vouchno = '$autovouch' order by srno") or die(mysqli_error($conn));
    if (mysqli_num_rows($getsale) > 0) {
        $getsalem = mysqli_query($conn, "Select discperc from $newdb.$maindb where vouchno = '$autovouch'") or die(mysqli_error($conn));
        $fetsalem = mysqli_fetch_array($getsalem);
        $discperc = $fetsalem['discperc'];
        $success = true;

        $mdata1 = "";
        $mdata2 = "";
        while ($tran = mysqli_fetch_array($getsale)) {
            $finditem = mysqli_query($conn, "SELECT * FROM $newdb.`item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
            $fitem = mysqli_fetch_array($finditem);

            $finditem2 = mysqli_query($conn, "SELECT * FROM `item` WHERE `name`='$fitem[name]'") or die(mysqli_error($conn));

//            $mdata1 .= "";
            if (mysqli_num_rows($finditem2) > 0) {
                $fitem2 = mysqli_fetch_array($finditem2);

                $discamt = $discperc * ($tran['amount'] / 100);
                $subtot = round($tran['amount'] - $discamt, 2);
                if ($oms == 1) {
                    $igstrate = $fitem2['gstperc'];
                    $igstamt = round($subtot * ($igstrate / 100), 2);
                    $cgstamt = $cgstrate = 0;
                } else {
                    $igstrate = $igstamt = 0;
                    $cgstrate = $fitem2['gstperc'] / 2;
                    $cgstamt = round($subtot * ($cgstrate / 100), 2);
                }

                $total = round($subtot + $cgstamt + $cgstamt + $igstamt, 2);


                $mdata1 .= "<tr>";
                $mdata1 .= "<td><i class='fa fa-close remove'></i></td>";
                $mdata1 .= "<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 cost' name='cost_$ctr' value='$tran[cost]'>
                  <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>";
                $mdata1 .= "<td>";
                $mdata1 .= "<select class='form-control form item select2' name='item_$ctr' style='width:100%'>";
                $mdata1 .= "<option value='$fitem2[code]'>$fitem2[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)) {
//                    $mdata1 .= "<option value='$item[code]'>$item[name]</option>";
//                }
                $mdata1 .= "</select>";
                $mdata1 .= "<input type='hidden' class='msp' name='msp_$ctr' value='$tran[msp]' >";
                $mdata1 .= "<input type='hidden' class='otprowsucs' name='otprowsucs_$ctr' value='$tran[otprowsucs]' >";
                $mdata1 .= "</td>";
                $mdata1 .= "<td>";
                $mdata1 .= "<select class='form-control form size select2' name='size_$ctr' style='width:100%'>";
                $mdata1 .= "<option value='$tran[size]'>$tran[size]</option>";
//                $gcolor = mysqli_query($conn, "SELECT `size` FROM `prodsize` where code ='$tran[itemcode]' order by size") or die(mysqli_error($conn));
//                while ($color = mysqli_fetch_array($gcolor)) {
//                    $mdata1 .= "<option value='$color[size]'>$color[size]</option>";
//                }
                $mdata1 .= "</select>";
                $mdata1 .= "</td>";
                $mdata1 .= "<td><input type='text' class='form-control form hsncode'                name='hsncode_$ctr'       value='$fitem2[hsncode]'></td>";
                $mdata1 .= "<td><input type='text' class='form-control form text-right trqty qty'    name='qty_$ctr'     value='$tran[qty]'>
                                                      <input type='hidden' class='form-control form text-right stqty'    name='stqty_$ctr'     value='$tran[stqty]'></td>";
                $mdata1 .= "<input type='hidden' class='msp' name='msp_$ctr' >";
                $mdata1 .= "<input type='hidden' class='otpvalue' id='otpvalue_$ctr' name='otpvalue_$ctr' value='$tran[otpvalue]'>";
                $mdata1 .= "<td><input type='text' class='form-control form text-right rate'     name='rate_$ctr'     value='$tran[rate]'></td>";
                $mdata1 .= "<td><input type='text' class='form-control form text-right amount'   name='amount_$ctr'   value='$tran[amount]' readonly>
                                            <input type='hidden' class='form-control form text-right mspamount'   name='mspamount_$ctr'   value='$tran[mspamount]' readonly></td>";
                $mdata1 .= "<td><input type='text' name='discperc_$ctr' class='discperc form-control text-right  form'  value='$discperc' ></td>";
                $mdata1 .= "<td><input type='text' name='discamt_$ctr' class='discamt form-control text-right  form' tabindex='-1' value='$discamt' readonly></td>";
                $mdata1 .= "<td><input type='text' name='subtot_$ctr' class='subtot form-control text-right  form' tabindex='-1' value='$subtot' readonly>
                  <input type='hidden' name='cgstrate_$ctr' class='cgstrate form-control text-right  form' tabindex='-1' value='$cgstrate' readonly>
                  <input type='hidden' name='cgstamt_$ctr' class='cgstamt form-control text-right  form' tabindex='-1' value='$cgstamt' readonly>
                  <input type='hidden' name='sgstrate_$ctr' class='sgstrate form-control text-right  form' tabindex='-1' value='$cgstrate' readonly>
                  <input type='hidden' name='sgstamt_$ctr' class='sgstamt form-control text-right  form' tabindex='-1' value='$cgstamt' readonly>
                  <input type='hidden' name='igstrate_$ctr' class='igstrate form-control text-right  form' tabindex='-1' value='$igstrate' readonly>
                  <input type='hidden' name='igstamt_$ctr' class='igstamt form-control text-right  form' tabindex='-1' value='$igstamt' readonly></td>";
                $mdata1 .= "<td><input type='text' name='total_$ctr' class='total form-control text-right  form' tabindex='-1' value='$total' readonly></td>";
                $mdata1 .= "</tr >";
            } else {
                $mdata2 .= "$fitem[name]" . ",";
//            break;
            }
            $ctr++;
        }
        $mdata1 .= "<input type = 'hidden' id = 'items' name = 'items' value = '$ctr' />";
    }
}
header("Content-type: application\json");
$data = array("success" => $success,
    "mdata1" => $mdata1,
    "mdata2" => $mdata2);
echo json_encode($data);
?>
