- Company details get an optional "Valikon nimi", shown at the left of the main menu; it falls back to the application name when empty. - A billing material gets its running number (billing_exports.number) only when it is created, so discarded drafts leave no gaps. Drafts have no number; existing created materials are numbered by the migration, and the CSV file name uses the number. - TaskAssigneeMigrationTest rolls back from its own migration onward instead of assuming it is the last one. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
92 lines
3.6 KiB
PHP
92 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Billing\Application\UseCases;
|
|
|
|
use App\Modules\Billing\Application\Contracts\BillingExpenses;
|
|
use App\Modules\Billing\Application\Contracts\BillingHours;
|
|
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;
|
|
use App\Modules\Billing\Domain\Exceptions\ExportNotDraft;
|
|
use App\Modules\Billing\Domain\Exceptions\JobNotBillable;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
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, gives the material the next running number (reserved only now, so
|
|
* discarded drafts leave no gaps), and moves the jobs to "Lasku luotu". Monthly fee jobs keep
|
|
* running, so their status stays.
|
|
*/
|
|
final class FinalizeBillingExport
|
|
{
|
|
public function __construct(
|
|
private BillingExportRepository $exports,
|
|
private BillingJobs $jobs,
|
|
private BillingHours $hours,
|
|
private BillingExpenses $expenses,
|
|
private BillingSnapshot $snapshots,
|
|
private TransactionManager $transactions,
|
|
private Clock $clock,
|
|
) {}
|
|
|
|
/**
|
|
* @return int How many invoices the material has.
|
|
*
|
|
* @throws BillingExportNotFound
|
|
* @throws ExportNotDraft
|
|
* @throws ExportEmpty
|
|
* @throws JobNotBillable
|
|
*/
|
|
public function execute(int $exportId, int $actorUserId): int
|
|
{
|
|
return $this->transactions->run(function () use ($exportId, $actorUserId): int {
|
|
$export = $this->exports->findById($exportId, lock: true) ?? throw new BillingExportNotFound($exportId);
|
|
$export->ensureDraft();
|
|
|
|
$snapshot = $this->snapshots->load($export->period(), $export->jobIds());
|
|
|
|
foreach ($export->jobIds() as $jobId) {
|
|
$job = $snapshot->jobs[$jobId] ?? throw new JobNotBillable($jobId, NotBillableReason::WrongStatus);
|
|
$reason = $snapshot->reasonNotBillable($job);
|
|
|
|
if ($reason !== null) {
|
|
throw new JobNotBillable($jobId, $reason);
|
|
}
|
|
}
|
|
|
|
$linesByJob = [];
|
|
|
|
foreach ($export->jobs() as $draftJob) {
|
|
$job = $snapshot->jobs[$draftJob->jobId];
|
|
$billedHours = $this->hours->markBilled($job->id, $exportId);
|
|
$billedExpenseIds = array_flip($this->expenses->markBilled($job->id, $exportId));
|
|
$billedExpenses = array_values(array_filter(
|
|
$snapshot->expensesOf($job->id),
|
|
fn (BillableExpense $expense): bool => isset($billedExpenseIds[$expense->id]),
|
|
));
|
|
|
|
$linesByJob[$job->id] = $snapshot->linesOf($job, $draftJob, $billedHours, $billedExpenses);
|
|
|
|
if ($job->mode !== BillingMode::MonthlyFee) {
|
|
$this->jobs->markInvoiced($job->id, $actorUserId);
|
|
}
|
|
}
|
|
|
|
$export->finalize($linesByJob, $this->exports->nextNumber(), $actorUserId, $this->clock->now());
|
|
$this->exports->save($export);
|
|
|
|
return count($export->invoices());
|
|
});
|
|
}
|
|
}
|