Andrew Wallo 6 mēnešus atpakaļ
vecāks
revīzija
05f2be51b3

+ 29
- 0
app/Enums/Accounting/AdjustmentStatus.php Parādīt failu

@@ -0,0 +1,29 @@
1
+<?php
2
+
3
+namespace App\Enums\Accounting;
4
+
5
+use Filament\Support\Contracts\HasColor;
6
+use Filament\Support\Contracts\HasLabel;
7
+
8
+enum AdjustmentStatus: string implements HasColor, HasLabel
9
+{
10
+    case Active = 'active';
11
+    case Upcoming = 'upcoming';
12
+    case Expired = 'expired';
13
+    case Archived = 'archived';
14
+
15
+    public function getLabel(): ?string
16
+    {
17
+        return $this->name;
18
+    }
19
+
20
+    public function getColor(): string | array | null
21
+    {
22
+        return match ($this) {
23
+            self::Active => 'primary',
24
+            self::Upcoming => 'warning',
25
+            self::Expired => 'danger',
26
+            self::Archived => 'gray',
27
+        };
28
+    }
29
+}

+ 15
- 2
app/Filament/Company/Clusters/Settings/Resources/AdjustmentResource.php Parādīt failu

@@ -5,6 +5,7 @@ namespace App\Filament\Company\Clusters\Settings\Resources;
5 5
 use App\Enums\Accounting\AdjustmentCategory;
6 6
 use App\Enums\Accounting\AdjustmentComputation;
7 7
 use App\Enums\Accounting\AdjustmentScope;
8
+use App\Enums\Accounting\AdjustmentStatus;
8 9
 use App\Enums\Accounting\AdjustmentType;
9 10
 use App\Filament\Company\Clusters\Settings;
10 11
 use App\Filament\Company\Clusters\Settings\Resources\AdjustmentResource\Pages;
@@ -76,7 +77,8 @@ class AdjustmentResource extends Resource
76 77
                 Forms\Components\Section::make('Dates')
77 78
                     ->schema([
78 79
                         Forms\Components\DateTimePicker::make('start_date'),
79
-                        Forms\Components\DateTimePicker::make('end_date'),
80
+                        Forms\Components\DateTimePicker::make('end_date')
81
+                            ->after('start_date'),
80 82
                     ])
81 83
                     ->columns()
82 84
                     ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isDiscount()),
@@ -94,6 +96,8 @@ class AdjustmentResource extends Resource
94 96
                     ->searchable(),
95 97
                 Tables\Columns\TextColumn::make('type')
96 98
                     ->searchable(),
99
+                Tables\Columns\TextColumn::make('status')
100
+                    ->badge(),
97 101
                 Tables\Columns\TextColumn::make('rate')
98 102
                     ->localizeLabel()
99 103
                     ->rate(static fn (Adjustment $record) => $record->computation->value)
@@ -104,7 +108,16 @@ class AdjustmentResource extends Resource
104 108
                 //
105 109
             ])
106 110
             ->actions([
107
-                Tables\Actions\EditAction::make(),
111
+                Tables\Actions\ActionGroup::make([
112
+                    Tables\Actions\EditAction::make(),
113
+                    Tables\Actions\Action::make('archive')
114
+                        ->label('Archive')
115
+                        ->icon('heroicon-o-archive-box-x-mark')
116
+                        ->color('danger')
117
+                        ->requiresConfirmation()
118
+                        ->visible(static fn (Adjustment $record) => $record->status !== AdjustmentStatus::Archived)
119
+                        ->action(fn (Adjustment $record) => $record->update(['status' => AdjustmentStatus::Archived])),
120
+                ]),
108 121
             ])
