Files
jandl-taskmanager-app/be/app/Services/Intranet/Vacation.php
2023-12-12 15:24:10 +01:00

102 lines
3.6 KiB
PHP

<?php
namespace App\Services\Intranet;
use App\Models\User;
use Carbon\Carbon;
class Vacation extends Api {
public $intranet_id;
public $user_id;
public $start_date;
public $end_date;
public $type;
public $name;
public $approved;
public function __construct($intranet_id, $ldap_username, $start_date, $end_date, $type, $halfday, $approved) {
$startend = $this->getStartEndDate($start_date, $end_date, $halfday, $ldap_username);
$this->intranet_id = $intranet_id;
$this->user_id = $this->getUserId($ldap_username);
$this->start_date = (isset($startend['start'])) ? $startend['start'] : null;
$this->end_date = (isset($startend['end'])) ? $startend['end'] : null;
$this->type = $type;
$this->name = $this->getVacationName($type);
$this->approved = $approved;
}
public static function fromDate($date) {
$vacations = self::getVacations($date);
return collect(array_map(function($vacation) {
return new Vacation( $vacation['id'], $vacation['user']['ldap_username'], $vacation['start_date'], $vacation['end_date'], $vacation['vacation_type']['name'], $vacation['halfday_position'], $vacation['approved']);
}, $vacations))->reject(function($vacation) {
return $vacation->user_id === null;
});
}
public function toArray() {
return [
'intranet_id' => $this->intranet_id,
'user_id' => $this->user_id,
'start' => date('Y-m-d H:i:s', strtotime($this->start_date)),
'end' => date('Y-m-d H:i:s', strtotime($this->end_date)),
'type' => $this->type,
'name' => $this->name,
'approved' => $this->approved,
];
}
private function getVacationName($vacation_type) {
switch ($vacation_type) {
case 'home_office':
return 'Home Office';
case 'vacation':
return 'Vacation';
case 'free_day':
return 'Free day';
default:
return 'Other';
}
}
private function getUserId($ldap_username) {
$user = User::where('ldap_username', $ldap_username)->first();
return ($user) ? $user->id : null;
}
// vyratanie zaciatku a konca dovolenky na zaklade toho, ci sa jedna o cely den alebo polden a na zaklade toho, co ma user zadane v pracovnej dobe
private function getStartEndDate($start_date, $end_date, $halfday, $ldap_username) {
$user = User::where('ldap_username', $ldap_username)->first();
if (!$user) return null;
$workStart = $user->work_start;
$workEnd = $user->work_end;
// convert hour integer number to time hours
$workStart = sprintf('%02d:%02d', (int) $workStart, fmod($workStart, 1) * 60);
$workEnd = sprintf('%02d:%02d', (int) $workEnd, fmod($workEnd, 1) * 60);
if ($halfday == 'start') {
$start = $start_date . ' ' . $workStart;
$end = date('Y-m-d H:i:s', strtotime($start . ' + 4 hours'));
return ['start' => $start, 'end' => $end];
} else if ($halfday == 'end') {
$end = $end_date . ' ' . $workEnd;
$start = date('Y-m-d H:i:s', strtotime($end . ' - 4 hours'));
return ['start' => $start, 'end' => $end];
} else if ($halfday == null) {
$start = date('Y-m-d H:i:s', strtotime($start_date . ' ' . $workStart));
$end = date('Y-m-d H:i:s', strtotime($end_date . ' ' . $workEnd));
return ['start' => $start, 'end' => $end];
}
}
}