Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.7 KiB

3 years ago
<?php
namespace App\Models;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use Carbon\Carbon;
10 months ago
class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
3 years ago
{
use Authenticatable, Authorizable, HasFactory;
protected $table = 'm_users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'role_id', 'username', 'session_login', 'phone_number', 'email', 'address',
'fcm_token', 'gender', 'birth_place', 'birth_date', 'blood_type', 'ktp_number', 'working_hours',
10 months ago
'created_at', 'created_by', 'updated_at', 'updated_by', 'status_resource', 'discount_id'
3 years ago
];
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
const DEFAULT_TZ = 'Asia/Jakarta';
const INSIDE = "INSIDE";
const OUTSIDE = "OUTSIDE";
const HOLIDAY = "HOLIDAY";
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password',
];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
/**
* Get working hours for given timestamp
*
* @return array of carbon or NULL in case HOLIDAY
*/
10 months ago
public function getWorkingTime(Carbon $ts, $f = "08:00", $t = "17:00", $tz = self::DEFAULT_TZ)
3 years ago
{
$workingTime = array(
10 months ago
"from" => Carbon::createFromTimeString($f, $tz),
"to" => Carbon::createFromTimeString($t, $tz)
3 years ago
);
10 months ago
$userShift = UserShift::where('user_id',)
3 years ago
->orderByDesc('from_date')
->first();
$shift = null;
10 months ago
if ($userShift !== null) {
3 years ago
$shiftId = null;
10 months ago
switch ($ts->shortEnglishDayOfWeek) {
case "Mon":
$shiftId = $userShift->mon_shift_id;
break;
case "Tue":
$shiftId = $userShift->tue_shift_id;
break;
case "Wed":
$shiftId = $userShift->wed_shift_id;
break;
case "Thu":
$shiftId = $userShift->thu_shift_id;
break;
case "Fri":
$shiftId = $userShift->fri_shift_id;
break;
case "Sat":
$shiftId = $userShift->sat_shift_id;
break;
case "Sun":
$shiftId = $userShift->sun_shift_id;
break;
3 years ago
}
if ($shiftId === null) {
return null;
}
$shift = Shift::where('id', $shiftId)->first();
} else {
$shift = Shift::where('is_non_shift', true)
->orderByDesc('created_at')
->first();
}
10 months ago
if ($shift !== null) {
3 years ago
$from = Carbon::createFromTimeString($shift->start_time, $tz)
->subMinutes($shift->flex_time_minute);
$to = Carbon::createFromTimeString($shift->end_time, $tz)
->addMinutes($shift->flex_time_minute);
/*
if ($to->lessThan($from))
{
$to->addDay();
}
*/
$workingTime['from'] = $from;
$workingTime['to'] = $to;
}
return $workingTime;
}
/**
* Get presence status
*/
10 months ago
public function presenceStatus(Carbon $at = null, $tz = self::DEFAULT_TZ)
3 years ago
{
$ts = $at;
10 months ago
if ($at !== null) {
3 years ago
$ts = Carbon::now($tz);
}
$tsSec = $ts->secondsSinceMidnight();
$status = "";
$wt = $this->getWorkingTime($ts, $tz);
10 months ago
if ($wt === null) {
3 years ago
$status = self::HOLIDAY;
} else {
$from = $wt->from;
$to = $wt->to;
$tsFrom = $from->secondsSinceMidnight();
$tsTo = $to->secondsSinceMidnight();
if ($from->greaterThan($to)) {
10 months ago
$tsMid = 24 * 60 * 60 - $tsFrom;
3 years ago
if ($tsSec >= $tsFrom || $tsSec < $tsTo) {
$status = self::INSIDE;
} else {
$status = self::OUTSIDE;
}
} else {
if ($tsSec < $tsFrom || $tsSec > $tsTo) {
$status = self::OUTSIDE;
} else {
$status = self::INSIDE;
}
}
}
$clockIn = null;
$clockOut = null;
$inout = Presence::where('user_id', $this->id)
->orderByDesc('clock_in')
->first();
if ($inout !== null) {
$clockIn = Carbon::parse($inout->clock_in, $tz);
$clockOut = Carbon::parse($inout->clock_out, $tz);
}
return array(
10 months ago
"status" => $status,
"ts" => $ts,
3 years ago
"clock_in" => $clockIn,
"clock_out" => $clockOut
);
10 months ago
}
3 years ago
}