109 122
             ->bulkActions([
110 123
                 //

+ 2
- 1
app/Filament/Forms/Components/CreateAdjustmentSelect.php Parādīt failu

@@ -155,7 +155,8 @@ class CreateAdjustmentSelect extends Select
155 155
             Group::make()
156 156
                 ->schema([
157 157
                     DateTimePicker::make('start_date'),
158
-                    DateTimePicker::make('end_date'),
158
+                    DateTimePicker::make('end_date')
159
+                        ->after('start_date'),
159 160
                 ])
160 161
                 ->visible(function (Get $get) {
161 162
                     $category = $this->getCategory() ?? AdjustmentCategory::parse($get('category'));

+ 20
- 0
app/Models/Accounting/Adjustment.php Parādīt failu

@@ -8,6 +8,7 @@ use App\Concerns\CompanyOwned;
8 8
 use App\Enums\Accounting\AdjustmentCategory;
9 9
 use App\Enums\Accounting\AdjustmentComputation;
10 10
 use App\Enums\Accounting\AdjustmentScope;
11
+use App\Enums\Accounting\AdjustmentStatus;
11 12
 use App\Enums\Accounting\AdjustmentType;
12 13
 use App\Models\Common\Offering;
13 14
 use App\Observers\AdjustmentObserver;
@@ -32,6 +33,7 @@ class Adjustment extends Model
32 33
         'company_id',
33 34
         'account_id',
34 35
         'name',
36
+        'status',
35 37
         'description',
36 38
         'category',
37 39
         'type',
@@ -46,6 +48,7 @@ class Adjustment extends Model
46 48
     ];
47 49
 
48 50
     protected $casts = [
51
+        'status' => AdjustmentStatus::class,
49 52
         'category' => AdjustmentCategory::class,
50 53
         'type' => AdjustmentType::class,
51 54
         'recoverable' => 'boolean',
@@ -91,6 +94,23 @@ class Adjustment extends Model
91 94
         return $this->category->isDiscount() && $this->type->isPurchase();
92 95
     }
93 96
 
97
+    public function calculateStatus(): AdjustmentStatus
98
+    {
99
+        if ($this->status === AdjustmentStatus::Archived) {
100
+            return AdjustmentStatus::Archived;
101
+        }
102
+
103
+        if ($this->start_date?->isFuture()) {
104
+            return AdjustmentStatus::Upcoming;
105
+        }
106
+
107
+        if ($this->end_date?->isPast()) {
108
+            return AdjustmentStatus::Expired;
109
+        }
110
+
111
+        return AdjustmentStatus::Active;
112
+    }
113
+
94 114
     protected static function newFactory(): Factory
95 115
     {
96 116
         return AdjustmentFactory::new();

+ 9
- 0
app/Observers/AdjustmentObserver.php Parādīt failu

@@ -2,6 +2,7 @@
2 2
 
3 3
 namespace App\Observers;
4 4
 
5
+use App\Enums\Accounting\AdjustmentStatus;
5 6
 use App\Models\Accounting\Account;
6 7
 use App\Models\Accounting\Adjustment;
7 8
 
@@ -26,6 +27,10 @@ class AdjustmentObserver
26 27
                 $adjustment->account()->associate($account);
27 28
             }
28 29
         }
30
+
31
+        if ($adjustment->status !== AdjustmentStatus::Archived) {
32
+            $adjustment->status = $adjustment->calculateStatus();
33
+        }
29 34
     }
30 35
 
31 36
     public function updating(Adjustment $adjustment): void
@@ -36,5 +41,9 @@ class AdjustmentObserver
36 41
                 'description' => $adjustment->description,
37 42
             ]);
38 43
         }
44
+
45
+        if ($adjustment->status !== AdjustmentStatus::Archived) {
46
+            $adjustment->status = $adjustment->calculateStatus();
47
+        }
39 48
     }
40 49
 }

+ 1
- 0
database/migrations/2024_11_14_230753_create_adjustments_table.php Parādīt failu

@@ -16,6 +16,7 @@ return new class extends Migration
16 16
             $table->foreignId('company_id')->constrained()->cascadeOnDelete();
17 17
             $table->foreignId('account_id')->nullable()->constrained('accounts')->nullOnDelete();
18 18
             $table->string('name')->nullable();
19
+            $table->string('status')->default('active');
19 20
             $table->text('description')->nullable();
20 21
             $table->string('category')->default('tax');
21 22
             $table->string('type')->default('sales');

Notiek ielāde…
Atcelt
Saglabāt