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]>
This commit is contained in:
2026-11-24 13:23:46 +02:00
co-authored by Claude Opus 5.5
parent 97bd23bed5
commit 5f9423a3c3
49 changed files with 799 additions and 82 deletions
@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace App\Modules\Billing\Application\UseCases;
use App\Modules\Billing\Application\Contracts\BillingJobs;
use App\Modules\Billing\Application\Services\BillingSnapshot;
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
use App\Modules\Billing\Domain\Entities\BillingExport;
use App\Modules\Billing\Domain\Exceptions\DraftAlreadyOpen;
@@ -11,13 +13,17 @@ use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Opens a new billing material draft. There is one draft at a time, so
* two people never bill the same jobs on separate drafts.
* Opens a new billing material draft for the current month, with every
* monthly fee job that has something to bill already on it (spec 5.5),
* each an invoice of its own. There is one draft at a time, so two people
* never bill the same jobs on separate drafts.
*/
final class OpenBillingDraft
{
public function __construct(
private BillingExportRepository $exports,
private BillingJobs $jobs,
private BillingSnapshot $snapshots,
private TransactionManager $transactions,
private Clock $clock,
) {}
@@ -34,7 +40,17 @@ final class OpenBillingDraft
throw new DraftAlreadyOpen;
}
return (int) $this->exports->save(BillingExport::startDraft($actorUserId, $this->clock->now()))->id();
$export = BillingExport::startDraft($actorUserId, $this->clock->today());
$monthly = $this->jobs->monthly();
$snapshot = $this->snapshots->load($export->period(), [], $monthly);
foreach ($monthly as $job) {
if ($snapshot->reasonNotBillable($job) === null) {
$export->addJob($job->toDraftJob());
}
}
return (int) $this->exports->save($export)->id();
});
}
}