$expenses * @param array $rates */ public function reasonNotBillable(BillableJob $job, JobHours $hours, array $expenses, bool $fixedPriceBilled, array $rates): ?NotBillableReason { if (! $job->isDone) { return NotBillableReason::WrongStatus; } if ($job->mode === null || $job->isInternal) { return NotBillableReason::NotBillableBasis; } if ($job->isConsumer && ! $job->hasIdentityCode) { return NotBillableReason::MissingIdentityCode; } if ($job->mode === BillingMode::FixedPrice) { if (! $fixedPriceBilled && $job->quotedAmountCents === null) { return NotBillableReason::MissingQuotedAmount; } return $fixedPriceBilled && $expenses === [] ? NotBillableReason::NothingToBill : null; } if ($this->missingRateNames($hours, $rates) !== []) { return NotBillableReason::MissingHourlyRate; } return $hours->approvedQuarters() === 0 && $expenses === [] ? NotBillableReason::NothingToBill : null; } /** * Work types with approved hours but no hourly rate, for an hours job. * * @param array $rates * @return list */ public function missingRateNames(JobHours $hours, array $rates): array { $names = []; foreach ($hours->approvedByWorkType as $workTypeId => $quarters) { if ($quarters > 0 && ($rates[$workTypeId]->rateCents ?? null) === null) { $names[] = $rates[$workTypeId]->name ?? (string) $workTypeId; } } sort($names); return $names; } /** * 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. * * @param array $approvedByWorkType * @param list $expenses * @param array $rates * @return list */ public function lines(BillableJob $job, DraftJob $draftJob, array $approvedByWorkType, array $expenses, bool $fixedPriceBilled, array $rates, int $vatBasisPoints): array { $lines = []; if ($job->mode === BillingMode::FixedPrice) { if (! $fixedPriceBilled && $job->quotedAmountCents !== null) { $lines[] = InvoiceLine::fixedPrice($job->id, $job->label(), $job->quotedAmountCents, $vatBasisPoints); } } else { foreach ($this->sortedByName($approvedByWorkType, $rates) as $workTypeId => $approved) { $rate = $rates[$workTypeId] ?? null; if ($approved <= 0 || $rate?->rateCents === null) { continue; } $lines[] = InvoiceLine::hours($job->id, $workTypeId, $rate->name, $approved, $draftJob->billableOf($workTypeId, $approved), $rate->rateCents, $vatBasisPoints); } } foreach ($expenses as $expense) { $lines[] = InvoiceLine::expense($job->id, $expense->id, $expense->description, $expense->amountCents, $vatBasisPoints); } return $lines; } /** * @param array $quarters * @param array $rates * @return array */ public function sortedByName(array $quarters, array $rates): array { uksort($quarters, fn (int $a, int $b): int => strcmp($rates[$a]->name ?? '', $rates[$b]->name ?? '') ?: $a <=> $b); return $quarters; } }