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

$formname = 'Receipt Entry';
$maindb = 'recpent';
$accdb = 'actran';

error_reporting(E_ERROR);
ini_set('display_errors', 1);

$file = 'recp_ent.php';
$grid = '';
$source = 'RC';
$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'recp_ent_fr.php';

$type = "S";

$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");

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

$username = $_SESSION["username_" . $dbname];
$usertype = $_SESSION["usertype"];

// if ($usertype != 1) {
// //    $keycond1 = " and `username`='$username'";
// } else {
$keycond1 = "";
// }

$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"];
    $party = $_POST["party"];
    $amount = $_POST["amount"];
    $bankname = $_POST["bankname"];
    $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: ; $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';
            }
        }

        #########################START BILL SETTLEMENT###############################################
        $getbankinit = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['accode'];
        $bgrpcode = $bankinit['grpcode'];

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

        #########################END BILL SETTLEMENT###############################################

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

        if ($amount > 0) {
            $minus_amt = ($amount * -1);
            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, bankinit, grpcode, amount)
                                              VALUES ('$vouchdt','$mvouch', '$source', '001', '$bankname', '$binit','$bgrpcode', '$minus_amt')");

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, bankinit, grpcode, amount)
                                              VALUES ('$vouchdt','$mvouch', '$source', '002', '$party', '$binit', '$pgrp', '$amount')");
        }
        
        
        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`( vouchno ,`vouchdt`, `bankname`, bankinit, `party`,`amount`,`remark1`,
                                                        `remark2`,`username`,`adduser`,`adddate`,`addtime`)
                                       Values ('$qvouch', '$vouchdt', '$bankname', '$binit','$party','$amount','$remark1',
                                                        '$remark2','$username','$adduser','$adddate','$addtime')") 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', `bankname`='$bankname', `bankinit`='$binit', `party`='$party', `amount`='$amount',
                                                       `remark1`='$remark1', `remark2`='$remark2', `edituser`='$adduser', `editdate`='$adddate', `edittime`='$addtime' 
                                                       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' $keycond1") 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]'");
    $fetparty = mysqli_fetch_array($getparty);

}

$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/recp_ent_search.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/shortkeys.js" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST'>
        <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>
                        <?php // include('includes/usermention.php'); ?>
                    </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-1 col-md-1 col-sm-1 col-xs-1 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-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 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>
                        <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">From Customer</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <select id="party" name="party" class="select2 form-control party form pad" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['party'] : " "; ?>"><?php echo ($addflag != 1) ? $fetparty['name'] : " "; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT * FROM `accmast` where accode!='' AND grpcode='010' order by `name`") or die(mysqli_error($conn));
                                    while ($fparty = mysqli_fetch_array($gparty)) {
                                        echo "<option value=$fparty[accode]>$fparty[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">Payment Type</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 pad">
                                <select class="form-control select2 paytype form" name="paytype" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['paytype'] : " "; ?>"><?php echo ($addflag != 1) ? $paymenttype : " "; ?></option>
                                    <option value=""></option>
                                    <option value="B">Bank</option>
                                    <option value="C">Cash</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" style="color: #c1121f;">To Account</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <select class="form-control form select2 bankname" name="bankname" style="width:100%" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['bankname'] : " "; ?>"><?php echo ($addflag != 1) ? $banknm['name'] : " "; ?></option>
                                    <?php
                                    $gagent = mysqli_query($conn, "SELECT * FROM `accmast` where accode!='' AND grpcode='030' order by `name`") or die(mysqli_error($conn));
                                    while ($fagent = mysqli_fetch_array($gagent)) {
                                        echo "<option value=$fagent[accode]>$fagent[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-2 col-md-2 col-sm-2 col-xs-2 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">Slip No.</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="slipno" value="<?php echo ($addflag != 1) ? $get['slipno'] : ''; ?>" 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">Remark</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 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-4 col-md-4 col-sm-4 col-xs-4 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>
        </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</option>
                                <option value="2">Date</option>
                                <option value="3">Customer</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</option>
                                <option value="2">Date</option>
                                <option value="3">Customer</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="asc">Asc</option>
                                <option value="desc">Desc</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:#b61f2c;">
                                    <th width="5%">Voucher</th>
                                    <th width="5%">Date</th>
                                    <th width="16%">From Customer</th>
                                    <th width="16%">To Account</th>
                                    <th width="12%" class="text-right">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>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>