/home/smartonegroup/www/system/system/controllers/hrm.php
<?php
/*
|--------------------------------------------------------------------------
| Controller
|--------------------------------------------------------------------------
|
*/
_auth();
$ui->assign('selected_navigation', 'hrm');
$ui->assign('_title', $_L['HRM'] . '- ' . $config['CompanyName']);
$action = route(1, 'employees');
$user = authenticate_admin();
Event::trigger('assets');

if (!has_access($user->roleid, 'hr', 'view')) {
    permissionDenied();
}

switch ($action) {


    case 'handle-timer':


        break;


        case 'timesheet':

            $employees = Employee::all();
            $selected_employee_id = route(2);

            $date_range = route(3);
            $start_date = date('Y-m-d', strtotime('-30 days'));
            $end_date = date('Y-m-d');
            if(!empty($date_range))
            {
                $date_range = explode('*', $date_range);
                $start_date = $date_range[0];
                $end_date = $date_range[1];
            }

            $users = User::all()->keyBy('id')->all();

            $time_entries = HrTimeLog::query()
                ->where('date', '>=', $start_date)
                ->where('date', '<=', $end_date);

            if($selected_employee_id)
            {
                $time_entries = $time_entries->where('employee_id', $selected_employee_id);
            }

            $time_entries = $time_entries
                ->limit(1000)
                ->orderBy('id', 'desc')
                ->get();

            view('employee_timesheet',[
                'employees' => $employees,
                'selected_employee_id' => $selected_employee_id,
                'users' => $users,
                'time_entries' => $time_entries,
                'start_date' => $start_date,
                'end_date' => $end_date,
            ]);

            break;

    case 'employees':
        if (!db_table_exist('employees')) {
            r2(U . 'hrm/schema');
        }

        $employees = Employee::all();

        view('employee_list', [
            'employees' => $employees,
        ]);

        break;

    case 'employee':
        $id = route(2);

        $departments = TicketDepartment::all();

        $employee = false;
        if ($id != '') {
            $employee = Employee::find($id);
        }

        $users = User::all()->keyBy('id')->all();

        view('employee', [
            'employee' => $employee,
            'departments' => $departments,
            'users' => $users,
        ]);

        break;

    case 'employee-post':
        $validation = Validation::init();
        $data = $request->all();
        $validator = $validation->make($data, [
            'name' => 'required',
            'job_title' => 'required',
            'date_hired' => 'required|date',
            'pay_frequency' => 'required',
            'email' => 'required|email',
        ]);

        if ($validator->fails()) {
            $message = '';
            foreach ($validator->errors()->all() as $key => $value) {
                $message .= $value . ' <br> ';
            }
            responseWithError($message);
        } else {
            if (isset($data['employee_id']) && $data['employee_id'] != '') {
                $employee = Employee::find($data['employee_id']);
            } else {
                $employee = new Employee();
            }

            $employee->name = $data['name'];
            $employee->job_title = $data['job_title'];

            if (isset($data['file_link']) && $data['file_link'] != '') {
                $employee->image = $data['file_link'];
            }

            $employee->pay_frequency = $data['pay_frequency'];

            $employee->currency = $config['home_currency'];

            $amount = 0.0;

            if (isset($data['amount']) && $data['amount'] != '') {
                $amount = $data['amount'];
                $amount = createFromCurrency($amount, $config['home_currency']);
            }

            $employee->amount = $amount;

            if (isset($data['address']) && $data['address'] != '') {
                $employee->address_line_1 = $data['address'];
            }

            if (isset($data['email'])) {
                $employee->email = $data['email'];
            }

            if (isset($data['phone'])) {
                $employee->phone = $data['phone'];
            }

            if (isset($data['city'])) {
                $employee->city = $data['city'];
            }

            if (isset($data['state'])) {
                $employee->state = $data['state'];
            }
            if (isset($data['zip'])) {
                $employee->zip = $data['zip'];
            }

            if (isset($data['country'])) {
                $employee->country = $data['country'];
            }

            if (isset($data['summary'])) {
                $employee->summary = $data['summary'];
            }
            if (isset($data['facebook'])) {
                $employee->facebook = $data['facebook'];
            }

            if (isset($data['linkedin'])) {
                $employee->linkedin = $data['linkedin'];
            }
            if (isset($data['twitter'])) {
                $employee->twitter = $data['twitter'];
            }
            if (isset($data['date_hired'])) {
                $employee->date_hired = $data['date_hired'];
            }

            if (isset($data['department_id'])) {
                $employee->department_id = (int) $data['department_id'];
            }

            $employee->user_id = (int) $data['user_id'];

            $employee->save();

            echo "Success!";
        }

        break;

    case 'attendance':
        $date = route(2, date('Y-m-d'));
        $employees = Employee::all();

        $attendances = Attendance::where('date', $date)
            ->get()
            ->keyBy('employee_id')
            ->all();

        view('employee_attendance', [
            'date' => $date,
            'employees' => $employees,
            'attendances' => $attendances,
        ]);

        break;

    case 'set-attendance-note':
        $date = _post('date');
        $employee_id = _post('employee_id');
        $employee_id = str_replace('attendance_note_', '', $employee_id);
        $note = _post('note');

        $attendance = Attendance::where('date', $date)
            ->where('employee_id', $employee_id)
            ->first();

        if (!$attendance) {
            $attendance = new Attendance();
            $attendance->date = $date;
            $attendance->employee_id = $employee_id;
        }

        $attendance->note = $note;
        $attendance->save();

        break;

    case 'set-attendance':
        $date = _post('date');
        $employee_id = _post('employee_id');
        $employee_id = str_replace('attendance_', '', $employee_id);
        $present = _post('present');


        $present = $present == 'yes' ? 1 : 0;

        $attendance = Attendance::where('date', $date)
            ->where('employee_id', $employee_id)
            ->first();

        if (!$attendance) {
            $attendance = new Attendance();
            $attendance->date = $date;
            $attendance->employee_id = $employee_id;
        }
        $attendance->is_present = $present;
        $attendance->save();

        break;

    case 'payroll':
        $employees = Employee::all();
        $total = 0;

        foreach ($employees as $employee) {
            $total += $employee->amount;
        }
        view('employee_payroll', [
            'employees' => $employees,
            'total' => $total,
        ]);

        break;

    case 'run-payroll':
        $employees = Employee::all();

        view('employee_payroll_run', [
            'employees' => $employees,
        ]);

        break;

    case 'upload-employee-image':
        if (APP_STAGE == 'Demo') {
            exit();
        }

        $uploader = new Uploader();
        $uploader->setDir('storage/employees/');
        $uploader->sameName(false);
        $uploader->setExtensions(['jpg', 'jpeg', 'png', 'gif']); //allowed extensions list//
        if ($uploader->uploadFile('file')) {
            $uploaded = $uploader->getUploadName();

            $file = $uploaded;
            $msg = $_L['Uploaded Successfully'];
            $success = 'Yes';

            // create thumb

            $image = new Img();

            // indicate a source image (a GIF, PNG or JPEG file)
            $image->source_path = 'storage/employees/' . $file;

            // indicate a target image
            // note that there's no extra property to set in order to specify the target
            // image's type -simply by writing '.jpg' as extension will instruct the script
            // to create a 'jpg' file
            $image->target_path = 'storage/employees/thumb' . $file;

            // since in this example we're going to have a jpeg file, let's set the output
            // image's quality
            $image->jpeg_quality = 100;

            // some additional properties that can be set
            // read about them in the documentation
            $image->preserve_aspect_ratio = true;
            $image->enlarge_smaller_images = true;
            $image->preserve_time = true;

            // resize the image to exactly 100x100 pixels by using the "crop from center" method
            // (read more in the overview section or in the documentation)
            //  and if there is an error, check what the error is about
            if (!$image->resize(200, 200, ZEBRA_IMAGE_CROP_CENTER)) {
                // if no errors
            } else {
                // echo 'Success!';
            }
        } else {
            //upload failed
            $file = '';
            $msg = $uploader->getMessage();
            $success = 'No';
        }

        $a = [
            'success' => $success,
            'msg' => $msg,
            'file' => $file,
        ];

        header('Content-Type: application/json');

        echo json_encode($a);

        break;

    case 'modal_asset':
        view('modal_asset', []);

        break;

    case 'proficiencies':
        $proficiencies = [];

        view('hrm_proficiencies', [
            'proficiencies' => $proficiencies,
        ]);

        break;

    case 'departments':
        \view('hrm_departments', []);
        break;

    case 'test':
        add_option('employee_proficiencies', 1);

        break;

    case 'schema':
        $script =
            '<script>
    $(function() {
        var delay = 10000;
        var $serverResponse = $("#serverResponse");
        var interval = setInterval(function(){
   $serverResponse.append(\'.\');
}, 500);
        
        setTimeout(function(){ window.location = \'' .
            U .
            'hrm/employees\'; }, delay);
    });
</script>';

        if (db_table_exist('employees')) {
            HtmlCanvas::createTerminal('Already updated!', $script);
            exit();
        }

        $message = 'Updating scehma to support HRM... ' . PHP_EOL;

        if (!db_table_exist('employees')) {
            ORM::execute('CREATE TABLE `employees` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `job_title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `date_hired` date DEFAULT NULL,
  `department_id` int(10) unsigned DEFAULT NULL,
  `manager_id` int(10) unsigned DEFAULT NULL,
  `image` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `pay_frequency` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `currency` char(3) COLLATE utf8mb4_unicode_ci NOT NULL,
  `amount` decimal(16,8) NOT NULL,
  `employee_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `legal_name_title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `legal_name_first` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `legal_name_mi` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `legal_name_last` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `banking_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `ssn` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `gender` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `date_of_birht` date DEFAULT NULL,
  `marital_status` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `is_citizen` tinyint(1) NOT NULL DEFAULT \'1\',
  `ethnicity` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `has_i9_form` tinyint(1) DEFAULT NULL,
  `work_authorization_expires` date DEFAULT NULL,
  `address_line_1` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `address_line_2` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `city` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `state` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `zip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `country` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `phone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `work_phone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `work_mobile` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `work_fax` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `cc_email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `other` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `emergency_contact_name_1` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `emergency_contact_phone_1` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `emergency_contact_relation_1` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `emergency_contact_name_2` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `emergency_contact_phone_2` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `emergency_contact_relation_2` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `last_day_worked` date DEFAULT NULL,
  `last_day_on_benefits` date DEFAULT NULL,
  `last_day_on_payroll` date DEFAULT NULL,
  `termination_type` date DEFAULT NULL,
  `termination_reason` date DEFAULT NULL,
  `is_recommended` tinyint(1) DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT \'1\',
  `facebook` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `google` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `linkedin` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `skype` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `twitter` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `summary` text COLLATE utf8mb4_unicode_ci,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci');
        }

        if (!db_table_exist('attendances')) {
            ORM::execute('CREATE TABLE `attendances` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `note` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `employee_id` int(10) unsigned NOT NULL,
  `date` date NOT NULL,
  `is_present` tinyint(1) NOT NULL DEFAULT \'1\',
  `total_time` int(10) unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci');
        }

        if (!db_table_exist('expertise')) {
            ORM::execute('');
        }

        $message .= 'Tables were created...' . PHP_EOL;

        $message .= '---------------------------' . PHP_EOL;
        $message .= 'Redirecting, please wait...';

        HtmlCanvas::createTerminal($message, $script);

        break;

    default:
        echo 'action not defined';
}