<?php
ob_start();
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];
$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, mastcost from `save_stkread_temp` 
                                          where vouchno ='$vouchno' and vouchdt = '$vouchdt' and shift = '$shift' order by srl ") or die(mysqli_error($conn));

    $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: 6%'  class="text-left">Code</th>                                        
                    <th style='width: 12%' class="text-left">Product</th>                                        
                    <th style='width: 7%'  class="text-right">Sale</th>
                    <th style='width: 6%'  class="text-right">A/c Sale</th>
                    <th style='width: 6%'  class="text-right">Cash Sale</th>                                        
                    <th style='width: 6%'  class="text-right">Cash Amt.</th>                                        
                    <th style='width: 7%'  class="text-right">Rate</th>
                    <th style='width: 7%'  class="text-right">Cost</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, cost 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));

                $ctr2 = 0;
                $tcashamt = 0;

                while ($row2 = mysqli_fetch_array($getqr1)) {
                    $prodcode = $row2['prodcode'];
                    $prodname = $row2['prodname'];
                    $tsale = $row2['tsale'];
                    $mastrate = $row2['mastrate'];
                    $mastcost = $row2['mastcost'];

                    $getcred = mysqli_query($conn, "select cqty, rate, cost from credd where prodcode = '$prodcode' ") or die(mysqli_error($conn));
                    $fetcred = mysqli_fetch_array($getcred);
                    $credqty = $fetcred['cqty'];
                    $credrate = $fetcred['rate'];
                    $credcost = $fetcred['cost'];

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

                    if ($credcost > 0) {
                        $cost = $credcost;
                    } else {
                        $cost = $mastcost;
                    }

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

                    Echo "<tr>";
                    Echo "<td><input type='text' class='form-control form tprodcode'            name='tprodcode_$ctr2' value='$row2[prodcode]' readonly tabindex='-1'></td>";
                    Echo "<td><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 "<td><input type='text' class='form-control form text-right tcost'     name='tcost_$ctr2'     value='$cost'   readonly tabindex='-1'></td>";
                    Echo "</tr>";
                    $ctr2++;
                    $tcashamt = $tcashamt + $cashamt;
                }
                ?>
            <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
    }
}
