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]>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingHours;
|
||||
use App\Modules\Billing\Application\DTOs\JobHours;
|
||||
use App\Modules\TimeTracking\Application\Contracts\TimeEntryQueries;
|
||||
use App\Modules\TimeTracking\Application\DTOs\JobBillingHours;
|
||||
use App\Modules\TimeTracking\Application\UseCases\MarkJobHoursBilled;
|
||||
|
||||
/**
|
||||
* Hours for billing through the TimeTracking module.
|
||||
*/
|
||||
class HoursForBilling implements BillingHours
|
||||
{
|
||||
public function __construct(
|
||||
private TimeEntryQueries $entries,
|
||||
private MarkJobHoursBilled $markBilled,
|
||||
) {}
|
||||
|
||||
public function forJobs(array $jobIds): array
|
||||
{
|
||||
return array_map(
|
||||
fn (JobBillingHours $hours): JobHours => new JobHours($hours->jobId, $hours->approvedQuartersByWorkType, $hours->unapprovedQuarters),
|
||||
$this->entries->billingHours($jobIds),
|
||||
);
|
||||
}
|
||||
|
||||
public function markBilled(int $jobId, int $billingExportId): array
|
||||
{
|
||||
return $this->markBilled->execute($jobId, $billingExportId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingJobs;
|
||||
use App\Modules\Billing\Application\DTOs\BillableJob;
|
||||
use App\Modules\Billing\Domain\Enums\BillingMode;
|
||||
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
||||
use App\Modules\Customers\Application\DTOs\CustomerSummary;
|
||||
use App\Modules\Customers\Domain\Enums\CustomerType;
|
||||
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\JobBillingInfo;
|
||||
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Domain\Enums\PaymentBasis;
|
||||
|
||||
/**
|
||||
* Customer jobs for billing through the Jobs module's queries and status
|
||||
* change, with the customer's type and identity code from Customers (only
|
||||
* whether there is a code, never the code).
|
||||
*/
|
||||
class JobsForBilling implements BillingJobs
|
||||
{
|
||||
public function __construct(
|
||||
private CustomerJobQueries $jobs,
|
||||
private CustomerQueries $customers,
|
||||
private ChangeJobStatus $changeStatus,
|
||||
) {}
|
||||
|
||||
public function done(): array
|
||||
{
|
||||
return $this->toBillable($this->jobs->billingInfoWithStatus(JobStatus::Done));
|
||||
}
|
||||
|
||||
public function find(array $jobIds): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this->toBillable(array_values($this->jobs->billingInfoOf($jobIds))) as $job) {
|
||||
$result[$job->id] = $job;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function markInvoiced(int $jobId, int $actorUserId): void
|
||||
{
|
||||
$this->changeStatus->execute($jobId, JobStatus::Invoiced, $actorUserId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<JobBillingInfo> $jobs
|
||||
* @return list<BillableJob>
|
||||
*/
|
||||
private function toBillable(array $jobs): array
|
||||
{
|
||||
/** @var array<int, CustomerSummary|null> $customers */
|
||||
$customers = [];
|
||||
|
||||
foreach ($jobs as $job) {
|
||||
if (! array_key_exists($job->customerId, $customers)) {
|
||||
$customers[$job->customerId] = $this->customers->findSummary($job->customerId);
|
||||
}
|
||||
}
|
||||
|
||||
return array_map(function (JobBillingInfo $job) use ($customers): BillableJob {
|
||||
$customer = $customers[$job->customerId] ?? null;
|
||||
|
||||
return new BillableJob(
|
||||
$job->id,
|
||||
$job->number,
|
||||
$job->name,
|
||||
$job->customerId,
|
||||
$job->customerName,
|
||||
$customer?->type === CustomerType::Consumer,
|
||||
$customer->isInternal ?? false,
|
||||
$customer?->hasPersonalIdentityCode() ?? false,
|
||||
$job->billingUnitId,
|
||||
$job->billingUnitName,
|
||||
$job->customerReference,
|
||||
match ($job->paymentBasis) {
|
||||
PaymentBasis::FixedQuote => BillingMode::FixedPrice,
|
||||
PaymentBasis::NotBillable, PaymentBasis::MonthlyFee => null,
|
||||
default => BillingMode::Hours,
|
||||
},
|
||||
$job->quotedAmountCents,
|
||||
$job->status === JobStatus::Done,
|
||||
);
|
||||
}, $jobs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Billing\Domain\Enums\ExportStatus;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Billing Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property ExportStatus $status
|
||||
* @property int $created_by
|
||||
* @property int|null $finalized_by
|
||||
* @property Carbon|null $finalized_at
|
||||
* @property Carbon $created_at
|
||||
*/
|
||||
#[Table('billing_exports')]
|
||||
#[Fillable(['status', 'created_by', 'finalized_by', 'finalized_at'])]
|
||||
class BillingExportModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return HasMany<BillingInvoiceModel, $this>
|
||||
*/
|
||||
public function invoices(): HasMany
|
||||
{
|
||||
return $this->hasMany(BillingInvoiceModel::class, 'billing_export_id')->orderBy('sort_order')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'status' => ExportStatus::class,
|
||||
'created_by' => 'integer',
|
||||
'finalized_by' => 'integer',
|
||||
'finalized_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* A job on an invoice of a billing material, with its job's billing
|
||||
* details as the draft knows them. Persistence model, used only inside the
|
||||
* Billing Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $billing_invoice_id
|
||||
* @property int $customer_job_id
|
||||
* @property int $customer_id
|
||||
* @property int|null $billing_unit_id
|
||||
* @property string|null $customer_reference
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('billing_invoice_jobs')]
|
||||
#[Fillable(['billing_invoice_id', 'customer_job_id', 'customer_id', 'billing_unit_id', 'customer_reference', 'sort_order'])]
|
||||
class BillingInvoiceJobModel extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* @return HasMany<BillingJobHoursModel, $this>
|
||||
*/
|
||||
public function hours(): HasMany
|
||||
{
|
||||
return $this->hasMany(BillingJobHoursModel::class, 'billing_invoice_job_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'billing_invoice_id' => 'integer',
|
||||
'customer_job_id' => 'integer',
|
||||
'customer_id' => 'integer',
|
||||
'billing_unit_id' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Billing Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $billing_export_id
|
||||
* @property int $customer_id
|
||||
* @property int|null $billing_unit_id
|
||||
* @property string|null $customer_reference
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('billing_invoices')]
|
||||
#[Fillable(['billing_export_id', 'customer_id', 'billing_unit_id', 'customer_reference', 'sort_order'])]
|
||||
class BillingInvoiceModel extends Model
|
||||
{
|
||||
/**
|
||||
* @return HasMany<BillingInvoiceJobModel, $this>
|
||||
*/
|
||||
public function jobs(): HasMany
|
||||
{
|
||||
return $this->hasMany(BillingInvoiceJobModel::class, 'billing_invoice_id')->orderBy('sort_order')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<BillingLineModel, $this>
|
||||
*/
|
||||
public function lines(): HasMany
|
||||
{
|
||||
return $this->hasMany(BillingLineModel::class, 'billing_invoice_id')->orderBy('sort_order')->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'billing_export_id' => 'integer',
|
||||
'customer_id' => 'integer',
|
||||
'billing_unit_id' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Billing Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $billing_invoice_job_id
|
||||
* @property int $work_type_id
|
||||
* @property int $billable_quarters
|
||||
*/
|
||||
#[Table('billing_job_hours')]
|
||||
#[Fillable(['billing_invoice_job_id', 'work_type_id', 'billable_quarters'])]
|
||||
class BillingJobHoursModel extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'billing_invoice_job_id' => 'integer',
|
||||
'work_type_id' => 'integer',
|
||||
'billable_quarters' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use App\Modules\Billing\Domain\Enums\LineKind;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Billing Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $billing_invoice_id
|
||||
* @property int $customer_job_id
|
||||
* @property LineKind $kind
|
||||
* @property int|null $work_type_id
|
||||
* @property int|null $expense_id
|
||||
* @property string $description
|
||||
* @property int|null $recorded_quarters
|
||||
* @property int|null $quantity_quarters
|
||||
* @property int $unit_price_cents
|
||||
* @property int $vat_basis_points
|
||||
* @property int $net_cents
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('billing_lines')]
|
||||
#[Fillable([
|
||||
'billing_invoice_id', 'customer_job_id', 'kind', 'work_type_id', 'expense_id', 'description',
|
||||
'recorded_quarters', 'quantity_quarters', 'unit_price_cents', 'vat_basis_points', 'net_cents', 'sort_order',
|
||||
])]
|
||||
class BillingLineModel extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'billing_invoice_id' => 'integer',
|
||||
'customer_job_id' => 'integer',
|
||||
'kind' => LineKind::class,
|
||||
'work_type_id' => 'integer',
|
||||
'expense_id' => 'integer',
|
||||
'recorded_quarters' => 'integer',
|
||||
'quantity_quarters' => 'integer',
|
||||
'unit_price_cents' => 'integer',
|
||||
'vat_basis_points' => 'integer',
|
||||
'net_cents' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Domain\Entities\BillingExport;
|
||||
use App\Modules\Billing\Domain\Entities\BillingInvoice;
|
||||
use App\Modules\Billing\Domain\Enums\ExportStatus;
|
||||
use App\Modules\Billing\Domain\Enums\LineKind;
|
||||
use App\Modules\Billing\Domain\ValueObjects\DraftJob;
|
||||
use App\Modules\Billing\Domain\ValueObjects\InvoiceLine;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingExportModel;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingInvoiceJobModel;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingInvoiceModel;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingJobHoursModel;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingLineModel;
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* Saving replaces a material's invoices with those of the aggregate: a
|
||||
* draft is small, and its invoices have no identity of their own.
|
||||
*/
|
||||
class EloquentBillingExportRepository implements BillingExportRepository
|
||||
{
|
||||
public function findById(int $id, bool $lock = false): ?BillingExport
|
||||
{
|
||||
$model = BillingExportModel::query()
|
||||
->when($lock, fn ($query) => $query->lockForUpdate())
|
||||
->with(['invoices.jobs.hours', 'invoices.lines'])
|
||||
->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function openDraftId(): ?int
|
||||
{
|
||||
$id = BillingExportModel::query()->where('status', ExportStatus::Draft)->value('id');
|
||||
|
||||
return $id === null ? null : (int) $id;
|
||||
}
|
||||
|
||||
public function save(BillingExport $export): BillingExport
|
||||
{
|
||||
$model = $export->id() === null ? new BillingExportModel : BillingExportModel::query()->findOrFail($export->id());
|
||||
|
||||
$model->fill([
|
||||
'status' => $export->status(),
|
||||
'created_by' => $export->createdBy(),
|
||||
'finalized_by' => $export->finalizedBy(),
|
||||
'finalized_at' => $export->finalizedAt()?->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
if ($export->id() === null) {
|
||||
$model->created_at = Carbon::instance($export->createdAt());
|
||||
}
|
||||
|
||||
$model->save();
|
||||
|
||||
BillingInvoiceModel::query()->where('billing_export_id', $model->id)->delete();
|
||||
|
||||
foreach ($export->invoices() as $invoiceOrder => $invoice) {
|
||||
$invoiceModel = BillingInvoiceModel::query()->create([
|
||||
'billing_export_id' => $model->id,
|
||||
'customer_id' => $invoice->customerId(),
|
||||
'billing_unit_id' => $invoice->billingUnitId(),
|
||||
'customer_reference' => $invoice->reference(),
|
||||
'sort_order' => $invoiceOrder,
|
||||
]);
|
||||
|
||||
foreach ($invoice->jobs() as $jobOrder => $job) {
|
||||
$jobModel = BillingInvoiceJobModel::query()->create([
|
||||
'billing_invoice_id' => $invoiceModel->id,
|
||||
'customer_job_id' => $job->jobId,
|
||||
'customer_id' => $job->customerId,
|
||||
'billing_unit_id' => $job->billingUnitId,
|
||||
'customer_reference' => $job->reference,
|
||||
'sort_order' => $jobOrder,
|
||||
]);
|
||||
|
||||
foreach ($job->billableQuarters as $workTypeId => $quarters) {
|
||||
BillingJobHoursModel::query()->create([
|
||||
'billing_invoice_job_id' => $jobModel->id,
|
||||
'work_type_id' => $workTypeId,
|
||||
'billable_quarters' => $quarters,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($invoice->lines() as $lineOrder => $line) {
|
||||
BillingLineModel::query()->create([
|
||||
'billing_invoice_id' => $invoiceModel->id,
|
||||
'customer_job_id' => $line->jobId,
|
||||
'kind' => $line->kind,
|
||||
'work_type_id' => $line->workTypeId,
|
||||
'expense_id' => $line->expenseId,
|
||||
'description' => mb_substr($line->description, 0, 255),
|
||||
'recorded_quarters' => $line->recordedQuarters,
|
||||
'quantity_quarters' => $line->quantityQuarters,
|
||||
'unit_price_cents' => $line->unitPriceCents,
|
||||
'vat_basis_points' => $line->vatBasisPoints,
|
||||
'net_cents' => $line->netCents,
|
||||
'sort_order' => $lineOrder,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->findById($model->id) ?? throw new \LogicException('The billing material was not saved.');
|
||||
}
|
||||
|
||||
public function delete(BillingExport $export): void
|
||||
{
|
||||
BillingExportModel::query()->whereKey($export->id())->delete();
|
||||
}
|
||||
|
||||
public function fixedPriceBilledJobIds(array $jobIds): array
|
||||
{
|
||||
if ($jobIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return BillingLineModel::query()
|
||||
->whereIn('customer_job_id', $jobIds)
|
||||
->where('kind', LineKind::FixedPrice)
|
||||
->distinct()
|
||||
->pluck('customer_job_id')
|
||||
->map(fn ($id): int => (int) $id)
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
private function toEntity(BillingExportModel $model): BillingExport
|
||||
{
|
||||
$invoices = $model->invoices->map(fn (BillingInvoiceModel $invoice): BillingInvoice => BillingInvoice::reconstitute(
|
||||
$invoice->jobs->map(fn (BillingInvoiceJobModel $job): DraftJob => new DraftJob(
|
||||
$job->customer_job_id,
|
||||
$job->customer_id,
|
||||
$job->billing_unit_id,
|
||||
$job->customer_reference,
|
||||
$job->hours->mapWithKeys(fn (BillingJobHoursModel $hours): array => [$hours->work_type_id => $hours->billable_quarters])->all(),
|
||||
))->values()->all(),
|
||||
$invoice->lines->map(fn (BillingLineModel $line): InvoiceLine => new InvoiceLine(
|
||||
$line->customer_job_id,
|
||||
$line->kind,
|
||||
$line->work_type_id,
|
||||
$line->expense_id,
|
||||
$line->description,
|
||||
$line->recorded_quarters,
|
||||
$line->quantity_quarters,
|
||||
$line->unit_price_cents,
|
||||
$line->vat_basis_points,
|
||||
$line->net_cents,
|
||||
))->values()->all(),
|
||||
))->values()->all();
|
||||
|
||||
return BillingExport::reconstitute(
|
||||
$model->id,
|
||||
$model->status,
|
||||
$model->created_by,
|
||||
DateTimeImmutable::createFromInterface($model->created_at),
|
||||
$model->finalized_by,
|
||||
$model->finalized_at === null ? null : DateTimeImmutable::createFromInterface($model->finalized_at),
|
||||
$invoices,
|
||||
);
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingQueries;
|
||||
use App\Modules\Billing\Application\DTOs\ExportSummary;
|
||||
use App\Modules\Billing\Domain\Enums\ExportStatus;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingExportModel;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingInvoiceJobModel;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Eloquent\BillingLineModel;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EloquentBillingQueries implements BillingQueries
|
||||
{
|
||||
public function __construct(private UserAccountQueries $users) {}
|
||||
|
||||
public function exports(): array
|
||||
{
|
||||
$models = BillingExportModel::query()->withCount('invoices')->orderByDesc('id')->get();
|
||||
|
||||
if ($models->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$ids = $models->pluck('id')->all();
|
||||
|
||||
$jobCounts = BillingInvoiceJobModel::query()
|
||||
->join('billing_invoices', 'billing_invoices.id', '=', 'billing_invoice_jobs.billing_invoice_id')
|
||||
->whereIn('billing_invoices.billing_export_id', $ids)
|
||||
->groupBy('billing_invoices.billing_export_id')
|
||||
->selectRaw('billing_invoices.billing_export_id as export_id, count(*) as job_count')
|
||||
->pluck('job_count', 'export_id');
|
||||
|
||||
$netSums = BillingLineModel::query()
|
||||
->join('billing_invoices', 'billing_invoices.id', '=', 'billing_lines.billing_invoice_id')
|
||||
->whereIn('billing_invoices.billing_export_id', $ids)
|
||||
->groupBy('billing_invoices.billing_export_id')
|
||||
->selectRaw('billing_invoices.billing_export_id as export_id, sum(billing_lines.net_cents) as net_cents')
|
||||
->pluck('net_cents', 'export_id');
|
||||
|
||||
$names = $this->users->namesByIds($models->pluck('created_by')->merge($models->pluck('finalized_by'))->filter()->unique()->values()->all());
|
||||
|
||||
return $models->map(fn (BillingExportModel $model): ExportSummary => new ExportSummary(
|
||||
$model->id,
|
||||
$model->status,
|
||||
DateTimeImmutable::createFromInterface($model->created_at),
|
||||
$names[$model->created_by] ?? '',
|
||||
$model->finalized_at === null ? null : DateTimeImmutable::createFromInterface($model->finalized_at),
|
||||
$model->finalized_by === null ? null : ($names[$model->finalized_by] ?? ''),
|
||||
(int) $model->getAttribute('invoices_count'),
|
||||
(int) ($jobCounts[$model->id] ?? 0),
|
||||
$model->status === ExportStatus::Created ? (int) ($netSums[$model->id] ?? 0) : null,
|
||||
))->values()->all();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure;
|
||||
|
||||
use App\Modules\Billing\Application\Contracts\BillingPrices;
|
||||
use App\Modules\Billing\Application\DTOs\HourlyRate;
|
||||
use App\Modules\Catalog\Application\Contracts\PriceListQueries;
|
||||
|
||||
/**
|
||||
* Hourly rates and the default VAT rate from the Catalog module's price list.
|
||||
*/
|
||||
class PricesForBilling implements BillingPrices
|
||||
{
|
||||
public function __construct(private PriceListQueries $priceList) {}
|
||||
|
||||
public function hourlyRates(): array
|
||||
{
|
||||
$rates = [];
|
||||
|
||||
foreach ($this->priceList->hourlyRates() as $group) {
|
||||
foreach ($group->rows as $row) {
|
||||
$rates[$row->workTypeId] = new HourlyRate($row->workTypeId, $row->name, $row->hourlyRateCents);
|
||||
}
|
||||
}
|
||||
|
||||
return $rates;
|
||||
}
|
||||
|
||||
public function defaultVatBasisPoints(): int
|
||||
{
|
||||
return $this->priceList->defaultVatRate()->basisPoints ?? 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Billing\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
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\Contracts\BillingPrices;
|
||||
use App\Modules\Billing\Application\Contracts\BillingQueries;
|
||||
use App\Modules\Billing\Domain\Contracts\BillingExportRepository;
|
||||
use App\Modules\Billing\Infrastructure\ExpensesForBilling;
|
||||
use App\Modules\Billing\Infrastructure\HoursForBilling;
|
||||
use App\Modules\Billing\Infrastructure\JobsForBilling;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Repositories\EloquentBillingExportRepository;
|
||||
use App\Modules\Billing\Infrastructure\Persistence\Repositories\EloquentBillingQueries;
|
||||
use App\Modules\Billing\Infrastructure\PricesForBilling;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Livewire\Livewire;
|
||||
|
||||
/**
|
||||
* Wires the Billing module (billing material for invoicing) into Laravel:
|
||||
* bindings, routes, the `billing::` Livewire namespace and the
|
||||
* `billing.manage` ability.
|
||||
*/
|
||||
class BillingServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* @var array<class-string, class-string>
|
||||
*/
|
||||
public array $bindings = [
|
||||
BillingExportRepository::class => EloquentBillingExportRepository::class,
|
||||
BillingQueries::class => EloquentBillingQueries::class,
|
||||
BillingJobs::class => JobsForBilling::class,
|
||||
BillingHours::class => HoursForBilling::class,
|
||||
BillingExpenses::class => ExpensesForBilling::class,
|
||||
BillingPrices::class => PricesForBilling::class,
|
||||
];
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$presentationPath = dirname(__DIR__, 2).'/Presentation';
|
||||
|
||||
Livewire::addNamespace('billing', viewPath: $presentationPath.'/Livewire');
|
||||
|
||||
if (! $this->app->routesAreCached()) {
|
||||
$this->loadRoutesFrom($presentationPath.'/Routes/web.php');
|
||||
}
|
||||
|
||||
// Management forms the billing material once a month (spec 12).
|
||||
Gate::define('billing.manage', fn (User $user): bool => $user->role->seesAllMoney());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user