- Company details get an optional "Valikon nimi", shown at the left of the main menu; it falls back to the application name when empty. - A billing material gets its running number (billing_exports.number) only when it is created, so discarded drafts leave no gaps. Drafts have no number; existing created materials are numbered by the migration, and the CSV file name uses the number. - TaskAssigneeMigrationTest rolls back from its own migration onward instead of assuming it is the last one. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
304 lines
7.9 KiB
PHP
304 lines
7.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Billing\Domain\Entities;
|
|
|
|
use App\Modules\Billing\Domain\Enums\ExportStatus;
|
|
use App\Modules\Billing\Domain\Exceptions\ExportEmpty;
|
|
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
|
|
use App\Modules\Billing\Domain\Exceptions\InvalidBillableHours;
|
|
use App\Modules\Billing\Domain\Exceptions\InvalidBillingPeriod;
|
|
use App\Modules\Billing\Domain\Exceptions\InvoicesCannotMerge;
|
|
use App\Modules\Billing\Domain\Exceptions\JobAlreadyInDraft;
|
|
use App\Modules\Billing\Domain\Exceptions\JobNotInDraft;
|
|
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
|
|
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
|
|
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
|
|
use DateTimeImmutable;
|
|
|
|
/**
|
|
* A billing material (laskuaineisto): invoices made of finished jobs.
|
|
* Management works on it as a draft, adding jobs, combining them on
|
|
* invoices and setting billable hours; creating it forms the invoice lines
|
|
* and locks it.
|
|
*/
|
|
final class BillingExport
|
|
{
|
|
/**
|
|
* @param list<BillingInvoice> $invoices
|
|
*/
|
|
private function __construct(
|
|
private ?int $id,
|
|
private ?int $number,
|
|
private ExportStatus $status,
|
|
private string $period,
|
|
private int $createdBy,
|
|
private DateTimeImmutable $createdAt,
|
|
private ?int $finalizedBy,
|
|
private ?DateTimeImmutable $finalizedAt,
|
|
private array $invoices,
|
|
) {}
|
|
|
|
/**
|
|
* A new draft billing monthly fees up to the current month.
|
|
*/
|
|
public static function startDraft(int $createdBy, DateTimeImmutable $now): self
|
|
{
|
|
return new self(null, null, ExportStatus::Draft, $now->format('Y-m'), $createdBy, $now, null, null, []);
|
|
}
|
|
|
|
/**
|
|
* @param list<BillingInvoice> $invoices
|
|
*/
|
|
public static function reconstitute(int $id, ?int $number, ExportStatus $status, string $period, int $createdBy, DateTimeImmutable $createdAt, ?int $finalizedBy, ?DateTimeImmutable $finalizedAt, array $invoices): self
|
|
{
|
|
return new self($id, $number, $status, $period, $createdBy, $createdAt, $finalizedBy, $finalizedAt, $invoices);
|
|
}
|
|
|
|
/**
|
|
* Sets the month (Y-m) up to which monthly fees are billed.
|
|
*
|
|
* @throws ExportNotDraft
|
|
* @throws InvalidBillingPeriod
|
|
*/
|
|
public function changePeriod(string $period): void
|
|
{
|
|
$this->ensureDraft();
|
|
$this->period = BillingMonths::ensureValid($period);
|
|
}
|
|
|
|
public function period(): string
|
|
{
|
|
return $this->period;
|
|
}
|
|
|
|
/**
|
|
* Adds a job as an invoice of its own.
|
|
*
|
|
* @throws ExportNotDraft
|
|
* @throws JobAlreadyInDraft
|
|
*/
|
|
public function addJob(DraftJob $job): void
|
|
{
|
|
$this->ensureDraft();
|
|
|
|
if ($this->invoiceOf($job->jobId) !== null) {
|
|
throw new JobAlreadyInDraft($job->jobId);
|
|
}
|
|
|
|
$this->invoices[] = BillingInvoice::for($job);
|
|
}
|
|
|
|
/**
|
|
* @throws ExportNotDraft
|
|
* @throws JobNotInDraft
|
|
*/
|
|
public function removeJob(int $jobId): void
|
|
{
|
|
$this->ensureDraft();
|
|
$this->requireInvoiceOf($jobId)->remove($jobId);
|
|
$this->dropEmptyInvoices();
|
|
}
|
|
|
|
/**
|
|
* Moves a job to the invoice of another job.
|
|
*
|
|
* @throws ExportNotDraft
|
|
* @throws JobNotInDraft
|
|
* @throws InvoicesCannotMerge
|
|
*/
|
|
public function moveJob(int $jobId, int $targetJobId): void
|
|
{
|
|
$this->ensureDraft();
|
|
$from = $this->requireInvoiceOf($jobId);
|
|
$to = $this->requireInvoiceOf($targetJobId);
|
|
|
|
if ($from === $to) {
|
|
return;
|
|
}
|
|
|
|
$job = $from->job($jobId);
|
|
assert($job !== null);
|
|
|
|
$refusal = $to->refusalFor($job);
|
|
if ($refusal !== null) {
|
|
throw new InvoicesCannotMerge($refusal);
|
|
}
|
|
|
|
$from->remove($jobId);
|
|
$to->add($job);
|
|
$this->dropEmptyInvoices();
|
|
}
|
|
|
|
/**
|
|
* Moves a job off a combined invoice to an invoice of its own.
|
|
*
|
|
* @throws ExportNotDraft
|
|
* @throws JobNotInDraft
|
|
*/
|
|
public function separate(int $jobId): void
|
|
{
|
|
$this->ensureDraft();
|
|
$from = $this->requireInvoiceOf($jobId);
|
|
|
|
if (count($from->jobs()) === 1) {
|
|
return;
|
|
}
|
|
|
|
$job = $from->job($jobId);
|
|
assert($job !== null);
|
|
|
|
$from->remove($jobId);
|
|
$this->invoices[] = BillingInvoice::for($job);
|
|
}
|
|
|
|
/**
|
|
* Sets management's billable hours of a work type; null returns to the approved hours.
|
|
*
|
|
* @throws ExportNotDraft
|
|
* @throws JobNotInDraft
|
|
* @throws InvalidBillableHours
|
|
*/
|
|
public function setBillableQuarters(int $jobId, int $workTypeId, ?int $quarters): void
|
|
{
|
|
$this->ensureDraft();
|
|
$invoice = $this->requireInvoiceOf($jobId);
|
|
$job = $invoice->job($jobId);
|
|
assert($job !== null);
|
|
|
|
$invoice->replace($job->withBillableQuarters($workTypeId, $quarters));
|
|
}
|
|
|
|
/**
|
|
* Forms the invoice lines, gives the material its number and locks it.
|
|
*
|
|
* @param array<int, list<InvoiceLine>> $linesByJob Keyed by job id.
|
|
*
|
|
* @throws ExportNotDraft
|
|
* @throws ExportEmpty
|
|
*/
|
|
public function finalize(array $linesByJob, int $number, int $finalizedBy, DateTimeImmutable $now): void
|
|
{
|
|
$this->ensureDraft();
|
|
|
|
if ($this->invoices === []) {
|
|
throw new ExportEmpty;
|
|
}
|
|
|
|
foreach ($this->invoices as $invoice) {
|
|
$lines = [];
|
|
foreach ($invoice->jobs() as $job) {
|
|
array_push($lines, ...($linesByJob[$job->jobId] ?? []));
|
|
}
|
|
$invoice->setLines($lines);
|
|
}
|
|
|
|
$this->number = $number;
|
|
$this->status = ExportStatus::Created;
|
|
$this->finalizedBy = $finalizedBy;
|
|
$this->finalizedAt = $now;
|
|
}
|
|
|
|
/**
|
|
* @throws ExportNotDraft
|
|
*/
|
|
public function ensureDraft(): void
|
|
{
|
|
if ($this->status !== ExportStatus::Draft) {
|
|
throw new ExportNotDraft;
|
|
}
|
|
}
|
|
|
|
public function invoiceOf(int $jobId): ?BillingInvoice
|
|
{
|
|
foreach ($this->invoices as $invoice) {
|
|
if ($invoice->has($jobId)) {
|
|
return $invoice;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @return list<DraftJob>
|
|
*/
|
|
public function jobs(): array
|
|
{
|
|
return array_merge(...array_map(fn (BillingInvoice $invoice): array => $invoice->jobs(), $this->invoices));
|
|
}
|
|
|
|
/**
|
|
* @return list<int>
|
|
*/
|
|
public function jobIds(): array
|
|
{
|
|
return array_map(fn (DraftJob $job): int => $job->jobId, $this->jobs());
|
|
}
|
|
|
|
public function id(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* The running number, reserved when the material is created; a draft has none.
|
|
*/
|
|
public function number(): ?int
|
|
{
|
|
return $this->number;
|
|
}
|
|
|
|
public function status(): ExportStatus
|
|
{
|
|
return $this->status;
|
|
}
|
|
|
|
public function isDraft(): bool
|
|
{
|
|
return $this->status === ExportStatus::Draft;
|
|
}
|
|
|
|
public function createdBy(): int
|
|
{
|
|
return $this->createdBy;
|
|
}
|
|
|
|
public function createdAt(): DateTimeImmutable
|
|
{
|
|
return $this->createdAt;
|
|
}
|
|
|
|
public function finalizedBy(): ?int
|
|
{
|
|
return $this->finalizedBy;
|
|
}
|
|
|
|
public function finalizedAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->finalizedAt;
|
|
}
|
|
|
|
/**
|
|
* @return list<BillingInvoice>
|
|
*/
|
|
public function invoices(): array
|
|
{
|
|
return $this->invoices;
|
|
}
|
|
|
|
/**
|
|
* @throws JobNotInDraft
|
|
*/
|
|
private function requireInvoiceOf(int $jobId): BillingInvoice
|
|
{
|
|
return $this->invoiceOf($jobId) ?? throw new JobNotInDraft($jobId);
|
|
}
|
|
|
|
private function dropEmptyInvoices(): void
|
|
{
|
|
$this->invoices = array_values(array_filter($this->invoices, fn (BillingInvoice $invoice): bool => ! $invoice->isEmpty()));
|
|
}
|
|
}
|