- 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]>
58 lines
2.7 KiB
PHP
58 lines
2.7 KiB
PHP
<?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->number,
|
|
$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();
|
|
}
|
|
}
|