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

$formname = 'Bank Receipt';
$maindb = 'bankrecp_de';
$maindbact = 'bankrecp';
$accdb = 'actran';
$billdb = 'billsett';

$file = 'bankrecp_de.php';
$grid = '';
$source = 'BR';
$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'bankrecp_fr.php';
$type = "S";

//$printfile = 'bankr_voucher.php';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);

$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);

if (!empty($zdb)) {
    $mysqli->select_db($zdb);
}

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

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

    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $qvouch = $_POST["vouchno"];
    $bankname = $_POST["bankname"];
    $recpno = $_POST["recpno"];
    $chqno = $_POST["chqno"];
    $chqdate = $_POST["chqdate"];
    $drawbank = $_POST["drawbank"];
    $party = $_POST["party"];
    $amount = $_POST["amount"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $counter = $_POST["counter"];
    $billflag = $_POST["billflag"];
    $retdate = $_POST["retdate"];
    $chqret = $_POST["chqret"];
    $actvouchno = $_POST["actvouchno"];

    $mvouch = $qvouch;

    if (trim($bankname) === "") {
        $a = 0;
        browse("Bank Name cannot be empty");
        header("refresh: 1; $file?vouchno={$_GET['vouchno']}");
        exit;
    }
    

    if ($a === 1) {
        try {
            // Lock tables
            $mysqli->query("LOCK TABLES `$maindb` WRITE, `accmast` WRITE, `$accdb` WRITE, `$billdb` WRITE, $maindbact WRITE");

            // Begin transaction
            $mysqli->begin_transaction();

            if ($addflag == 1) {
                $result = $mysqli->query("SELECT MAX(vouchno) AS maxcode FROM `$maindb` LIMIT 1");
                $qvouch = ($row = $result->fetch_assoc()) ? $row['maxcode'] + 1 : 1;
                $mvouch = $qvouch;

                $result2 = $mysqli->query("SELECT MAX(vouchno) AS maxcode FROM `$maindbact` LIMIT 1");
                $qvoucha = ($row2 = $result2->fetch_assoc()) ? $row2['maxcode'] + 1 : 1;
                $actvouchno = $qvoucha;
            }

            // Delete existing entries
            $mysqli->query("DELETE FROM `$accdb` WHERE vouchno='$actvouchno' AND source='$source'");

            if ($amount > 0) {
                $bankinit = $mysqli->query("SELECT bankinit FROM accmast WHERE accode = '$bankname'")->fetch_assoc()['bankinit'] ?? '';
                $pgrp = $mysqli->query("SELECT grpcode FROM accmast WHERE accode = '$party'")->fetch_assoc()['grpcode'] ?? '';
                $minus_amt = $amount * -1;

                // Insert bank (cash) part
                $mysqli->query("INSERT INTO `$accdb` (date, vouchno, source, trnno, accode, bankinit, grpcode, 
                    amount, chqno, chqdate, prodtype, actamt, chqret, retdate)
                    VALUES ('$vouchdt','$actvouchno', '$source', '001', '$bankname', '$bankinit', 
                    '001', '$minus_amt','$chqno', '$chqdate','$prodtype','$minus_amt', '$chqret', '$retdate')");

                // Insert party part
                $mysqli->query("INSERT INTO `$accdb` (date, vouchno, source, trnno, accode, bankinit, grpcode, 
                    amount, chqno, chqdate, prodtype, actamt, chqret, retdate)
                    VALUES ('$vouchdt','$actvouchno', '$source', '002', '$party', '$bankinit', 
                    '$pgrp', '$amount','$chqno', '$chqdate','$prodtype','$amount', '$chqret', '$retdate')");
            }

            if ($addflag == 1) {
                $mysqli->query("INSERT INTO `$maindb` (vouchno, vouchdt, bankname, bankinit, recpno, chqno, chqdate, drawbank, 
                    party, amount, remark1, remark2, type, chqret, retdate, actvouchno)
                    VALUES ('$qvouch', '$vouchdt', '$bankname', '$bankinit', '$recpno','$chqno','$chqdate',
                    '$drawbank', '$party','$amount','$remark1','$remark2','S', '$chqret', '$retdate', '$actvouchno')");

                $mysqli->query("INSERT INTO `$maindbact` (vouchno, vouchdt, bankname, bankinit, recpno, chqno, chqdate, drawbank,
                    party, amount, remark1, remark2, type, chqret, retdate)
                    VALUES ('$actvouchno', '$vouchdt', '$bankname', '$bankinit', '$recpno','$chqno','$chqdate',
                    '$drawbank', '$party','$amount','$remark1','$remark2','S', '$chqret', '$retdate')");

                $mysqli->commit();
                $mysqli->query("UNLOCK TABLES");

                insmess();
                header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            } else {
                $mysqli->query("UPDATE `$maindb` SET vouchdt='$vouchdt', bankname='$bankname', bankinit='$bankinit',
                    recpno='$recpno', chqno='$chqno', chqdate='$chqdate', drawbank='$drawbank', party='$party', actvouchno='$actvouchno',
                    amount='$amount', remark1='$remark1', remark2='$remark2', type='S', chqret='$chqret', retdate='$retdate' 
                    WHERE vouchno = '$mvouch'");

                $mysqli->query("UPDATE `$maindbact` SET vouchdt='$vouchdt', bankname='$bankname', bankinit='$bankinit',
                    recpno='$recpno', chqno='$chqno', chqdate='$chqdate', drawbank='$drawbank', party='$party',
                    amount='$amount', remark1='$remark1', remark2='$remark2', type='S', chqret='$chqret', retdate='$retdate' 
                    WHERE vouchno = '$actvouchno'");

                $mysqli->commit();
                $mysqli->query("UNLOCK TABLES");

                updmess();
                header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            }

        } catch (Exception $e) {
            $mysqli->rollback();
            $mysqli->query("UNLOCK TABLES");
            browse("Transaction failed: " . $e->getMessage());
        }
    }    
    
  
}

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

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

    $getbank = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[bankname]' and grpcode = '001'");
    $banknm = mysqli_fetch_array($getbank);

    $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[party]'");
    $gtpt = mysqli_fetch_array($getparty);

    $avouch = $get["actvouchno"];
}

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

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$accdb` WRITE, `$maindbact` WRITE") or die(mysqli_error($conn));

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$maindbact", "$keyfld", "$avouch", "$file?mode=last");

//    $delsett = " source = '$source' AND bnkvouch = '$_GET[vouchno]'";
    $delcond = " source = '$source' and vouchno = '$avouch' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
//    delnew($zdb, "billsett", "$delsett", "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/bankrecp.js" type="text/javascript"></script>
<script src="MyScript/bankrecp_search.js" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<script src="MyScript/shortkeys.js" type="text/javascript"></script>
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>

                    </div>
                    <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                    <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                    <div class="panel-body">
                        <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">Receipt Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form vouchno" id="vouchno"  name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required readonly>
                                <input type="hidden" class="form-control form trnflag" name="trnflag" value="<?php Echo ($addflag != 1) ? $get["trnflag"] : ""; ?>" readonly>
                                <input type="hidden" class="form-control actvouchno" name="actvouchno" value="<?php Echo ($addflag != 1) ? $get["actvouchno"] : ""; ?>" readonly>
                                <input type="hidden" class="form-control form billflag" name="billflag" value="0">
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form vouchdt focusfield" id="vouchdt"  name="vouchdt" autofocus value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <a class="btn btn-primary" href="bankr_voucher.php?vouchno=<?= $qvouch; ?>" style="height:25px" target="_blank" tabindex="-1">Voucher</a>
                            </div>
                        </div>
                        <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">Bank Name</label>
                            </div>
                            <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 pad">
                                <select class="form-control form select2" id="bankcode"  name="bankname" style='width:100%' required>
                                    <option value="<?php echo ($addflag != 1) ? $banknm['accode'] : " "; ?>" ><?php echo ($addflag != 1) ? $banknm['name'] : " "; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT * FROM `accmast` where `grpcode` = '001' ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value=$trow[accode]>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label style="color:#26A69A"label-control marg ><b id="bankamt"></b></label>
                            </div>
                        </div>
                        <!--                        <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">Receipt No.</label>
                                                    </div>
                                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                                        <input type="text" name="recpno" value="<?php echo ($addflag != 1) ? $get['recpno'] : ""; ?>" class="form-control form recpno" readonly>                                    
                                                    </div>
                                                </div>-->
                        <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">Cheque No.</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="chqno" value="<?php echo ($addflag != 1) ? $get['chqno'] : ''; ?>" class="form-control form" >
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date"  name="chqdate" value="<?php echo ($addflag != 1) ? $get['chqdate'] : date('d-m-Y'); ?>" class="form-control form chqdate">
                            </div>
                        </div>
                        <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">Drawee Bank</label>
                            </div>
                            <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 pad">
                                <input type="text" name="drawbank" value="<?php echo ($addflag != 1) ? $get['drawbank'] : ''; ?>" class="form-control form">
                            </div>
                        </div>
                        <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">Account</label>
                            </div>
                            <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 pad">
                                <select id="party" name="party" class="select2 form-control form">
                                    <option value="<?php echo ($addflag != 1) ? $gtpt['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $gtpt['name'] : " "; ?></option>
                                    <?php
                                    $mown1 = mysqli_query($conn, "SELECT * FROM `accmast` where grpcode > '002' order by `name`") or die(mysqli_error($conn));
                                    while ($trow1 = mysqli_fetch_array($mown1)) {
                                        echo "<option value=$trow1[accode]>$trow1[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <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">Amount</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="amount" id="amount" text-right value="<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>" class="form-control form amount  numeric text-right"  required>
                            </div>
                        </div>
                        <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">Remark</label>
                            </div>
                            <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 pad">
                                <input type="text" name="remark1" value="<?php echo ($addflag != 1) ? $get['remark1'] : ''; ?>" class="form-control form remark1"  >
                            </div>
                        </div>

                        <?php
//                        if ($get['remark2'] != '') {
//                            $billstr = $get['remark2'];
//                        } else {
//                            if (!empty($result2)) {
//                                $billstr2 = 'Receieved For Bill No. ';
//                                $count = mysqli_num_rows($result2);
//                                $no = 1;
//                                $billstri = '';
//                                while ($row2 = mysqli_fetch_array($result2)) {
//                                    $bbno = $row2['billno'];
////                                browse($bbno);
//                                    $billstri = $billstri . $bbno . ',';
//                                }
////                            browse($billstri);
//                                $billstr = $billstr2 . " " . $billstri;
//                            }
//                        }
                        ?>
                        <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"></label>
                            </div>
                            <div class="col-lg-7 col-md-7 col-sm-8 col-xs-8 pad">
                                <input type="text" name="remark2" value="<?php echo ($addflag != 1) ? $get['remark2'] : ''; ?>" class="form-control form remark2" >
                            </div>
                        </div>

                        <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" style="color: red;">Cheque Return</label>
                            </div>
                            <div class="col-lg-2 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="checkbox" class="form-control form gel text-right" name="chqret" value="Y"   style="color: red;"
                                <?php
                                if ($get['chqret'] == "Y") {
                                    echo "checked";
                                }
                                ?>>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg"  style="color: red;" >Returned Date</label>
                            </div>
                            <div class="col-lg-2 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="date"  name="retdate"  style="color: red;" value="<?php echo ($addflag != 1) ? $get['retdate'] : date('d-m-Y'); ?>" class="form-control form clrdate">
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
        <!--        <div class="modal fade " tabindex="" id="settmodal" role="dialog" aria-labelledby="myLargeModalLabel">
                    <div class="modal-dialog modal-lg">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                                <h4 class="modal-title" id="myLargeModalLabel">Bill Settlement</h4>
                            </div>
                            <div class="modal-body">
                                <div class=" table-responsive">
                                    <div class="toolbar">
                                        <div class="row">
                                            <div class="col-lg-2"><label class="label-control marg" style="color:red">Total Remain</label></div>
                                            <div class="col-lg-2"><input type="text" id="remain" name="remain" class="form-control" readonly style=""></div>
                                            <div class="col-lg-2"><label class="label-control marg" style="color:blue">Total Setoff</label></div>
                                            <div class="col-lg-2"><input type="text" id="totsetoff" name="totsetoff" class="form-control" readonly style=""></div>
                                            <div class="col-lg-2"><input id="submit" type='submit' name='submit' value='<?php // echo ($addflag == 0) ? "Update" : "Save";                          ?>' class="btn btn-primary"></div>
        <?php
//                                    if (!isset($_GET["method"])) {
        ?>
                                            <div class="col-lg-2"><input type='button' name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<?php echo $grid; ?>'"></div>
        <?php // }    ?>
                                        </div>
                                    </div>
                                    </header>
                                    <div id="div-2" class="accordion-body collapse in body">
                                        <div style="max-height:300px;overflow: auto; border: 0px solid;">
                                            <div id="data"></div>  
                                        </div>
                                    </div>    
                                </div>
                            </div>
                            <div class="modal-footer"></div>
                        </div>
                    </div>
                </div>-->
    </div>
</form>
<div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
            </div>
            <div class="modal-body">
                <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                <div class="row">
                    <div class="col-lg-2">
                        <select class="form-control sfield">
                            <option value="1">Party</option>
                            <option value="2">Voucher No.</option>
                            <option value="3">Bank Name</option>
                            <option value="4">Drawee Bank</option>
                            <option value="5">Cheque No.</option>
                            <option value="6">Amount</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control soperator">
                            <option value="">Type of Search</option>
                            <option value="%">Like</option>
                            <option value="=">Exactly</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control sortBy">
                            <option value="">Sort By</option>
                            <option value="1">Party</option>
                            <option value="2">Voucher No.</option>
                            <option value="3">Bank Name</option>
                            <option value="4">Drawee Bank</option>
                            <option value="5">Cheque No.</option>
                            <option value="6">Amount</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control orderAs">
                            <option value="desc">Desc</option>
                            <option value="asc">Asc</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                    </div>
                    <div class="col-lg-2">
                        <input type="button" class="btn btn-primary sload" value="Search" >
                    </div>
                </div>
                <div class="col-lg-12 sloader text-center">
                    <!--<img src="images/search.gif" alt="loading">-->       
                </div>
                <div class="col-lg-12 snoRecord text-center">
                    <h3>NO RECORDS FOUND</h3>
                </div>
                <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                    <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                        <thead>
                            <tr style="background-color:#1ABC9C;">
                                <th width="10%">Voucher</th>
                                <th width="15%">Bank Name</th>
                                <th width="27%">Party</th>
                                <th width="21%">Drawee Bank</th>
                                <th width="12%">Cheque No.</th>
                                <th width="10%">Amount</th>
                                <th width="5%">Action</th>
                            </tr>
                        </thead>
                        <tbody class="sTbody">
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right search-action-bar">
                        <input type="button"  class="btn btn-primary sprev" value="Previous">
                        <input type="button"  class="btn btn-primary snext" value="Next">
                        <input type="hidden"  class="para" value="N">
                    </div>
                </div>
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>
<?php include("includes/linksfoot.php"); ?>
</body>
</html>
