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

$formname = 'Bank Entry';
$maindb = 'bankent';

$printfile = "inward_print.php";
$file = 'bankent.php';
$trandbexist = 0;
$keyfld = "vouchno";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

$stkdb1 = 'tran';
$zroot = $_SESSION["zdir"];
$combstkdb = $stkdb1 . $myyear;

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

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $party = $_POST["party"];
    $shopwk = $_POST["shopwk"];
    $group = $_POST["group"];
    $prname = $_POST["prname"];
    $amount = $_POST["amount"];
    $remark = $_POST["remark"];

    $mvouch = $qvouch;

    if ($a == 1) {

        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `party`, `shopwk`, `group`, `prname`, `amount`, `remark`)
                                               VALUES ('$qvouch', '$vouchdt', '$party', '$shopwk', '$group', '$prname', '$amount', '$remark')") 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', `party`='$party', `shopwk`='$shopwk', `group`='$group', `prname`='$prname', 
                                                      `amount`='$amount', `remark`='$remark' 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);

    $findparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$get[party]'") or die(mysqli_error($conn));
    $lparty = mysqli_fetch_array($findparty);
    $findshopwk = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`='$get[shopwk]'") or die(mysqli_error($conn));
    $lshopwk = mysqli_fetch_array($findshopwk);
}

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

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

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

<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?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-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-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control text-size form" name="vouchno"
                                    value="<?php echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required
                                    readonly>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form focusfield" name="vouchdt"
                                    value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" 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 text-size marg">Shop Worker</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form  select2 shopwk" name="shopwk" style="width: 100%">
                                    <option value="<?php echo ($addflag != 1) ? $get["shopwk"] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $lshopwk["name"] : ""; ?>
                                    </option>
                                    <?php
                                    $mown4 = mysqli_query($conn, "SELECT `code`, `name` FROM `transport` ORDER BY name") or die(mysqli_error($conn));
                                    while ($trow4 = mysqli_fetch_array($mown4)) {
                                        echo "<option value='$trow4[code]'>$trow4[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 text-size marg">Customer</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 party" name="party" style="width:100%"
                                    required>
                                    <option value="">Select Customer</option>
                                    <?php
                                    if ($addflag != 1) {
                                        $shopwk = $get['shopwk'];
                                        $showpwkcond = "";
                                        if (!empty($shopwk)) {
                                            $showpwkcond = "AND `transporter`='$shopwk'";
                                        }
                                        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE  `grpcode`='010' $showpwkcond ORDER BY `name`") or die(mysqli_error($conn));
                                        while ($fparty = mysqli_fetch_array($gparty)) {
                                            $selected = ($get["party"] == $fparty["accode"]) ? 'selected' : '';
                                            echo "<option value='$fparty[accode]' $selected>$fparty[name]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                                <input type="hidden" id="selectedparty"
                                    value="<?= ($addflag != 1) ? $get['party'] : '' ?>">
                            </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 text-size marg">Group</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form  select2 group" name="group" style="width: 100%">
                                    <?php
                                    $groupoptions = [
                                        '1' => 'BANK',
                                        '2' => 'SATURDAY',
                                        '3' => 'WEDNESDAY',
                                        '4' => 'RP',
                                        '5' => 'OTHERS'
                                    ];

                                    foreach ($groupoptions as $value => $label) {
                                        $selected = ($get["group"] == $value) ? 'selected' : '';
                                        echo "<option value=\"$value\" $selected>$label</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 text-size marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form prname" name="prname" maxlength="20"
                                    value="<?php echo ($addflag != 1) ? $get["prname"] : ""; ?>">
                            </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-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="number" step="any" class="form-control form text-right amount"
                                    name="amount" value="<?php echo ($addflag != 1) ? $get["amount"] : ""; ?>">
                            </div>
                        </div>

                    </div>
                </div>
                <div class="panel-footer">
                    <?php include("includes/buttons.php"); ?>
                </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 <?= $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>Customer</th>
                                    <th>Name</th>
                                    <th>Amount</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT a.*,b.name as partynm FROM `$maindb` a
                                                                              LEFT JOIN `accmast` b ON a.party = b.accode ") or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        $vdt = date('d-m-Y', strtotime($row['vouchdt']));
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='left'>$row[vouchno]</td>";
                                        echo "<td align='left'>$vdt</td>";
                                        echo "<td align='left'>$row[partynm]</td>";
                                        echo "<td align='left'>$row[prname]</td>";
                                        echo "<td align='left'>$row[amount]</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>