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

$formname = 'Cash/Bank';
$maindb = 'accmast';
$printfile = "cash_print.php";
$file = 'cash.php';
$trandbexist = 0;
$grid = '';
$keyfld = "accode";
$focusfield = "name";
$mgrpcode = '002';
$keycond = " AND `grpcode`='002' ";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$adduser = $_SESSION["username_" . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$editdate = date("Y-m-d");
$edittime = date("h:i:sa");

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $a = 1;

    $name = $_POST["name"];
    $init = $_POST["init"];
    $type = $_POST["type"];
    $openbal = $_POST["openbal"];
    $opendc = $_POST["opendc"];

    if ($name == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND accode != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?mode=last");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` WHERE $keyfld !='' 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';
            }
        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(accode, `name`, `init`, `bankinit`, `type`, `openbal`, `dc`, grpcode, `adduser`)
                                               VALUES($qvouch,'$name', '$init', '$init', '$type', '$openbal', '$opendc', '$mgrpcode', '$adduser')") or die(mysqli_error($conn));

            insmess();
            $mvouch = $_GET["vouchno"];
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
         
            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name',`init`='$init', `bankinit`='$init',
                                                                    `type`='$type', openbal='$openbal', `dc`='$opendc',
                                                                    `edituser`='$adduser' WHERE accode = '$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' AND `grpcode`='002' ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

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

if (isset($_GET["delete"])) {
    $getcustdtl = mysqli_query($conn, "SELECT * FROM `actran` WHERE `accode` = '$getvouch'") or die(mysqli_error($conn));
    if (mysqli_num_rows($getcustdtl) > 0) {
        browse("Entry Exist in Transaction");
        header("refresh: 1; $file?mode=last");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
}

include("includes/linkshead.php");
?>
<script src="MyScript/cash_search.js" type="text/javascript"></script>

<style type="text/css">
    /* .select2-container--default .select2-selection--single .select2-selection__rendered {
        color: #444;
        line-height: 12px;
        font-size: 25px;
        font-weight: bold;
    } */
</style>
</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>

                    <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">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control form accode" value="<?php echo ($addflag != 1) ? $qvouch : $qvouch; ?>">
                                <input type="text" class="form-control form focusfield" name="name"  value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" required autofocus>
                            </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">Initial</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form init" maxlength="3" name="init" value="<?php echo ($addflag != 1) ? $get["init"] : ""; ?>" 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">Type</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <select name="type" class="form-control form pad select2">
                                    <option value="C" <?= ($get['type'] == 'C') ? 'selected' : '' ?>>CASH</option>
                                    <option value="B" <?= ($get['type'] == 'B') ? 'selected' : '' ?>>BANK</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">Opening Balance</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" size="40" name="openbal" class="form-control form numeric text-right openbal" value="<?php echo ($addflag != 1) ? $get['openbal'] : ""; ?> " />
                            </div>
                        </div>
                    </div>

                    <div class="panel-footer">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-right pad">

                            <div class="col-lg-10 col-md-10 col-sm-9 col-xs-9 text-right pad">
                                <?php include("includes/buttons.php"); ?>
                            </div>

                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <table class="table-condensed" style="margin: 0 auto;">
                                    <tr>
                                        <!-- <td class=" buttonform tooltip-bottom " id="book" data-toggle="tooltip"
                                            data-placement="top" title="">
                                            <button type="button" class="btn btn-pad skbtn"
                                                style="background-color:#003f75;color:white;border-radius:7px"
                                                onClick="window.open('bankbook.php?accode=<?= $qvouch; ?>', '_blank');">
                                                BOOK REPORT
                                            </button>
                                        </td> -->
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </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">Name</option>
                                <option value="2">Initial</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">Name</option>
                                <option value="2">Initial</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="45%">Name</th>
                                    <th width="45%">Initial</th>
                                    <th 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>