<?php
ob_start();

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

$formname = 'Bank Payment';
$maindb = 'bankpay';
$trandb = 'bptran';

$accdb = 'actran';
$file = 'bankmultipay.php';

$grid = '';
$source = 'BP';
$keyfld = "vouchno";
$keycond = "";
$trandbexist = 0;
$focusfld = "name";
//$printfile = 'bankrecp_fr.php';
$printfile = 'brmultipay_voucher.php';
date_default_timezone_set('Asia/Kolkata');
$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];
$adduser = $_SESSION['username_' . $dbname];
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");

$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);
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"];
    $branch = $_POST["branch"];
    $chqno = $_POST["chqno"];
    $chqdate = $_POST["chqdate"];
    $drawbank = $_POST["drawbank"];
    $count = $_POST["items"];
    $mtotamt = $_POST["totamt"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];

    $mvouch = $qvouch;
    
    if ($bankname == "") {
        $a = 0;
        browse("Name Cannot Be Empty");
        
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh:1; $file?vouchno=$_GET[vouchno]");
        }
    }
    
    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE, `$accdb` WRITE, `$trandb` WRITE") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
            
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch ;
        }


        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch' ") or die(mysqli_error($conn));
        
        mysqli_query($conn, "DELETE FROM `$accdb`  WHERE `$keyfld`='$mvouch' and source = '$source' ") or die(mysqli_error($conn));

        $getbankinit = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$bankname'");
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];

        // first bank part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, bankinit, grpcode, amount, chqno, chqdate)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$bankname', '$binit', '001', '$mtotamt', '$chqno', '$chqdate')") or die(mysqli_error($conn));


        $ctr = 1;
        $srl = 0;
        
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mparty = $_POST["party_$i"];
            $mgrpcode = $_POST["grpcode_$i"];
            $mamount = $_POST["amount_$i"];
            $mremark = $_POST["remark_$i"];

            if (!empty($mparty)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `accode`, `amount`, `remark`)
                                            VALUES('$mvouch', '$vouchdt', '$msrno', '$mparty', '$mamount', '$mremark')") or die(mysqli_error($conn));


                $ctr++;
                $mctr1 = 90000 + $ctr;
                $mctr = substr(strval($mctr1), -3);
                // party part
                $minus_amt = $mamount * -1;

                mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, bankinit, grpcode, amount, chqno, chqdate)
                    VALUES ('$vouchdt','$mvouch', '$source', '$mctr', '$mparty', '$binit', '$mgrpcode', '$minus_amt', '$chqno', '$chqdate')") or die(mysqli_error($conn));
            }
        }

