Files
petriandClaude Opus 5.5 97bd23bed5 Add the billing material with drafts, combined invoices and billed marks
Management builds the monthly billing material (laskuaineisto) as a
draft: done jobs are added as invoices of their own, combined when the
customer, billing unit and reference allow it, and their billable hours
are set per work type. Creating the material checks the jobs again,
marks the approved hours and billable expenses billed, forms invoice
lines with price and VAT snapshots and moves the jobs to "Lasku luotu".
Fixed price jobs bill their quoted amount once. Billed hours keep their
approval and billed expenses can no longer change. The Fennoa export
and monthly fee lines come later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 13:08:24 +02:00

58 lines
2.0 KiB
PHP

<?php
namespace App\Modules\Billing\Infrastructure;
use App\Modules\Billing\Application\Contracts\BillingExpenses;
use App\Modules\Billing\Application\DTOs\BillableExpense;
use App\Modules\Expenses\Application\Contracts\ExpenseQueries;
use App\Modules\Expenses\Application\DTOs\ExpenseSummary;
use App\Modules\Expenses\Application\UseCases\MarkJobExpensesBilled;
use App\Modules\Expenses\Domain\Enums\ExpenseCategory;
use App\Modules\Expenses\Domain\ValueObjects\Kilometers;
use Carbon\CarbonImmutable;
/**
* Billable expenses through the Expenses module. The line text is the
* category, the day and what was bought (or the trip).
*/
class ExpensesForBilling implements BillingExpenses
{
public function __construct(
private ExpenseQueries $expenses,
private MarkJobExpensesBilled $markBilled,
) {}
public function forJobs(array $jobIds): array
{
$result = array_fill_keys($jobIds, []);
foreach ($this->expenses->unbilledBillable($jobIds) as $expense) {
$result[$expense->job->id][] = new BillableExpense($expense->id, $expense->job->id, $this->description($expense), (int) $expense->billedAmountCents);
}
return $result;
}
public function markBilled(int $jobId, int $billingExportId): array
{
return $this->markBilled->execute($jobId, $billingExportId);
}
private function description(ExpenseSummary $expense): string
{
$what = $expense->category === ExpenseCategory::Mileage && $expense->kilometerTenths !== null
? __('expenses.mileage_summary', [
'km' => (new Kilometers($expense->kilometerTenths))->format(),
'destination' => $expense->destination,
'purpose' => $expense->purpose,
])
: (string) $expense->description;
return __('billing.expense_line', [
'category' => __('expenses.categories.'.$expense->category->value),
'day' => CarbonImmutable::parse($expense->spentOn)->format('j.n.Y'),
'what' => $what,
]);
}
}