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]>
56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Billing\Application\UseCases;
|
|
|
|
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\Enums\NotBillableReason;
|
|
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
|
|
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
|
|
use App\Modules\Billing\Domain\Exceptions\JobAlreadyInDraft;
|
|
use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
|
|
/**
|
|
* Adds a done job (or a running monthly fee job) to the draft as an
|
|
* invoice of its own, if it has
|
|
* something to bill and nothing stops it from being billed.
|
|
*/
|
|
final class AddJobToDraft
|
|
{
|
|
use ChangesDraft;
|
|
|
|
public function __construct(
|
|
BillingExportRepository $exports,
|
|
TransactionManager $transactions,
|
|
private BillingSnapshot $snapshots,
|
|
) {
|
|
$this->exports = $exports;
|
|
$this->transactions = $transactions;
|
|
}
|
|
|
|
/**
|
|
* @throws BillingExportNotFound
|
|
* @throws ExportNotDraft
|
|
* @throws JobAlreadyInDraft
|
|
* @throws JobNotBillable
|
|
*/
|
|
public function execute(int $exportId, int $jobId): void
|
|
{
|
|
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId): void {
|
|
$snapshot = $this->snapshots->load($export->period(), [$jobId]);
|
|
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
|
|
$reason = $snapshot->reasonNotBillable($job);
|
|
|
|
if ($reason !== null) {
|
|
throw new JobNotBillable($jobId, $reason);
|
|
}
|
|
|
|
$export->addJob($job->toDraftJob());
|
|
});
|
|
}
|
|
}
|