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

mysqli_set_charset($conn,"utf8mb4");

$vouchno = $_REQUEST["vouchno"];
$tqty = $_REQUEST["purqty"];
$tcalby = $_REQUEST["calby"];
$tsqft = $_REQUEST["sqft"];
$srno = $_REQUEST["srno"];
$size = mysqli_real_escape_string($conn,$_REQUEST["size"]);
$itemname = trim(mysqli_real_escape_string($conn,$_REQUEST["itemname"]));

//$qry = mysqli_query($conn, "SELECT * FROM  `saletran` WHERE `itemname` ='$itemname' and delvdate = '0000-00-00' ORDER BY vouchno,srno") or die(mysqli_error($conn));
$qry = mysqli_query($conn, "SELECT * FROM  `saletran` WHERE itemname ='$itemname' and size = '$size' ORDER BY vouchno,srno") or die(mysqli_error($conn));
$num = mysqli_num_rows($qry);


$ctr = 0;
$mctr = 1;
if ($num > 0) {
    ?>
    <h4>Item : <?php echo $itemname; ?> Size : <?= $size; ?><span style="float:right;padding-right: 1%"> Qty. &nbsp;&nbsp;<?= $tqty; ?></span></h4>
    <table class="table-striped table-condensed table-bordered tab" width="100%">
        <thead  class="mySearchTr">
            <tr>
                <th style="width: 1%"></th>
                <th style="width: 5%">Sr. No.</th>
                <th style='width: 20%' class="text-left">Party</th>
                <th style='width: 5%' >Qty.</th>
                <th style="width: 10%;">Other Purchase (Qty.)</th> 
                <th style="width: 10%;">Other Purchase (Sq.Ft.)</th> 
                <th style="width: 8%;">Received Qty.</th> 
                <th style="width: 8%;">Received (Sq.Ft.)</th> 
                <!--<th style="width: 5%;">Complete</th>--> 
            </tr>
        </thead>
        <tbody class="">

            <?php
            while ($row = mysqli_fetch_array($qry)) {
                $slvouch = $row['vouchno'];
                $gtsl = mysqli_query($conn, "SELECT vouchno, party FROM `salemain` WHERE `vouchno`='$slvouch'") or die(mysqli_error($conn));
                $ftsl = mysqli_fetch_array($gtsl);

                $gtslqt = mysqli_query($conn, "SELECT sum(recdqty) as tqty, sum(recdsqft) as sqft FROM `salepurch` WHERE `purvouch`='$vouchno' 
                                                      and salevouch ='$slvouch' and itemname = '$itemname' and size = '$size'") or die(mysqli_error($conn));
                
                $ftslqt = mysqli_fetch_array($gtslqt);
                if ($ftslqt['tqty'] > 0 || $ftslqt['sqft'] > 0) {
                    $recdqty = $ftslqt['tqty'];
                    $recdsqft = $ftslqt['sqft'];
                } else {
                    $recdqty = 0;
                    $recdsqft = 0;
                }

                $gtslqt2 = mysqli_query($conn, "SELECT sum(recdqty) as tqty, sum(recdsqft) as sqft FROM `salepurch` WHERE `salevouch`='$slvouch' 
                                                       and itemname = '$itemname' and salevouch ='$slvouch' and purvouch != '$vouchno'") or die(mysqli_error($conn));
                $ftslqt2 = mysqli_fetch_array($gtslqt2);
                if ($ftslqt2['tqty'] != 0 || $ftslqt2['sqft'] != 0 ) {
                    $recdqty2 = $ftslqt2['tqty'];
                    $recdsqft2 = $ftslqt2['sqft'];
                } else {
                    $recdqty2 = 0;
                    $recdsqft2 = 0;
                }

                $sup = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`='$ftsl[party]'") or die(mysqli_error($conn));
                $fsup = mysqli_fetch_array($sup);

                Echo "<tr>";
                Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                Echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$mctr' readonly tabindex='-1'></td>";
                Echo "<td><input type='text' name='partyname_$ctr' class='partyname form-control  form'  readonly value='$fsup[name]' tabindex='-1'>"
                . "<input type='hidden' name='party_$ctr' class='party form-control  form'  value='$fsup[accode]' readonly>"
                . "<input type='hidden' name='tcalby_$ctr' class='tcalby form-control  form'  value='$tcalby' readonly>"
                . "<input type='hidden' name='salevouch_$ctr' class='salevouch form-control  form'  value='$slvouch' readonly></td>";

                Echo "<td><input type='text' class='form-control form trqty'     name='trqty_$ctr'  value='$row[qty]' readonly tabindex='-1'></td>";
                Echo "<td><input type='text' class='form-control form othqty'     name='othqty_$ctr'  value='$recdqty2' readonly tabindex='-1'></td>";
                Echo "<td><input type='text' class='form-control form othsqft'     name='othsqft_$ctr'  value='$recdsqft2' readonly tabindex='-1'></td>";
                Echo "<td><input type='text' class='form-control form text-right recdqty' name='recdqty_$ctr'  value='$recdqty'></td>";
                Echo "<td><input type='text' name='recdsqft_$ctr' class='recdsqft form-control  form'  value='$recdsqft' ></td>";
//                Echo "<td style='align:center;padding-left: 5%;padding-bottom: 3.5%;' class=''><b><input type='checkbox' style='border:none;height:25px;width:25px;' align='center' name= 'complete_$ctr' class='complete form-control form'></a></b></td>";

                Echo "</tr>";
                $ctr++;
                $mctr++;
            }
        }
        ?>
    </tbody>

</table>
<input type="hidden" id="itemsqty" name="itemsqty" value="<?php Echo $tqty; ?>">  
<input type="hidden" id="itemspur" name="itemspur" value="<?php Echo $ctr; ?>">  

