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]>
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Contracts;
|
||||
|
||||
use App\Modules\Billing\Domain\Entities\BillingExport;
|
||||
|
||||
interface BillingExportRepository
|
||||
{
|
||||
/**
|
||||
* The material with its invoices, jobs, billable hours and lines. With
|
||||
* $lock the row stays locked until the transaction ends.
|
||||
*/
|
||||
public function findById(int $id, bool $lock = false): ?BillingExport;
|
||||
|
||||
/**
|
||||
* The id of the open draft, if there is one.
|
||||
*/
|
||||
public function openDraftId(): ?int;
|
||||
|
||||
/**
|
||||
* Inserts a new material or replaces a stored one's invoices, and returns it.
|
||||
*/
|
||||
public function save(BillingExport $export): BillingExport;
|
||||
|
||||
public function delete(BillingExport $export): void;
|
||||
|
||||
/**
|
||||
* The jobs among the given ones whose fixed price is already on a created material.
|
||||
*
|
||||
* @param list<int> $jobIds
|
||||
* @return list<int>
|
||||
*/
|
||||
public function fixedPriceBilledJobIds(array $jobIds): array;
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
<?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\InvoicesCannotMerge;
|
||||
use App\Modules\Billing\Domain\Exceptions\JobAlreadyInDraft;
|
||||
use App\Modules\Billing\Domain\Exceptions\JobNotInDraft;
|
||||
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 ExportStatus $status,
|
||||
private int $createdBy,
|
||||
private DateTimeImmutable $createdAt,
|
||||
private ?int $finalizedBy,
|
||||
private ?DateTimeImmutable $finalizedAt,
|
||||
private array $invoices,
|
||||
) {}
|
||||
|
||||
public static function startDraft(int $createdBy, DateTimeImmutable $now): self
|
||||
{
|
||||
return new self(null, ExportStatus::Draft, $createdBy, $now, null, null, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<BillingInvoice> $invoices
|
||||
*/
|
||||
public static function reconstitute(int $id, ExportStatus $status, int $createdBy, DateTimeImmutable $createdAt, ?int $finalizedBy, ?DateTimeImmutable $finalizedAt, array $invoices): self
|
||||
{
|
||||
return new self($id, $status, $createdBy, $createdAt, $finalizedBy, $finalizedAt, $invoices);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 and locks the material.
|
||||
*
|
||||
* @param array<int, list<InvoiceLine>> $linesByJob Keyed by job id.
|
||||
*
|
||||
* @throws ExportNotDraft
|
||||
* @throws ExportEmpty
|
||||
*/
|
||||
public function finalize(array $linesByJob, 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->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;
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Enums;
|
||||
|
||||
/**
|
||||
* How a job is billed: its hours per work type at the hourly rates, or its
|
||||
* quoted fixed price as one line. Expenses are billed separately either way.
|
||||
*/
|
||||
enum BillingMode: string
|
||||
{
|
||||
case Hours = 'hours';
|
||||
case FixedPrice = 'fixed_price';
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Enums;
|
||||
|
||||
/**
|
||||
* A billing material is a draft until it is created, which locks it.
|
||||
*/
|
||||
enum ExportStatus: string
|
||||
{
|
||||
case Draft = 'draft';
|
||||
case Created = 'created';
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Enums;
|
||||
|
||||
/**
|
||||
* What an invoice line bills.
|
||||
*/
|
||||
enum LineKind: string
|
||||
{
|
||||
case Hours = 'hours';
|
||||
case FixedPrice = 'fixed_price';
|
||||
case Expense = 'expense';
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Enums;
|
||||
|
||||
/**
|
||||
* Why two jobs cannot be on the same invoice (spec 12).
|
||||
*/
|
||||
enum MergeRefusal: string
|
||||
{
|
||||
case DifferentCustomer = 'different_customer';
|
||||
case DifferentBillingUnit = 'different_billing_unit';
|
||||
case DifferentReference = 'different_reference';
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Enums;
|
||||
|
||||
/**
|
||||
* Why a job cannot go on a billing material (spec 12).
|
||||
*/
|
||||
enum NotBillableReason: string
|
||||
{
|
||||
/** The job is not done. */
|
||||
case WrongStatus = 'wrong_status';
|
||||
|
||||
/** Not billable, billed monthly, or the internal customer. */
|
||||
case NotBillableBasis = 'not_billable_basis';
|
||||
|
||||
/** A consumer customer without a personal identity code. */
|
||||
case MissingIdentityCode = 'missing_identity_code';
|
||||
|
||||
/** A work type with approved hours has no hourly rate. */
|
||||
case MissingHourlyRate = 'missing_hourly_rate';
|
||||
|
||||
/** A fixed price job without a quoted amount. */
|
||||
case MissingQuotedAmount = 'missing_quoted_amount';
|
||||
|
||||
/** No approved hours, expenses or fixed price left to bill. */
|
||||
case NothingToBill = 'nothing_to_bill';
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class BillingExportNotFound extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $id)
|
||||
{
|
||||
parent::__construct("Billing material {$id} was not found.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class DraftAlreadyOpen extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A billing material draft is already open.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ExportEmpty extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The billing material has no jobs.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class ExportNotDraft extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The billing material is already created.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidBillableHours extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The billable hours are invalid.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Billing\Domain\Enums\MergeRefusal;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvoicesCannotMerge extends DomainException
|
||||
{
|
||||
public function __construct(public readonly MergeRefusal $refusal)
|
||||
{
|
||||
parent::__construct("The jobs cannot be on the same invoice: {$refusal->value}.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class JobAlreadyInDraft extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $jobId)
|
||||
{
|
||||
parent::__construct("Job {$jobId} is already on the billing material.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Billing\Domain\Enums\NotBillableReason;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class JobNotBillable extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $jobId, public readonly NotBillableReason $reason)
|
||||
{
|
||||
parent::__construct("Job {$jobId} cannot be billed: {$reason->value}.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class JobNotInDraft extends DomainException
|
||||
{
|
||||
public function __construct(public readonly int $jobId)
|
||||
{
|
||||
parent::__construct("Job {$jobId} is not on the billing material.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Billing\Domain\Exceptions\InvalidBillableHours;
|
||||
|
||||
/**
|
||||
* A job on a draft invoice: who it bills (customer, billing unit, the
|
||||
* customer's reference) and management's billable hours per work type.
|
||||
* A work type without its own billable hours bills its approved hours.
|
||||
*/
|
||||
final readonly class DraftJob
|
||||
{
|
||||
/** 9 999 hours. */
|
||||
public const int MAX_QUARTERS = 39_996;
|
||||
|
||||
/**
|
||||
* @param array<int, int> $billableQuarters Keyed by work type id.
|
||||
*/
|
||||
public function __construct(
|
||||
public int $jobId,
|
||||
public int $customerId,
|
||||
public ?int $billingUnitId,
|
||||
public ?string $reference,
|
||||
public array $billableQuarters = [],
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws InvalidBillableHours
|
||||
*/
|
||||
public function withBillableQuarters(int $workTypeId, ?int $quarters): self
|
||||
{
|
||||
$billable = $this->billableQuarters;
|
||||
|
||||
if ($quarters === null) {
|
||||
unset($billable[$workTypeId]);
|
||||
} elseif ($quarters < 0 || $quarters > self::MAX_QUARTERS) {
|
||||
throw new InvalidBillableHours;
|
||||
} else {
|
||||
$billable[$workTypeId] = $quarters;
|
||||
}
|
||||
|
||||
return new self($this->jobId, $this->customerId, $this->billingUnitId, $this->reference, $billable);
|
||||
}
|
||||
|
||||
/**
|
||||
* The billable quarters of a work type: management's own, or the approved ones.
|
||||
*/
|
||||
public function billableOf(int $workTypeId, int $approvedQuarters): int
|
||||
{
|
||||
return $this->billableQuarters[$workTypeId] ?? $approvedQuarters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses billable hours such as "2,5", "2.5" or "2" to quarters of an
|
||||
* hour; null for an empty value (back to the approved hours).
|
||||
*
|
||||
* @throws InvalidBillableHours
|
||||
*/
|
||||
public static function parseHours(string $value): ?int
|
||||
{
|
||||
$value = str_replace(',', '.', trim($value));
|
||||
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (preg_match('/^\d{1,4}(\.\d{1,2})?$/', $value) !== 1) {
|
||||
throw new InvalidBillableHours;
|
||||
}
|
||||
|
||||
$quarters = (float) $value * 4;
|
||||
|
||||
if (abs($quarters - round($quarters)) > 0.0001) {
|
||||
throw new InvalidBillableHours;
|
||||
}
|
||||
|
||||
return (int) round($quarters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Billing\Domain\Enums\LineKind;
|
||||
|
||||
/**
|
||||
* A line of an invoice, without VAT: a work type's billable hours at its
|
||||
* hourly rate, a job's fixed price, or an expense. Prices and descriptions
|
||||
* are snapshots taken when the material is formed.
|
||||
*/
|
||||
final readonly class InvoiceLine
|
||||
{
|
||||
public function __construct(
|
||||
public int $jobId,
|
||||
public LineKind $kind,
|
||||
public ?int $workTypeId,
|
||||
public ?int $expenseId,
|
||||
public string $description,
|
||||
public ?int $recordedQuarters,
|
||||
public ?int $quantityQuarters,
|
||||
public int $unitPriceCents,
|
||||
public int $vatBasisPoints,
|
||||
public int $netCents,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Billable hours × the hourly rate, rounded half up to the cent. The
|
||||
* approved (recorded) hours are kept beside them for reports.
|
||||
*/
|
||||
public static function hours(int $jobId, int $workTypeId, string $workTypeName, int $recordedQuarters, int $billableQuarters, int $hourlyRateCents, int $vatBasisPoints): self
|
||||
{
|
||||
return new self(
|
||||
$jobId,
|
||||
LineKind::Hours,
|
||||
$workTypeId,
|
||||
null,
|
||||
$workTypeName,
|
||||
$recordedQuarters,
|
||||
$billableQuarters,
|
||||
$hourlyRateCents,
|
||||
$vatBasisPoints,
|
||||
intdiv($billableQuarters * $hourlyRateCents * 2 + 4, 8),
|
||||
);
|
||||
}
|
||||
|
||||
public static function fixedPrice(int $jobId, string $description, int $amountCents, int $vatBasisPoints): self
|
||||
{
|
||||
return new self($jobId, LineKind::FixedPrice, null, null, $description, null, null, $amountCents, $vatBasisPoints, $amountCents);
|
||||
}
|
||||
|
||||
public static function expense(int $jobId, int $expenseId, string $description, int $amountCents, int $vatBasisPoints): self
|
||||
{
|
||||
return new self($jobId, LineKind::Expense, null, $expenseId, $description, null, null, $amountCents, $vatBasisPoints, $amountCents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
|
||||
|
||||
/**
|
||||
* An invoice's sum without VAT, its VAT per rate (counted from the rate's
|
||||
* total, rounded half up) and the sum with VAT.
|
||||
*/
|
||||
final readonly class InvoiceTotals
|
||||
{
|
||||
/**
|
||||
* @param array<int, array{net: int, vat: int}> $byVatRate Keyed by VAT basis points, highest first.
|
||||
*/
|
||||
public function __construct(
|
||||
public int $netCents,
|
||||
public int $vatCents,
|
||||
public array $byVatRate,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param list<InvoiceLine> $lines
|
||||
*/
|
||||
public static function of(array $lines): self
|
||||
{
|
||||
$netByRate = [];
|
||||
foreach ($lines as $line) {
|
||||
$netByRate[$line->vatBasisPoints] = ($netByRate[$line->vatBasisPoints] ?? 0) + $line->netCents;
|
||||
}
|
||||
krsort($netByRate);
|
||||
|
||||
$byRate = [];
|
||||
$net = 0;
|
||||
$vat = 0;
|
||||
|
||||
foreach ($netByRate as $basisPoints => $rateNet) {
|
||||
$rateVat = Money::fromCents($rateNet)->addVat(new VatPercentage($basisPoints))->cents - $rateNet;
|
||||
$byRate[$basisPoints] = ['net' => $rateNet, 'vat' => $rateVat];
|
||||
$net += $rateNet;
|
||||
$vat += $rateVat;
|
||||
}
|
||||
|
||||
return new self($net, $vat, $byRate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<self> $totals
|
||||
*/
|
||||
public static function sum(array $totals): self
|
||||
{
|
||||
$byRate = [];
|
||||
foreach ($totals as $total) {
|
||||
foreach ($total->byVatRate as $basisPoints => $amounts) {
|
||||
$byRate[$basisPoints] = [
|
||||
'net' => ($byRate[$basisPoints]['net'] ?? 0) + $amounts['net'],
|
||||
'vat' => ($byRate[$basisPoints]['vat'] ?? 0) + $amounts['vat'],
|
||||
];
|
||||
}
|
||||
}
|
||||
krsort($byRate);
|
||||
|
||||
return new self(
|
||||
array_sum(array_column($byRate, 'net')),
|
||||
array_sum(array_column($byRate, 'vat')),
|
||||
$byRate,
|
||||
);
|
||||
}
|
||||
|
||||
public function grossCents(): int
|
||||
{
|
||||
return $this->netCents + $this->vatCents;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user