Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
54 / 54 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| GetFinancialDashboardQuery | |
100.00% |
54 / 54 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| execute | |
100.00% |
54 / 54 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Financial\Application\Queries; |
| 6 | |
| 7 | use ImovelZapAi\Financial\Domain\Enums\CommissionStatus; |
| 8 | use ImovelZapAi\Financial\Domain\Enums\PaymentScheduleStatus; |
| 9 | use ImovelZapAi\Financial\Domain\Enums\TransferStatus; |
| 10 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialCommissionModel; |
| 11 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialPaymentModel; |
| 12 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialPaymentScheduleModel; |
| 13 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialReceiptModel; |
| 14 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransactionModel; |
| 15 | use ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent\FinancialTransferModel; |
| 16 | use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealModel; |
| 17 | |
| 18 | final 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 | } |