Files
ladill-care/app/Models/StaffAssignment.php
T
isaaccladandCursor 3735be3425
Deploy Ladill Care / deploy (push) Successful in 1m8s
Add Care Units, beds, and dated staff assignments.
Models employment as department/unit/shift placements with primary and temporary kinds so nurses are not permanently bound to a ward.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-20 10:04:39 +00:00

96 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\BelongsToOwner;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class StaffAssignment extends Model
{
use BelongsToOwner, SoftDeletes;
protected $table = 'care_staff_assignments';
protected $fillable = [
'owner_ref',
'organization_id',
'member_id',
'department_id',
'care_unit_id',
'kind',
'assignment_role',
'shift_code',
'starts_on',
'ends_on',
'status',
'notes',
];
protected function casts(): array
{
return [
'starts_on' => 'date',
'ends_on' => 'date',
];
}
public function organization(): BelongsTo
{
return $this->belongsTo(Organization::class, 'organization_id');
}
public function member(): BelongsTo
{
return $this->belongsTo(Member::class, 'member_id');
}
public function department(): BelongsTo
{
return $this->belongsTo(Department::class, 'department_id');
}
public function careUnit(): BelongsTo
{
return $this->belongsTo(CareUnit::class, 'care_unit_id');
}
public function scopeEffectiveOn(Builder $query, CarbonInterface|string|null $date = null): Builder
{
$day = $date
? (\Illuminate\Support\Carbon::parse($date)->toDateString())
: now()->toDateString();
return $query
->whereDate('starts_on', '<=', $day)
->where(function (Builder $q) use ($day) {
$q->whereNull('ends_on')->orWhereDate('ends_on', '>=', $day);
});
}
public function scopeActiveStatus(Builder $query): Builder
{
return $query->where('status', 'active');
}
public function isEffectiveOn(CarbonInterface|string|null $date = null): bool
{
$day = $date
? \Illuminate\Support\Carbon::parse($date)->startOfDay()
: now()->startOfDay();
if ($this->starts_on->gt($day)) {
return false;
}
if ($this->ends_on !== null && $this->ends_on->lt($day)) {
return false;
}
return $this->status === 'active';
}
}