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

$formname = 'Cash Payment';
$maindb = 'cashpay';
$accdb = 'actran';

$file = 'cashpay.php';
$grid = '';
$source = 'CP';
$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'cashpay_fr.php';
$type = "P";
//$printfile   = 'cashp_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"];
    $projcode = $_POST["projcode"];
    $payfor = $_POST["payfor"];

    $bankname = $_POST["bankname"];
    $recpno = $_POST["recpno"];
    $party = $_POST["party"];
    $customer = $_POST["customer"];
    $amount = $_POST["amount"];
    $ratelakhs = $_POST["ratelakhs"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $amtinwords = $_POST["amtinwords"];
    $feamt = $_POST["feamt"];
    $curr = $_POST["curr"];
    $roe = $_POST["roe"];

    // browse($qvouch);
    $mvouch = $qvouch;
    $counter = $_POST["counter"];
    if ($bankname == "") {
        $a = 0;
        browse("Name Cannot Be Empty");
        header("refresh: ; $file?vouchno=$_GET[vouchno]");
    }


    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';
            }
            $mvouch = $qvouch;
        }

//        $chknarr = mysqli_query($conn, "SELECT `narr1` FROM `narrat` WHERE `narr1` = '$remark1'") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chknarr) > 0) {
//            
//        } else {
//            $gennarr = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `narrat` LIMIT 0,1 ") or die(mysqli_error($conn));
//            $fgennarr = mysqli_fetch_assoc($gennarr);
//            $remcode = $fgennarr["maxcode"] + 1;
//
//            mysqli_query($conn, "INSERT INTO `narrat`( code ,`narr1`, `narr2`)
//                                       Values ('$remcode', '$remark1','$remark2')") or die(mysqli_error($conn));
//        }


        $in = mysqli_query($conn, "SELECT * FROM accmast WHERE accode='$bankname'") or die(mysqli_error($conn));
        $getinit = mysqli_fetch_array($in);
        $bankinit = $getinit["bankinit"];

        $gr = mysqli_query($conn, "SELECT * FROM accmast WHERE accode='$party'") or die(mysqli_error($conn));
        $grp = mysqli_fetch_array($gr);
        $pgrp = $grp["grpcode"];


        ######################## 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'");
            $fetinit = mysqli_fetch_array($getbankinit);
            $binit = $fetinit['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'];


            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, recpno, trnno, bankinit, accode,  grpcode, amount,curr,feamt,roe)
                    VALUES ('$vouchdt','$mvouch', '$source', '$recpno',  '001', '$binit', '$bankname', '002', '$amount','$curr','$feamt','$roe')");

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, recpno, trnno, bankinit, accode, grpcode, amount,curr,feamt,roe)
                     VALUES ('$vouchdt','$mvouch', '$source', '$recpno', '002', '$binit', '$party',  '$pgrp', '$minus_amt','$curr','$feamt','$roe')");
        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `recpno`, `party`,`amount`,`remark1`,
                                               `remark2`,`type`,projcode,`payfor`,customer,`amtwords`,`feamt`,`roe`,`curr`,ratelakhs)
                                       Values ('$qvouch', '$vouchdt', '$bankname', '$binit', '$recpno','$party','$amount','$remark1',
                                               '$remark2','S','$projcode','$payfor','$customer','$amtinwords','$feamt','$roe','$curr','$ratelakhs')") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET  vouchdt='$vouchdt', bankname='$bankname', bankinit='$binit', recpno='$recpno' ,`party`='$party', `customer`='$customer',ratelakhs='$ratelakhs',
                        projcode='$projcode',`payfor`='$payfor',amount='$amount', `remark1`='$remark1', `remark2`='$remark2',`amtwords`='$amtinwords',`feamt`='$feamt',`roe`='$roe',`curr`='$curr',
                       `type`='S'  where vouchno = '$mvouch' ") or die(mysqli_error($conn));
        }
        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            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 accode,name  FROM `accmast` WHERE `accode` = '$get[bankname]'");
    $banknm = mysqli_fetch_array($getbank);

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

    $getcust = mysqli_query($conn, "SELECT accode,name  FROM `accmast` WHERE `accode` = '$get[customer]'");
    $gtcs = mysqli_fetch_array($getcust);

//    $getproj = mysqli_query($conn, "SELECT code,name FROM `project` WHERE `code`='$get[projcode]'") or die(mysqli_error($conn));
//    $fetproj = mysqli_fetch_array($getproj);
}

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


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


    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delsett = " source = '$source' AND bnkvouch = '$_GET[vouchno]'";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script type="text/javascript">
    $(document).ready(function () {

//        $("body").on("change keyup", ".feamt, .roe", calAmount);
//        function calAmount() {
//            var roe = $(".roe").val(),
//                    feamt = $(".feamt").val(),
//                    amount = roe * feamt;
//            $(".amount").val(amount);
//        }

    });
