Andrew Wallo 6 个月前
父节点
当前提交
05f2be51b3

+ 29
- 0
app/Enums/Accounting/AdjustmentStatus.php 查看文件

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 查看文件

5
 use App\Enums\Accounting\AdjustmentCategory;
5
 use App\Enums\Accounting\AdjustmentCategory;
6
 use App\Enums\Accounting\AdjustmentComputation;
6
 use App\Enums\Accounting\AdjustmentComputation;
7
 use App\Enums\Accounting\AdjustmentScope;
7
 use App\Enums\Accounting\AdjustmentScope;
8
+use App\Enums\Accounting\AdjustmentStatus;
8
 use App\Enums\Accounting\AdjustmentType;
9
 use App\Enums\Accounting\AdjustmentType;
9
 use App\Filament\Company\Clusters\Settings;
10
 use App\Filament\Company\Clusters\Settings;
10
 use App\Filament\Company\Clusters\Settings\Resources\AdjustmentResource\Pages;
11
 use App\Filament\Company\Clusters\Settings\Resources\AdjustmentResource\Pages;
76
                 Forms\Components\Section::make('Dates')
77
                 Forms\Components\Section::make('Dates')
77
                     ->schema([
78
                     ->schema([
78
                         Forms\Components\DateTimePicker::make('start_date'),
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
                     ->columns()
83
                     ->columns()
82
                     ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isDiscount()),
84
                     ->visible(fn (Forms\Get $get) => AdjustmentCategory::parse($get('category'))->isDiscount()),
94
                     ->searchable(),
96
                     ->searchable(),
95
                 Tables\Columns\TextColumn::make('type')
97
                 Tables\Columns\TextColumn::make('type')
96
                     ->searchable(),
98
                     ->searchable(),
99
+                Tables\Columns\TextColumn::make('status')
100
+                    ->badge(),
97
                 Tables\Columns\TextColumn::make('rate')
101
                 Tables\Columns\TextColumn::make('rate')
98
                     ->localizeLabel()
102
                     ->localizeLabel()
99
                     ->rate(static fn (Adjustment $record) => $record->computation->value)
103
                     ->rate(static fn (Adjustment $record) => $record->computation->value)
104
                 //
108
                 //
105
             ])
109
             ])
106
             ->actions([
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
             ->bulkActions([
122
             ->bulkActions([
110
                 //
123
                 //

+ 2
- 1
app/Filament/Forms/Components/CreateAdjustmentSelect.php 查看文件

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

+ 20
- 0
app/Models/Accounting/Adjustment.php 查看文件

8
 use App\Enums\Accounting\AdjustmentCategory;
8
 use App\Enums\Accounting\AdjustmentCategory;
9
 use App\Enums\Accounting\AdjustmentComputation;
9
 use App\Enums\Accounting\AdjustmentComputation;
10
 use App\Enums\Accounting\AdjustmentScope;
10
 use App\Enums\Accounting\AdjustmentScope;
11
+use App\Enums\Accounting\AdjustmentStatus;
11
 use App\Enums\Accounting\AdjustmentType;
12
 use App\Enums\Accounting\AdjustmentType;
12
 use App\Models\Common\Offering;
13
 use App\Models\Common\Offering;
13
 use App\Observers\AdjustmentObserver;
14
 use App\Observers\AdjustmentObserver;
32
         'company_id',
33
         'company_id',
33
         'account_id',
34
         'account_id',
34
         'name',
35
         'name',
36
+        'status',
35
         'description',
37
         'description',
36
         'category',
38
         'category',
37
         'type',
39
         'type',
46
     ];
48
     ];
47
 
49
 
48
     protected $casts = [
50
     protected $casts = [
51
+        'status' => AdjustmentStatus::class,
49
         'category' => AdjustmentCategory::class,
52
         'category' => AdjustmentCategory::class,
50
         'type' => AdjustmentType::class,
53
         'type' => AdjustmentType::class,
51
         'recoverable' => 'boolean',
54
         'recoverable' => 'boolean',
91
         return $this->category->isDiscount() && $this->type->isPurchase();
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
     protected static function newFactory(): Factory
114
     protected static function newFactory(): Factory
95
     {
115
     {
96
         return AdjustmentFactory::new();
116
         return AdjustmentFactory::new();

+ 9
- 0
app/Observers/AdjustmentObserver.php 查看文件

2
 
2
 
3
 namespace App\Observers;
3
 namespace App\Observers;
4
 
4
 
5
+use App\Enums\Accounting\AdjustmentStatus;
5
 use App\Models\Accounting\Account;
6
 use App\Models\Accounting\Account;
6
 use App\Models\Accounting\Adjustment;
7
 use App\Models\Accounting\Adjustment;
7
 
8
 
26
                 $adjustment->account()->associate($account);
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
     public function updating(Adjustment $adjustment): void
36
     public function updating(Adjustment $adjustment): void
36
                 'description' => $adjustment->description,
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 查看文件

16
             $table->foreignId('company_id')->constrained()->cascadeOnDelete();
16
             $table->foreignId('company_id')->constrained()->cascadeOnDelete();
17
             $table->foreignId('account_id')->nullable()->constrained('accounts')->nullOnDelete();
17
             $table->foreignId('account_id')->nullable()->constrained('accounts')->nullOnDelete();
18
             $table->string('name')->nullable();
18
             $table->string('name')->nullable();
19
+            $table->string('status')->default('active');
19
             $table->text('description')->nullable();
20
             $table->text('description')->nullable();
20
             $table->string('category')->default('tax');
21
             $table->string('category')->default('tax');
21
             $table->string('type')->default('sales');
22
             $table->string('type')->default('sales');

正在加载...
取消
保存