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]>
166 lines
3.8 KiB
PHP
166 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Billing\Domain\Entities;
|
|
|
|
use App\Modules\Billing\Domain\Enums\MergeRefusal;
|
|
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
|
|
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
|
|
use App\Modules\Billing\Domain\ValueObjects\InvoiceTotals;
|
|
|
|
/**
|
|
* An invoice of a billing material: one or more jobs of the same customer
|
|
* and billing unit, with at most one customer reference among them (spec
|
|
* 12). Its lines are formed when the material is created.
|
|
*/
|
|
final class BillingInvoice
|
|
{
|
|
/**
|
|
* @param list<DraftJob> $jobs
|
|
* @param list<InvoiceLine> $lines
|
|
*/
|
|
private function __construct(
|
|
private array $jobs,
|
|
private array $lines = [],
|
|
) {}
|
|
|
|
public static function for(DraftJob $job): self
|
|
{
|
|
return new self([$job]);
|
|
}
|
|
|
|
/**
|
|
* @param list<DraftJob> $jobs
|
|
* @param list<InvoiceLine> $lines
|
|
*/
|
|
public static function reconstitute(array $jobs, array $lines = []): self
|
|
{
|
|
return new self($jobs, $lines);
|
|
}
|
|
|
|
/**
|
|
* Why the job may not join this invoice, or null when it may. The job
|
|
* itself is left out of the comparison.
|
|
*/
|
|
public function refusalFor(DraftJob $job): ?MergeRefusal
|
|
{
|
|
foreach ($this->jobs as $other) {
|
|
if ($other->jobId === $job->jobId) {
|
|
continue;
|
|
}
|
|
|
|
if ($other->customerId !== $job->customerId) {
|
|
return MergeRefusal::DifferentCustomer;
|
|
}
|
|
|
|
if ($other->billingUnitId !== $job->billingUnitId) {
|
|
return MergeRefusal::DifferentBillingUnit;
|
|
}
|
|
|
|
if ($other->reference !== null && $job->reference !== null && $other->reference !== $job->reference) {
|
|
return MergeRefusal::DifferentReference;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function add(DraftJob $job): void
|
|
{
|
|
$this->jobs[] = $job;
|
|
}
|
|
|
|
public function remove(int $jobId): void
|
|
{
|
|
$this->jobs = array_values(array_filter($this->jobs, fn (DraftJob $job): bool => $job->jobId !== $jobId));
|
|
}
|
|
|
|
public function replace(DraftJob $job): void
|
|
{
|
|
$this->jobs = array_map(fn (DraftJob $current): DraftJob => $current->jobId === $job->jobId ? $job : $current, $this->jobs);
|
|
}
|
|
|
|
public function has(int $jobId): bool
|
|
{
|
|
return $this->job($jobId) !== null;
|
|
}
|
|
|
|
public function job(int $jobId): ?DraftJob
|
|
{
|
|
foreach ($this->jobs as $job) {
|
|
if ($job->jobId === $jobId) {
|
|
return $job;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function isEmpty(): bool
|
|
{
|
|
return $this->jobs === [];
|
|
}
|
|
|
|
/**
|
|
* @param list<InvoiceLine> $lines
|
|
*/
|
|
public function setLines(array $lines): void
|
|
{
|
|
$this->lines = $lines;
|
|
}
|
|
|
|
/**
|
|
* @return list<DraftJob>
|
|
*/
|
|
public function jobs(): array
|
|
{
|
|
return $this->jobs;
|
|
}
|
|
|
|
/**
|
|
* @return list<InvoiceLine>
|
|
*/
|
|
public function lines(): array
|
|
{
|
|
return $this->lines;
|
|
}
|
|
|
|
/**
|
|
* The first job identifies the invoice on the page.
|
|
*/
|
|
public function key(): int
|
|
{
|
|
return $this->jobs[0]->jobId;
|
|
}
|
|
|
|
public function customerId(): int
|
|
{
|
|
return $this->jobs[0]->customerId;
|
|
}
|
|
|
|
public function billingUnitId(): ?int
|
|
{
|
|
return $this->jobs[0]->billingUnitId;
|
|
}
|
|
|
|
/**
|
|
* The one customer reference of its jobs, if any.
|
|
*/
|
|
public function reference(): ?string
|
|
{
|
|
foreach ($this->jobs as $job) {
|
|
if ($job->reference !== null) {
|
|
return $job->reference;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function totals(): InvoiceTotals
|
|
{
|
|
return InvoiceTotals::of($this->lines);
|
|
}
|
|
}
|