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]>
This commit is contained in:
@@ -33,4 +33,12 @@ interface BillingExportRepository
|
||||
* @return list<int>
|
||||
*/
|
||||
public function fixedPriceBilledJobIds(array $jobIds): array;
|
||||
|
||||
/**
|
||||
* The months (Y-m) whose monthly fee is on a created material, by job.
|
||||
*
|
||||
* @param list<int> $jobIds
|
||||
* @return array<int, list<string>>
|
||||
*/
|
||||
public function billedMonths(array $jobIds): array;
|
||||
}
|
||||
|
||||
@@ -8,9 +8,11 @@ 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;
|
||||
@@ -29,6 +31,7 @@ final class BillingExport
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private ExportStatus $status,
|
||||
private string $period,
|
||||
private int $createdBy,
|
||||
private DateTimeImmutable $createdAt,
|
||||
private ?int $finalizedBy,
|
||||
@@ -36,17 +39,37 @@ final class BillingExport
|
||||
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, ExportStatus::Draft, $createdBy, $now, null, null, []);
|
||||
return new self(null, ExportStatus::Draft, $now->format('Y-m'), $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
|
||||
public static function reconstitute(int $id, ExportStatus $status, string $period, int $createdBy, DateTimeImmutable $createdAt, ?int $finalizedBy, ?DateTimeImmutable $finalizedAt, array $invoices): self
|
||||
{
|
||||
return new self($id, $status, $createdBy, $createdAt, $finalizedBy, $finalizedAt, $invoices);
|
||||
return new self($id, $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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,6 +50,10 @@ final class BillingInvoice
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($other->ownInvoice || $job->ownInvoice) {
|
||||
return MergeRefusal::MonthlyFeeAlone;
|
||||
}
|
||||
|
||||
if ($other->customerId !== $job->customerId) {
|
||||
return MergeRefusal::DifferentCustomer;
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@ 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.
|
||||
* How a job is billed: its hours per work type at the hourly rates, its
|
||||
* quoted fixed price as one line, or its monthly fee for every month.
|
||||
* Expenses are billed separately in every case.
|
||||
*/
|
||||
enum BillingMode: string
|
||||
{
|
||||
case Hours = 'hours';
|
||||
case FixedPrice = 'fixed_price';
|
||||
case MonthlyFee = 'monthly_fee';
|
||||
}
|
||||
|
||||
@@ -12,4 +12,5 @@ enum LineKind: string
|
||||
case Hours = 'hours';
|
||||
case FixedPrice = 'fixed_price';
|
||||
case Expense = 'expense';
|
||||
case MonthlyFee = 'monthly_fee';
|
||||
}
|
||||
|
||||
@@ -12,4 +12,7 @@ enum MergeRefusal: string
|
||||
case DifferentCustomer = 'different_customer';
|
||||
case DifferentBillingUnit = 'different_billing_unit';
|
||||
case DifferentReference = 'different_reference';
|
||||
|
||||
/** Monthly fee contracts are billed on invoices of their own (K22). */
|
||||
case MonthlyFeeAlone = 'monthly_fee_alone';
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace App\Modules\Billing\Domain\Enums;
|
||||
*/
|
||||
enum NotBillableReason: string
|
||||
{
|
||||
/** The job is not done. */
|
||||
/** The job is not done (or for a monthly fee, not running). */
|
||||
case WrongStatus = 'wrong_status';
|
||||
|
||||
/** Not billable, billed monthly, or the internal customer. */
|
||||
/** Not billable, or the internal customer. */
|
||||
case NotBillableBasis = 'not_billable_basis';
|
||||
|
||||
/** A consumer customer without a personal identity code. */
|
||||
@@ -24,6 +24,9 @@ enum NotBillableReason: string
|
||||
/** A fixed price job without a quoted amount. */
|
||||
case MissingQuotedAmount = 'missing_quoted_amount';
|
||||
|
||||
/** No approved hours, expenses or fixed price left to bill. */
|
||||
/** A monthly fee job without a monthly fee. */
|
||||
case MissingMonthlyFee = 'missing_monthly_fee';
|
||||
|
||||
/** No approved hours, expenses, fixed price or months 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 InvalidBillingPeriod extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The billing month is invalid.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Billing\Domain\Exceptions\InvalidBillingPeriod;
|
||||
|
||||
/**
|
||||
* The months a monthly fee job still has to bill (spec 5.5): from the month
|
||||
* it starts to the material's month, or to the month it ends or was done
|
||||
* if that is earlier, leaving out months already billed. Months are Y-m.
|
||||
*/
|
||||
final class BillingMonths
|
||||
{
|
||||
/**
|
||||
* @param string $startsOn Y-m-d: the job's start (or creation) day.
|
||||
* @param string|null $endsOn Y-m-d: the job's last day or completion day, if any.
|
||||
* @param string $period Y-m: the material's month.
|
||||
* @param list<string> $billed Months billed before, as Y-m.
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function unbilled(string $startsOn, ?string $endsOn, string $period, array $billed): array
|
||||
{
|
||||
$end = $endsOn === null ? $period : min($period, substr($endsOn, 0, 7));
|
||||
$billedMonths = array_flip($billed);
|
||||
$months = [];
|
||||
|
||||
for ($month = substr($startsOn, 0, 7); $month <= $end; $month = self::next($month)) {
|
||||
if (! isset($billedMonths[$month])) {
|
||||
$months[] = $month;
|
||||
}
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidBillingPeriod
|
||||
*/
|
||||
public static function ensureValid(string $period): string
|
||||
{
|
||||
if (preg_match('/^(\d{4})-(0[1-9]|1[0-2])$/', $period) !== 1) {
|
||||
throw new InvalidBillingPeriod;
|
||||
}
|
||||
|
||||
return $period;
|
||||
}
|
||||
|
||||
/**
|
||||
* "11/2026" for "2026-11".
|
||||
*/
|
||||
public static function label(string $month): string
|
||||
{
|
||||
return ((int) substr($month, 5, 2)).'/'.substr($month, 0, 4);
|
||||
}
|
||||
|
||||
private static function next(string $month): string
|
||||
{
|
||||
$year = (int) substr($month, 0, 4);
|
||||
$number = (int) substr($month, 5, 2) + 1;
|
||||
|
||||
if ($number > 12) {
|
||||
$year++;
|
||||
$number = 1;
|
||||
}
|
||||
|
||||
return sprintf('%04d-%02d', $year, $number);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ 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
|
||||
{
|
||||
@@ -25,6 +26,7 @@ final readonly class DraftJob
|
||||
public ?int $billingUnitId,
|
||||
public ?string $reference,
|
||||
public array $billableQuarters = [],
|
||||
public bool $ownInvoice = false,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -42,7 +44,7 @@ final readonly class DraftJob
|
||||
$billable[$workTypeId] = $quarters;
|
||||
}
|
||||
|
||||
return new self($this->jobId, $this->customerId, $this->billingUnitId, $this->reference, $billable);
|
||||
return new self($this->jobId, $this->customerId, $this->billingUnitId, $this->reference, $billable, $this->ownInvoice);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,7 @@ 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
|
||||
* hourly rate, a job's fixed price, a month's fee, or an expense. Prices and descriptions
|
||||
* are snapshots taken when the material is formed.
|
||||
*/
|
||||
final readonly class InvoiceLine
|
||||
@@ -24,6 +24,7 @@ final readonly class InvoiceLine
|
||||
public int $unitPriceCents,
|
||||
public int $vatBasisPoints,
|
||||
public int $netCents,
|
||||
public ?string $period = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -51,6 +52,14 @@ final readonly class InvoiceLine
|
||||
return new self($jobId, LineKind::FixedPrice, null, null, $description, null, null, $amountCents, $vatBasisPoints, $amountCents);
|
||||
}
|
||||
|
||||
/**
|
||||
* The fee of one month (Y-m).
|
||||
*/
|
||||
public static function monthlyFee(int $jobId, string $period, string $description, int $amountCents, int $vatBasisPoints): self
|
||||
{
|
||||
return new self($jobId, LineKind::MonthlyFee, null, null, $description, null, null, $amountCents, $vatBasisPoints, $amountCents, $period);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user