Files
Toiminnanohjaus/app/Modules/Billing/Infrastructure/JobsForBilling.php
T
petriandClaude Opus 5.5 5f9423a3c3 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]>
2026-11-24 13:23:46 +02:00

113 lines
4.0 KiB
PHP

<?php
namespace App\Modules\Billing\Infrastructure;
use App\Modules\Billing\Application\Contracts\BillingJobs;
use App\Modules\Billing\Application\DTOs\BillableJob;
use App\Modules\Billing\Domain\Enums\BillingMode;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\DTOs\CustomerSummary;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\DTOs\JobBillingInfo;
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\PaymentBasis;
/**
* Customer jobs for billing through the Jobs module's queries and status
* change, with the customer's type and identity code from Customers (only
* whether there is a code, never the code).
*/
class JobsForBilling implements BillingJobs
{
/** Statuses in which a monthly fee job is billed: running or done. */
private const array MONTHLY_STATUSES = [JobStatus::Order, JobStatus::InProduction, JobStatus::OnHold, JobStatus::Done];
public function __construct(
private CustomerJobQueries $jobs,
private CustomerQueries $customers,
private ChangeJobStatus $changeStatus,
) {}
public function candidates(): array
{
$jobs = [];
foreach ([...$this->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<JobBillingInfo> $jobs
* @return list<BillableJob>
*/
private function toBillable(array $jobs): array
{
/** @var array<int, CustomerSummary|null> $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);
}
}