|  | @@ -7,6 +7,7 @@ use App\Filament\Widgets\EnhancedStatsOverviewWidget;
 | 
		
	
		
			
			| 7 | 7 |  use App\Utilities\Currency\CurrencyAccessor;
 | 
		
	
		
			
			| 8 | 8 |  use App\Utilities\Currency\CurrencyConverter;
 | 
		
	
		
			
			| 9 | 9 |  use Illuminate\Database\Eloquent\Model;
 | 
		
	
		
			
			|  | 10 | +use Illuminate\Support\Facades\DB;
 | 
		
	
		
			
			| 10 | 11 |  use Illuminate\Support\Number;
 | 
		
	
		
			
			| 11 | 12 |  
 | 
		
	
		
			
			| 12 | 13 |  class InvoiceOverview extends EnhancedStatsOverviewWidget
 | 
		
	
	
		
			
			|  | @@ -43,9 +44,20 @@ class InvoiceOverview extends EnhancedStatsOverviewWidget
 | 
		
	
		
			
			| 43 | 44 |              ? (int) round($totalValidInvoiceAmount / $totalValidInvoiceCount)
 | 
		
	
		
			
			| 44 | 45 |              : 0;
 | 
		
	
		
			
			| 45 | 46 |  
 | 
		
	
		
			
			| 46 |  | -        $averagePaymentTime = $this->record->invoices()
 | 
		
	
		
			
			| 47 |  | -            ->whereNotNull('paid_at')
 | 
		
	
		
			
			| 48 |  | -            ->selectRaw('AVG(TIMESTAMPDIFF(DAY, date, paid_at)) as avg_days')
 | 
		
	
		
			
			|  | 47 | +        $driver = DB::getDriverName();
 | 
		
	
		
			
			|  | 48 | +
 | 
		
	
		
			
			|  | 49 | +        $query = $this->record->invoices()
 | 
		
	
		
			
			|  | 50 | +            ->whereNotNull('paid_at');
 | 
		
	
		
			
			|  | 51 | +
 | 
		
	
		
			
			|  | 52 | +        if ($driver === 'pgsql') {
 | 
		
	
		
			
			|  | 53 | +            $query->selectRaw('AVG(EXTRACT(EPOCH FROM (paid_at - date)) / 86400) as avg_days');
 | 
		
	
		
			
			|  | 54 | +        } else {
 | 
		
	
		
			
			|  | 55 | +            $query->selectRaw('AVG(TIMESTAMPDIFF(DAY, date, paid_at)) as avg_days');
 | 
		
	
		
			
			|  | 56 | +        }
 | 
		
	
		
			
			|  | 57 | +
 | 
		
	
		
			
			|  | 58 | +        $averagePaymentTime = $query
 | 
		
	
		
			
			|  | 59 | +            ->groupBy('company_id')
 | 
		
	
		
			
			|  | 60 | +            ->reorder()
 | 
		
	
		
			
			| 49 | 61 |              ->value('avg_days');
 | 
		
	
		
			
			| 50 | 62 |  
 | 
		
	
		
			
			| 51 | 63 |          $averagePaymentTimeFormatted = Number::format($averagePaymentTime ?? 0, maxPrecision: 1);
 |