<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Ramzan Ration Kit';
$maindb = 'ramzankit';
// $printfile = "company_print.php";
$file = 'rationkit.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$focusfield = "name";
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";
$focusfield = "name";
$username = $_SESSION["username_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

include("includes/vouchno_generate.php");
// include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $a = 1;
    $totqty = $_POST["totqty"];
    $totamt = $_POST["totamt"];
    $count = $_POST["items"];
    $totamt = $_POST["totamt"];
    if ($a == 1) {

        #################################TRAN PART #################################
        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `series`='$fseries'") or die(mysqli_error($conn));
        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $srno = $_POST["srno_$i"];
            $trdesc = $_POST["trdesc_$i"];
            $trqty = $_POST["trqty_$i"];
            $rate = $_POST["rate_$i"];
            $amount = $_POST["amount_$i"];

            if (!empty($trdesc)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$maindb`(`srno`, series,  `description`, `qty`, `rate`, `amount`, `totqty`, `totamt`)
                                        VALUES( '$srl', '$fseries', '$trdesc', '$trqty', '$rate', '$amount', '$totqty', '$totamt')") or die(mysqli_error($conn));
            }
        }
        // updmess();
        // header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' $keycond") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

$senable = "";
$cenable = "";

include("includes/linkshead.php");
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("#hadd").hide();
        $("#hprint").hide();
        $("#hdelete").hide();
        $("#hprevious").hide();
        $("#hsearch").hide();
        $("#hnext").hide();
    });
</script>
<script src="MyScript/rationkit.js" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST'>

        <div class="row">
            <div class="col-lg-8">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>
                    </div>

                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="20%">Description</th>
                                        <th width="11%" class='text-right'>Qty.</th>
                                        <th width="10%" class='text-right'>Rate</th>
                                        <th width="10%" class='text-right'>Amount</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $trate = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`!='' order by srno") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            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='srno_$ctr'        value='$tran[srno]'></td>";
                                            echo "<td><input type='text' class='form-control form trdesc'  name='trdesc_$ctr' value='$tran[description]'>  </td>";
                                            echo "<td><input type='text' class='form-control form text-right trqty'  name='trqty_$ctr' value='$tran[qty]'>  </td>";
                                            echo "<td><input type='text' class='form-control form text-right rate'   name='rate_$ctr'        value='$tran[rate]'>   </td>";
                                            echo "<td><input type='text' class='form-control form text-right amount' name='amount_$ctr'      value='$tran[amount]'> </td>";
                                            echo "</tr >";
                                            $ctr++;
                                            $tqty = $tqty + $tran['qty'];
                                            $tamt = $tamt + $tran['amount'];
                                        }
                                    } else {
                                        $pctr = 0;
                                    ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_<?= $pctr ?>" value="1"></td>
                                            <td><input type="text" class="form-control form text-right trdesc" name="trdesc_<?= $pctr ?>" value=""></td>
                                            <td><input type="text" class="form-control form text-right trqty" name="trqty_<?= $pctr ?>" value=""></td>
                                            <td><input type="text" class="form-control form text-right rate" name="rate_<?= $pctr ?>" value=""></td>
                                            <td><input type="text" class="form-control form text-right amount" name="amount_<?= $pctr ?>" value="" readonly></td>
                                        </tr>
                                    <?php
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>
                        </div>

                        <div class="row">
                            <div class="col-lg-1">
                                <button type="button" class="btn btn-square form" style="background-color: #78909C" id="addnew">
                                    <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>
                                </button>
                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-5 col-sm-3 col-xs-3 text-right pad">
                                <label for="totqty">Total Qty.</label>
                            </div>
                            <?php
                            $query2 = mysqli_query($conn, "SELECT `totqty`, `totamt` FROM `$maindb` WHERE `code`!=''") or die(mysqli_error($conn));
$res = mysqli_fetch_object($query2);
                            ?>
                            <div class="col-lg-2 col-md-5 col-sm-3 col-xs-3 text-right pad">
                            
                                <td><input type="text" class="form-control form text-right totqty" name="totqty" value="<?php echo ($addflag != 1) ? $res->totqty : ""; ?> "></td>
                            </div>
                            <div class="col-lg-3 col-md-5 col-sm-3 col-xs-3 text-right pad">
                                <label for="totamt">Total Amt.</label>
                            </div>
                            <div class="col-lg-2 col-md-5 col-sm-3 col-xs-3 text-right pad">
                                <td><input type="text" class="form-control form text-right totamt" name="totamt" value="<?php echo ($addflag != 1) ? $res->totamt : ""; ?>"></td>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-8">
                    <div class="panel panel-default">
                        <div class="panel-footer">
                            <? include("includes/buttons.php"); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>