Add the billing material with drafts, combined invoices and billed marks
Management builds the monthly billing material (laskuaineisto) as a draft: done jobs are added as invoices of their own, combined when the customer, billing unit and reference allow it, and their billable hours are set per work type. Creating the material checks the jobs again, marks the approved hours and billable expenses billed, forms invoice lines with price and VAT snapshots and moves the jobs to "Lasku luotu". Fixed price jobs bill their quoted amount once. Billed hours keep their approval and billed expenses can no longer change. The Fennoa export and monthly fee lines come later. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+29
@@ -5,6 +5,7 @@ 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\JobBillingHours;
|
||||
use App\Modules\TimeTracking\Application\DTOs\TimeEntrySummary;
|
||||
use App\Modules\TimeTracking\Application\DTOs\WeekApproval;
|
||||
use App\Modules\TimeTracking\Application\DTOs\WeekRow;
|
||||
@@ -68,6 +69,34 @@ class EloquentTimeEntryQueries implements TimeEntryQueries
|
||||
return new WeekSheet($days, $rows, $dayTotals, array_sum($dayTotals), (int) $unapproved, $this->latestApproval($entries));
|
||||
}
|
||||
|
||||
public function billingHours(array $jobIds): array
|
||||
{
|
||||
$approved = array_fill_keys($jobIds, []);
|
||||
$unapproved = array_fill_keys($jobIds, 0);
|
||||
|
||||
if ($jobIds !== []) {
|
||||
$entries = TimeEntryModel::query()
|
||||
->whereIn('customer_job_id', $jobIds)
|
||||
->whereNull('billing_export_id')
|
||||
->get(['customer_job_id', 'work_type_id', 'hours', 'approved_at']);
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
if ($entry->approved_at === null) {
|
||||
$unapproved[$entry->customer_job_id] += $entry->quarters();
|
||||
} else {
|
||||
$approved[$entry->customer_job_id][$entry->work_type_id] = ($approved[$entry->customer_job_id][$entry->work_type_id] ?? 0) + $entry->quarters();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($jobIds as $jobId) {
|
||||
$result[$jobId] = new JobBillingHours($jobId, $approved[$jobId], $unapproved[$jobId]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function entriesForTask(int $taskId, int $userId): array
|
||||
{
|
||||
return TimeEntryModel::query()
|
||||
|
||||
+16
@@ -42,6 +42,7 @@ class EloquentTimeEntryRepository implements TimeEntryRepository
|
||||
'note' => $entry->note(),
|
||||
'approved_by' => $entry->approvedBy(),
|
||||
'approved_at' => $entry->approvedAt()?->format('Y-m-d H:i:s'),
|
||||
'billing_export_id' => $entry->billingExportId(),
|
||||
])->save();
|
||||
|
||||
return $this->toEntity($model->refresh());
|
||||
@@ -64,6 +65,20 @@ class EloquentTimeEntryRepository implements TimeEntryRepository
|
||||
TimeEntryModel::query()->whereKey($entry->id())->delete();
|
||||
}
|
||||
|
||||
public function approvedUnbilledForJob(int $jobId): array
|
||||
{
|
||||
return TimeEntryModel::query()
|
||||
->where('customer_job_id', $jobId)
|
||||
->whereNotNull('approved_at')
|
||||
->whereNull('billing_export_id')
|
||||
->orderBy('worked_on')
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->map(fn (TimeEntryModel $model): TimeEntry => $this->toEntity($model))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function quartersOn(int $userId, DateTimeImmutable $day, ?int $exceptEntryId = null): int
|
||||
{
|
||||
return (int) round((float) TimeEntryModel::query()
|
||||
@@ -86,6 +101,7 @@ class EloquentTimeEntryRepository implements TimeEntryRepository
|
||||
$model->note,
|
||||
$model->approved_by,
|
||||
$model->approved_at === null ? null : DateTimeImmutable::createFromInterface($model->approved_at),
|
||||
$model->billing_export_id,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user