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]>
57 lines
1.9 KiB
PHP
57 lines
1.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use App\Modules\Users\Application\Contracts\Clock;
|
|
|
|
/**
|
|
* 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,
|
|
) {}
|
|
|
|
/**
|
|
* @return int The draft's id.
|
|
*
|
|
* @throws DraftAlreadyOpen
|
|
*/
|
|
public function execute(int $actorUserId): int
|
|
{
|
|
return $this->transactions->run(function () use ($actorUserId): int {
|
|
if ($this->exports->openDraftId() !== null) {
|
|
throw new DraftAlreadyOpen;
|
|
}
|
|
|
|
$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();
|
|
});
|
|
}
|
|
}
|