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:
2026-11-24 13:23:46 +02:00
co-authored by Claude Opus 5.5
parent 97bd23bed5
commit 5f9423a3c3
49 changed files with 799 additions and 82 deletions
@@ -21,15 +21,30 @@ use App\Modules\Jobs\Domain\Enums\PaymentBasis;
*/
class JobsForBilling implements BillingJobs
{
/** Statuses in which a monthly fee job is billed: running or done. */
private const array MONTHLY_STATUSES = [JobStatus::Order, JobStatus::InProduction, JobStatus::OnHold, JobStatus::Done];
public function __construct(
private CustomerJobQueries $jobs,
private CustomerQueries $customers,
private ChangeJobStatus $changeStatus,
) {}
public function done(): array
public function candidates(): array
{
return $this->toBillable($this->jobs->billingInfoWithStatus(JobStatus::Done));
$jobs = [];
foreach ([...$this->jobs->billingInfoWithStatus(JobStatus::Done), ...$this->jobs->billingInfoMonthly()] as $job) {
$jobs[$job->id] = $job;
}
uasort($jobs, fn (JobBillingInfo $a, JobBillingInfo $b): int => strcmp($a->number, $b->number));
return $this->toBillable(array_values($jobs));
}
public function monthly(): array
{
return $this->toBillable($this->jobs->billingInfoMonthly());
}
public function find(array $jobIds): array
@@ -65,6 +80,13 @@ class JobsForBilling implements BillingJobs
return array_map(function (JobBillingInfo $job) use ($customers): BillableJob {
$customer = $customers[$job->customerId] ?? null;
$mode = match ($job->paymentBasis) {
PaymentBasis::FixedQuote => BillingMode::FixedPrice,
PaymentBasis::MonthlyFee => BillingMode::MonthlyFee,
PaymentBasis::NotBillable => null,
default => BillingMode::Hours,
};
$ends = array_filter([$job->dueOn, $job->completedOn]);
return new BillableJob(
$job->id,
@@ -78,13 +100,12 @@ class JobsForBilling implements BillingJobs
$job->billingUnitId,
$job->billingUnitName,
$job->customerReference,
match ($job->paymentBasis) {
PaymentBasis::FixedQuote => BillingMode::FixedPrice,
PaymentBasis::NotBillable, PaymentBasis::MonthlyFee => null,
default => BillingMode::Hours,
},
$mode,
$job->quotedAmountCents,
$job->status === JobStatus::Done,
$mode === BillingMode::MonthlyFee ? in_array($job->status, self::MONTHLY_STATUSES, true) : $job->status === JobStatus::Done,
$job->monthlyFeeCents,
$job->startsOn ?? $job->createdOn,
$ends === [] ? null : min($ends),
);
}, $jobs);
}
@@ -14,13 +14,14 @@ use Illuminate\Support\Carbon;
*
* @property int $id
* @property ExportStatus $status
* @property string|null $period Y-m
* @property int $created_by
* @property int|null $finalized_by
* @property Carbon|null $finalized_at
* @property Carbon $created_at
*/
#[Table('billing_exports')]
#[Fillable(['status', 'created_by', 'finalized_by', 'finalized_at'])]
#[Fillable(['status', 'period', 'created_by', 'finalized_by', 'finalized_at'])]
class BillingExportModel extends Model
{
/**
@@ -18,10 +18,11 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
* @property int $customer_id
* @property int|null $billing_unit_id
* @property string|null $customer_reference
* @property bool $own_invoice
* @property int $sort_order
*/
#[Table('billing_invoice_jobs')]
#[Fillable(['billing_invoice_id', 'customer_job_id', 'customer_id', 'billing_unit_id', 'customer_reference', 'sort_order'])]
#[Fillable(['billing_invoice_id', 'customer_job_id', 'customer_id', 'billing_unit_id', 'customer_reference', 'own_invoice', 'sort_order'])]
class BillingInvoiceJobModel extends Model
{
public $timestamps = false;
@@ -44,6 +45,7 @@ class BillingInvoiceJobModel extends Model
'customer_job_id' => 'integer',
'customer_id' => 'integer',
'billing_unit_id' => 'integer',
'own_invoice' => 'boolean',
'sort_order' => 'integer',
];
}
@@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\Model;
* @property LineKind $kind
* @property int|null $work_type_id
* @property int|null $expense_id
* @property string|null $period Y-m
* @property string $description
* @property int|null $recorded_quarters
* @property int|null $quantity_quarters
@@ -26,7 +27,7 @@ use Illuminate\Database\Eloquent\Model;
*/
#[Table('billing_lines')]
#[Fillable([
'billing_invoice_id', 'customer_job_id', 'kind', 'work_type_id', 'expense_id', 'description',
'billing_invoice_id', 'customer_job_id', 'kind', 'work_type_id', 'expense_id', 'period', 'description',
'recorded_quarters', 'quantity_quarters', 'unit_price_cents', 'vat_basis_points', 'net_cents', 'sort_order',
])]
class BillingLineModel extends Model
@@ -46,6 +46,7 @@ class EloquentBillingExportRepository implements BillingExportRepository
$model->fill([
'status' => $export->status(),
'period' => $export->period(),
'created_by' => $export->createdBy(),
'finalized_by' => $export->finalizedBy(),
'finalized_at' => $export->finalizedAt()?->format('Y-m-d H:i:s'),
@@ -75,6 +76,7 @@ class EloquentBillingExportRepository implements BillingExportRepository
'customer_id' => $job->customerId,
'billing_unit_id' => $job->billingUnitId,
'customer_reference' => $job->reference,
'own_invoice' => $job->ownInvoice,
'sort_order' => $jobOrder,
]);
@@ -94,6 +96,7 @@ class EloquentBillingExportRepository implements BillingExportRepository
'kind' => $line->kind,
'work_type_id' => $line->workTypeId,
'expense_id' => $line->expenseId,
'period' => $line->period,
'description' => mb_substr($line->description, 0, 255),
'recorded_quarters' => $line->recordedQuarters,
'quantity_quarters' => $line->quantityQuarters,
@@ -129,6 +132,21 @@ class EloquentBillingExportRepository implements BillingExportRepository
->all();
}
public function billedMonths(array $jobIds): array
{
if ($jobIds === []) {
return [];
}
$months = [];
foreach (BillingLineModel::query()->whereIn('customer_job_id', $jobIds)->where('kind', LineKind::MonthlyFee)->whereNotNull('period')->get(['customer_job_id', 'period']) as $line) {
$months[$line->customer_job_id][] = (string) $line->period;
}
return $months;
}
private function toEntity(BillingExportModel $model): BillingExport
{
$invoices = $model->invoices->map(fn (BillingInvoiceModel $invoice): BillingInvoice => BillingInvoice::reconstitute(
@@ -138,6 +156,7 @@ class EloquentBillingExportRepository implements BillingExportRepository
$job->billing_unit_id,
$job->customer_reference,
$job->hours->mapWithKeys(fn (BillingJobHoursModel $hours): array => [$hours->work_type_id => $hours->billable_quarters])->all(),
$job->own_invoice,
))->values()->all(),
$invoice->lines->map(fn (BillingLineModel $line): InvoiceLine => new InvoiceLine(
$line->customer_job_id,
@@ -150,12 +169,14 @@ class EloquentBillingExportRepository implements BillingExportRepository
$line->unit_price_cents,
$line->vat_basis_points,
$line->net_cents,
$line->period,
))->values()->all(),
))->values()->all();
return BillingExport::reconstitute(
$model->id,
$model->status,
$model->period ?? $model->created_at->format('Y-m'),
$model->created_by,
DateTimeImmutable::createFromInterface($model->created_at),
$model->finalized_by,