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]>
147 lines
5.0 KiB
PHP
147 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Billing\Application\Services;
|
|
|
|
use App\Modules\Billing\Application\Contracts\BillingExpenses;
|
|
use App\Modules\Billing\Application\Contracts\BillingHours;
|
|
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
|
use App\Modules\Billing\Application\Contracts\BillingPrices;
|
|
use App\Modules\Billing\Application\DTOs\BillableExpense;
|
|
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
|
|
* and which monthly fees are already billed, the hourly rates, the VAT
|
|
* rate and the material's month.
|
|
*/
|
|
final class BillingSnapshot
|
|
{
|
|
/** @var array<int, BillableJob> */
|
|
public array $jobs = [];
|
|
|
|
/** @var array<int, JobHours> */
|
|
public array $hours = [];
|
|
|
|
/** @var array<int, list<BillableExpense>> */
|
|
public array $expenses = [];
|
|
|
|
/** @var array<int, true> */
|
|
public array $fixedPriceBilled = [];
|
|
|
|
/** @var array<int, HourlyRate> */
|
|
public array $rates = [];
|
|
|
|
public int $vatBasisPoints = 0;
|
|
|
|
/** @var array<int, list<string>> */
|
|
public array $billedMonths = [];
|
|
|
|
public string $period = '';
|
|
|
|
public function __construct(
|
|
private BillingJobs $billingJobs,
|
|
private BillingHours $billingHours,
|
|
private BillingExpenses $billingExpenses,
|
|
private BillingPrices $prices,
|
|
private BillingExportRepository $exports,
|
|
public readonly BillingRules $rules,
|
|
) {}
|
|
|
|
/**
|
|
* @param string $period Y-m: the material's month.
|
|
* @param list<int> $jobIds
|
|
* @param list<BillableJob> $knownJobs Jobs already read, e.g. the candidates.
|
|
*/
|
|
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;
|
|
}
|
|
|
|
$missing = array_values(array_diff($jobIds, array_keys($snapshot->jobs)));
|
|
$snapshot->jobs += $this->billingJobs->find($missing);
|
|
|
|
$ids = array_keys($snapshot->jobs);
|
|
$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();
|
|
|
|
return $snapshot;
|
|
}
|
|
|
|
public function hoursOf(int $jobId): JobHours
|
|
{
|
|
return $this->hours[$jobId] ?? new JobHours($jobId);
|
|
}
|
|
|
|
/**
|
|
* @return list<BillableExpense>
|
|
*/
|
|
public function expensesOf(int $jobId): array
|
|
{
|
|
return $this->expenses[$jobId] ?? [];
|
|
}
|
|
|
|
public function isFixedPriceBilled(int $jobId): bool
|
|
{
|
|
return isset($this->fixedPriceBilled[$jobId]);
|
|
}
|
|
|
|
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, $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));
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
public function missingRateNames(BillableJob $job): array
|
|
{
|
|
return $this->rules->missingRateNames($this->hoursOf($job->id), $this->rates);
|
|
}
|
|
}
|