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:
@@ -15,7 +15,8 @@ use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Adds a done job to the draft as an invoice of its own, if it has
|
||||
* 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
|
||||
@@ -40,7 +41,7 @@ final class AddJobToDraft
|
||||
public function execute(int $exportId, int $jobId): void
|
||||
{
|
||||
$this->changeDraft($exportId, function (BillingExport $export) use ($jobId): void {
|
||||
$snapshot = $this->snapshots->load([$jobId]);
|
||||
$snapshot = $this->snapshots->load($export->period(), [$jobId]);
|
||||
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
|
||||
$reason = $snapshot->reasonNotBillable($job);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use App\Modules\Billing\Application\Contracts\BillingJobs;
|
||||
use App\Modules\Billing\Application\DTOs\BillableExpense;
|
||||
use App\Modules\Billing\Application\Services\BillingSnapshot;
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Domain\Enums\BillingMode;
|
||||
use App\Modules\Billing\Domain\Enums\NotBillableReason;
|
||||
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
|
||||
use App\Modules\Billing\Domain\Exceptions\ExportEmpty;
|
||||
@@ -22,7 +23,8 @@ use App\Modules\Users\Application\Contracts\Clock;
|
||||
* Creates the billing material from its draft (spec 12), all or nothing:
|
||||
* checks every job again, marks the approved hours and billable expenses
|
||||
* billed, forms the invoice lines from what was marked with the current
|
||||
* prices, and moves the jobs to "Lasku luotu".
|
||||
* prices, and moves the jobs to "Lasku luotu". Monthly fee jobs keep
|
||||
* running, so their status stays.
|
||||
*/
|
||||
final class FinalizeBillingExport
|
||||
{
|
||||
@@ -50,7 +52,7 @@ final class FinalizeBillingExport
|
||||
$export = $this->exports->findById($exportId, lock: true) ?? throw new BillingExportNotFound($exportId);
|
||||
$export->ensureDraft();
|
||||
|
||||
$snapshot = $this->snapshots->load($export->jobIds());
|
||||
$snapshot = $this->snapshots->load($export->period(), $export->jobIds());
|
||||
|
||||
foreach ($export->jobIds() as $jobId) {
|
||||
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
|
||||
@@ -72,8 +74,11 @@ final class FinalizeBillingExport
|
||||
fn (BillableExpense $expense): bool => isset($billedExpenseIds[$expense->id]),
|
||||
));
|
||||
|
||||
$linesByJob[$job->id] = $snapshot->rules->lines($job, $draftJob, $billedHours, $billedExpenses, $snapshot->isFixedPriceBilled($job->id), $snapshot->rates, $snapshot->vatBasisPoints);
|
||||
$this->jobs->markInvoiced($job->id, $actorUserId);
|
||||
$linesByJob[$job->id] = $snapshot->linesOf($job, $draftJob, $billedHours, $billedExpenses);
|
||||
|
||||
if ($job->mode !== BillingMode::MonthlyFee) {
|
||||
$this->jobs->markInvoiced($job->id, $actorUserId);
|
||||
}
|
||||
}
|
||||
|
||||
$export->finalize($linesByJob, $actorUserId, $this->clock->now());
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Billing\Application\UseCases;
|
||||
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Domain\Entities\BillingExport;
|
||||
use App\Modules\Billing\Domain\Exceptions\BillingExportNotFound;
|
||||
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
|
||||
use App\Modules\Billing\Domain\Exceptions\InvalidBillingPeriod;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Sets the month (Y-m) up to which the draft bills monthly fees.
|
||||
*/
|
||||
final class SetBillingPeriod
|
||||
{
|
||||
use ChangesDraft;
|
||||
|
||||
public function __construct(BillingExportRepository $exports, TransactionManager $transactions)
|
||||
{
|
||||
$this->exports = $exports;
|
||||
$this->transactions = $transactions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BillingExportNotFound
|
||||
* @throws ExportNotDraft
|
||||
* @throws InvalidBillingPeriod
|
||||
*/
|
||||
public function execute(int $exportId, string $period): void
|
||||
{
|
||||
$this->changeDraft($exportId, function (BillingExport $export) use ($period): void {
|
||||
$export->changePeriod($period);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user