Files
Toiminnanohjaus/app/Modules/Billing/Application/Services/BillingSnapshot.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

107 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Billing\Application\Services;
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\Contracts\BillingPrices;
use App\Modules\Billing\Application\DTOs\BillableExpense;
use App\Modules\Billing\Application\DTOs\BillableJob;
use App\Modules\Billing\Application\DTOs\HourlyRate;
use App\Modules\Billing\Application\DTOs\JobHours;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Enums\NotBillableReason;
/**
* What the given jobs have to bill right now, read once for a page or a
* use case: the jobs, their unbilled hours and expenses, whose fixed price
* is already billed, the hourly rates and the VAT rate.
*/
final class BillingSnapshot
{
/** @var array<int, BillableJob> */
public array $jobs = [];
/** @var array<int, JobHours> */
public array $hours = [];
/** @var array<int, list<BillableExpense>> */
public array $expenses = [];
/** @var array<int, true> */
public array $fixedPriceBilled = [];
/** @var array<int, HourlyRate> */
public array $rates = [];
public int $vatBasisPoints = 0;
public function __construct(
private BillingJobs $billingJobs,
private BillingHours $billingHours,
private BillingExpenses $billingExpenses,
private BillingPrices $prices,
private BillingExportRepository $exports,
public readonly BillingRules $rules,
) {}
/**
* @param list<int> $jobIds
* @param list<BillableJob> $knownJobs Jobs already read, e.g. the done ones.
*/
public function load(array $jobIds, array $knownJobs = []): self
{
$snapshot = clone $this;
foreach ($knownJobs as $job) {
$snapshot->jobs[$job->id] = $job;
}
$missing = array_values(array_diff($jobIds, array_keys($snapshot->jobs)));
$snapshot->jobs += $this->billingJobs->find($missing);
$ids = array_keys($snapshot->jobs);
$snapshot->hours = $this->billingHours->forJobs($ids);
$snapshot->expenses = $this->billingExpenses->forJobs($ids);
$snapshot->fixedPriceBilled = array_fill_keys($this->exports->fixedPriceBilledJobIds($ids), true);
$snapshot->rates = $this->prices->hourlyRates();
$snapshot->vatBasisPoints = $this->prices->defaultVatBasisPoints();
return $snapshot;
}
public function hoursOf(int $jobId): JobHours
{
return $this->hours[$jobId] ?? new JobHours($jobId);
}
/**
* @return list<BillableExpense>
*/
public function expensesOf(int $jobId): array
{
return $this->expenses[$jobId] ?? [];
}
public function isFixedPriceBilled(int $jobId): bool
{
return isset($this->fixedPriceBilled[$jobId]);
}
public function reasonNotBillable(BillableJob $job): ?NotBillableReason
{
return $this->rules->reasonNotBillable($job, $this->hoursOf($job->id), $this->expensesOf($job->id), $this->isFixedPriceBilled($job->id), $this->rates);
}
/**
* @return list<string>
*/
public function missingRateNames(BillableJob $job): array
{
return $this->rules->missingRateNames($this->hoursOf($job->id), $this->rates);
}
}