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

$formname = 'Advance';
$maindb = 'cashgive';
$acdb = 'actran';

$printfile = "kargpay_fr.php";
$utype = $_SESSION['usertype'];
$usertype = $_SESSION["usertype"];

$file = 'kargpay.php';
$acdbexist = 0;
$keyfld = "vouchno";
$source = 'CP';
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

$admin = $_SESSION["admin_" . $dbname];

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $karigar = $_POST["karigar"];
    $amount = $_POST["amount"];
    $rem1 = $_POST["remark1"];
    $rem2 = $_POST["remark2"];
    $approve = $_POST["approve"];

    $bname = "3";

    $count = $_POST["items"];

    $mvouch = $qvouch;
    $mgroup = $source . $qvouch;

    if (empty($vouchdt)) {
        browse("Voucher Date Cannot Be Empty");
        $a = 0;
    }

    if (empty($karigar)) {
        $a = 0;
        browse("Karigar Cannot Be Empty");
    }

    if ($amount <= 0) {
        browse("Amount Cannot Be Zero");
        $a = 0;
    }

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$acdb` 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;




        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `accode`, `amount`, `remark1`, `remark2`,approve)
                                        VALUES('$qvouch', '$vouchdt', '$karigar', '$amount','$rem1', '$rem2','$approve')") or die(mysqli_error($conn));
            insmess();

            header("refresh: 1; $file?mode=edit&vouchno=$mvouch");
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `accode`='$karigar', `amount`='$amount',approve='$approve',
                        `remark1`='$rem1', `remark2`='$rem2' WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?mode=edit&vouchno=$mvouch");
        }

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

        if ($amount > 0) {
            $minus_amt = ($amount * -1);
            // first cash part
            mysqli_query($conn, " INSERT INTO `$acdb`(date, vouchno, trnno, type, `source`, `bankinit`, `grpcode`, init, accode, amount)
                                     VALUES ('$vouchdt','$mvouch', '001', '', '$source','$bankinit','002','','$bname','$amount')");
            // party part
            mysqli_query($conn, " INSERT INTO `$acdb`(date, vouchno, trnno, type, `source`, `bankinit`, `grpcode`, init, accode, amount)
                                VALUES ('$vouchdt','$mvouch', '002', '', '$source','$bankinit','009','','$karigar', '$minus_amt')");
        }
        mysqli_query($conn, "UNLOCK TABLES") OR die(mysqli_error($conn));
    }
}

if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

//    $getvouch = $_GET["vouchno"];
    $result = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE vouchno='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getkarigr = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$get[accode]' and `active` = 1") or die(mysqli_error($conn));
    $karigr = mysqli_fetch_array($getkarigr);
    $getkarigr2 = mysqli_query($conn, "SELECT imgpath FROM `kariimg` WHERE `code`='$get[accode]'") or die(mysqli_error($conn));
    $karigr2 = mysqli_fetch_array($getkarigr2);
}

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

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "vouchno", $_GET["vouchno"], "$file?mode=last");

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$acdb", "$delcond", "$file?mode=last");
}
include("includes/files.php");
include("includes/linkshead.php");
?>
<!--<script src="MyScript/kargpay.js" type="text/javascript"></script>-->
<script src="MyScript/kargpay_search.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>

        <!-- Default box -->
        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12  pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php Echo $formname; ?></h3>
                </div>
                   
                <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-control marg">Voucher</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                            <input type="text"  name="vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>"  class="form-control" readonly/> 
                            <input type="hidden"  name="utype" value="<?php echo "$utype"; ?>"  class="utype form-control" /> 
                        </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">Voucher Date</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                            <input type="date" id="vouchdt" name="vouchdt" value="<?php echo ($addflag != 1) ? $get['vouchdt'] : date("Y-m-d"); ?>" class="form-control form mota" autofocus  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">Employee</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                            <select id="karigar" name="karigar"  class="karigar form-control select2 form mota" required>
                                <option value="<?php echo ($addflag != 1) ? $karigr['accode'] : ""; ?>"><?php echo($addflag != 1) ? $karigr['name'] : ''; ?></option>
                                <?php
                                $getk = mysqli_query($conn, "SELECT `accode`,`name`, `custcode` FROM `accmast` WHERE `grpcode`='040' and active = '1' ORDER BY `name`") or die(mysqli_error($conn));
                                while ($kgt = mysqli_fetch_array($getk)) {
                                    echo"<option value='$kgt[accode]'>$kgt[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">Amount</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <input type="text"  name="amount" value="<?php echo ($addflag != 1) ? $get['amount'] : ""; ?>"  class="mota form-control form text-right" 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-6 col-sm-8 col-xs-8 pad">
                            <input type="text"  name="remark1" class="form-control form mota" value="<?php echo ($addflag != 1) ? $get['remark1'] : ""; ?>"/>
                            <input type="text"  name="remark2" class="form-control form mota" value="<?php echo ($addflag != 1) ? $get['remark2'] : ""; ?>"/>
                        </div>
                    </div>


                    <?php
                    if ($usertype == 9) {
                        ?>
                        <div class="row approvechk">
                            <label class="control-label col-lg-3 text-right padd">Approved</label>
                            <div class="col-lg-8 pad"> 
                                <input type="checkbox" name="approve" value="1" class="form approve form-control" <?
                                if ($get['approve'] == 1) {
                                    Echo "checked";
                                } else {
                                    Echo "unchecked";
                                }
                                ?>
                                style="width: 30px" >
                            </div> 
                        </div>
                    <?php } ?>
                </div>

                <div class="panel-footer">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>
        <div class="col-lg-2">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                    <img id='preview' src="<?php echo ($addflag != 1) ? $karigr2['imgpath'] : ""; ?>" alt='your image' style='height:50%;width:50%'/>

                </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">Karigar</option>
                                <option value="4">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">Karigar</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:#05B2EA;">
                                    <th style="color: white;"width="1%">Voucher No</th>
                                    <th style="color: white;"width="5%"> Date</th>
                                    <th style="color: white;"width="8%">Karigar</th>
                                    <th style="color: white;"width="5%">Amount</th>
                                    <th style="color: white;"width="1%">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>

