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

if (isset($_REQUEST['vouchno'])) {
    $shift = $_REQUEST["shift"];
    $vouchno = $_REQUEST["vouchno"];
    $vouchdt = $_REQUEST["vouchdt"];
    if (count($_REQUEST["pumpReadingGrouping"]) > 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: 7%' class="text-center">Rate</th>
                    <th style='width: 8%' class="text-center">Credit Sale</th>
                    <th style='width: 8%' class="text-center">Credit Amt.</th>
                    <th style='width: 8%' class="text-left">Cash Sale</th>                                        
                    <th style='width: 8%' class="text-left">Cash Amt.</th>                                        

                </tr>
            </thead>
            <tbody class="tbody2">
                <?php
                $ctr2 = 0;
                $tcashamt = 0;
                foreach ($_REQUEST["pumpReadingGrouping"] as $prodcode => $tsale) {

                    $getpd = mysqli_query($conn, "SELECT code,name,rate FROM `product` WHERE `prodcode`= '$prodcode'") or die(mysqli_error($conn));
                    $lpd = mysqli_fetch_array($getpd);
                    $code = $lpd['code'];

                    $getcred = mysqli_query($conn, "select sum(qty) as cqty, rate from saletran where vouchno in  
                                                           (select vouchno from salemain where paidby = 'A' and direct != 'Y' and vouchdt = '$vouchdt' and 
                                                            shift = '$shift' and dept = 'DW') and product = '$prodcode' group by product") or die(mysqli_error($conn));
                    $lcd = mysqli_fetch_array($getcred);
                    $credqty = $lcd['cqty'];


                    if ($lcd['rate'] > 0) {
                        $rate = $lcd['rate'];
                    } else {
                        $rate = $lpd['rate'];
                    }


                    $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='$prodcode' readonly tabindex='-1'>"
                    . "<input type='text' class='form-control form tproduct'                    name='tproduct_$ctr2' value='$lpd[name]'  readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right tsale'     name='tsale_$ctr2'  value='$tsale'   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 credsale'  name='credsale_$ctr2'  value='$credqty'   readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right credamt'   name='credamt_$ctr2'       value='$credamt'   readonly tabindex='-1'></td>";
                    Echo "<td><input type='text' class='form-control form text-right cashsale'  name='cashsale_$ctr2'  value='$cashqty'   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 "</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
    }
}
