Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
GetFinancialDashboardQuery
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Financial\Application\Queries;
6
7use ImovelZapAi\Financial\Domain\Enums\CommissionStatus;
8use ImovelZapAi\Financial\Domain\Enums\PaymentScheduleStatus;
9use ImovelZapAi\Financial\Domain\Enums\TransferStatus;
10use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialCommissionModel;
11use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialPaymentModel;
12use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialPaymentScheduleModel;
13use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialReceiptModel;
14use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransactionModel;
15use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransferModel;
16use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealModel;
17
18final class GetFinancialDashboardQuery
19{
20    /**
21     * @param  array{from?: string|null, to?: string|null}  $period
22     * @return array<string, mixed>
23     */
24    public function execute(string $tenantId, array $period = []): array
25    {
26        $from = isset($period['from']) ? now()->parse($period['from'])->startOfDay() : now()->startOfMonth();
27        $to = isset($period['to']) ? now()->parse($period['to'])->endOfDay() : now()->endOfMonth();
28
29        $dealQuery = DealModel::query()->where('tenant_id', $tenantId);
30        $totalNegotiated = (float) (clone $dealQuery)->whereNotNull('amount')->sum('amount');
31
32        $commissionForecast = (float) FinancialCommissionModel::query()
33            ->where('tenant_id', $tenantId)
34            ->whereIn('status', [CommissionStatus::Calculated, CommissionStatus::Released])
35            ->sum('total_amount');
36
37        $commissionPaid = (float) FinancialCommissionModel::query()
38            ->where('tenant_id', $tenantId)
39            ->where('status', CommissionStatus::Paid)
40            ->sum('total_amount');
41
42        $pendingTransfers = (float) FinancialTransferModel::query()
43            ->where('tenant_id', $tenantId)
44            ->where('status', TransferStatus::Pending)
45            ->sum('amount');
46
47        $pendingSchedules = FinancialPaymentScheduleModel::query()
48            ->where('tenant_id', $tenantId)
49            ->where('status', PaymentScheduleStatus::Pending)
50            ->count();
51
52        $overdueSchedules = FinancialPaymentScheduleModel::query()
53            ->where('tenant_id', $tenantId)
54            ->where('status', PaymentScheduleStatus::Overdue)
55            ->count();
56
57        $periodInflow = (float) FinancialPaymentModel::query()
58            ->where('tenant_id', $tenantId)
59            ->whereBetween('paid_at', [$from, $to])
60            ->sum('amount');
61
62        $openTransactions = FinancialTransactionModel::query()
63            ->where('tenant_id', $tenantId)
64            ->whereIn('status', ['pending', 'partial', 'overdue'])
65            ->count();
66
67        $receiptsIssued = FinancialReceiptModel::query()
68            ->where('tenant_id', $tenantId)
69            ->whereBetween('issued_at', [$from, $to])
70            ->count();
71
72        $dealsWithFinance = FinancialTransactionModel::query()
73            ->where('tenant_id', $tenantId)
74            ->distinct('deal_id')
75            ->count('deal_id');
76
77        return [
78            'total_negotiated' => $totalNegotiated,
79            'commission_forecast' => $commissionForecast,
80            'commission_paid' => $commissionPaid,
81            'pending_transfers' => $pendingTransfers,
82            'pending_schedules' => $pendingSchedules,
83            'overdue_schedules' => $overdueSchedules,
84            'period_inflow' => $periodInflow,
85            'period_from' => $from->toDateString(),
86            'period_to' => $to->toDateString(),
87            'open_transactions' => $openTransactions,
88            'receipts_issued' => $receiptsIssued,
89            'deals_with_finance' => $dealsWithFinance,
90        ];
91    }
92}