$jobs * @param list $lines */ private function __construct( private array $jobs, private array $lines = [], ) {} public static function for(DraftJob $job): self { return new self([$job]); } /** * @param list $jobs * @param list $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 $lines */ public function setLines(array $lines): void { $this->lines = $lines; } /** * @return list */ public function jobs(): array { return $this->jobs; } /** * @return list */ 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); } }