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

$formname = 'Other Receipt';
$maindb = 'othrecp';
$accdb = 'actran';
$billdb = 'billsett';

$file = 'othrecp.php';
$grid = '';
$source = 'OTR';
$type = "S";

$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'othrecp_fr.php';
//$printfile = 'cashr_voucher.php';
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');

//for adding entry and edit timings
$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$usertype = $_SESSION["usertype_" . $dbname];
//end
$zroot = $_SESSION["zdbroot"];

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"];
    $bankname = trim($_POST["bankname"]);
    $recpno = $_POST["recpno"];
    $party = trim($_POST["party"]);
    $amount = $_POST["amount"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $logdate = $_POST["logdate"];
    $counter = $_POST["counter"];
    $upledger = $_POST["upledger"];
    $memno = $_POST["memno"];
    $memname = $_POST["memname"];

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

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

        $getbr = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `name`='$memname' and series='$fseries'") or die(mysqli_error($conn));
        if (mysqli_num_rows($getbr) > 0) {
            $bcode = mysqli_fetch_array($getbr);
            $brcode = $bcode['accode'];
        } else {
            $genbr = mysqli_query($conn, "SELECT MAX(accode) AS maxcode FROM `accmast` where accode != '' LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genbr) > 0) {
                $gbr = mysqli_fetch_assoc($genbr);
                $brcode = $gbr["maxcode"] + 1;
            } else {
                $brcode = '1';
            }
            mysqli_query($conn, "INSERT INTO `accmast`(`accode`, `name`, grpcode, init,series)
                           VALUES('$brcode', '$memname', '040', 'MEM','$fseries')") or die(mysqli_error($conn));
        }

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

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

//        browse($amount);
//        if ($upledger == 1) {
        if ($amount > 0) {
//            browse('suf');
            $minus_amt = ($amount * -1);

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


//                $qrygrp = mysqli_query($conn, "select `grpcode` from accmast where `accode` = '$memname'") or die(mysqli_error($conn));
//                $fetgrp = mysqli_fetch_array($qrygrp);
//                $pgrp = $fetgrp['grpcode'];
// cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(series, date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount)
                    VALUES ('$fseries', '$vouchdt','$mvouch', '$recpno', '$source', '001', '$binit', '$bankname','002', '$minus_amt')") or die(mysqli_error($conn));

// party part
            mysqli_query($conn, "INSERT INTO `$accdb`(series, date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount)
                    VALUES ('$fseries', '$vouchdt','$mvouch', '$recpno', '$source', '002', '$binit', '$brcode', '040', '$amount')") or die(mysqli_error($conn));
//            }
        }
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(series,  vouchno ,`vouchdt`, `bankname`, `bankinit`, `recpno`, `amount`, party,
                                                       `remark1`, `remark2`,logdate,`adduser`, `adddate`, `addtime`, `upledger`, `memno`, `memname`)
                                               VALUES ('$fseries', '$qvouch', '$vouchdt', '$bankname', '$binit', '$recpno', '$amount', '$brcode', 
                                                       '$remark1','$remark2','$logdate','$adduser','$adddate','$addtime', '$upledger', '$memno', '$memname')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");

            mysqli_query($conn, "UPDATE `$maindb` SET  vouchdt='$vouchdt', bankname='$bankname', bankinit='$binit', recpno='$recpno', party='$brcode',
                                                       logdate='$logdate', amount='$amount',`remark1`='$remark1', `remark2`='$remark2',
                                                       `edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate', `upledger`='$upledger',  `memno`='$memno', `memname`='$memname' 
                                                        where vouchno = '$mvouch' $keycond") 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' $keycond") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getbank = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[bankname]'");
    $fetbn = mysqli_fetch_array($getbank);
}

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

//    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");

    $delsett = " source = '$source' AND bnkvouch = '$_GET[vouchno]' $keycond";
    $delcondn = " vouchno = '$_GET[vouchno]' $keycond";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' $keycond";
    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last&series=$fseries");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last&series=$fseries");
    delnew($zdb, "billsett", "$delsett", "$file?mode=last&series=$fseries");

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

include("includes/linkshead.php");
?>
<style>
    input[type = checkbox]{
        width: 20px;
        height: 20px;
    }
</style>
<script src="MyScript/othrecp.js" type="text/javascript"></script>
<script src="MyScript/othrecp_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 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <?php include("includes/usermention.php");
                        ?>
                        <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-4 col-xs-3  text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-4 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>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-4 col-xs-1 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 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 focusfield vouchdt" style="height: 30px;" required autofocus="autofocus">
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-3 text-right pad">
                                <label class="label-control marg">Bank / Cash</label>
                            </div>
                            <div class="col-lg-6 col-md-3 col-sm-8 col-xs-7 pad"> 
                                <select class="form-control form bankname select2" name="bankname" style="width: 100%">
                                    <option value="<?php Echo ($addflag != 1) ? $get["bankname"] : ""; ?>"><?php Echo ($addflag != 1) ? $fetbn["name"] : ""; ?></option>
                                    <?php
                                    $getcurr = mysqli_query($conn, "SELECT accode, name FROM `accmast` where (grpcode = '001' or grpcode = '002') and series = '$fseries'")or die(mysqli_error($conn));
                                    while ($curr = mysqli_fetch_array($getcurr)) {
                                        Echo "<option value='$curr[accode]'>$curr[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div> 
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-3 text-right pad">
                                <label class="label-control marg">Account</label>
                            </div>
                            <div class="col-lg-6 col-md-3 col-sm-8 col-xs-7 pad"> 
                                <input list='memname' name='memname' value="<?php echo ($addflag != 1 ) ? $get['memname'] : '' ?>" class='memname form-control form ' autocomplete="off">
                                <datalist id='memname' char="#">
                                    <option value=''></option>
                                    <?php
                                    $mname = mysqli_query($conn, "SELECT `name` FROM $zroot.`member` where grpcode = '040' order by name") or die(mysqli_error($conn));

                                    while ($tname = mysqli_fetch_array($mname)) {
                                        $name = $tname['name'];
                                        echo "<option value='$name'>";
                                    }
                                    ?>
                                </datalist>
                            </div> 
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-3 text-right pad">
                                <label class="control-label marg"> Amount </label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-8 col-xs-7 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" >
                                <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">Update Ledger</label>
                            </div>
                            <div class="col-lg-1 col-md-7 col-sm-8 col-xs-8 pad" >
                                <input type="checkbox" class="form-control form upledgerz" name="upledger" value="1" <?
                                    if ($get['upledger'] == 1) {
                                        Echo "checked";
                                    }
                                    ?>>
                            </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">
                    <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">Member No.</option> -->
                                <option value="4">Member Name</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">Member No.</option> -->
                                <option value="4">Member Name</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">
                    </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="5%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <!-- <th width="7%">Member No.</th> -->
                                    <th width="20%">Member Name</th>
                                    <th width="10%" style="text-align: 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>
