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

$formname    = 'Bank Receipt';
$maindb      = 'bankrecp';
$accdb      = 'actran';
$file        = 'bankrecp.php';
$grid        = '';
$source      = 'BR';
$keyfld      = "vouchno";
$trandbexist = 0;
$focusfld    = "name";
$printfile   = 'bankrprin.php';

$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;
    $vouchdt     = $_POST["vouchdt"];
    $bankname    = $_POST["bankname"];
    $recpno      = $_POST["recpno"];
    $chqno       = $_POST["chqno"];
    $chqdate     = $_POST["chqdate"];
    $drawbank    = $_POST["drawbank"];
    $party       = $_POST["party"];
    $amount      = $_POST["amount"];
    $narration   = $_POST["narration"];
 
    
    // browse($qvouch);
    $mvouch = $qvouch;

    if ($bankname == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }

    if ($a == 1) {
        ######################## ACCOUNTS PART ######################################

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

        if ($amount > 0) {
            $minus_amt = ($amount * -1);
            $getbankinit = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$bankname'");
            $bankinit = mysqli_fetch_array($getbankinit);
            $init = $bankinit['init'];
            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, init, grpcode, amount, chqno, chqdate)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$bankname', '$init', '001', '$minus_amt', '$chqno', '$chqdate')");
   
            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, init, grpcode, amount, chqno, chqdate)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party', '$init', '010', '$amount', '$chqno', '$chqdate')");
        }
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( vouchno ,`vouchdt`, `bankname`, `recpno`, `chqno`,`chqdate`,
                                                `drawbank`,`party`,`amount`,`narration`)
                                       Values ('$qvouch', '$vouchdt', '$bankname', '$recpno','$chqno','$chqdate',
                                    '$drawbank', '$party','$amount','$narration')") or die(mysqli_error($conn));
            insmess();
//            $mvouch = $qvouch;
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET  vouchno='$qvouch',  vouchdt='$vouchdt', bankname='$bankname',
                    recpno='$recpno' ,chqno='$chqno',chqdate='$chqdate',drawbank='$drawbank',`party`='$party',
                     amount='$amount',narration='$narration'  where vouchno = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

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]'");
    $party = mysqli_fetch_array($getparty);    
}

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");  
}
include("includes/files.php");
?>

