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

$formname = 'Year Creation';
$maindb = 'compyear';
$trandb = '';
$printfile = "";
$file = 'yearcreate.php';
$trandbexist = 0;
$grid = '';
$keyfld = "id";
$keycond = "";

$admin = $_SESSION["admin_" . $dbname];
$username = $_SESSION["username_" . $dbname];
$zroot = $_SESSION["zdbroot"];
$zroot1 = substr($zroot, 0, -4);
$myear = $_SESSION[$myyear];
$zdb = $_SESSION["zdbyear_" . $dbname];

//browse($myear);

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $company = $_POST["company"];
    $society = $_POST["society"];
    $finyear = $_POST["finyear"];
    $startdate = $_POST["startdate"];
    $lastdate = $_POST["lastdate"];
    $descrip = $_POST["descrip"];
    ################## MASTERS ##################

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

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


        // Comment Done By Fairoz and Abdur on 16 April 2020
//        if ($addflag == 1) {
//            $currdata = $zroot1 . $company . '_' . $myear;
//            $database = $zroot1 . $company . '_' . $finyear;
//
//            $dbhost = 'localhost';
//            $dbuser = 'apps4rea_user';
//            $dbpass = 'rajF@1998';
//            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
//
//            if (!$conn) {
//                die('Could not connect: ' . mysqli_error($conn));
//            }
//
//            browse('Connected successfully');
//
//
//
//
//            $sql = 'CREATE Database ' . $database;
//            $retval = mysqli_query($conn, $sql, $conn);
//
//            if (!$retval) {
//                die('Could not create database: ' . mysqli_error($conn));
//            }
//
//            browse("Database $database created successfully");
//
//            $tables = mysqli_query($conn, "SHOW TABLES FROM $currdata");
//
//            while ($line = mysqli_fetch_row($tables)) {
//                $tab = $line[0];
//                mysqli_query($conn, "DROP TABLE IF EXISTS $database.$tab");
//                mysqli_query($conn, "CREATE TABLE $database.$tab LIKE $currdata.$tab") or die(mysqli_error($conn));
//
////                mysqli_query($conn, "INSERT INTO bulksms1.csvv SELECT * FROM sms.csv");
//////    echo "Table: <b>" . $line[0] . " </b>Done<br>";
//            }
//        }
        ############################## MAIN  ######################################################

        if ($addflag == 1) {
            
            mysqli_query($conn, "INSERT INTO `$maindb`(`id`, `cono`, `society`,`finyear`, `startdate`, `lastdate`, `descrip`)   
                                      VALUES($qvouch, '$company', '$society','$finyear', '$startdate', '$lastdate', '$descrip')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;

            mysqli_query($conn, "UPDATE `$maindb` SET `cono`='$company', `society`='$society',`finyear`='$finyear',`startdate`='$startdate',`lastdate`='$lastdate',
                                              `descrip`='$descrip' WHERE $keyfld = '$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);

    $getcomp = mysqli_query($conn, "SELECT * FROM `company` WHERE `code`='$get[cono]'") or die(mysqli_error($conn));
    $fetcomp = mysqli_fetch_array($getcomp);

    // $getsoc = mysqli_query($conn, "SELECT * FROM `society` WHERE `cono`='$get[society]'") or die(mysqli_error($conn));
    // $fetsoc = mysqli_fetch_array($getsoc);
}

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

/* DELETE */
if (isset($_GET["delete"])) {
//    $chk = mysqli_query($conn, "SELECT usertype FROM `$maindb` WHERE `code`='$keyfld'") or die(mysqli_error($conn));
//    $getchk = mysqli_fetch_array($chk);
//    $uname = $getchk['user'];
//    if($uname == 'supervisor'){
//        
//    }
    delete($zroot, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
?>
<?php include("includes/linkshead.php"); ?>
<style>
    input[type = checkbox]{
        width: 20px;
        height: 20px;
    }
</style>
<script src="MyScript/yearcreate.js" type="text/javascript"></script>
<script src="MyScript/yearcreate_search.js" type="text/javascript"></script>
<body>
    <!-- Default box -->
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="">
            <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">
                            <label class="col-sm-3 control-label marg">Company</label>
                            <div class="col-sm-6">
                                <select name="company" id="company" class="form-control form select2 company"  >
                                    <option value="<? Echo ($addflag != 1) ? $fetcomp['cono'] : ""; ?>" ><? Echo ($addflag != 1) ? $fetcomp['compname'] : ""; ?></option>
                                    <?
                                    if ($addflag == 1) {
                                        $getcmp = mysqli_query($conn, "SELECT * FROM $zroot.`company` ORDER BY `compname`")or die(mysqli_error($conn));
                                        while ($fetcmp = mysqli_fetch_array($getcmp)) {
                                            Echo "<option value='$fetcmp[cono]'>$fetcmp[compname]<option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Year</label>
                            <div class="col-sm-6">
                                <input type="hidden" class="form-control form vouchno" name="vouchno"  value='<? echo ($addflag != 1) ? $get['id'] : $qvouch; ?>'  >
                                <input type="text" class="form-control form finyear" name="finyear" placeholder="Financial Year" autocomplete="off" 
                                <?php
                                if ($addflag != 1) {
                                    echo 'readonly ';
                                }
                                ?> value='<? echo ($addflag != 1) ? $get['finyear'] : ''; ?>' required="" >
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg" style="">Sale Lock</label>
                            <div class="col-sm-6">
                            <? $salelockdt = date('Y-m-d'); ?>
                                <input type="date" class="form-control form salelockdt" name="salelockdt" id="salelockdt" value='<? echo $salelockdt; ?>' required="">
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Start Date</label>
                            <div class="col-sm-6">
                                <input type="date" class="form-control form startdate" name="startdate" id="startdate" value='<? echo ($addflag != 1) ? $get['startdate'] : ''; ?>' required="">
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Last Date</label>
                            <div class="col-sm-6">
                                <input type="date" class="form-control form lastdate" name="lastdate" id="lastdate" value='<? echo ($addflag != 1) ? $get['lastdate'] : ''; ?>' required="">
                            </div>
                        </div>
                        <div class="row">
                            <label class="col-sm-3 control-label marg">Year Description</label>
                            <div class="col-sm-6">
                                <input type="text" name="descrip" id="descrip" class="form-control form descrip"  placeholder="Year Description" autocomplete="off"  required="" value='<? echo ($addflag != 1) ? $get['descrip'] : ''; ?>'>
                            </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">Company</option>
                                <option value="2">Year</option>
                                <option value="3">Start Date</option>
                                <option value="4">Last 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">Company</option>
                                <option value="2">Year</option>
                                <option value="3">Start Date</option>
                                <option value="4">Last 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" style="color: black; background-color:#F17B25;" 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" style="width: 100%" width="100%">
                            <thead>
                                <tr style="background-color:#EFE4B0;">
                                    <th style="color: black; width:20% !important">Company</th>
                                    <th style="color: black; width:35% !important">Society</th>
                                    <th style="color: black; width:10%  !important">Year</th>
                                    <th style="color: black; width:15% !important">Start Date</th>
                                    <th style="color: black; width:15% !important">Last Date</th>
                                    <th style="color: black; width:5%  !important">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" style="color: black; background-color:#EFE4B0;" value="Previous">
                            <input type="button"  class="btn btn-primary snext" style="color: black; background-color:#EFE4B0;" value="Next">
                        </div>
                    </div>

                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
</body>
</html>
<?php include("includes/linksfoot.php"); ?>