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:
@@ -10,23 +10,26 @@ use App\Modules\Billing\Application\DTOs\HourlyRate;
|
||||
use App\Modules\Billing\Application\DTOs\JobHours;
|
||||
use App\Modules\Billing\Domain\Enums\BillingMode;
|
||||
use App\Modules\Billing\Domain\Enums\NotBillableReason;
|
||||
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
|
||||
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
|
||||
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
|
||||
|
||||
/**
|
||||
* What a job may bill and its invoice lines (spec 12): hours per work type
|
||||
* at the hourly rates, or the quoted fixed price once, and the billable
|
||||
* expenses separately. Only approved hours are billed.
|
||||
* at the hourly rates, the quoted fixed price once, or the monthly fee of
|
||||
* every unbilled month (spec 5.5), and the billable expenses separately.
|
||||
* Only approved hours are billed; a fixed price or a monthly fee covers them.
|
||||
*/
|
||||
final class BillingRules
|
||||
{
|
||||
/**
|
||||
* @param list<BillableExpense> $expenses
|
||||
* @param array<int, HourlyRate> $rates
|
||||
* @param list<string> $unbilledMonths For a monthly fee job, as Y-m.
|
||||
*/
|
||||
public function reasonNotBillable(BillableJob $job, JobHours $hours, array $expenses, bool $fixedPriceBilled, array $rates): ?NotBillableReason
|
||||
public function reasonNotBillable(BillableJob $job, JobHours $hours, array $expenses, bool $fixedPriceBilled, array $rates, array $unbilledMonths = []): ?NotBillableReason
|
||||
{
|
||||
if (! $job->isDone) {
|
||||
if (! $job->inBillableStatus) {
|
||||
return NotBillableReason::WrongStatus;
|
||||
}
|
||||
|
||||
@@ -38,6 +41,14 @@ final class BillingRules
|
||||
return NotBillableReason::MissingIdentityCode;
|
||||
}
|
||||
|
||||
if ($job->mode === BillingMode::MonthlyFee) {
|
||||
if ($job->monthlyFeeCents === null) {
|
||||
return NotBillableReason::MissingMonthlyFee;
|
||||
}
|
||||
|
||||
return $unbilledMonths === [] && $expenses === [] ? NotBillableReason::NothingToBill : null;
|
||||
}
|
||||
|
||||
if ($job->mode === BillingMode::FixedPrice) {
|
||||
if (! $fixedPriceBilled && $job->quotedAmountCents === null) {
|
||||
return NotBillableReason::MissingQuotedAmount;
|
||||
@@ -76,18 +87,26 @@ final class BillingRules
|
||||
|
||||
/**
|
||||
* The job's lines: hours per work type by name (management's billable
|
||||
* hours or the approved ones) or the fixed price, then the expenses.
|
||||
* hours or the approved ones), the fixed price or the monthly fees,
|
||||
* then the expenses.
|
||||
*
|
||||
* @param array<int, int> $approvedByWorkType
|
||||
* @param list<BillableExpense> $expenses
|
||||
* @param array<int, HourlyRate> $rates
|
||||
* @param list<string> $unbilledMonths For a monthly fee job, as Y-m.
|
||||
* @return list<InvoiceLine>
|
||||
*/
|
||||
public function lines(BillableJob $job, DraftJob $draftJob, array $approvedByWorkType, array $expenses, bool $fixedPriceBilled, array $rates, int $vatBasisPoints): array
|
||||
public function lines(BillableJob $job, DraftJob $draftJob, array $approvedByWorkType, array $expenses, bool $fixedPriceBilled, array $rates, int $vatBasisPoints, array $unbilledMonths = []): array
|
||||
{
|
||||
$lines = [];
|
||||
|
||||
if ($job->mode === BillingMode::FixedPrice) {
|
||||
if ($job->mode === BillingMode::MonthlyFee) {
|
||||
if ($job->monthlyFeeCents !== null) {
|
||||
foreach ($unbilledMonths as $month) {
|
||||
$lines[] = InvoiceLine::monthlyFee($job->id, $month, BillingMonths::label($month).' '.$job->label(), $job->monthlyFeeCents, $vatBasisPoints);
|
||||
}
|
||||
}
|
||||
} elseif ($job->mode === BillingMode::FixedPrice) {
|
||||
if (! $fixedPriceBilled && $job->quotedAmountCents !== null) {
|
||||
$lines[] = InvoiceLine::fixedPrice($job->id, $job->label(), $job->quotedAmountCents, $vatBasisPoints);
|
||||
}
|
||||
|
||||
@@ -13,12 +13,17 @@ use App\Modules\Billing\Application\DTOs\BillableJob;
|
||||
use App\Modules\Billing\Application\DTOs\HourlyRate;
|
||||
use App\Modules\Billing\Application\DTOs\JobHours;
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Domain\Enums\BillingMode;
|
||||
use App\Modules\Billing\Domain\Enums\NotBillableReason;
|
||||
use App\Modules\Billing\Domain\ValueObjects\BillingMonths;
|
||||
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
|
||||
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
|
||||
|
||||
/**
|
||||
* What the given jobs have to bill right now, read once for a page or a
|
||||
* use case: the jobs, their unbilled hours and expenses, whose fixed price
|
||||
* is already billed, the hourly rates and the VAT rate.
|
||||
* and which monthly fees are already billed, the hourly rates, the VAT
|
||||
* rate and the material's month.
|
||||
*/
|
||||
final class BillingSnapshot
|
||||
{
|
||||
@@ -39,6 +44,11 @@ final class BillingSnapshot
|
||||
|
||||
public int $vatBasisPoints = 0;
|
||||
|
||||
/** @var array<int, list<string>> */
|
||||
public array $billedMonths = [];
|
||||
|
||||
public string $period = '';
|
||||
|
||||
public function __construct(
|
||||
private BillingJobs $billingJobs,
|
||||
private BillingHours $billingHours,
|
||||
@@ -49,12 +59,14 @@ final class BillingSnapshot
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param string $period Y-m: the material's month.
|
||||
* @param list<int> $jobIds
|
||||
* @param list<BillableJob> $knownJobs Jobs already read, e.g. the done ones.
|
||||
* @param list<BillableJob> $knownJobs Jobs already read, e.g. the candidates.
|
||||
*/
|
||||
public function load(array $jobIds, array $knownJobs = []): self
|
||||
public function load(string $period, array $jobIds, array $knownJobs = []): self
|
||||
{
|
||||
$snapshot = clone $this;
|
||||
$snapshot->period = $period;
|
||||
|
||||
foreach ($knownJobs as $job) {
|
||||
$snapshot->jobs[$job->id] = $job;
|
||||
@@ -67,6 +79,7 @@ final class BillingSnapshot
|
||||
$snapshot->hours = $this->billingHours->forJobs($ids);
|
||||
$snapshot->expenses = $this->billingExpenses->forJobs($ids);
|
||||
$snapshot->fixedPriceBilled = array_fill_keys($this->exports->fixedPriceBilledJobIds($ids), true);
|
||||
$snapshot->billedMonths = $this->exports->billedMonths($ids);
|
||||
$snapshot->rates = $this->prices->hourlyRates();
|
||||
$snapshot->vatBasisPoints = $this->prices->defaultVatBasisPoints();
|
||||
|
||||
@@ -93,7 +106,34 @@ final class BillingSnapshot
|
||||
|
||||
public function reasonNotBillable(BillableJob $job): ?NotBillableReason
|
||||
{
|
||||
return $this->rules->reasonNotBillable($job, $this->hoursOf($job->id), $this->expensesOf($job->id), $this->isFixedPriceBilled($job->id), $this->rates);
|
||||
return $this->rules->reasonNotBillable($job, $this->hoursOf($job->id), $this->expensesOf($job->id), $this->isFixedPriceBilled($job->id), $this->rates, $this->unbilledMonthsOf($job));
|
||||
}
|
||||
|
||||
/**
|
||||
* The months (Y-m) a monthly fee job still has to bill up to the material's month.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function unbilledMonthsOf(BillableJob $job): array
|
||||
{
|
||||
if ($job->mode !== BillingMode::MonthlyFee || $job->billingStartsOn === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return BillingMonths::unbilled($job->billingStartsOn, $job->billingEndsOn, $this->period, $this->billedMonths[$job->id] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* The job's lines with the given hours and expenses (those just marked
|
||||
* billed when the material is created).
|
||||
*
|
||||
* @param array<int, int> $approvedByWorkType
|
||||
* @param list<BillableExpense> $expenses
|
||||
* @return list<InvoiceLine>
|
||||
*/
|
||||
public function linesOf(BillableJob $job, DraftJob $draftJob, array $approvedByWorkType, array $expenses): array
|
||||
{
|
||||
return $this->rules->lines($job, $draftJob, $approvedByWorkType, $expenses, $this->isFixedPriceBilled($job->id), $this->rates, $this->vatBasisPoints, $this->unbilledMonthsOf($job));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,8 +42,8 @@ final class ExportViews
|
||||
return null;
|
||||
}
|
||||
|
||||
$done = $export->isDraft() ? $this->jobs->done() : [];
|
||||
$snapshot = $this->snapshots->load($export->jobIds(), $done);
|
||||
$candidates = $export->isDraft() ? $this->jobs->candidates() : [];
|
||||
$snapshot = $this->snapshots->load($export->period(), $export->jobIds(), $candidates);
|
||||
$onDraft = array_flip($export->jobIds());
|
||||
|
||||
$invoices = [];
|
||||
@@ -52,7 +52,7 @@ final class ExportViews
|
||||
}
|
||||
|
||||
$available = [];
|
||||
foreach ($done as $job) {
|
||||
foreach ($candidates as $job) {
|
||||
if (! isset($onDraft[$job->id])) {
|
||||
$hours = $snapshot->hoursOf($job->id);
|
||||
$available[] = new AvailableJob(
|
||||
@@ -71,6 +71,7 @@ final class ExportViews
|
||||
return new ExportView(
|
||||
(int) $export->id(),
|
||||
$export->status(),
|
||||
$export->period(),
|
||||
$export->createdAt(),
|
||||
$names[$export->createdBy()] ?? '',
|
||||
$export->finalizedAt(),
|
||||
@@ -121,7 +122,7 @@ final class ExportViews
|
||||
}
|
||||
|
||||
$hours = $snapshot->hoursOf($job->id);
|
||||
$lines = $snapshot->rules->lines($job, $draftJob, $hours->approvedByWorkType, $snapshot->expensesOf($job->id), $snapshot->isFixedPriceBilled($job->id), $snapshot->rates, $snapshot->vatBasisPoints);
|
||||
$lines = $snapshot->linesOf($job, $draftJob, $hours->approvedByWorkType, $snapshot->expensesOf($job->id));
|
||||
|
||||
$rows = [];
|
||||
foreach ($snapshot->rules->sortedByName($hours->approvedByWorkType, $snapshot->rates) as $workTypeId => $approved) {
|
||||
|
||||
Reference in New Issue
Block a user