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:
@@ -12,11 +12,19 @@ use App\Modules\Billing\Application\DTOs\BillableJob;
|
||||
interface BillingJobs
|
||||
{
|
||||
/**
|
||||
* Done jobs, by job number.
|
||||
* Jobs that may go on a material: done jobs, and monthly fee jobs that
|
||||
* are running or done, by job number.
|
||||
*
|
||||
* @return list<BillableJob>
|
||||
*/
|
||||
public function done(): array;
|
||||
public function candidates(): array;
|
||||
|
||||
/**
|
||||
* Monthly fee jobs that are running or done, by job number.
|
||||
*
|
||||
* @return list<BillableJob>
|
||||
*/
|
||||
public function monthly(): array;
|
||||
|
||||
/**
|
||||
* @param list<int> $jobIds
|
||||
|
||||
@@ -9,7 +9,9 @@ use App\Modules\Billing\Domain\ValueObjects\DraftJob;
|
||||
|
||||
/**
|
||||
* A customer job as billing sees it. A null mode means the job is not
|
||||
* billed this way (not billable, or billed monthly).
|
||||
* billed on a material. A monthly fee job runs from $billingStartsOn
|
||||
* (its start or creation day) to $billingEndsOn (its last day or the day
|
||||
* it was done), if that is set.
|
||||
*/
|
||||
final readonly class BillableJob
|
||||
{
|
||||
@@ -27,7 +29,10 @@ final readonly class BillableJob
|
||||
public ?string $customerReference,
|
||||
public ?BillingMode $mode,
|
||||
public ?int $quotedAmountCents,
|
||||
public bool $isDone,
|
||||
public bool $inBillableStatus,
|
||||
public ?int $monthlyFeeCents = null,
|
||||
public string $billingStartsOn = '',
|
||||
public ?string $billingEndsOn = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -40,6 +45,6 @@ final readonly class BillableJob
|
||||
|
||||
public function toDraftJob(): DraftJob
|
||||
{
|
||||
return new DraftJob($this->id, $this->customerId, $this->billingUnitId, $this->customerReference);
|
||||
return new DraftJob($this->id, $this->customerId, $this->billingUnitId, $this->customerReference, [], $this->mode === BillingMode::MonthlyFee);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ use App\Modules\Billing\Domain\ValueObjects\InvoiceTotals;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* A billing material for its page: a draft with its preview and the done
|
||||
* jobs that may be added, or a created material with its lines.
|
||||
* A billing material for its page: a draft with its preview and the jobs
|
||||
* that may be added, or a created material with its lines. The period
|
||||
* (Y-m) is the month monthly fees are billed up to.
|
||||
*/
|
||||
final readonly class ExportView
|
||||
{
|
||||
@@ -21,6 +22,7 @@ final readonly class ExportView
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public ExportStatus $status,
|
||||
public string $period,
|
||||
public DateTimeImmutable $createdAt,
|
||||
public string $createdByName,
|
||||
public ?DateTimeImmutable $finalizedAt,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -15,7 +15,8 @@ use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Adds a done job to the draft as an invoice of its own, if it has
|
||||
* Adds a done job (or a running monthly fee job) to the draft as an
|
||||
* invoice of its own, if it has
|
||||
* something to bill and nothing stops it from being billed.
|
||||
*/
|
||||
final class AddJobToDraft
|
||||
@@ -40,7 +41,7 @@ final class AddJobToDraft
|
||||
public function execute(int $exportId, int $jobId): void
|
||||
{
|
||||
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId): void {
|
||||
$snapshot = $this->snapshots->load([$jobId]);
|
||||
$snapshot = $this->snapshots->load($export->period(), [$jobId]);
|
||||
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
|
||||
$reason = $snapshot->reasonNotBillable($job);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use App\Modules\Billing\Application\Contracts\BillingJobs;
|
||||
use App\Modules\Billing\Application\DTOs\BillableExpense;
|
||||
use App\Modules\Billing\Application\Services\BillingSnapshot;
|
||||
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\Exceptions\BillingExportNotFound;
|
||||
use App\Modules\Billing\Domain\Exceptions\ExportEmpty;
|
||||
@@ -22,7 +23,8 @@ use App\Modules\Users\Application\Contracts\Clock;
|
||||
* Creates the billing material from its draft (spec 12), all or nothing:
|
||||
* checks every job again, marks the approved hours and billable expenses
|
||||
* billed, forms the invoice lines from what was marked with the current
|
||||
* prices, and moves the jobs to "Lasku luotu".
|
||||
* prices, and moves the jobs to "Lasku luotu". Monthly fee jobs keep
|
||||
* running, so their status stays.
|
||||
*/
|
||||
final class FinalizeBillingExport
|
||||
{
|
||||
@@ -50,7 +52,7 @@ final class FinalizeBillingExport
|
||||
$export = $this->exports->findById($exportId, lock: true) ?? throw new BillingExportNotFound($exportId);
|
||||
$export->ensureDraft();
|
||||
|
||||
$snapshot = $this->snapshots->load($export->jobIds());
|
||||
$snapshot = $this->snapshots->load($export->period(), $export->jobIds());
|
||||
|
||||
foreach ($export->jobIds() as $jobId) {
|
||||
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
|
||||
@@ -72,8 +74,11 @@ final class FinalizeBillingExport
|
||||
fn (BillableExpense $expense): bool => isset($billedExpenseIds[$expense->id]),
|
||||
));
|
||||
|
||||
$linesByJob[$job->id] = $snapshot->rules->lines($job, $draftJob, $billedHours, $billedExpenses, $snapshot->isFixedPriceBilled($job->id), $snapshot->rates, $snapshot->vatBasisPoints);
|
||||
$this->jobs->markInvoiced($job->id, $actorUserId);
|
||||
$linesByJob[$job->id] = $snapshot->linesOf($job, $draftJob, $billedHours, $billedExpenses);
|
||||
|
||||
if ($job->mode !== BillingMode::MonthlyFee) {
|
||||
$this->jobs->markInvoiced($job->id, $actorUserId);
|
||||
}
|
||||
}
|
||||
|
||||
$export->finalize($linesByJob, $actorUserId, $this->clock->now());
|
||||
|
||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\UseCases;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
||||
use App\Modules\Billing\Application\Services\BillingSnapshot;
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Domain\Entities\BillingExport;
|
||||
use App\Modules\Billing\Domain\Exceptions\DraftAlreadyOpen;
|
||||
@@ -11,13 +13,17 @@ use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
use App\Modules\Users\Application\Contracts\Clock;
|
||||
|
||||
/**
|
||||
* Opens a new billing material draft. There is one draft at a time, so
|
||||
* two people never bill the same jobs on separate drafts.
|
||||
* Opens a new billing material draft for the current month, with every
|
||||
* monthly fee job that has something to bill already on it (spec 5.5),
|
||||
* each an invoice of its own. There is one draft at a time, so two people
|
||||
* never bill the same jobs on separate drafts.
|
||||
*/
|
||||
final class OpenBillingDraft
|
||||
{
|
||||
public function __construct(
|
||||
private BillingExportRepository $exports,
|
||||
private BillingJobs $jobs,
|
||||
private BillingSnapshot $snapshots,
|
||||
private TransactionManager $transactions,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
@@ -34,7 +40,17 @@ final class OpenBillingDraft
|
||||
throw new DraftAlreadyOpen;
|
||||
}
|
||||
|
||||
return (int) $this->exports->save(BillingExport::startDraft($actorUserId, $this->clock->now()))->id();
|
||||
$export = BillingExport::startDraft($actorUserId, $this->clock->today());
|
||||
$monthly = $this->jobs->monthly();
|
||||
$snapshot = $this->snapshots->load($export->period(), [], $monthly);
|
||||
|
||||
foreach ($monthly as $job) {
|
||||
if ($snapshot->reasonNotBillable($job) === null) {
|
||||
$export->addJob($job->toDraftJob());
|
||||
}
|
||||
}
|
||||
|
||||
return (int) $this->exports->save($export)->id();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\UseCases;
|
||||
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Domain\Entities\BillingExport;
|
||||
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
|
||||
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
|
||||
use App\Modules\Billing\Domain\Exceptions\InvalidBillingPeriod;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Sets the month (Y-m) up to which the draft bills monthly fees.
|
||||
*/
|
||||
final class SetBillingPeriod
|
||||
{
|
||||
use ChangesDraft;
|
||||
|
||||
public function __construct(BillingExportRepository $exports, TransactionManager $transactions)
|
||||
{
|
||||
$this->exports = $exports;
|
||||
$this->transactions = $transactions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BillingExportNotFound
|
||||
* @throws ExportNotDraft
|
||||
* @throws InvalidBillingPeriod
|
||||
*/
|
||||
public function execute(int $exportId, string $period): void
|
||||
{
|
||||
$this->changeDraft($exportId, function (BillingExport $export) use ($period): void {
|
||||
$export->changePeriod($period);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user