Andrew Wallo 6 months ago
parent
commit
574cad7af4

+ 30
- 9
app/Filament/Company/Resources/Sales/EstimateResource/Widgets/EstimateOverview.php View File

21
 
21
 
22
     protected function getStats(): array
22
     protected function getStats(): array
23
     {
23
     {
24
+        $activeTab = $this->activeTab;
25
+
26
+        if ($activeTab === 'draft') {
27
+            return [
28
+                EnhancedStatsOverviewWidget\EnhancedStat::make('Active Estimates', '-')
29
+                    ->suffix('')
30
+                    ->description(''),
31
+                EnhancedStatsOverviewWidget\EnhancedStat::make('Accepted Estimates', '-')
32
+                    ->suffix('')
33
+                    ->description(''),
34
+                EnhancedStatsOverviewWidget\EnhancedStat::make('Converted Estimates', '-')
35
+                    ->suffix('')
36
+                    ->description(''),
37
+                EnhancedStatsOverviewWidget\EnhancedStat::make('Average Estimate Total', '-')
38
+                    ->suffix(''),
39
+            ];
40
+        }
41
+
24
         $activeEstimates = $this->getPageTableQuery()->active();
42
         $activeEstimates = $this->getPageTableQuery()->active();
25
 
43
 
26
         $totalActiveCount = $activeEstimates->count();
44
         $totalActiveCount = $activeEstimates->count();
36
             ->where('status', EstimateStatus::Converted);
54
             ->where('status', EstimateStatus::Converted);
37
 
55
 
38
         $totalConvertedCount = $convertedEstimates->count();
56
         $totalConvertedCount = $convertedEstimates->count();
39
-        $totalEstimatesCount = $this->getPageTableQuery()->count();
40
 
57
 
41
-        $percentConverted = $totalEstimatesCount > 0
42
-            ? Number::percentage(($totalConvertedCount / $totalEstimatesCount) * 100, maxPrecision: 1)
43
-            : Number::percentage(0, maxPrecision: 1);
58
+        $validEstimates = $this->getPageTableQuery()
59
+            ->whereNotIn('status', [
60
+                EstimateStatus::Draft,
61
+            ]);
44
 
62
 
45
-        $totalEstimateAmount = $this->getPageTableQuery()
46
-            ->get()
47
-            ->sumMoneyInDefaultCurrency('total');
63
+        $totalValidEstimatesCount = $validEstimates->count();
64
+        $totalValidEstimateAmount = $validEstimates->get()->sumMoneyInDefaultCurrency('total');
65
+
66
+        $percentConverted = $totalValidEstimatesCount > 0
67
+            ? Number::percentage(($totalConvertedCount / $totalValidEstimatesCount) * 100, maxPrecision: 1)
68
+            : Number::percentage(0, maxPrecision: 1);
48
 
69
 
49
-        $averageEstimateTotal = $totalEstimatesCount > 0
50
-            ? (int) round($totalEstimateAmount / $totalEstimatesCount)
70
+        $averageEstimateTotal = $totalValidEstimatesCount > 0
71
+            ? (int) round($totalValidEstimateAmount / $totalValidEstimatesCount)
51
             : 0;
72
             : 0;
52
 
73
 
53
         return [
74
         return [

+ 2
- 1
app/Models/Accounting/Estimate.php View File

124
     protected function isCurrentlyExpired(): Attribute
124
     protected function isCurrentlyExpired(): Attribute
125
     {
125
     {
126
         return Attribute::get(function () {
126
         return Attribute::get(function () {
127
-            return $this->expiration_date?->isBefore(today()) && $this->canBeExpired();
127
+            return $this->expiration_date?->isBefore(today());
128
         });
128
         });
129
     }
129
     }
130
 
130
 
170
             EstimateStatus::Accepted,
170
             EstimateStatus::Accepted,
171
             EstimateStatus::Declined,
171
             EstimateStatus::Declined,
172
             EstimateStatus::Converted,
172
             EstimateStatus::Converted,
173
+            EstimateStatus::Expired,
173
         ]);
174
         ]);
174
     }
175
     }
175
 
176
 

+ 11
- 1
app/Observers/EstimateObserver.php View File

11
 {
11
 {
12
     public function saving(Estimate $estimate): void
12
     public function saving(Estimate $estimate): void
13
     {
13
     {
14
-        if ($estimate->approved_at && $estimate->is_currently_expired) {
14
+        if (! $estimate->wasApproved()) {
15
+            return;
16
+        }
17
+
18
+        if ($estimate->isDirty('expiration_date') && $estimate->status === EstimateStatus::Expired && ! $estimate->is_currently_expired) {
19
+            $estimate->status = $estimate->hasBeenSent() ? EstimateStatus::Sent : EstimateStatus::Unsent;
20
+
21
+            return;
22
+        }
23
+
24
+        if ($estimate->is_currently_expired && $estimate->canBeExpired()) {
15
             $estimate->status = EstimateStatus::Expired;
25
             $estimate->status = EstimateStatus::Expired;
16
         }
26
         }
17
     }
27
     }

Loading…
Cancel
Save