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

$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');
$file = "prevbill.php";
$formname = "Previous Outstanding";
$action = "prevbill.php";
mysqli_select_db($conn, $zdb);
$maindb = "prevbill";

if (isset($_POST["submit"])) {
    $party = $_POST["party"];
    $count = $_POST["items"];

    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE");

    mysqli_query($conn, "DELETE FROM `$maindb` WHERE `accode`='$party'") or die(mysqli_error($conn));

    for ($i = 0; $i <= $count; $i++) {
        $billno = $_POST["billno_$i"];
        $billdt = $_POST["billdate_$i"];
        $year = $_POST["year_$i"];
        $billamt = $_POST["billamt_$i"];


        if (!empty($billno)) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`srno`, `accode`, `yearno`, `billno`, `billdate`, `billamt`, `vouchno`)
                                      VALUES('$i', '$party', '$year', '$billno', '$billdt', '$billamt','$billno')") or die(mysqli_error($conn));
        }
    }
    mysqli_query($conn, "UNLOCK TABLES");
    insmess();
    header("refresh: 1; $file");
}


include("includes/linkshead.php");
?>
<script src="MyScript/mastprevbill.js" type="text/javascript"></script>
</head>
<body>
    <form method='POST' action="">
        <!-- Default box -->

        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12  pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php Echo $formname; ?></h3>
                </div>
                <div class="panel-body">
                    <div class="partyent">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Party Name</label>
                            </div>
                            <div class="col-lg-5 col-md-6 col-sm-8 col-xs-8 pad">
                                <select class="form-control select2 party" name="party" >
                                    <option></option>
                                    <?php
                                    $gcust = mysqli_query($conn, "SELECT accode, name FROM `accmast` WHERE (grpcode = '010' or grpcode = '009') order by name") or die(mysqli_error($conn));
                                    while ($scust = mysqli_fetch_array($gcust)) {
                                        echo"<option value='$scust[accode]'>$scust[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 text-right pad">
                                <button type="button" class="hideParty btn btn-primary" name="search" style="font-size: 15px;">Proceed</button>
                            </div>
                        </div>
                    </div>
                    <div class="ShowParty" style="display: none;">
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-8 col-xs-8 text-right pad">
                                <button type="button" class="goback btn btn-primary" name="goback" style="font-size: 12px;">Go Back</button>
                            </div>
                        </div>
                        <div class="row">
                            <div class="table-responsive">
                                <table class="table-striped table-condensed table-bordered" style="width: 100%;">
                                    <thead  class="mySearchTr">
                                        <tr>
                                            <th style="width: 5%"></th>
                                            <th style="width: 10%">Year</th>
                                            <th style="width: 30%;">Bill No</th>
                                            <th style="width: 20%;">Bill Date</th> 
                                            <th style='width: 35%; text-align: right'>Bill Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody class="tbody">

                                    </tbody>
                                </table>
                            </div>
                            <div class="col-lg-12 pad">
                                <button type="button" class="btn btn-primary btn-square"  id="addnew">
                                    <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                                </button>
                                <input type="hidden" id="items" name="items" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>">  
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 text-right pad">
                                <input type="submit" class="btn btn-primary" name="submit" style="font-size: 15px;">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>