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

$formname = 'JV Entry / Settlement';
$maindb = 'jventry';
$accdb = 'actran';
$billdb = 'billsett';

$file = 'jventry.php';
$grid = '';
$source = 'JVS';
$type = "S";

$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'cashrecp_fr.php';
$fseries = $_SESSION["series"];
$keycond = "";

$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;
    $username = $_SESSION["username_" . $dbname];

    $vouchdt = $_POST["vouchdt"];
    $credac = trim($_POST["credac"]);
    $debac = trim($_POST["debac"]);
    $recpno = $_POST["recpno"];
    $party = trim($_POST["party"]);
    $amount = $_POST["amount"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $counter = $_POST["counter"];
    $counter2 = $_POST["counter2"];
    $billflag = $_POST["billflag"];

    $mvouch = $qvouch;

    if ($credac == "") {
        $a = 0;
        browse("Credit A/c. Cannot Be Empty");
        header("refresh: ; $file?vouchno=$_GET[vouchno]");
    }

    if ($debac == "") {
        $a = 0;
        browse("Debit A/c. Cannot Be Empty");
        header("refresh: ; $file?vouchno=$_GET[vouchno]");
    }

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE, `$accdb` WRITE, $billdb WRITE ") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != '' $keycond  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';
            }
        }


        if ($billflag == 1) {
            mysqli_query($conn, "DELETE FROM `$billdb` WHERE `bnkvouch` = '$mvouch' AND `source` = '$source' AND `bankinit` = '$binit' $keycond  ") or die(mysqli_error($conn));
            //debit 
            for ($i = 0; $i <= $counter; $i++) {
                $dmyearno = $_POST["dyearno_" . $i];
                $dmvouchno = $_POST["dvouchno_" . $i];
                $dmbillno = $_POST["dbillno_" . $i];
                $dmsetoff = $_POST["dsetoff_" . $i];
                $dmbillsrc = $_POST["dbillsrc_" . $i];
                $dbilldate = $_POST["dbilldate_" . $i];
                $dmdays = $_POST["ddays_" . $i];

                if (!empty($dmsetoff)) {
                    mysqli_query($conn, "INSERT INTO `billsett`(`yearno`, `type`, `vouchno`, `billno`, `setoff`, `party`, `source`, `bnkvouch`, `bankinit`, nodays, `billsrc`, bnkdate, billdate)
                                VALUES('$dmyearno', 'P', '$dmvouchno', '$dmbillno', '$dmsetoff', '$debac', '$source', '$mvouch', '$binit', '$dmdays', '$dmbillsrc', '$vouchdt', '$dbilldate')") or die(mysqli_error($conn));
                }
            }
            for ($j = 0; $j <= $counter2; $j++) {
                $cmyearno = $_POST["cyearno_" . $j];
                $cmvouchno = $_POST["cvouchno_" . $j];
                $cmbillno = $_POST["cbillno_" . $j];
                $cmsetoff = $_POST["csetoff_" . $j];
                $cmbillsrc = $_POST["cbillsrc_" . $j];
                $cmdays = $_POST["cdays_" . $j];

                if (!empty($cmsetoff)) {
                    mysqli_query($conn, "INSERT INTO `billsett`(`yearno`, `type`, `vouchno`, `billno`, `setoff`, `party`, `source`, `bnkvouch`, `bankinit`, nodays, `billsrc`)
                                VALUES('$cmyearno', 'S', '$cmvouchno', '$cmbillno', '$cmsetoff', '$credac', '$source', '$mvouch', '$binit', '$cmdays', '$cmbillsrc')") or cie(mysqli_error($conn));
                }
            }
        }


        ######################## ACCOUNTS PART ######################################

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

        if ($amount > 0) {
            $qrygrp = mysqli_query($conn, "select `grpcode` from accmast where `accode` = '$debac'") or die(mysqli_error($conn));
            $fetgrp = mysqli_fetch_array($qrygrp);
            $dgrp = $fetgrp['grpcode'];
            $qrycgrp = mysqli_query($conn, "select `grpcode` from accmast where `accode` = '$credac'") or die(mysqli_error($conn));
            $fetcgrp = mysqli_fetch_array($qrycgrp);
            $cgrp = $fetcgrp['grpcode'];

            $minus_amt = ($amount * -1);
            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, vouchno, source, trnno, bankinit, accode, grpcode, amount, drawbank)
                                        VALUES ('$vouchdt', '$mvouch', '$source', '001', '$binit', '$debac','$dgrp', '$minus_amt', '$fetgrp[name]')") or die(mysqli_error($conn));
            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(`date`, vouchno, source, trnno, bankinit, accode, grpcode, amount, drawbank)
                                            VALUES ('$vouchdt', '$mvouch', '$source', '002', '$binit', '$credac', '$cgrp', '$amount', '$bankinit[name]')") or die(mysqli_error($conn));
        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( vouchno ,`vouchdt`, `debac`, `bankinit`, `credac`, `amount`, `remark1`, `remark2`)
                                       VALUES ('$qvouch', '$vouchdt', '$debac', '$binit', '$credac', '$amount', '$remark1','$remark2')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', debac='$debac', bankinit='$binit', 
                                                                     `credac`='$credac', `amount`='$amount', `remark1`='$remark1', 
                                                                     `remark2`='$remark2' WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
        }

        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last&series=$fseries");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        }
    }
}

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[debac]' ") or die(mysqli_error($conn));
    $banknm = mysqli_fetch_array($getbank);

    $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[credac]' ") or die(mysqli_error($conn));
    $gtpt = mysqli_fetch_array($getparty);
}

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

    $delcond2 = " vouchno = '$_GET[vouchno]'";

    delnew($zdb, "$maindb", "$delcond2", "$file?mode=last");

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]'  ";
    $delcondb = " source = '$source' and bnkvouch = '$_GET[vouchno]'  ";

    delnew($zdb, "$billdb", "$delcondb", "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");

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

