jobs->billingInfoWithStatus(JobStatus::Done), ...$this->jobs->billingInfoMonthly()] as $job) { $jobs[$job->id] = $job; } uasort($jobs, fn (JobBillingInfo $a, JobBillingInfo $b): int => strcmp($a->number, $b->number)); return $this->toBillable(array_values($jobs)); } public function monthly(): array { return $this->toBillable($this->jobs->billingInfoMonthly()); } public function find(array $jobIds): array { $result = []; foreach ($this->toBillable(array_values($this->jobs->billingInfoOf($jobIds))) as $job) { $result[$job->id] = $job; } return $result; } public function markInvoiced(int $jobId, int $actorUserId): void { $this->changeStatus->execute($jobId, JobStatus::Invoiced, $actorUserId); } /** * @param list $jobs * @return list */ private function toBillable(array $jobs): array { /** @var array $customers */ $customers = []; foreach ($jobs as $job) { if (! array_key_exists($job->customerId, $customers)) { $customers[$job->customerId] = $this->customers->findSummary($job->customerId); } } return array_map(function (JobBillingInfo $job) use ($customers): BillableJob { $customer = $customers[$job->customerId] ?? null; $mode = match ($job->paymentBasis) { PaymentBasis::FixedQuote => BillingMode::FixedPrice, PaymentBasis::MonthlyFee => BillingMode::MonthlyFee, PaymentBasis::NotBillable => null, default => BillingMode::Hours, }; $ends = array_filter([$job->dueOn, $job->completedOn]); return new BillableJob( $job->id, $job->number, $job->name, $job->customerId, $job->customerName, $customer?->type === CustomerType::Consumer, $customer->isInternal ?? false, $customer?->hasPersonalIdentityCode() ?? false, $job->billingUnitId, $job->billingUnitName, $job->customerReference, $mode, $job->quotedAmountCents, $mode === BillingMode::MonthlyFee ? in_array($job->status, self::MONTHLY_STATUSES, true) : $job->status === JobStatus::Done, $job->monthlyFeeCents, $job->startsOn ?? $job->createdOn, $ends === [] ? null : min($ends), ); }, $jobs); } }