Files
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

85 lines
2.4 KiB
PHP

<?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.
* A monthly fee job is always an invoice of its own.
*/
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 = [],
public bool $ownInvoice = false,
) {}
/**
* @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, $this->ownInvoice);
}
/**
* 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);
}
}