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

$formname = 'Year Setup';
$maindb = 'compyear';
// $printfile = "staff_print.php";
$file = 'compyear.php';
$trandbexist = 0;
$keyfld = "id";
$focusfield = "name";

$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');

$zroot = $_SESSION["zdbroot"];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
//mysqli_select_db($conn, $zdb);
//browse($zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    foreach ($_POST as $key => $value) {
        $exclude_fld = ["test"];
        if (in_array($key, $exclude_fld)) {
            continue;
        }
        $$key = $value;
    //    echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
    }
    // $yearname = mysqli_real_escape_string($conn, $_POST["yearname"]);
    browse($yearname);

 
    $a = 1;

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLE `$maindb` WRITE");

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

            mysqli_query($conn, "INSERT INTO `$maindb`(`yearname`, `startdate`, `lastdate`, `invno`, `acopchgs` , `prod1`, `perc1`, `prod2`, `perc2`, `prod3`, `perc3`, `prod4` , `perc4` , 
                                            `minopchg`, `apparr`,`acoparr`, `overcred`, `rmcash`, `cashac`, `suppcode`)
                                        VALUES($yearname, '$startdate', '$lastdate', '$invno', '$acopchgs','$prod1', '$perc1', '$prod2', '$perc2', '$prod3', '$perc3', '$prod4' , '$perc4' ,
                                         '$minopchg', '$apparr','$acoparr', '$overcred', '$rmcash', '$cashac', '$suppcode')") or die(mysqli_error($conn));
           
            $mvouch = $_GET["vouchno"];
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");

            //            header("refresh: 1; $file?mode=true");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET `yearname`='$yearname',`startdate`='$startdate',`lastdate`='$lastdate', `invno`='$invno',`acopchgs`='$acopchgs', `prod1`='$prod1', `perc1`='$perc1', `prod2`='$prod2', `perc2`='$perc2', 
            `prod3`='$prod3', `perc3`='$perc3',  `prod4`='$prod4',`perc4`='$perc4', `minopchg` = '$minopchg', `apparr`= '$apparr', `acoparr`='$acoparr', `overcred`='$overcred',
             `rmcash`='$rmcash',`cashac`='$cashac', `suppcode`='$suppcode' where yearname = '$yearname'") or die(mysqli_error($conn));

            //            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            //            header("refresh: 1; $grid");
        }
        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"])) {
    mysqli_query($conn, "LOCK TABLE `$maindb` WRITE");

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

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/compyear_search.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function () {
        $('#hadd').hide();
        // $('#hsearch').hide();
        $('#hprint').hide();
        $('#hdelete').hide();
        // $('#hprevious').hide();
        // $('#hnext').hide();

    });
</script>
<style>
    .hrline {
        border: 2px solid black;
    }
</style>
</head>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>
                        <div class="actions pull-right">
                        </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">Year</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form yearname" name="yearname" value="<?php echo ($addflag != 1) ? $get["yearname"] : ""; ?>" readonly>
                            </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">Start Date</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form startdate" name="startdate" tabindex="-1" value="<?php echo $mfrdate; ?>" readonly>
                            </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">last Date</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form lastdate" name="lastdate" tabindex="-1" value="<?php echo $mtodate; ?>" readonly>
                            </div>
                        </div>
                        <hr class="hrline">
                        <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">Starting Invoice No.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <input type="text" class="form-control invno form focusfield" name="invno" value="<?php echo ($addflag != 1) ? $get["invno"] : ""; ?>">
                            </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">A/c Operating Chgs.%</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control acopchgs form" name="acopchgs" value="<?php echo ($addflag != 1) ? $get["acopchgs"] : ""; ?>">
                            </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">Product 1 %</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control prod1 form" name="prod1" value="<?php echo ($addflag != 1) ? $get["prod1"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control perc1 form" name="perc1" value="<?php echo ($addflag != 1) ? $get["perc1"] : ""; ?>">
                            </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">Product 2 %</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control prod2 form" name="prod2" value="<?php echo ($addflag != 1) ? $get["prod2"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control perc2 form" name="perc2" value="<?php echo ($addflag != 1) ? $get["perc2"] : ""; ?>">
                            </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">Product 3 %</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control prod3 form" name="prod3" value="<?php echo ($addflag != 1) ? $get["prod3"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control perc3 form" name="perc3" value="<?php echo ($addflag != 1) ? $get["perc3"] : ""; ?>">
                            </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">Product 4 %</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control prod4 form" name="prod4" value="<?php echo ($addflag != 1) ? $get["prod4"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control perc4 form" name="perc4" value="<?php echo ($addflag != 1) ? $get["perc4"] : ""; ?>">
                            </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">Minimum Op. Chgs.</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control minopchg form" name="minopchg" value="<?php echo ($addflag != 1) ? $get["minopchg"] : ""; ?>">
                            </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">Apply Arrears</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="apparr" class="form-control form  select2 apparr" style="width:100%">
                                <?php 
                                $getarr = $get["apparr"];
                                $apparrear = [
                                    'Y' => 'YES',
                                    'N' => 'No'
                                ];
                                $arrear = $apparrear[$getarr];
                                ?>
                                    <option value="<?php echo ($addflag != 1) ? $arrear : ""; ?>"><?php echo ($addflag != 1) ? $arrear : ""; ?></option>
                                    <option value="YES">YES </option>
                                    <option value="NO">NO</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">Op.chgs. On Arrears</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form acoparr" name="acoparr" value="<?php echo ($addflag != 1) ? $get["acoparr"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Prompt Overcredit A/c.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form overcred" name="overcred" value="<?php echo ($addflag != 1) ? $get["overcred"] : ""; ?>">
                            </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 For RM Cash</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control rmcash form" name="rmcash" value="<?php echo ($addflag != 1) ? $get["rmcash"] : ""; ?>">
                            </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">Pump & Stock Reading Cash A/c</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control cashac form" name="cashac" value="<?php echo ($addflag != 1) ? $get["cashac"] : ""; ?>">
                            </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">Supplier Code</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control suppcode form" name="suppcode" value="<?php echo ($addflag != 1) ? $get["suppcode"] : ""; ?>">
                            </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 <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Year</option>
                                <option value="2">Start Date</option>
                                <option value="3">End Date</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">Year</option>
                                <option value="2">Start Date</option>
                                <option value="3">End Date</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 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:#26A69A;">
                                    <th width="30%">Year</th>
                                    <th width="20%">Start Date</th>
                                    <th width="20%">End Date</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>