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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user