include("includes/linkshead.php");
?>
<script src="MyScript/jventry.js" type="text/javascript"></script>
<script src="MyScript/jventry_search.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>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 ">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">&nbsp;<?php echo $formname; ?></h3>
                    </div>

                    <input type="hidden" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                    <input type="hidden" 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-4 col-xs-4  pad text-right">
                                <label class="label-control amrg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" name="vouchno" id="vouchno" placeholder="vouchno"
                                    value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>"
                                    class="form-control form vouchno" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control form billflag" name="billflag" value="0">
                                <input type="hidden" class="form-control form source" name="source"
                                    value="<?= $source ?>">

                                <input type="date" name="vouchdt" id="vouchdt"
                                    value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>"
                                    class="form-control form vouchdt focusfield" required autofocus>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4 text-right pad">
                                <label class="label-control marg">Debit A/c.</label>
                            </div>
                            <div class="col-lg-6 col-md-8 col-sm-8 col-xs-8  pad">
                                <select id="debac" name="debac" class="form-control form select2 debac"
                                    style="width: 100%;">
                                    <option value="<?php echo ($addflag != 1) ? $banknm['accode'] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $banknm['name'] : ""; ?></option>
                                    <?php
                                    $mown1 = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` where `grpcode` > '002' order by `name` ") 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-4 col-xs-4  text-right pad">
                                <label class="label-control marg">Credit A/c.</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select id="credac" name="credac" class="form-control form select2 credac"
                                    style="width: 100%;">
                                    <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 > '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-4 col-xs-4 text-right pad">
                                <label class="control-label marg">Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" name="amount" id="amount" placeholder="Amount" text-right
                                    value="<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>"
                                    class="form-control form text-right numeric " 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-6 col-md-7 col-sm-8 col-xs-8 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">
                            </div>
                            <div class="col-lg-6 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 class="row" tabindex="" id="settmodaldeb">
                            <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">Debit Party</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class=" table-responsive">
                                            <div class="toolbar">
                                                <div class="row">
                                                    <div class="col-lg-2"><label class="label-control marg"
                                                            style="color:red">Total Remain</label></div>
                                                    <div class="col-lg-2"><input type="text" id="remaindeb"
                                                            name="remaindeb" class="form-control" readonly style="">
                                                    </div>
                                                    <div class="col-lg-2"><label class="label-control marg"
                                                            style="color:blue">Total Setoff</label></div>
                                                    <div class="col-lg-2"><input type="text" id="totsetoffdeb"
                                                            name="totsetoffdeb" class="form-control" readonly style="">
                                                    </div>
                                                    <!--<div class="col-lg-2"><input id="submit" type='submit' name='submit' value='<?php // echo ($addflag == 0) ? "Update" : "Save";                                                                                    ?>' class="btn btn-primary"></div>-->
                                                    <?php
                                                    //                                    if (!isset($_GET["method"])) {
                                                    ?>
                                                    <!--<div class="col-lg-2"><input type='button' name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<?php echo $grid; ?>'"></div>-->
                                                    <?php // }               ?>
                                                </div>
                                            </div>
                                            </header>
                                            <div id="div-2" class="accordion-body collapse in body">
                                                <div style="max-height:350px;overflow: auto; border: 0px solid;">
                                                    <div id="datadeb"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>

                        <div class="row" tabindex="" id="settmodalcred">
                            <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">Credit Party</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class=" table-responsive">
                                            <div class="toolbar">
                                                <div class="row">
                                                    <div class="col-lg-2"><label class="label-control marg"
                                                            style="color:red">Total Remain</label></div>
                                                    <div class="col-lg-2"><input type="text" id="remaincred"
                                                            name="remaincred" class="form-control" readonly style="">
                                                    </div>
                                                    <div class="col-lg-2"><label class="label-control marg"
                                                            style="color:blue">Total Setoff</label></div>
                                                    <div class="col-lg-2"><input type="text" id="totsetoffcred"
                                                            name="totsetoffcred" class="form-control" readonly style="">
                                                    </div>
                                                    <!--<div class="col-lg-2"><input id="submit" type='submit' name='submit' value='<?php // echo ($addflag == 0) ? "Update" : "Save";                                                                                    ?>' class="btn btn-primary"></div>-->
                                                    <?php
                                                    //                                    if (!isset($_GET["method"])) {
                                                    ?>
                                                    <!--<div class="col-lg-2"><input type='button' name='exit' value='Exit' class="btn btn-danger" onClick="window.location.href = '<?php echo $grid; ?>'"></div>-->
                                                    <?php // }               ?>
                                                </div>
                                            </div>
                                            </header>
                                            <div id="div-2" class="accordion-body collapse in body">
                                                <div style="max-height:350px;overflow: auto; border: 0px solid;">
                                                    <div id="datacred"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </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">Debit A/c</option>
                                <option value="4">Credit A/c</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">Debit A/c</option>
                                <option value="4">Credit A/c</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 class="mySearchTr">
                                    <th width="10%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="25%">Debit A/c</th>
                                    <th width="25%">Credit A/c</th>
                                    <th class='text-right' width="14%">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>