*/ public array $jobs = []; /** @var array */ public array $hours = []; /** @var array> */ public array $expenses = []; /** @var array */ public array $fixedPriceBilled = []; /** @var array */ public array $rates = []; public int $vatBasisPoints = 0; /** @var array> */ 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 $jobIds * @param list $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 */ 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 */ 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 $approvedByWorkType * @param list $expenses * @return list */ 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 */ public function missingRateNames(BillableJob $job): array { return $this->rules->missingRateNames($this->hoursOf($job->id), $this->rates); } }