<?php
ob_start();
echo "1";
require("includes/check_auth_user.php");
echo "2";
require("includes/dbconnect.php");
echo "3";
include('includes/function.php');
    browse('feg');

$zdb = $_SESSION["zdbyear_" . $dbname];
    browse('feg');

mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
    browse('feg');
$zroot = $dbname;

$zcono = $_SESSION["comp_" . $dbname];
$zyear = $_SESSION["tit_" . $dbname] ;

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

    $getqr1 = mysqli_query($conn, "Select vouchdt, vouchno, netsale as tsale, prodcode, prodname, mastrate from `save_stkread_temp` 
                                          where vouchno ='$vouchno' and vouchdt = '$vouchdt' and shift = '$shift' order by srl ") or die(mysqli_error($conn));
    
    browse('feg');
    $mno = mysqli_num_rows($getqr1);
    if ($mno > 0) {
        ?>
        <table class="table-striped table-condensed table-bordered tab" width="100%">
            <thead  class="mySearchTr">
                <tr>
                    <th style='width: 10%' class="text-left">Product</th>                                        
                    <th style='width: 7%' class="text-center">Sale</th>
                    <th style='width: 8%' class="text-center">A/c Sale</th>
                    <th style='width: 8%' class="text-left">Cash Sale</th>                                        
                    <th style='width: 8%' class="text-left">Cash Amt.</th>                                        
                    <th style='width: 7%' class="text-center">Rate</th>
                </tr>
            </thead>
            <tbody class="tbody2">
                <?php
                $qryyear = mysqli_query($conn, "select prod1, prod2, prod3, prod4 from $zroot.compyear where cono = '$zcono' and finyear = '$zyear' " ) or die(mysqli_error($conn));
                $fetyear = mysqli_fetch_array($qryyear) ;
                $prod1 = $fetyear['prod1'];
                $prod2 = $fetyear['prod2'];
                $prod3 = $fetyear['prod3'];
                $prod4 = $fetyear['prod4'];
                
                mysqli_query($conn, "drop temporary table if exists credd" ) or die(mysqli_error($conn));
                mysqli_query($conn, "create temporary table credd 
                                            select vouchno, vouchdt, prodcode, sum(qty) as cqty, rate from saletran 
                                                   where prodcode in ('$prod1', '$prod2', '$prod3', '$prod4') and 
                                                   vouchno in (select vouchno from salemain where direct != 'Y' 
                                                   and vouchdt = '$vouchdt' and shift = '$shift' and dept = 'DW') group by product") or die(mysqli_error($conn));
                
                browse()'pqr');
                $ctr2 = 0;
                $tcashamt = 0;

                while ($row2 = mysqli_fetch_array($getqr1)) {
                    $prodcode = $row2['prodcode'];
                    $prodname = $row2['prodname'];
                    $tsale = $row2['tsale'];
                    $mastrate = $row2['mastrate'];
                    
                    $getcred = mysqli_query($conn, "select cqty, rate from credd where prodcode = '$prodcode' ") or die(mysqli_error($conn));
                    $fetcred = mysqli_fetch_array($getcred);
                    $credqty = $fetcred['cqty'];
                    $credrate= $fetcred['rate'];

                    if ($credrate > 0) {
                        $rate = $credrate;
                    } else {
                        $rate = $mastrate;
                    }

                    $credamt = $credqty * $rate;
                    $cashqty = $tsale - $credqty;
                    $cashamt = $cashqty * $rate;

                    Echo "<tr>";
                    Echo "<td><input type='hidden' class='form-control form tprodcode'          name='tprodcode_$ctr2' value='$row2[prodcode]' readonly tabindex='-1'>"
                    . "<input type='text' class='form-control form tproduct'                    name='tproduct_$ctr2'  value='$row2[prodname]'  readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right tsale'     name='tsale_$ctr2'     value='$row2[tsale]'   readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right credsale'  name='credsale_$ctr2'  value='$credqty'   readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right cashsale'  name='cashsale_$ctr2'  value='$cash'   readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right cashamt'   name='cashamt_$ctr2'   value='$cashamt'   readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right trate'     name='trate_$ctr2'     value='$rate'   readonly tabindex='-1'></td>";
                    Echo "</tr>";
                    $ctr2++;
                    $tcashamt = $tcashamt + $cashamt;
                }
                browse('xyz');
                
                ?>
            <input type="hidden" id="items2" name="items2" value="<?php Echo $ctr2; ?>">  
            </tbody>
        </table>
        <div class="row">

            <div class="col-lg-10 col-xs-8 pad text-right">
                <label class="label-control marg">Cash Amount(Rs.)</label>
            </div>
            <div class="col-lg-2 col-xs-3 pad">
                <input type="number" step="any" name="cashamt" class="form-control form text-right cashamt" readonly value="<?php Echo $tcashamt; ?>" style="width: 90%">    
            </div>
        </div>
        <?php
    }
}
