<?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];
$success = false;

if (isset($_REQUEST['estvcno'])) {
    $estvcno = $_REQUEST['estvcno'];

    // $qry = mysqli_query($conn, "SELECT * FROM esttran  WHERE  `vouchno` = '$estvcno'  ORDER BY srno") or die(mysqli_error($conn));
    $qry = mysqli_query($conn, "SELECT * FROM esttran  WHERE `vouchno` not in (SELECT estvcno FROM salemain) AND `vouchno` = '$estvcno' 
                                  ORDER BY srno") or die(mysqli_error($conn));


    $ctr = 0;
    $showctr = 1 ;
    while ($row = mysqli_fetch_array($qry)) {

        if ($row['calby'] == "P") {
            $calname = "Pcs";
        } elseif ($row['calby'] == "S") {
            $calname = "Sq.ft";
        } elseif ($row['calby'] == "B") {
            $calname = "Box";
        } else {
            $calname = "";
        }

        echo "<tr>";
        echo "<td><i class='fa fa-close remove' style=''></i></td>";
        echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$showctr' readonly></td>";

        echo "<td><input type='text' class='form-control form text-right qty'     name='qty_$ctr' value='$row[qty]'></td>";

        echo "<td>";
        echo "<input list='unit' name='unit_$ctr' class='unitname form-control form ' autocomplete='off' value='$row[unitbox]'> ";
        echo "<datalist id='unit' char='#'>";
        echo "<option>";

        $mun = mysqli_query($conn, "SELECT `unit` FROM `product` group by unit") or die(mysqli_error($conn));

        while ($tun = mysqli_fetch_array($mun)) {
            $unname = $tun['name'];
            echo "<option value='$unname'>";
        }
        echo "</datalist>";
        echo "</td>";

        echo "<td>";
        echo "<input list='size' name='size_$ctr' class='size form-control form ' autocomplete='off' value='$row[size]'> ";
        echo "<datalist id='size' char='#'>";
        echo "<option>";

        $msi = mysqli_query($conn, "SELECT `name` FROM `size` group by name") or die(mysqli_error($conn));

        while ($tsi = mysqli_fetch_array($msi)) {
            $szname = $tsi['name'];
            echo "<option value='$szname'>";
        }
        echo "</datalist>";
        echo "</td>";
        echo "<td><input type='text' class='form-control form text-right sqft'     name='sqft_$ctr' value='$row[sqft]'></td>";

        echo "<td>";
        echo "<select name='calby_$ctr' class='form-control form calby'>";
        echo "<option value='$row[calby]'>$calname</option>";
        echo "<option value='S'>Sq.ft</option>";
        echo "<option value='P'>Pcs</option>";
        echo "<option value='B'>Box</option>";
        echo "</select>";
        echo "</td>";

        echo "<td><input type='number' step='any' class='form-control form text-right rate'     name='rate_$ctr'     value='$row[rate]'           ></td>";
        echo "<td><input type='number' step='any' class='form-control form text-right amount'   name='amount_$ctr'   value='$row[amount]' readonly></td>";
        echo "<td><input type='number' step='any' class='form-control form text-right discperc' name='discperc_$ctr' value='$row[discperc]'       ></td>";
        echo "<td><input type='number' step='any' class='form-control form text-right discamt'  name='discamt_$ctr'  value='$row[discamt]'        ></td>";
        echo "<td><input type='number' step='any' class='form-control form text-right total'    name='total_$ctr'    value='$row[total]'  readonly></td>";

        echo "<td>";
        echo "<input list='itemname' name='itemname_$ctr' class='itemname form-control form ' autocomplete='off' value='$row[particulars]'> ";
        echo "<datalist id='itemname' char='#'>";
        echo "<option>";

        $mit = mysqli_query($conn, "SELECT `name` FROM `product` group by name") or die(mysqli_error($conn));

        while ($tit = mysqli_fetch_array($mit)) {
            $itname = $tit['name'];
            echo "<option value='$itname'>";
        }

        echo "</datalist>";
        echo "</td>";
        echo "<td><input type='date' class='form-control form delivery_date'  name='delivery_date_$ctr'  value='$row[delvdate]' ></td>";
        echo "<td><input type='number' class='form-control form delchal'  name='delchal_$ctr' value='$row[delchal]' ></td>";
        echo "<td><input type='text' class='form-control form text-right mancp'  name='mancp_$ctr' value='$row[mancp]'></td> ";
        echo "<td><input type='text' class='form-control form text-right manitemcode'  name='manitemcode_$ctr' value='$row[manitemcode]'></td> ";
        echo "<td><input type='number' class='form-control form text-right puramt'  name='puramt_$ctr' value='$puramt' readonly></td>";
        echo "<td><input type='text' class='form-control form suppname'  name='suppname_$ctr' value='$suppname' readonly ></td> ";
        echo "<td><input type='text' class='form-control form purvouch'  name='purvouch_$ctr' value='$purvouch' readonly ></td> ";
        echo "</tr>";
        $ctr++;
        $showctr++;
    }
?>
    <input type="hidden" id="items" name="items" value="<?php echo $ctr; ?>">
<?php
}

if (isset($_REQUEST['estexpvcno'])) {
    $estexpvcno = $_REQUEST['estexpvcno'];
    
    $ctr2 = 0;
    $gettran2 = mysqli_query($conn, "SELECT * FROM `estexp`  WHERE `vouchno` not in (SELECT estvcno FROM salemain) AND `vouchno` = '$estexpvcno' ORDER BY `srno`") or die(mysqli_error($conn));
    while ($tran2 = mysqli_fetch_array($gettran2)) {
        $gparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$tran2[descrip]'") or die(mysqli_error($conn));
        $getdesc = mysqli_fetch_array($gparty);
        echo "<tr>";
        echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
        echo "<td><input type='text' class='form-control form text-right srno' name='srno2_$ctr2' value='$tran2[srno]'></td>";
        echo "<td><input type='number' class='form-control form text-right samount' name='samount_$ctr2' value='$tran2[amount]'></td>";
        echo "<td>";
        echo "<input list='expdescrip' name='descrip_$ctr2' value='$getdesc[name]' class='descrip form-control form ' autocomplete='off'>";
        echo "<datalist id='expdescrip' char='#'>";
        echo "<option value=''></option>";
        $gexp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='012' order by name") or die(mysqli_error($conn));
        while ($showexp = mysqli_fetch_array($gexp)) {
            $expname = $showexp['name'];
            echo "<option value='$expname'>";
        }
        echo "</datalist>";
        echo "</td>";
        echo "<td><input type='number' class='form-control form text-right eamount' name='tamount_$ctr2' value='$tran2[paid]'></td>";
        echo "</tr>";
        $ctr2++;
    }
?>

    <input type="hidden" id="items2" name="items2" value="<?php echo ($addflag != 1) ? $ctr2 : $pctr2; ?>" />
<?php
}


if (isset($_REQUEST["vouchno"])) {
    $vouchno = $_REQUEST['vouchno'];

    $query = "SELECT * FROM `estmain` WHERE vouchno ='$vouchno'";
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
    $r = mysqli_fetch_assoc($result);

    $getest = mysqli_query($conn, "SELECT estvcno FROM `salemain` WHERE `estvcno` ='$vouchno'") or die(mysqli_error($conn));
    $rowest = mysqli_num_rows($getest);

    $rows = array();
    $ctr = 0;
    $rows = array(
        'success'     => true,
        'clientno'    => "$r[clientno]",
        'architect'   => "$r[architect]",
        'client'      => "$r[party]",
        'createby'    => "$r[createby]",
        'estvcno'     => "$rowest"
    );

    header("Content-type: application\json");
    echo json_encode($rows);
}
