Add approval of hours and lock opening for management
- Time entries store who approved them and when. Approved hours are locked: cells, task page edits and removals are refused. - "Hyväksy viikko" in the weekly view approves the user's own week; subcontractors' hours are approved by management. - Management gets an approval page (unapproved weeks by person, recently approved weeks) and can open anyone's week read-only to approve it or cancel its approval, which only management may do. - Only management may move a done job back to an earlier status, which opens the lock on its hours and expenses; the history marks it. - The Clock contract gets now() for approval times. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -19,9 +19,11 @@ use Illuminate\Database\Eloquent\Model;
|
||||
* @property CarbonImmutable $worked_on
|
||||
* @property string $hours
|
||||
* @property string|null $note
|
||||
* @property CarbonImmutable|null $approved_at
|
||||
* @property int|null $approved_by
|
||||
*/
|
||||
#[Table('time_entries')]
|
||||
#[Fillable(['user_id', 'task_id', 'customer_job_id', 'work_type_id', 'worked_on', 'hours', 'note'])]
|
||||
#[Fillable(['user_id', 'task_id', 'customer_job_id', 'work_type_id', 'worked_on', 'hours', 'note', 'approved_at', 'approved_by'])]
|
||||
#[UseFactory(TimeEntryFactory::class)]
|
||||
class TimeEntryModel extends Model
|
||||
{
|
||||
@@ -39,6 +41,8 @@ class TimeEntryModel extends Model
|
||||
'customer_job_id' => 'integer',
|
||||
'work_type_id' => 'integer',
|
||||
'worked_on' => 'immutable_date',
|
||||
'approved_at' => 'immutable_datetime',
|
||||
'approved_by' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
+87
-4
@@ -4,15 +4,22 @@ namespace App\Modules\TimeTracking\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||
use App\Modules\TimeTracking\Application\Contracts\TimeEntryQueries;
|
||||
use App\Modules\TimeTracking\Application\DTOs\ApprovalWeek;
|
||||
use App\Modules\TimeTracking\Application\DTOs\TimeEntrySummary;
|
||||
use App\Modules\TimeTracking\Application\DTOs\WeekApproval;
|
||||
use App\Modules\TimeTracking\Application\DTOs\WeekRow;
|
||||
use App\Modules\TimeTracking\Application\DTOs\WeekSheet;
|
||||
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class EloquentTimeEntryQueries implements TimeEntryQueries
|
||||
{
|
||||
public function __construct(private TaskQueries $tasks) {}
|
||||
public function __construct(
|
||||
private TaskQueries $tasks,
|
||||
private UserAccountQueries $users,
|
||||
) {}
|
||||
|
||||
public function week(int $userId, string $monday, array $extraTaskIds = []): WeekSheet
|
||||
{
|
||||
@@ -34,8 +41,13 @@ class EloquentTimeEntryQueries implements TimeEntryQueries
|
||||
)));
|
||||
|
||||
$byTask = [];
|
||||
$approvedByTask = [];
|
||||
foreach ($entries as $entry) {
|
||||
$byTask[$entry->task_id][$entry->worked_on->toDateString()] = $entry->quarters();
|
||||
|
||||
if ($entry->approved_at !== null) {
|
||||
$approvedByTask[$entry->task_id][] = $entry->worked_on->toDateString();
|
||||
}
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
@@ -48,10 +60,12 @@ class EloquentTimeEntryQueries implements TimeEntryQueries
|
||||
$dayTotals[$day] += $value;
|
||||
}
|
||||
|
||||
$rows[] = new WeekRow($task, $quarters, array_sum($quarters));
|
||||
$rows[] = new WeekRow($task, $quarters, array_sum($quarters), $approvedByTask[$task->taskId] ?? []);
|
||||
}
|
||||
|
||||
return new WeekSheet($days, $rows, $dayTotals, array_sum($dayTotals));
|
||||
$unapproved = $entries->whereNull('approved_at')->sum(fn (TimeEntryModel $entry): int => $entry->quarters());
|
||||
|
||||
return new WeekSheet($days, $rows, $dayTotals, array_sum($dayTotals), (int) $unapproved, $this->latestApproval($entries));
|
||||
}
|
||||
|
||||
public function entriesForTask(int $taskId, int $userId): array
|
||||
@@ -61,7 +75,7 @@ class EloquentTimeEntryQueries implements TimeEntryQueries
|
||||
->where('user_id', $userId)
|
||||
->orderByDesc('worked_on')
|
||||
->get()
|
||||
->map(fn (TimeEntryModel $model): TimeEntrySummary => new TimeEntrySummary($model->id, $model->worked_on->toDateString(), $model->quarters(), $model->note))
|
||||
->map(fn (TimeEntryModel $model): TimeEntrySummary => new TimeEntrySummary($model->id, $model->worked_on->toDateString(), $model->quarters(), $model->note, $model->approved_at !== null))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
@@ -70,4 +84,73 @@ class EloquentTimeEntryQueries implements TimeEntryQueries
|
||||
{
|
||||
return TimeEntryModel::query()->where('task_id', $taskId)->exists();
|
||||
}
|
||||
|
||||
public function pendingWeeks(?int $userId = null): array
|
||||
{
|
||||
$entries = TimeEntryModel::query()
|
||||
->whereNull('approved_at')
|
||||
->when($userId !== null, fn ($query) => $query->where('user_id', $userId))
|
||||
->orderBy('worked_on')
|
||||
->get();
|
||||
|
||||
return $this->byUserAndWeek($entries, withApproval: false, newestFirst: false);
|
||||
}
|
||||
|
||||
public function approvedWeeks(string $sinceMonday): array
|
||||
{
|
||||
$entries = TimeEntryModel::query()
|
||||
->whereNotNull('approved_at')
|
||||
->where('worked_on', '>=', $sinceMonday)
|
||||
->get();
|
||||
|
||||
return $this->byUserAndWeek($entries, withApproval: true, newestFirst: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, TimeEntryModel> $entries
|
||||
* @return list<ApprovalWeek>
|
||||
*/
|
||||
private function byUserAndWeek(Collection $entries, bool $withApproval, bool $newestFirst): array
|
||||
{
|
||||
$names = $this->users->namesByIds($entries->pluck('user_id')->unique()->values()->all());
|
||||
$weeks = $entries
|
||||
->groupBy(fn (TimeEntryModel $entry): string => $entry->user_id.'|'.$entry->worked_on->startOfWeek()->toDateString())
|
||||
->map(function (Collection $group, string $key) use ($names, $withApproval): ApprovalWeek {
|
||||
[$userId, $monday] = explode('|', $key);
|
||||
|
||||
return new ApprovalWeek(
|
||||
(int) $userId,
|
||||
$names[(int) $userId] ?? '',
|
||||
$monday,
|
||||
(int) $group->sum(fn (TimeEntryModel $entry): int => $entry->quarters()),
|
||||
$group->count(),
|
||||
$withApproval ? $this->latestApproval($group) : null,
|
||||
);
|
||||
})
|
||||
->sortBy([
|
||||
fn (ApprovalWeek $a, ApprovalWeek $b): int => $newestFirst ? $b->monday <=> $a->monday : $a->monday <=> $b->monday,
|
||||
fn (ApprovalWeek $a, ApprovalWeek $b): int => $a->userName <=> $b->userName,
|
||||
]);
|
||||
|
||||
return $weeks->values()->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, TimeEntryModel> $entries
|
||||
*/
|
||||
private function latestApproval(Collection $entries): ?WeekApproval
|
||||
{
|
||||
/** @var TimeEntryModel|null $latest */
|
||||
$latest = $entries->whereNotNull('approved_at')->sortByDesc('approved_at')->first();
|
||||
|
||||
if ($latest === null || $latest->approved_at === null || $latest->approved_by === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new WeekApproval(
|
||||
$latest->approved_by,
|
||||
$this->users->namesByIds([$latest->approved_by])[$latest->approved_by] ?? '',
|
||||
$latest->approved_at->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -40,11 +40,25 @@ class EloquentTimeEntryRepository implements TimeEntryRepository
|
||||
'worked_on' => $entry->workedOn()->format('Y-m-d'),
|
||||
'hours' => $entry->hours()->decimal(),
|
||||
'note' => $entry->note(),
|
||||
'approved_by' => $entry->approvedBy(),
|
||||
'approved_at' => $entry->approvedAt()?->format('Y-m-d H:i:s'),
|
||||
])->save();
|
||||
|
||||
return $this->toEntity($model->refresh());
|
||||
}
|
||||
|
||||
public function forUserBetween(int $userId, DateTimeImmutable $from, DateTimeImmutable $to): array
|
||||
{
|
||||
return TimeEntryModel::query()
|
||||
->where('user_id', $userId)
|
||||
->whereBetween('worked_on', [$from->format('Y-m-d'), $to->format('Y-m-d')])
|
||||
->orderBy('worked_on')
|
||||
->get()
|
||||
->map(fn (TimeEntryModel $model): TimeEntry => $this->toEntity($model))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function delete(TimeEntry $entry): void
|
||||
{
|
||||
TimeEntryModel::query()->whereKey($entry->id())->delete();
|
||||
@@ -70,6 +84,8 @@ class EloquentTimeEntryRepository implements TimeEntryRepository
|
||||
DateTimeImmutable::createFromFormat('!Y-m-d', $model->worked_on->format('Y-m-d')) ?: new DateTimeImmutable($model->worked_on->format('Y-m-d')),
|
||||
new WorkedHours($model->quarters()),
|
||||
$model->note,
|
||||
$model->approved_by,
|
||||
$model->approved_at === null ? null : DateTimeImmutable::createFromInterface($model->approved_at),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,14 @@ class TimeTrackingServiceProvider extends ServiceProvider
|
||||
|
||||
Gate::define('time.use', fn (User $user): bool => true);
|
||||
|
||||
// Users approve their own hours; management anyone's, including subcontractors' (spec 9).
|
||||
Gate::define('time.approve', fn (User $user, int $ownerId): bool => $user->role->seesAllMoney()
|
||||
|| ($user->role !== Role::Subcontractor && $user->id === $ownerId));
|
||||
|
||||
Gate::define('time.approve-others', fn (User $user): bool => $user->role->seesAllMoney());
|
||||
|
||||
Gate::define('time.cancel-approval', fn (User $user): bool => $user->role->seesAllMoney());
|
||||
|
||||
// Hours go on any task of an unlocked job; subcontractors only on jobs they are attached to (K12).
|
||||
Gate::define('time.book', fn (User $user, int $jobId): bool => $user->role !== Role::Subcontractor
|
||||
|| app(CustomerJobQueries::class)->isMember($jobId, $user->id));
|
||||
|
||||
Reference in New Issue
Block a user