///////////// blank accode from multi bank payment
        
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( vouchno ,`vouchdt`, `bankname`, bankinit, `recpno`, `chqno`,`chqdate`,`adduser`, `adddate`, `addtime`,
                                                `drawbank`,`amount`, `type`,remark1,remark2, party)
                                       Values ('$mvouch', '$vouchdt', '$bankname', '$binit',  '$recpno','$chqno','$chqdate', '$adduser', '$adddate', '$addtime',
                                    '$drawbank','$mtotamt','M','$remark1','$remark2', '')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            
            header("refresh:1; $file?&mode=last");
            
        } else {
            
            $mvouch = $qvouch;
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");    
            mysqli_query($conn, "UPDATE `$maindb` SET  vouchdt='$vouchdt', bankname='$bankname', bankinit='$binit', 
                    recpno='$recpno' ,chqno='$chqno',chqdate='$chqdate',drawbank='$drawbank',remark1='$remark1',remark2='$remark2',
                    amount='$mtotamt', `type`='M', `party`='',`edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate'  where vouchno = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh:1; $file?$keyfld=$mvouch&mode=edit");
            
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}

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]'");
    $banknm = mysqli_fetch_array($getbank);

//    $getparty = mysqli_query($conn,"SELECT * FROM `accmast` WHERE `accode` = '$get[party]'");
//    $gtpt = mysqli_fetch_array($getparty);
//    $getbranch = mysqli_query($conn,"SELECT * FROM `branch` where `code` = '$get[branch]' ORDER BY `name`") or die(mysqli_error($conn));
//    $mbranch = mysqli_fetch_array($getbranch);
}

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

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

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last&type=M");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last&type=M");

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last&type=M");

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

include("includes/linkshead.php");
?>
<html>
<!--<script src="MyScript/challan.js" type="text/javascript"></script>-->
    <script src="MyScript/bankmultipay_search.js" type="text/javascript"></script>

    <script src="MyScript/checkdate.js" type="text/javascript"></script>
    <script src="MyScript/shortkeys.js" type="text/javascript"></script>
    <script src="MyScript/brmulti.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-10">
                <div class="panel panel-default">
                <div class="panel-heading" style="height:60px">
                        <div class="col-lg-6 col-md-10 col-sm-10  col-xs-10">
                            <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php echo $formname; ?></a></h3>
                        </div>
                        <div class="col-lg-6 col-md-2 col-sm-2 text-right col-xs-2">
                            <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                            <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            <input type="date" value="<?= $salelock ?>" style="width: 1px;border: none" class="salelock">
                            <?php
                            $useadd = $edituser = '';
                            if ($get['username'] != '' || $get['adduser'] != '') {
                                if ($get['adduser'] != '') {
                                    $addt = date("d-m-Y", strtotime($get['adddate']));
                                    $useradd = "<h4 class='panel-title' style='float:right;color:green'>" . $get['adduser'] . "&nbsp;&nbsp;&nbsp;" . $addt . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $get['addtime'] . "</h4>";
                                } else {
                                    $addt = date("d-m-Y", strtotime($get['adddate']));
                                    $useradd = "<h4 class='panel-title' style='color:green'>" . $get['username'] . "</h4>";
                                }
                            }
                            if ($get['edituser'] != '') {
                                $editdt = date("d-m-Y", strtotime($get['editdate']));

                                $edituser = "<h4 class='panel-title' style='float:right;color:orange'>" . $get['edituser'] . "&nbsp;&nbsp;&nbsp;" . $editdt . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $get['edittime'] . "</h4>";
                            }
                            if ($get['adduser'] != '' && $get['edituser'] != '') {
                                $user = $useradd . "<br>" . $edituser;
                            } else {
                                $user = $useradd . $edituser;
                            }
                            if ($usertype == 1) { ?>
                                <label class="marg" style="">&nbsp;<?php echo $user; ?></label>
                            <? } else { ?>
                                <label class="marg" style="">&nbsp;<?php echo $get['adduser']; ?></label>
                            <? } ?>
                        </div>
                    </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-2 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-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                            </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-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form vouchdt focusfield" name="vouchdt" 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="brmulti_voucher.php?vouchno=<? $qvouch; ?>" target="_blank">Voucher</a>-->
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 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-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <select class="form-control form select2" 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-2 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-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="recpno" value="<?php echo ($addflag != 1) ? $get['recpno'] : $qvouch; ?>" class="form-control form" readonly>                                    
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 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" required>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Chq.Date</label>
                            </div>
                            <div class="col-lg-2 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-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Drawee Bank</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" name="drawbank" value="<?php echo ($addflag != 1) ? $get['drawbank'] : ''; ?>" class="form-control form">
                            </div>
                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-striped table-condensed table-bordered" width="100%">
                                            <thead  style="" >
                                                <tr class="mySearchTr">
                                                    <th width="4%"></th>
                                                    <th width="8%" class='text-right'>Sr.</th>
                                                    <th width="40%"class='text-left'>Accounts</th>
                                                    <th width="20%"class='text-right amt'>Amount</th>
                                                    <th width="30%">Remarks</th>
                                                </tr>
                                            </thead>
                                            <tbody class="tbody">
                                                <?php
                                                if ($addflag != 1) {
                                                    $ctr = 0;
                                                    $totamt = 0;
                                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                                                    while ($tran = mysqli_fetch_array($gettran)) {
                                                        $gparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$tran[accode]'") or die(mysqli_error($conn));
                                                        $getparty = mysqli_fetch_array($gparty);

                                                        Echo "<tr>";
                                                        Echo "<td width='4%'><i class='fa fa-close remove' style='display:none;'></i></td>";
                                                        Echo "<td width='8%'><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                                        Echo "<td width='40%'>";
                                                        Echo "<select class='form-control form party select2' name='party_$ctr' style='width:100%'>";
                                                        Echo "<option value='$getparty[accode]'>$getparty[name]</option>";
                                                        $gexp = mysqli_query($conn, "SELECT * FROM `accmast` where `grpcode` > '002' order by name")or die(mysqli_error($conn));
                                                        while ($showexp = mysqli_fetch_array($gexp)) {
                                                            Echo "<option value='$showexp[accode]'>$showexp[name]</option>";
                                                        }
                                                        Echo "</select>";
                                                        Echo "</td>";
                                                        Echo "<td width='20%'><input type='number' class='form-control form text-right amount' name='amount_$ctr' step='any' value='$tran[amount]'></td>";

                                                        Echo "<input type='hidden' class='form-control form grpcode' name='grpcode_$ctr' value='$getparty[grpcode]'>";
                                                        Echo "<td width='30%'><input type='text' class='form-control form remark' name='remark_$ctr' value='$tran[remark]'></td>";
                                                        Echo "</tr >";
                                                        $ctr++;
                                                    }
                                                } else {
                                                    $pctr = 0;
                                                    ?>
                                                    <tr>
                                                        <td width='4%'><i class="fa fa-close remove"></i></td>
                                                        <td width='8%'><input type="text" class="form-control form text-right srno" name="srno_<?= $pctr ?>" value="1"></td>
                                                        <td  width='40%'>
                                                            <select class="form-control form party select2" name="party_<?= $pctr ?>" style='width:100%'>
                                                                <option value=""></option>
                                                                <?php
                                                                $getparty = mysqli_query($conn, "SELECT * FROM `accmast` where `grpcode` > '002' order by name")or die(mysqli_error($conn));
                                                                while ($showparty = mysqli_fetch_array($getparty)) {
                                                                    Echo "<option value='$showparty[accode]'>$showparty[name]</option>";
                                                                }
                                                                ?>
                                                            </select>
                                                        </td>
                                                        <td  width='20%'><input type='number' step="any" class='form-control form text-right amount' name='amount_<?= $pctr ?>' value='0'></td>
                                                <input type="hidden"     class="form-control form grpcode"   name="grpcode_<?= $pctr ?>" value="" >                                        
                                                <td  width='30%'><input type='text' class='form-control form remark' name='remark_<?= $pctr ?>' value=''></td>
                                                </tr>  
                                                <?
                                                $pctr++;
                                            }
                                            ?>


                                            </tbody>
                                            <tr>
                                                <td></td>
                                                <td></td>
                                                <td>
                                                    <label class="text-right">TOTAL</label>
                                                </td>
                                                <td width='20%'><b><input type='number' step="any" readonly class='form-control form text-right totamt' name='totamt' value='<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>' ></b></td>
                                                <td></td>
                                            </tr>
                                        </table>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-10 pad">
                                            <button type="button" class="btn btn-success btn-square form"  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-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                            <label class="label-control marg">Remark</label>
                                        </div>
                                        <div class="col-lg-5 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>
                                   
                                    <div class="row">
                                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                            <label class="label-control marg"></label>
                                        </div>
                                        <div class="col-lg-5 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>
                                <div class="panel-footer">
                                    <?php include("includes/buttons.php"); ?>
                                </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">Voucher No.</option>
                                                <!--<option value="2">Bank Name</option>-->
                                                <option value="4">Party</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">Voucher No. </option>
                                                <!--<option value="2">Bank Name</option>-->
                                                <option value="3">Party</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 class="mySearchTr">
                                                    <th width="15%">Voucher No.</th>
                                                    <th width="25%">Bank Name</th>
                                                    <!--<th width="25%">Account</th>-->
                                                    <th width="14%">Cheque </th>
                                                    <th width="14%">Amount</th>
                                                    <th width="14%">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">
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer"></div>
                            </div>
                        </div>
                    </div>
                    <?php include("includes/linksfoot.php"); ?>
                    </body>
                    </html>
