<?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];
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"];
    $chqno = $_POST["chqno"];
    $chqdate = $_POST["chqdate"];
    $amount = $_POST["amount"];
    $narration = $_POST["narration"];


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


    if ($a == 1) {
        ######################## 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);
            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, chqno, chqdate)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$from','001', '$amount', '$chqno', '$chqdate')");

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, chqno, chqdate)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$to','010', '$minus_amt', '$chqno', '$chqdate')");
        }
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( vouchno ,`vouchdt`, `frombc`, `tobc`, `chqno`,`chqdate`,
                                                `amount`,`narration`)
                                       Values ('$qvouch', '$vouchdt', '$from', '$to','$chqno','$chqdate',
                                    '$amount','$narration')") 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  vouchno='$qvouch',  vouchdt='$vouchdt', frombc='$from',
                    tobc='$to' ,chqno='$chqno',chqdate='$chqdate',
                     amount='$amount',narration='$narration'  where vouchno = '$mvouch' ") or die(mysqli_error($conn));
            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"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
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/contra_search.js" type="text/javascript"></script>
<!--<script src="MyScript/challan.js" type="text/javascript"></script>-->
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <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 class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class='col-sm-3 text-right pad'><label class="control-label col-lg-4" style="width: 150px;"> Voucher</label></div>
                            <div class="col-lg-3 pad"> 
                                <input type="text" name="vouchno"  placeholder="vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch; ?>" class="form-control form" style="width:130px; height: 30px;" readonly>
                            </div>
                            <div class='col-sm-2 text-right pad'><label class="control-label col-lg-2" style="">Date</label></div>
                            <div class="col-lg-3 pad" style="padding-left:3px;"> 
                                <input type="date" name="vouchdt" style="padding-left: 3px;width:148px;"  placeholder="date" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date('Y-m-d'); ?>" class="form-control form" style="width:129px; height: 30px;" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class='col-sm-3 text-right pad'><label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">From</label></div>
                            <div class="col-lg-8 pad"> 
                                <select id="cmbfrom" name="cmbfrom" class="form-control form select2" style="height: 33px;">
                                    <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' ") 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-sm-3 text-right pad'><label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">To</label></div>
                            <div class="col-lg-8 pad"> 
                                <select id="cmbto" name="cmbto" class="form-control form select2" style="height: 33px;">
                                    <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' ") 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-sm-3 text-right pad'><label class="control-label col-lg-4" style="width: 150px;"> Cheque No. </label></div>
                            <div class="col-lg-3 pad"> 
                                <input type="text" name="chqno"  placeholder="Cheque No." value="<?php echo ($addflag != 1) ? $get['chqno'] : ''; ?>" class="form-control form" style="width:130px; height: 30px;">
                            </div>
                            <div class='col-sm-2 text-right pad'><label class="control-label col-lg-1" style="width:100px;padding-right: 0px;padding-left: 0px;">Cheque Date</label></div>
                            <div class="col-lg-1"> 
                                <input type="date" style="padding-left: 3px;width:145px;" name="chqdate"  placeholder="" value="<?php echo ($addflag != 1) ? $get['chqdate'] : ''; ?>" class="form-control form" style="width:129px; height: 30px;">
                            </div>
                        </div>
                        <div class="row">
                            <div class='col-sm-3 text-right pad'><label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Amount </label></div>
                            <div class="col-lg-3 pad"> 
                                <input type="text" name="amount" placeholder="Amount" value="<?php echo ($addflag != 1) ? $get['amount'] : ''; ?>" style="height: 30px;" class="form-control form"  required>
                            </div> 
                        </div>
                        <div class="row">
                            <div class='col-sm-3 text-right pad'><label class="control-label col-lg-4" style="width: 150px;font-family: cipher-regular,helvetica;"> Narration </label></div>
                            <div class="col-lg-8 pad"> 
                                <textarea style="padding-left: 0px;" class="narration form-control form" rows="3" cols="50" maxlength="100" name="narration"  placeholder="Narration">  <?php echo ($addflag != 1) ? $get['narration'] : ''; ?></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">
                    <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">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">From</option>
                                <option value="4">To</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 style="background-color:#1abc9c;">
<!--                                    <th width="10%">Code</th>-->
                                    <th width="5%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="30%">From</th>
                                    <th width="30%">To</th>                                    
                                    <th width="20%">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>
    <!--    <div class="modal fade bs-example-modal-lg" 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=" table-responsive">
                            <table id="example1" class="table table-striped">
                                <thead>
                                    <tr style="background-color: #1ABC9C;">
                                        <th>Name</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
    <?php
    if ($addflag != 1) {
        $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")or die(mysqli_error($conn));
        while ($row = mysqli_fetch_array($srchresult)) {
            echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
            echo "<td align='left'>$row[name]</td>";
            echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
            echo "</tr>";
        }
    }
    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="modal-footer"></div>
                </div>
            </div>
        </div>-->
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
