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

$formname = 'Cash Receive';
$maindb = 'chrecd';
$printfile = "chrecd_print.php";
$file = 'ch_recd.php';
$keyfld = "vouchno";
$focusfield = "name";

$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"];
    $amtrecd = $_POST["amtrecd"];

    $mvouch = $qvouch;

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

        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `amtrecd`)
                                        VALUES($qvouch, '$vouchdt', '$amtrecd')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt',`amtrecd`='$amtrecd' WHERE `vouchno` = '$qvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, 'unlock tables');
    }
}

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

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


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

include("includes/linkshead.php");
?>
<!--<script src="MyScript/challan.js" type="text/javascript"></script>-->
<style>
    @media print
    {    
        .no-print, .no-print *
        {
            display: none !important;
        }
    }
</style>
</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-8">
                <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 text-right">
                            <div class="col-lg-12">
                                <a onclick="window.location.href = 'menu.php'" class="btn btn-primary noPrint">GO Back</a>
                            </div>
                        </div>
                        <!--<div class="col-lg-6 pad text-center" id="overlay_form" style="display:none;z-index:999;">-->
                        <div class="col-lg-1 col-md-2 col-sm-4 col-xs-4 text-right pad">
                            <label class="label-control">Voucher</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-7 col-xs-7 pad">
                            <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" readonly >
                        </div>
                        <div class="col-lg-1 col-md-2 col-sm-4 col-xs-4 text-right pad">
                            <label class="label-control">Date</label>
                        </div>
                        <div class="col-lg-3 col-md-2 col-sm-7 col-xs-7 pad">
                            <input type="date" class="form-control form" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" autofocus>
                        </div>
                        <div class="col-lg-2 col-md-1 col-sm-4 col-xs-4 text-right pad">
                            <label class="label-control">Amount Recd.</label>
                        </div>
                        <div class="col-lg-3 col-md-2 col-sm-7 col-xs-7 text-right pad">
                            <input type="text" name="amtrecd" class="form-control form amtrecd text-right" value="<?php Echo ($addflag != 1) ? $get["amtrecd"] : ""; ?>" >
                        </div>
                        <div class="row">
                            &nbsp;
                        </div>
                        <div class="row">
                            &nbsp;
                        </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" 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 class="mySearchTr">
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Amount Received</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[vouchno]</td>";
                                        echo "<td align='left'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$row[amtrecd]</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>
