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