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

$formname = 'Contra';
$maindb = 'contra';
$accdb = 'actran';

$file = 'contra.php';
$grid = '';
$source = 'TR';
$keyfld = "vouchno";
$trandbexist = 0;
$focusfld = "cmbfrom";
$printfile = 'contrec.php';

$root = $_SESSION["zdbroot"];

$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"];
    $from = $_POST["frombc"];
    $to = $_POST["tobc"];
    $chqno = $_POST["chqno"];
    $chqdate = $_POST["chqdate"];
    $amount = $_POST["amount"];
    $narr1 = $_POST["narr1"];
    $narr2 = $_POST["narr2"];

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


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

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

        ######################## 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);

            $qry1 = mysqli_query($conn, "SELECT grpcode, bankinit FROM `accmast` where accode = '$from' ") or die(mysqli_error($conn));
            $fet1 = mysqli_fetch_array($qry1);
            $grp1 = $fet1['grpcode'];
            $binit1 = $fet1['bankinit'];

            $qry2 = mysqli_query($conn, "SELECT grpcode, bankinit FROM `accmast` where accode = '$to' ") or die(mysqli_error($conn));
            $fet2 = mysqli_fetch_array($qry2);
            $grp2 = $fet2['grpcode'];
            $binit2 = $fet2['bankinit'];

            mysqli_query($conn, "INSERT INTO `$accdb`( date, vouchno, source, trnno, accode, grpcode, bankinit, amount, chqno, chqdate)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$from', '$grp1', '$binit1', '$amount', '$chqno', '$chqdate')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$accdb`( date, vouchno, source, trnno, accode, grpcode, bankinit, amount, chqno, chqdate)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$to', '$grp2', '$binit2', '$minus_amt', '$chqno', '$chqdate')") or die(mysqli_error($conn));
        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`,`vouchdt`, `frombc`, `tobc`, `chqno`, `chqdate`,
                                                                     `amount`, `narr1`, `narr2`, `frombankinit`, `tobankinit`)
                                                             VALUES ('$mvouch', '$vouchdt', '$from', '$to', '$chqno', '$chqdate',
                                                                     '$amount', '$narr1', '$narr2', '$binit1', '$binit2')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `frombc`='$from', `tobc`='$to',
                                                                    `chqno`='$chqno', `chqdate`='$chqdate', `amount`='$amount',
                                                                    `narr1`='$narr1', `narr2`='$narr2', frombankinit='$binit1', tobankinit='$binit2'
                                                                    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 * FROM `accmast` WHERE `accode` = '$get[frombc]' ") or die(mysqli_error($conn));
    $banknm = mysqli_fetch_array($getbank);

    $getcash = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[tobc]' ") or die(mysqli_error($conn));
    $cash = mysqli_fetch_array($getcash);

}

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

/* DELETE */
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>
    $(document).ready(function () {
        $("#cmbto").change(function () {
            var frm = $("#cmbfrom").val();
            var to = $("#cmbto").val();
            if (frm == to) {
                alert("From & To Value Canot Be Same");
                $('#cmbto').focus();
            }

        });
    });
</script>
<script src="MyScript/shortkeys.js" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<script src="MyScript/contra.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
</head>
<body>

    <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>
                    <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-3 col-xs-3 text-right pad">
                                <label class="label-conrol marg"> Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad"> 
                                <input type="text" class="form-control form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required readonly>

                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 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="date" name="vouchdt"   placeholder="date" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>" class="form-control form vouchdt"  required 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 marg">From</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad"> 
                                <select id="frombc" name="frombc" class="form-control form select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $banknm['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $banknm['name'] : ""; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` where `grpcode` < '003' $keycond") 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">To</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select id="tobc" name="tobc" class="form-control form select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $cash['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $cash['name'] : ""; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` where `grpcode` < '003' $keycond") 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">Cheque No. </label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 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-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Cheque Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="date" name="chqdate" value="<?php echo ($addflag != 1) ? $get['chqdate'] : ''; ?>" 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"> Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" name="amount" value="<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>"  class="form-control text-right form"  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"> Narration </label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="narr1 form-control form" name="narr1" value="<?php echo ($addflag != 1) ? $get['narr1'] : ''; ?>">
                                <input type="text" class="narr2 form-control form" name="narr2" value="<?php echo ($addflag != 1) ? $get['narr2'] : ''; ?>">
                            </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">From</option>
                                <option value="4">To</option>
                                <option value="5">Cheque No.</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">From</option>
                                <option value="4">To</option>
                                <option value="5">Cheque No.</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" style="color: black;" style="color: black; background-color:#F17B25;" 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 class="mySearchTr">
                                <tr>
                                <th style="color: black;" width="5%">Voucher</th>
                                <th style="color: black;" width="10%">Date</th>
                                <th style="color: black;" width="20%">From</th>
                                <th style="color: black;" width="20%">To</th>
                                <th style="color: black;" width="7%">Cheque No.</th>
                                <th style="color: black;text-align: right" width="10%">Amount</th>
                                <th style="color: black;" width="23%">Narration</th>
                                <th style="color: black;" width="5%">Action</th>

                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>

                    <div class="row">
                        <br>
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" style="color: black;" class="btn btn-primary sprev" value="Previous">
                            <input type="button" style="color: black;" 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>
                    