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

$formname = 'Previous Bill ';
$maindb = 'prevbill';
$file = 'prevbill.php';


$keyfld = "vouchno";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];

$zroot = $_SESSION["zdbroot"];
$accode = $_POST["supplier"];

$username = $_SESSION["username_" . $dbname];
mysqli_select_db($conn, $zdb) or die(mysqli_error($conn));

$supplier = $_GET['supplier'];

if (isset($_POST["submit"])) {
    $a = 1;

    $count = $_POST["items"];
    $supp  = $_POST["supp"];
    

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE") or die(mysqli_error($conn));
        

################################# TRAN PART 1 - START #################################
        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `accode`='$supp' ") or die(mysqli_error($conn));

        $cntr = 0;
        for ($i = 0; $i < $count; $i++) {
            $srno = $_POST["srno_$i"];
            $yearno = $_POST["yearno_$i"];
            $billno = $_POST["billno_$i"];
            $billdate = $_POST["billdate_$i"];
            $billamt = $_POST["billamt_$i"];
            $exam = $_POST["exam_$i"];

            if (!empty($billno) && ($billamt > 0)) {
                $cntr++;
                mysqli_query($conn, "INSERT INTO `$maindb`(`accode`,`srno`, `yearno`, `vouchno`, `billno`, `billdate`,`billamt`,`type`, `examcode`)
                             VALUES ('$supp','$cntr', '$yearno', '$billno', '$billno', '$billdate', '$billamt', 'S', '$exam')") or die(mysqli_error($conn));
            }
        }
        header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
################################# TRAN PART 1 - END #################################        
###################################################### MAIN ######################################################

        $mvouch = $qvouch; //1
    }
    mysqli_query($conn, "unlock tables");
}

