Files
Toiminnanohjaus/app/Modules/Billing/Domain/Entities/BillingInvoice.php
petriandClaude Opus 5.5 5f9423a3c3 Add monthly fee billing lines to the billing material
Jobs billed by a fixed monthly fee get a monthly fee field, kept behind
the quotes permission like the quoted amount. A billing material now has
a billing month: a new draft adds every monthly fee job with something
to bill, each on an invoice of its own, and bills one line for every
unbilled month from the job's start up to the billing month (or its end
or completion), so skipped months are caught up and no month is billed
twice. Their expenses are billed alongside, their hours are covered by
the fee and marked billed, and the job keeps running.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 13:23:46 +02:00

170 lines
3.9 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->ownInvoice || $job->ownInvoice) {
return MergeRefusal::MonthlyFeeAlone;
}
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);
}
}