<body>
    <form method="POST" class="form-horizontal" role="form" action='' name="bankpayment">
        <div class="row" style="">
            <div class="col-lg-6" style=''>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5 style="font-family: cipher-regular,helvetica; "><?= $formname ?></h5>
                        <!-- .toolbar -->
                        <div class="toolbar">
                            <ul class="nav">
                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>
                            </ul>
                        </div>
                        <!-- /.toolbar -->
                    </header>
                    <div id="div-1" class="accordion-body collapse in body" style="font-family: cipher-regular,helvetica;">
                        <div class="form-horizontal">
                            
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;"> Voucher</label>
                                <div class="col-lg-3"> 
                                    <input type="text" name="vouchno"  placeholder="vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch;   ?>" class="form-control form" style="width:130px; height: 30px;" readonly>
                                </div>
                                <label class="control-label col-lg-2" style="">Date</label>
                                <div class="col-lg-3" style="padding-left:3px;"> 
                                    <input type="date" name="vouchdt" style="padding-left: 3px;width:148px;padding-right: 0px;"  placeholder="date" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('d-m-Y');   ?>" class="form-control form"  required>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Bank Name</label>
                                <div class="col-lg-8"> 
                                    <select id="bankname" name="bankname" class="select2 form-control form" style="height: 33px;">
                                        <option value="<?php echo ($addflag != 1) ? $banknm['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $banknm['name'] : " "; ?></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT accode, `name` 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> 
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;"> Receipt No. </label>
                                <div class="col-lg-3"> 
                                    <input type="text" name="recpno" placeholder="recpno" value="<?php echo ($addflag != 1) ? $get['recpno'] : $qvouch; ?>" style="height: 30px;" class="form-control form" readonly>                                    
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;"> Cheque No. </label>
                                <div class="col-lg-3"> 
                                    <input type="text" name="chqno"  placeholder="Cheque No." value="<?php echo ($addflag != 1) ? $get['chqno'] : '';   ?>" class="form-control form" style="width:130px; height: 30px;" required>
                                </div>
                                <label class="control-label col-lg-1" style="width:100px;padding-right: 0px;padding-left: 0px;">Cheque Date</label>
                                <div class="col-lg-1"> 
                                    <input type="date" style="padding-left: 3px;width:145px;padding-right: 0px;" name="chqdate"  placeholder="" value="<?php echo ($addflag != 1) ? $get['chqdate'] : date('d-m-Y');   ?>" class="form-control form">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Drawee Bank </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="drawbank"  placeholder="Drawee Bank" value="<?php echo ($addflag != 1) ? $get['drawbank'] : '';   ?>" class="form-control form" style="height: 30px;">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Party Name </label>
                                <div class="col-lg-8"> 
                                    <select id="party" name="party" class="select2 form-control form" style="height: 33px;">
                                        <option value="<?php echo ($addflag != 1) ? $party['accode'] : " "; ?>"><?php echo ($addflag != 1) ? $party['name'] : " "; ?></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT * FROM `accmast` where grpcode = '010' order by `name`") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value=$trow[accode]>$trow[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Amount </label>
                                <div class="col-lg-3"> 
                                    <input type="text" name="amount" placeholder="Amount" value="<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>" style="text-align: right; height: 30px;" class="form-control form"  required>
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Narration </label>
                                <div class="col-lg-8"> 
                                    <textarea style="padding-left: 3px;" class="narration form-control form" rows="4" cols="50" maxlength="100" name="narration"  placeholder="Narration">  <?php echo ($addflag != 1) ? $get['narration'] : ''; ?></textarea>
                                </div> 
                            </div>                             
                        </div>   
                    </div> 
                </div> 
            </div> 
        </div>
        <div id="formfoot" style="width:50%;">                        
            <h1><?php include("includes/buttons.php"); ?></h1>
        </div>

        <!--SEARCH PART-->

        <div class="modal fade in slacker-modal" id="searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="false">
            <div class="modal-dialog modal-slacker">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myLargeModalLabel"></h4>
                    </div>
                    <div class="modal-body" style="height: 100%;white-space: nowrap;">
                        <div class="row">

                            <div class="col-md-12">

                                <div class="col-lg-12">

                                    <div class="box">
                                        <div id="collapse4" class="body">
                                            <table id="example1" class="table table-bordered table-condensed table-hover table-striped">
                                                <thead>
                                                    <tr>
                                                        <th align='left'>Voucher Date</th>
                                                        <th align='left'>Voucher No.</th>
                                                        <th align='left'>Party Name</th>
                                                        <th align='left'>Amount</th>
                                                        <th align='left'>Cheque No.</th>
                                                        <th align='left'>Cheque Date</th>
                                                        
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld!=''") or die(mysqli_error($conn));
                                                    while ($row = mysqli_fetch_array($srchresult)) {
                                                        $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$row[party]'")or die(mysqli_error($conn));
                                                        $party    = mysqli_fetch_array($getparty);
//                                                      
                                                        echo "<tr>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[vouchdt]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[vouchno]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$party[name]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[amount]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[chqno]</td>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[chqdate]</td>";
                                                        
                                                        echo "</tr>";
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                            <script>
                                                $(function () {
                                                    metisTable();
                                                    metisSortable();
                                                });
                                            </script>

                                        </div>

                                    </div>

                                </div>

                            </div><!-- /.row -->

                        </div>
                    </div>  
                    <div class="modal-footer" style="bottom: 0px;position: absolute;padding:0px;">
                        <!--"This is where i want to load my salemain.php"-->
                    </div>
                </div>
            </div>
        </div>
    </form>

    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>
</html>