Add the billing material with drafts, combined invoices and billed marks

Management builds the monthly billing material (laskuaineisto) as a
draft: done jobs are added as invoices of their own, combined when the
customer, billing unit and reference allow it, and their billable hours
are set per work type. Creating the material checks the jobs again,
marks the approved hours and billable expenses billed, forms invoice
lines with price and VAT snapshots and moves the jobs to "Lasku luotu".
Fixed price jobs bill their quoted amount once. Billed hours keep their
approval and billed expenses can no longer change. The Fennoa export
and monthly fee lines come later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 13:08:24 +02:00
co-authored by Claude Opus 5.5
parent 6de5d9bef1
commit 97bd23bed5
98 changed files with 4887 additions and 22 deletions
@@ -0,0 +1,91 @@
<?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
{
public function __construct(
private CustomerJobQueries $jobs,
private CustomerQueries $customers,
private ChangeJobStatus $changeStatus,
) {}
public function done(): array
{
return $this->toBillable($this->jobs->billingInfoWithStatus(JobStatus::Done));
}
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;
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,
match ($job->paymentBasis) {
PaymentBasis::FixedQuote => BillingMode::FixedPrice,
PaymentBasis::NotBillable, PaymentBasis::MonthlyFee => null,
default => BillingMode::Hours,
},
$job->quotedAmountCents,
$job->status === JobStatus::Done,
);
}, $jobs);
}
}