$fsupp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$accode' order by `name`") or die(mysqli_error($conn));
$supp = mysqli_fetch_array($fsupp);

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

        <!-- Default box -->
        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title" style="color:#0098DA"><?php Echo $formname; ?></h3>
                    <div class="actions pull-right">
                        <i class="fa fa-chevron-down"></i>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <input type="text" class="form-control" name="supp" style="display:  none" value="<?php echo $accode; ?>">
                        <label class="col-lg-5 col-md-5 col-sm-3 col-xs-3 label-control text-right"><?php echo $supp['name']; ?></label>
                    </div>
                    <table class="table table-striped table-condensed table-bordered" style="width: 100%">
                        <thead >
                            <tr style="background-color: #0098DA">
                                <th width="3%"></th>
                                <th width="5%">Sr</th>
                                <th width="8%">Year No.</th>
                                <th width="10%">Bill No.</th>
                                <th width="10%">Bill Date</th>
                                <th width="12%" class="text-right">Bill Amount</th>
                                <th width="12%" class="">Exam</th>
                            </tr>
                        </thead>
                        <tbody class="tbody">
                            <?php
                            $ctr = 0;
                            $mctr = 0;
                            $totamt = 0;

                            $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `accode`='$accode' order by `srno`") or die(mysqli_error($conn));
                            $gttrn = mysqli_num_rows($result);
                            if ($gttrn > 0) {
                                while ($tran2 = mysqli_fetch_array($result)) {
                                    $getmexm1 = mysqli_query($conn, "SELECT code, name FROM `exam` where code = '$tran2[examcode]' order by name")or die(mysqli_error($conn));
                                    $mexm1 = mysqli_fetch_array($getmexm1);

                                    $mctr ++;
                                    Echo"<tr>";
                                    Echo "<td><i class='fa fa-close remove'></i></td>";
                                    Echo"<td><input type='text' class='form-control srno'   name='srno_$ctr'      value='$mctr' readonly></td>";
                                    Echo"<td><input type='text' class='form-control yearno'   name='yearno_$ctr'  value='$tran2[yearno]'  ></td>";
                                    Echo"<td><input type='text' class='form-control billno'   name='billno_$ctr'    value='$tran2[billno]' ></td>";
                                    Echo"<td><input type='date' class='form-control billdate'   name='billdate_$ctr'  value='$tran2[billdate]' ></td>";
                                    Echo"<td><input type='text' class='form-control billamt text-right'   name='billamt_$ctr'  value='$tran2[billamt]' ></td>";
                                    Echo "<td>";
                                    Echo "<select class='form-control exam select2' name='exam_$ctr' style='width:100%'>";
                                    Echo "<option value='$mexm1[code]'>$mexm1[name]</option>";
                                    Echo "<option value=''></option>";
                                    $getmexm = mysqli_query($conn, "SELECT code, name FROM `exam` order by name")or die(mysqli_error($conn));
                                    while ($mexm = mysqli_fetch_array($getmexm)) {
                                        Echo "<option value='$mexm[code]'>$mexm[name]</option>";
                                    }
                                    Echo "</select>";
//                                            echo "<input type='hidden' name='examname_$ctr' class='examname' value='$tran[examname]'>";
                                    Echo "</td>";
                                    Echo"</tr>";

                                        $totamt = $totamt + $tran2['billamt'];

                                    $ctr++;
                                }
                            } else {
                                $pctr = 0;
                                Echo"<tr>";
                                Echo "<td><i class='fa fa-close remove' ></i></td>";
                                Echo"<td><input type='text' class='form-control srno'   name='srno_$pctr'      value='1' readonly></td>";
                                Echo"<td><input type='text' class='form-control yearno'   name='yearno_$pctr'  value='' autofocus></td>";
                                Echo"<td><input type='text' class='form-control billno'   name='billno_$pctr'    value='' ></td>";
                                Echo"<td><input type='date' class='form-control billdate'   name='billdate_$pctr'  value=''></td>";
                                Echo"<td><input type='text' class='form-control billamt text-right'   name='billamt_$pctr'  value=''></td>";
                                Echo "<td>";
                                Echo "<select class='form-control exam select2' name='exam_$pctr' style='width:100%'>";
                                Echo "<option value=''></option>";
                                $getmexm = mysqli_query($conn, "SELECT code, name FROM `exam` order by name")or die(mysqli_error($conn));
                                while ($mexm = mysqli_fetch_array($getmexm)) {
                                    Echo "<option value='$mexm[code]'>$mexm[name]</option>";
                                }
                                Echo "</select>";
//                                            echo "<input type='hidden' name='examname_$ctr' class='examname' value='$tran[examname]'>";
                                Echo "</td>";
                                Echo"</tr>";
                                $pctr++;
                                
                                
                            }
                            ?>
                        </tbody>
                    </table>
                    <div class="row">
                    <div class="col-lg-6 pad">
                        <button type="button"  class="btn btn-success btn-square"  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 ($gttrn > 0) ? $ctr : $pctr; ?>" />  
                    </div>
                    <div class="col-lg-1 col-md-2 text-right">
                        <label>Total</label>
                        </div>
                        <div class="col-lg-2 pad">
                        <input type="text" class="form-control form text-right totamt" name="totamt" value="<?php echo $totamt ; ?>">  
                    </div>
<!--                    <div class="col-lg-1 col-md-1">
                        <input type="text" class="form-control form text-right" name="totamt" value="<?php echo $totamt ; ?>">  
                    </div>-->
                </div>
            </div>
        </div>
        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div style="padding-left: 40%;" class="panel-footer">
                        <button id='view' type='submit' name='submit' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                            <i class="fa fa-check" style="color:#ffffff"></i>
                        </button>
                    <!--<input type="submit" name="exit" value="Exit" id="exit" class="btn btn-danger" onclick="window.location.href='followup.php'">-->
                        <button id='cancel' type="reset" class="btn btn-pad btn-square" style="background-color:red;" onclick="window.location.href = 'prevbill.php'">
                            <i class="fa fa-power-off" style="color:#ffffff"></i>
                        </button>
                    </div>     
                </div>
            </div>
        </div>

    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
