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]>
185 lines
6.7 KiB
PHP
185 lines
6.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Billing\Application\Services;
|
|
|
|
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
|
use App\Modules\Billing\Application\DTOs\AvailableJob;
|
|
use App\Modules\Billing\Application\DTOs\BillableJob;
|
|
use App\Modules\Billing\Application\DTOs\ExportView;
|
|
use App\Modules\Billing\Application\DTOs\HoursRow;
|
|
use App\Modules\Billing\Application\DTOs\InvoiceJobView;
|
|
use App\Modules\Billing\Application\DTOs\InvoiceView;
|
|
use App\Modules\Billing\Application\DTOs\MergeTarget;
|
|
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
|
use App\Modules\Billing\Domain\Entities\BillingExport;
|
|
use App\Modules\Billing\Domain\Entities\BillingInvoice;
|
|
use App\Modules\Billing\Domain\Enums\BillingMode;
|
|
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
|
|
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
|
|
use App\Modules\Billing\Domain\ValueObjects\InvoiceTotals;
|
|
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
|
|
|
/**
|
|
* Builds a billing material's page: a draft is previewed with the hours
|
|
* and expenses the jobs have now, and a created material shows its lines.
|
|
*/
|
|
final class ExportViews
|
|
{
|
|
public function __construct(
|
|
private BillingExportRepository $exports,
|
|
private BillingJobs $jobs,
|
|
private BillingSnapshot $snapshots,
|
|
private UserAccountQueries $users,
|
|
) {}
|
|
|
|
public function of(int $exportId): ?ExportView
|
|
{
|
|
$export = $this->exports->findById($exportId);
|
|
|
|
if ($export === null) {
|
|
return null;
|
|
}
|
|
|
|
$candidates = $export->isDraft() ? $this->jobs->candidates() : [];
|
|
$snapshot = $this->snapshots->load($export->period(), $export->jobIds(), $candidates);
|
|
$onDraft = array_flip($export->jobIds());
|
|
|
|
$invoices = [];
|
|
foreach ($export->invoices() as $invoice) {
|
|
$invoices[] = $this->invoiceView($export, $invoice, $snapshot);
|
|
}
|
|
|
|
$available = [];
|
|
foreach ($candidates as $job) {
|
|
if (! isset($onDraft[$job->id])) {
|
|
$hours = $snapshot->hoursOf($job->id);
|
|
$available[] = new AvailableJob(
|
|
$job,
|
|
$hours->approvedQuarters(),
|
|
$hours->unapprovedQuarters,
|
|
count($snapshot->expensesOf($job->id)),
|
|
$snapshot->reasonNotBillable($job),
|
|
$job->mode === BillingMode::Hours ? $snapshot->missingRateNames($job) : [],
|
|
);
|
|
}
|
|
}
|
|
|
|
$names = $this->users->namesByIds(array_values(array_filter([$export->createdBy(), $export->finalizedBy()])));
|
|
|
|
return new ExportView(
|
|
(int) $export->id(),
|
|
$export->status(),
|
|
$export->period(),
|
|
$export->createdAt(),
|
|
$names[$export->createdBy()] ?? '',
|
|
$export->finalizedAt(),
|
|
$export->finalizedBy() === null ? null : ($names[$export->finalizedBy()] ?? ''),
|
|
$invoices,
|
|
InvoiceTotals::sum(array_map(fn (InvoiceView $invoice): InvoiceTotals => $invoice->totals, $invoices)),
|
|
$available,
|
|
);
|
|
}
|
|
|
|
private function invoiceView(BillingExport $export, BillingInvoice $invoice, BillingSnapshot $snapshot): InvoiceView
|
|
{
|
|
$jobViews = [];
|
|
$lines = [];
|
|
$mergeTargets = [];
|
|
|
|
foreach ($invoice->jobs() as $draftJob) {
|
|
$job = $snapshot->jobs[$draftJob->jobId] ?? null;
|
|
$view = $export->isDraft()
|
|
? $this->draftJobView($draftJob, $job, $snapshot)
|
|
: $this->createdJobView($draftJob, $job, $invoice->lines());
|
|
|
|
$jobViews[] = $view;
|
|
array_push($lines, ...$view->lines);
|
|
|
|
if ($export->isDraft()) {
|
|
$mergeTargets[$draftJob->jobId] = $this->mergeTargets($export, $invoice, $draftJob, $snapshot);
|
|
}
|
|
}
|
|
|
|
$first = $snapshot->jobs[$invoice->key()] ?? null;
|
|
|
|
return new InvoiceView(
|
|
$invoice->key(),
|
|
$first->customerName ?? '',
|
|
$first?->billingUnitName,
|
|
$invoice->reference(),
|
|
$jobViews,
|
|
InvoiceTotals::of($lines),
|
|
$mergeTargets,
|
|
);
|
|
}
|
|
|
|
private function draftJobView(DraftJob $draftJob, ?BillableJob $job, BillingSnapshot $snapshot): InvoiceJobView
|
|
{
|
|
if ($job === null) {
|
|
return new InvoiceJobView($draftJob->jobId, '', '', null, [], [], 0, null);
|
|
}
|
|
|
|
$hours = $snapshot->hoursOf($job->id);
|
|
$lines = $snapshot->linesOf($job, $draftJob, $hours->approvedByWorkType, $snapshot->expensesOf($job->id));
|
|
|
|
$rows = [];
|
|
foreach ($snapshot->rules->sortedByName($hours->approvedByWorkType, $snapshot->rates) as $workTypeId => $approved) {
|
|
$rate = $snapshot->rates[$workTypeId] ?? null;
|
|
$billable = $draftJob->billableOf($workTypeId, $approved);
|
|
$line = $rate?->rateCents === null ? null : InvoiceLine::hours($job->id, $workTypeId, $rate->name, $approved, $billable, $rate->rateCents, $snapshot->vatBasisPoints);
|
|
|
|
$rows[] = new HoursRow($workTypeId, $rate->name ?? '', $approved, $billable, isset($draftJob->billableQuarters[$workTypeId]), $rate?->rateCents, $line->netCents ?? 0);
|
|
}
|
|
|
|
return new InvoiceJobView(
|
|
$job->id,
|
|
$job->number,
|
|
$job->name,
|
|
$job->mode,
|
|
$rows,
|
|
$lines,
|
|
$hours->unapprovedQuarters,
|
|
$snapshot->reasonNotBillable($job),
|
|
$job->mode === BillingMode::Hours ? $snapshot->missingRateNames($job) : [],
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param list<InvoiceLine> $invoiceLines
|
|
*/
|
|
private function createdJobView(DraftJob $draftJob, ?BillableJob $job, array $invoiceLines): InvoiceJobView
|
|
{
|
|
return new InvoiceJobView(
|
|
$draftJob->jobId,
|
|
$job->number ?? '',
|
|
$job->name ?? '',
|
|
$job?->mode,
|
|
[],
|
|
array_values(array_filter($invoiceLines, fn (InvoiceLine $line): bool => $line->jobId === $draftJob->jobId)),
|
|
0,
|
|
null,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return list<MergeTarget>
|
|
*/
|
|
private function mergeTargets(BillingExport $export, BillingInvoice $current, DraftJob $job, BillingSnapshot $snapshot): array
|
|
{
|
|
$targets = [];
|
|
|
|
foreach ($export->invoices() as $invoice) {
|
|
if ($invoice === $current || $invoice->refusalFor($job) !== null) {
|
|
continue;
|
|
}
|
|
|
|
$labels = array_map(fn (DraftJob $other): string => ($snapshot->jobs[$other->jobId] ?? null)?->label() ?? (string) $other->jobId, $invoice->jobs());
|
|
$targets[] = new MergeTarget($invoice->key(), implode(', ', $labels));
|
|
}
|
|
|
|
return $targets;
|
|
}
|
|
}
|