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

$formname = 'Bank Payment from Customer to Karigar';
$maindb = 'custtokari';
$accdb = 'actran';
$billdb = '';

$file = 'custtokari.php';
$grid = '';
$source = 'BRBP';
$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'custtokari_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);
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"];
    $rtgsno = $_POST["rtgsno"];
    $chqno = $_POST["chqno"];
    $chqdate = $_POST["chqdate"];
    $drawbank = $_POST["drawbank"];
    $party = $_POST["party"];
    $amount = $_POST["amount"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $karigar = $_POST["karigar"];
    $counter = $_POST["counter"];
    $acnum = $_POST["acnum"];



    // browse($qvouch);
    $mvouch = $qvouch;


    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE, `$accdb` 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';
            }
        }

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

        $qrygrp = mysqli_query($conn, "select `grpcode` from accmast where `accode` = '$party'") or die(mysqli_error($conn));
        $fetgrp = mysqli_fetch_array($qrygrp);
        $pgrp = $fetgrp['grpcode'];



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

        if ($amount > 0) {
            $minus_amt = ($amount * -1);

            // customer part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode,  grpcode, amount, chqno, chqdate)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$karigar',  '010', '$minus_amt','$chqno', '$chqdate')");

            // karigar part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, chqno, chqdate)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party',  '009', '$amount','$chqno', '$chqdate')");
        }
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno ,`vouchdt`, `rtgsno`, `drawbank`,`chqno`,`chqdate`,acnum,
                                               `karigar`, `party`,`amount`,`remark1`,`remark2`,type)
                                       Values ('$qvouch', '$vouchdt', '$rtgsno', '$drawbank', '$chqno','$chqdate','$acnum',
                                               '$karigar', '$party','$amount','$remark1','$remark2','S')") 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   vouchdt='$vouchdt', `karigar`='$karigar',acnum='$acnum',
                                                rtgsno='$rtgsno' , drawbank='$drawbank' ,chqno='$chqno',chqdate='$chqdate',`party`='$party',
                                                amount='$amount',`remark1`='$remark1',`remark2`='$remark2',`type`='S' 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);

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

    $getkari = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[karigar]'");
    $fetkari = mysqli_fetch_array($getkari);
}

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

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$accdb` WRITE") or die(mysqli_error($conn));
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");


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

include("includes/linkshead.php");
?>
<script src="MyScript/custtokari.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 class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </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">
                                        <input type="hidden" class="logdate" name="logdate" value="<?php Echo ($addflag != 1) ? $get["logdate"] : ""; ?>" readonly style="">

                    <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">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>
                            </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" 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="bankr_voucher.php?vouchno=<?= $qvouch; ?>" target="_blank">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">Customer</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" required>
                                    <option value="<?php echo ($addflag != 1) ? $gtpt[accode] : " "; ?>"><?php echo ($addflag != 1) ? $gtpt[name] : " "; ?></option>
                                    <?php
                                    $mown1 = mysqli_query($conn, "SELECT accode,name FROM `accmast` where grpcode = '010' order by `name`") or die(mysqli_error($conn));
                                    while ($trow1 = mysqli_fetch_array($mown1)) {
                                        echo "<option value=$trow1[accode]>$trow1[name]</option>";
                                    }
//                                    if($gtpt[accode] != ''){
//                                        alert('Customer Cannot Be Empty');
//                                    }
                                    ?>
                                </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">Karigar</label>
                            </div>
                            <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7 pad">
                                <select id="karigar" name="karigar" class="select2 form-control karigar form" required>
                                    <option value="<?php echo ($addflag != 1) ? $fetkari[accode] : ""; ?>"><?php echo ($addflag != 1) ? $fetkari[name] : ""; ?></option>
                                    <?php
                                    $mown2 = mysqli_query($conn, "SELECT accode,name FROM `accmast` where grpcode = '009' and active = '1' order by `name`") or die(mysqli_error($conn));
                                    while ($trow2 = mysqli_fetch_array($mown2)) {
                                        echo "<option value=$trow2[accode]>$trow2[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">Bank Name</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 drawbank" 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">A/c. Number</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="acnum" value="<?php echo ($addflag != 1) ? $get['acnum'] : ""; ?>" class="form-control form acnum">
                            </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">RTGS No.</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="rtgsno" value="<?php echo ($addflag != 1) ? $get['rtgsno'] : ""; ?>" class="form-control form rtgsno" >                                    
                            </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">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>
                        <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) ? $billstr : ''; ?>" class="form-control form remark2" >
                            </div>
                        </div> 
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
    </form>

    <div class="modal fade bs-example-modal-lg" 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">
                    <div class=" table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #78909C;">
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Customer</th>
                                    <th>Karigar</th>
                                    <th>Amount</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` ")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        $getbank1 = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$row[karigar]'");
                                        $gbank = mysqli_fetch_array($getbank1);

                                        $gparty1 = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$row[party]' ");
                                        $gtpt1 = mysqli_fetch_array($gparty1);

                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[vouchno]</td>";
                                        echo "<td align='left'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$gbank[name]</td>";
                                        echo "<td align='left'>$gtpt1[name]</td>";
                                        echo "<td align='right'>$row[amount]</td>";
                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
