141 lines
4.4 KiB
PHP
141 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\AssetExchange\Services;
|
|
|
|
use App\Business;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\AssetExchange\Models\Commission;
|
|
use Modules\AssetExchange\Models\Deal;
|
|
use Modules\AssetExchange\Models\Listing;
|
|
use Modules\AssetExchange\Utils\AssetExchangeUtil;
|
|
|
|
class AssetExchangeDashboardService
|
|
{
|
|
public function __construct(
|
|
protected AssetExchangeUtil $aexUtil
|
|
) {
|
|
}
|
|
|
|
public function getKpiStats(Business $business): array
|
|
{
|
|
if (! Schema::hasTable('aex_listings')) {
|
|
return $this->emptyStats();
|
|
}
|
|
|
|
$bid = $business->id;
|
|
|
|
$listingsByStatus = Listing::where('business_id', $bid)
|
|
->selectRaw('status, COUNT(*) as count')
|
|
->groupBy('status')
|
|
->pluck('count', 'status')
|
|
->toArray();
|
|
|
|
$openDeals = 0;
|
|
$dealsByType = [];
|
|
$dealsByStatus = [];
|
|
|
|
if (Schema::hasTable('aex_deals')) {
|
|
$openDeals = Deal::where('business_id', $bid)
|
|
->whereNotIn('status', ['completed', 'cancelled'])
|
|
->count();
|
|
|
|
$dealsByType = Deal::where('business_id', $bid)
|
|
->selectRaw('deal_type, COUNT(*) as count')
|
|
->groupBy('deal_type')
|
|
->pluck('count', 'deal_type')
|
|
->toArray();
|
|
|
|
$dealsByStatus = Deal::where('business_id', $bid)
|
|
->selectRaw('status, COUNT(*) as count')
|
|
->groupBy('status')
|
|
->pluck('count', 'status')
|
|
->toArray();
|
|
}
|
|
|
|
$commissionsPending = 0;
|
|
$commissionsPendingAmount = 0;
|
|
|
|
if (Schema::hasTable('aex_commissions')) {
|
|
$commissionsPending = Commission::where('business_id', $bid)
|
|
->whereIn('status', ['pending', 'synced'])
|
|
->count();
|
|
|
|
$commissionsPendingAmount = (int) Commission::where('business_id', $bid)
|
|
->whereIn('status', ['pending', 'synced'])
|
|
->sum('amount');
|
|
}
|
|
|
|
$inAppraisal = (int) ($listingsByStatus['under_review'] ?? 0)
|
|
+ (int) ($listingsByStatus['submitted'] ?? 0);
|
|
|
|
$activeListings = (int) ($listingsByStatus['appraised'] ?? 0)
|
|
+ (int) ($listingsByStatus['in_deal'] ?? 0)
|
|
+ (int) ($listingsByStatus['submitted'] ?? 0)
|
|
+ (int) ($listingsByStatus['under_review'] ?? 0);
|
|
|
|
return [
|
|
'listings' => [
|
|
'by_status' => $this->labelizeCounts($listingsByStatus, 'status'),
|
|
'active' => $activeListings,
|
|
'in_appraisal_pipeline' => $inAppraisal,
|
|
'appraised' => (int) ($listingsByStatus['appraised'] ?? 0),
|
|
'sold' => (int) ($listingsByStatus['sold'] ?? 0),
|
|
],
|
|
'deals' => [
|
|
'open' => $openDeals,
|
|
'by_type' => $this->labelizeCounts($dealsByType, 'deal_type'),
|
|
'by_status' => $this->labelizeCounts($dealsByStatus, 'status'),
|
|
],
|
|
'commissions' => [
|
|
'pending_count' => $commissionsPending,
|
|
'pending_amount' => $commissionsPendingAmount,
|
|
'pending_amount_formatted' => $this->aexUtil->formatMoney($commissionsPendingAmount),
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function labelizeCounts(array $counts, string $labelType): array
|
|
{
|
|
$result = [];
|
|
|
|
foreach ($counts as $key => $count) {
|
|
$label = match ($labelType) {
|
|
'status' => $this->aexUtil->statusLabel($key),
|
|
'deal_type' => $this->aexUtil->dealTypeLabel($key),
|
|
default => $key,
|
|
};
|
|
|
|
$result[] = [
|
|
'key' => $key,
|
|
'label' => $label,
|
|
'count' => (int) $count,
|
|
];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
protected function emptyStats(): array
|
|
{
|
|
return [
|
|
'listings' => [
|
|
'by_status' => [],
|
|
'active' => 0,
|
|
'in_appraisal_pipeline' => 0,
|
|
'appraised' => 0,
|
|
'sold' => 0,
|
|
],
|
|
'deals' => [
|
|
'open' => 0,
|
|
'by_type' => [],
|
|
'by_status' => [],
|
|
],
|
|
'commissions' => [
|
|
'pending_count' => 0,
|
|
'pending_amount' => 0,
|
|
'pending_amount_formatted' => $this->aexUtil->formatMoney(0),
|
|
],
|
|
];
|
|
}
|
|
}
|