</script>
<script src="MyScript/getbank.js?c=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/cashpay.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/cashpay_search.js?b=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<script src="MyScript/shortkeys.js?s=<?= mt_rand() ?>" 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">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3  pad text-right">
                                <label class="label-control">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-2 pad"> 
                                <input type="text" name="vouchno" id="vouchno"  placeholder="vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>" class="form-control form"  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 form recptype" name="recptype" value="C" readonly>

                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control">Date</label>
                            </div>

                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 pad"> 
                                <input type="date" name="vouchdt"   placeholder="date" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>" class="form-control form vouchdt focusfield" style="height: 30px;" required autofocus="autofocus">
                            </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">Cash Name</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8   pad"> 
                                <select id="bankname" name="bankname" class="form-control form select2" >
                                    <option value="<?php echo ($addflag != 1) ? $banknm['accode'] : "3"; ?>"><?php echo ($addflag != 1) ? $banknm['name'] : "CASH ON HAND"; ?></option>
                                    <?php
                                    $mown1 = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` where `grpcode` = '002'") 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"> Receipt No. </label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"> 
                                <input type="text" name="recpno" placeholder="recpno" value="<?php echo ($addflag != 1) ? $get['recpno'] : $qvouch; ?>" class="form-control form" 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">Account</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad"> 
                                <select id="party" name="party" class="form-control form focusfield  select2" autofocus="autofocus">
                                    <option value="<?php echo ($addflag != 1) ? $gtpt['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $gtpt['name'] : ""; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT * FROM `accmast` where grpcode != '001' and grpcode != '002' 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="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">FE Amount</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="feamt" value="<?php echo ($addflag != 1) ? $get['feamt'] : '0'; ?>" class="form-control form feamt  numeric text-right">
                            </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">Currency</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form curr" name="curr" id="curr">
                                    <option value="<?php echo ($addflag != 1) ? $get['curr'] : "" ?>"><?php echo ($addflag != 1) ? $get['curr'] : "" ?></option>
                                    <?php
                                    $getpj = mysqli_query($conn, "Select code,name from `currency` order by name") or die(mysqli_error($conn));
                                    while ($fetpj = mysqli_fetch_array($getpj)) {
                                        echo "<option value='$fetpj[name]'>$fetpj[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">ROE</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="roe" value="<?php echo ($addflag != 1) ? $get['roe'] : '1'; ?>" class="form-control form roe 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="control-label">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" placeholder="Amount" text-right value="<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>" class="form-control amount form text-right numeric "  required>
                            </div> 
                        </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">Remarks</label>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                            <input type="text" name="remark1" value="<?php echo ($addflag != 1) ? $get['remark1'] : ''; ?>" class="form-control form remark1" >
                            <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">Pay For</label>
                                                </div>
                                                <div class="col-lg-2 col-md-2 col-sm-4 col-xs-4 pad">
                                                    <select class="form-control form payfor" style="color: red" name="payfor" id="payfor" required>
                                                        <option
                    <?php
                    if ($get['payfor'] == 'P') {
                        echo 'selected="selected"';
                    }
                    ?> value="P">Project</option>
                                                        <option <?php
                    if ($get['payfor'] == 'U') {
                        echo 'selected="selected"';
                    }
                    ?> value="U">Upper</option>
                                                    </select>
                                                </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">Date</option>
                            <option value="3">Cash Name</option>
                            <option value="4">Account</option>
                            <option value="5">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">Date</option>
                            <option value="3">Cash Name</option>
                            <option value="4">Account</option>
                            <option value="5">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:#1abc9c">
                                <th width="15%">Voucher No.</th>
                                <th width="10%">Date</th>
                                <th width="25%">Cash Name</th>
                                <th width="25%">Account</th>
                                <th class="text-right" width="20%">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">
                    </div>
                </div>
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>
<div class="modal fade" id="NarrModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myLargeModalLabel">Search Narration</h4>
            </div>
            <div class="modal-body">
                <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                    <div class="row">
                        <div class="col-lg-3">
                            <select class="form-control remoptn" name="serachfor">
                                <option value="1">Remark 1</option>
                                <option value="2">Remark 2</option>
                            </select>
                        </div>
                        <div class="col-lg-3">
                            <input type="text" id="remsrch" class="form-control " placeholder="Search Here">
                        </div>
                    </div>
                    <table class='table table-condensed tabel-bordered'>
                        <thead>
                            <tr style="background-color: #3a8084;">
                                <th>Remark 1</th>
                                <th>Remark 2</th>
                            </tr>
                        </thead>
                        <tbody class="modalbody">

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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