<?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';

$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["cmbfrom"];
    $to = $_POST["cmbto"];
//    $branch = $_POST["branch"];
    $chqno = $_POST["chqno"];
    $chqdate = $_POST["chqdate"];
    $amount = $_POST["amount"];
    $narration = $_POST["narration"];

    // 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) 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;
        }
        
        ######################## 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`, `frombankinit`, `tobankinit`, `chqno`,`chqdate`,
                                                `amount`, `narration`)
                                       Values ('$mvouch', '$vouchdt', '$from', '$to', '$binit1', '$binit2', '$chqno','$chqdate',
                                    '$amount', '$narration')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET  vouchno='$qvouch',  vouchdt='$vouchdt', frombc='$from',
                        tobc='$to', frombankinit='$binit1', tobankinit='$binit2', chqno='$chqno',chqdate='$chqdate',
                        amount='$amount', narration='$narration'  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]'");
    $banknm = mysqli_fetch_array($getbank);

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

    
}

/* 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_search.js" type="text/javascript"></script>

<!--<script src="MyScript/challan.js" 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="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-conrol"> Voucher</label>
                                </div>
                                <div class="col-lg-1 col-md-3 col-sm-8 col-xs-8 pad"> 
                                    <input type="text" name="vouchno" placeholder="vouchno" value="1" class="form-control form" readonly="" disabled="disabled">
                                </div>
                                <div class="col-lg-3 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control">Date</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad"> 
                                    <input type="date" name="vouchdt" placeholder="date" value="2017-11-11" class="form-control form vouchdt" required="" autofocus="" disabled="disabled">
                                </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">From</label>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad"> 
                                    <select id="cmbfrom" name="cmbfrom" class="form-control form select2" disabled="disabled">
                                        <option value="3">CASH ON HAND</option>
                                        <option value="3">CASH ON HAND</option><option value="61">BANK OF INDIA</option><option value="59">BANK OF RAJ COMPUTERS</option><option value="62">PETTY 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">To</label>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                    <select id="cmbto" name="cmbto" class="form-control form select2" disabled="disabled">
                                        <option value="59">BANK OF RAJ COMPUTERS</option>
                                        <option value="3">CASH ON HAND</option><option value="61">BANK OF INDIA</option><option value="59">BANK OF RAJ COMPUTERS</option><option value="62">PETTY 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">Cheque No. </label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                    <input type="text" name="chqno" placeholder="Cheque No." value="123456" class="form-control form" disabled="disabled">
                                </div>
                                <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                    <label class="label-control ">Cheque Date</label>
                                </div>
                                <div class="col-lg-2 col-md-3 col-sm-8 col-xs-8 pad">
                                    <input type="date" name="chqdate" placeholder="" value="2017-11-11" class="form-control form" disabled="disabled">
                                </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"> Amount</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                    <input type="text" name="amount" value="25000.00" class="form-control text-right form" required="" disabled="disabled">
                                </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"> Narration </label>
                                </div>
                                <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                    <textarea style="padding-left: 0px;" class="narration form-control form" rows="3" cols="50" maxlength="100" name="narration" placeholder="Narration" disabled="disabled">  testttete  </textarea>
                                </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">From</option>
                                <option value="4">To</option>
                                <option value="6">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">From</option>
                                <option value="3">To</option>
                                <option value="6">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="15%">Voucher No.</th>
                                    <th width="25%">From</th>
                                    <th width="25%">To</th>
                                    <th width="14%">Amount</th>
                                    <th width="14%">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>
