Files
Toiminnanohjaus/app/Modules/Billing/Application/UseCases/FinalizeBillingExport.php
T
petriandClaude Opus 5.5 97bd23bed5 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]>
2026-11-24 13:08:24 +02:00

86 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Application\Contracts\BillingExpenses;
use App\Modules\Billing\Application\Contracts\BillingHours;
use App\Modules\Billing\Application\Contracts\BillingJobs;
use App\Modules\Billing\Application\DTOs\BillableExpense;
use App\Modules\Billing\Application\Services\BillingSnapshot;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Enums\NotBillableReason;
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
use App\Modules\Billing\Domain\Exceptions\ExportEmpty;
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Creates the billing material from its draft (spec 12), all or nothing:
* checks every job again, marks the approved hours and billable expenses
* billed, forms the invoice lines from what was marked with the current
* prices, and moves the jobs to "Lasku luotu".
*/
final class FinalizeBillingExport
{
public function __construct(
private BillingExportRepository $exports,
private BillingJobs $jobs,
private BillingHours $hours,
private BillingExpenses $expenses,
private BillingSnapshot $snapshots,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @return int How many invoices the material has.
*
* @throws BillingExportNotFound
* @throws ExportNotDraft
* @throws ExportEmpty
* @throws JobNotBillable
*/
public function execute(int $exportId, int $actorUserId): int
{
return $this->transactions->run(function () use ($exportId, $actorUserId): int {
$export = $this->exports->findById($exportId, lock: true) ?? throw new BillingExportNotFound($exportId);
$export->ensureDraft();
$snapshot = $this->snapshots->load($export->jobIds());
foreach ($export->jobIds() as $jobId) {
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
$reason = $snapshot->reasonNotBillable($job);
if ($reason !== null) {
throw new JobNotBillable($jobId, $reason);
}
}
$linesByJob = [];
foreach ($export->jobs() as $draftJob) {
$job = $snapshot->jobs[$draftJob->jobId];
$billedHours = $this->hours->markBilled($job->id, $exportId);
$billedExpenseIds = array_flip($this->expenses->markBilled($job->id, $exportId));
$billedExpenses = array_values(array_filter(
$snapshot->expensesOf($job->id),
fn (BillableExpense $expense): bool => isset($billedExpenseIds[$expense->id]),
));
$linesByJob[$job->id] = $snapshot->rules->lines($job, $draftJob, $billedHours, $billedExpenses, $snapshot->isFixedPriceBilled($job->id), $snapshot->rates, $snapshot->vatBasisPoints);
$this->jobs->markInvoiced($job->id, $actorUserId);
}
$export->finalize($linesByJob, $actorUserId, $this->clock->now());
$this->exports->save($export);
return count($export->invoices());
});
}
}