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

$formname = 'Personal Expense';
$maindb = 'persexpense';
$accdb = 'actran';
$printfile = "persexp_print.php";
$file = 'persexpense.php';
$source = 'PE';

$grid = '';
$keyfld = "vouchno";


$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"];
    $paidto = $_POST["paidto"];
    $amount = $_POST["amount"];
    $qty = $_POST["qty"];
    $rate = $_POST["rate"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $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);
            $getbankinit = mysqli_query($conn,"SELECT * FROM `accmast` WHERE `accode` = '3'");
            $bankinit = mysqli_fetch_array($getbankinit);
            $init = $bankinit['init'];
            // cash part

            mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, accode, init, grpcode, amount)
                    VALUES ('$vouchdt','$mvouch', '$mvouch', '$source', '001', '3', '$init', '003', '$amount')");

            // party part
            mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, source, recpno, trnno, accode, init, grpcode, amount)
                     VALUES ('$vouchdt','$mvouch', '$source', '$mvouch', '002', '$paidto', '$init', '015', '$minus_amt')");
        }
        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `paidto`,    `amount`, `qty`,`rate`,`remark1`,`remark2`)
                                       VALUES($qvouch, '$vouchdt', '$paidto', '$amount',  '$qty', '$rate','$remark1','$remark2')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn,"UPDATE `$maindb` SET `vouchdt`='$vouchdt', `paidto`='$paidto',  `amount`='$amount', `qty`='$qty', `rate`='$rate', 
                         `remark1`='$remark1', `remark2`='$remark2' 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);

    $qrystaff = mysqli_query($conn,"SELECT `name`, `accode` FROM `accmast`  where accode = '$get[paidto]'")or die(mysqli_error($conn));
    $fecstaff = mysqli_fetch_array($qrystaff);
}



if (isset($_GET["delete"])) {

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

include("includes/linkshead.php");
?>
<script>
    $(document).ready(function () {
        $("body").on('keyup blur', '.qty,.amount', ratecal);

        function ratecal() {
            var amount = $(".amount").val(),
                    qty = $(".qty").val();

            if (amount > 0 && qty > 0) {
                var rate = amount / qty;
                $(".rate").val(rate);
            }
        }
    });


</script>
<script src="MyScript/persexp_search.js" type="text/javascript"></script>
<script src="MyScript/persexp.js" type="text/javascript"></script>

</head>
<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 col-sm-12 pad">
                <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>
                            <!--<i class="fa fa-times"></i>-->
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-sm-4  text-right pad">
                                <label class="label-control marg">Voucher No</label>
                            </div>
                            <div class="col-lg-2 col-sm-8  pad">
                                <input type="text" class="form-control form " readonly="" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>" required>
                            </div>
                            <div class="col-lg-3 col-sm-4  text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-sm-8  pad">
                                <input type="date" class="form-control form focusfield" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required autofocus="">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <a class="btn btn-primary" href="persexp_voucher.php?vouchno=<?= $qvouch; ?>" target="_blank">Voucher</a>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-sm-4 text-right pad">
                                <label class="label-control marg">Paid.To</label>
                            </div>
                            <div class="col-lg-8 col-sm-8 pad">
                                <select class="form-control form select2" name="paidto" required>
                                    <option value="<?php Echo ($addflag != 1) ? $fecstaff["accode"] : ""; ?>"><?php Echo ($addflag != 1) ? $fecstaff["name"] : ""; ?> </option>

                                    <?php
//                                    echo "<option value=''> </option>";
                                    $getstaff = mysqli_query($conn,"SELECT `name`, `accode` FROM `accmast` where grpcode > 2 order by name")or die(mysqli_error($conn));
                                    while ($showstaff = mysqli_fetch_array($getstaff)) {
                                        echo "<option value='$showstaff[accode]'>$showstaff[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-sm-4  text-right pad">
                                <label class="label-control marg">Amount</label> <br>
                                <label class="label-control marg">Qty</label> <br>
                                <label class="label-control marg">Rate</label>
                            </div>
                            <div class="col-lg-2 col-sm-8  pad">
                                <input type="text" class="form-control form amount text-right" id="amount" name="amount" value="<?php Echo ($addflag != 1) ? $get["amount"] : 0; ?>" required>
                                <input type="text" class="form-control form qty text-right"  name="qty" value="<?php Echo ($addflag != 1) ? $get["qty"] : 0; ?>" >
                                <input type="text" class="form-control form  rate text-right"  name="rate" value="<?php Echo ($addflag != 1) ? $get["rate"] : 0; ?>" readonly="">
                            </div>
                            <div class="col-lg-6 col-sm-8  pad">
                                <!--<input type="text" class="form-control form input-lg rsword"  style="min-height: 71px" name="rsword" value="<?php // echo ($addflag != 1) ? rsword(round($get['rate'])) : '';          ?>" >-->
                                <textarea rows="4" cols="60" class="form-control form  rsword" name="rsword" readonly><?php echo ($addflag != 1) ? rsword(round($get['rate'])) : ''; ?></textarea>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-sm-4 text-right pad">
                                <label class="label-control marg">Remark</label>
                            </div>
                            <div class="col-lg-8 col-sm-8 pad">
                                <input type="text" name="remark1" class="form-control form " value="<?php echo ($addflag != 1) ? $get ["remark1"] : ""; ?>">
                                <input type="text" name="remark2" class="form-control form " value="<?php echo ($addflag != 1) ? $get ["remark2"] : ""; ?>">
                            </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 <?= $formname; ?></h4>
                </div>
                <div class="modal-body">
<!--                    <input type="hidden" value="<?php // Echo $zseries               ?>" class="form-control series">
                    <input type="hidden" value="<?php // Echo $get[code];               ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php // Echo $file               ?>" class="form-control file">
                    <input type="hidden" value="<?php // Echo $para               ?>" class="form-control para">-->
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher No.</option>
                                <option value="2">Paid To</option>
                                <option value="3">Amount</option>
                                <option value="4">Rate</option>
                                <option value="5">Narration1</option>
                                <option value="6">Narration2</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">Date</option>
                                <option value="3">Party</option>
                                <option value="4">Remarks</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" style="background-color: #64CCFC">
                                    <th style="width:10%">Sr No.</th>
                                    <th style="width:10%">Voucher No.</th>
                                    <th style="width:20%">Date</th>
                                    <th style="width:10%">Party</th>
                                    <th style="width:10%">Remarks</th>
                                    <th style="width:10%">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>
