Browse Source

wip adjustment statuses

3.x
Andrew Wallo 6 months ago
parent
commit
15e251b09d

+ 67
- 9
app/Filament/Company/Clusters/Settings/Resources/AdjustmentResource.php View File

15
 use Filament\Notifications\Notification;
15
 use Filament\Notifications\Notification;
16
 use Filament\Resources\Resource;
16
 use Filament\Resources\Resource;
17
 use Filament\Tables;
17
 use Filament\Tables;
18
+use Filament\Tables\Filters\Indicator;
18
 use Filament\Tables\Table;
19
 use Filament\Tables\Table;
20
+use Illuminate\Database\Eloquent\Builder;
19
 use Illuminate\Database\Eloquent\Collection;
21
 use Illuminate\Database\Eloquent\Collection;
20
 
22
 
21
 class AdjustmentResource extends Resource
23
 class AdjustmentResource extends Resource
120
                     ->toggleable(isToggledHiddenByDefault: true),
122
                     ->toggleable(isToggledHiddenByDefault: true),
121
             ])
123
             ])
122
             ->filters([
124
             ->filters([
123
-                Tables\Filters\TernaryFilter::make('archived')
124
-                    ->label('Archived')
125
-                    ->default(false)
126
-                    ->attribute('archived_at')
127
-                    ->nullable(),
128
                 Tables\Filters\SelectFilter::make('status')
125
                 Tables\Filters\SelectFilter::make('status')
129
                     ->label('Status')
126
                     ->label('Status')
130
                     ->native(false)
127
                     ->native(false)
131
-                    ->options(AdjustmentStatus::class),
128
+                    ->default('unarchived')
129
+                    ->options(
130
+                        collect(AdjustmentStatus::cases())
131
+                            ->mapWithKeys(fn (AdjustmentStatus $status) => [$status->value => $status->getLabel()])
132
+                            ->merge([
133
+                                'unarchived' => 'Unarchived',
134
+                            ])
135
+                            ->toArray()
136
+                    )
137
+                    ->indicateUsing(function (Tables\Filters\SelectFilter $filter, array $state) {
138
+                        if (blank($state['value'] ?? null)) {
139
+                            return [];
140
+                        }
141
+
142
+                        $label = collect($filter->getOptions())
143
+                            ->mapWithKeys(fn (string | array $label, string $value): array => is_array($label) ? $label : [$value => $label])
144
+                            ->get($state['value']);
145
+
146
+                        if (blank($label)) {
147
+                            return [];
148
+                        }
149
+
150
+                        $indicator = $filter->getIndicator();
151
+
152
+                        if (! $indicator instanceof Indicator) {
153
+                            if ($state['value'] === 'unarchived') {
154
+                                $indicator = $label;
155
+                            } else {
156
+                                $indicator = Indicator::make("{$indicator}: {$label}");
157
+                            }
158
+                        }
159
+
160
+                        return [$indicator];
161
+                    })
162
+                    ->query(function (Builder $query, array $data): Builder {
163
+                        if (blank($data['value'] ?? null)) {
164
+                            return $query;
165
+                        }
166
+
167
+                        if ($data['value'] !== 'unarchived') {
168
+                            return $query->where('status', $data['value']);
169
+                        } else {
170
+                            return $query->where('status', '!=', AdjustmentStatus::Archived->value);
171
+                        }
172
+                    }),
132
                 Tables\Filters\SelectFilter::make('category')
173
                 Tables\Filters\SelectFilter::make('category')
133
                     ->label('Category')
174
                     ->label('Category')
134
                     ->native(false)
175
                     ->native(false)
157
                                 ->label('Reason for pausing')
198
                                 ->label('Reason for pausing')
158
                                 ->maxLength(255),
199
                                 ->maxLength(255),
159
                         ])
200
                         ])
201
+                        ->databaseTransaction()
202
+                        ->successNotificationTitle('Adjustment paused')
203
+                        ->failureNotificationTitle('Failed to pause adjustment')
160
                         ->visible(fn (Adjustment $record) => $record->canBePaused())
204
                         ->visible(fn (Adjustment $record) => $record->canBePaused())
161
-                        ->action(function (Adjustment $record, array $data) {
205
+                        ->action(function (Adjustment $record, array $data, Tables\Actions\Action $action) {
162
                             $pausedUntil = $data['paused_until'] ?? null;
206
                             $pausedUntil = $data['paused_until'] ?? null;
163
                             $reason = $data['status_reason'] ?? null;
207
                             $reason = $data['status_reason'] ?? null;
164
                             $record->pause($reason, $pausedUntil);
208
                             $record->pause($reason, $pausedUntil);
209
+
210
+                            $action->success();
165
                         }),
211
                         }),
166
                     Tables\Actions\Action::make('resume')
212
                     Tables\Actions\Action::make('resume')
167
                         ->label('Resume')
213
                         ->label('Resume')
168
                         ->icon('heroicon-m-play')
214
                         ->icon('heroicon-m-play')
169
                         ->requiresConfirmation()
215
                         ->requiresConfirmation()
216
+                        ->databaseTransaction()
217
+                        ->successNotificationTitle('Adjustment resumed')
218
+                        ->failureNotificationTitle('Failed to resume adjustment')
170
                         ->visible(fn (Adjustment $record) => $record->canBeResumed())
219
                         ->visible(fn (Adjustment $record) => $record->canBeResumed())
171
-                        ->action(fn (Adjustment $record) => $record->resume()),
220
+                        ->action(function (Adjustment $record, Tables\Actions\Action $action) {
221
+                            $record->resume();
222
+
223
+                            $action->success();
224
+                        }),
172
                     Tables\Actions\Action::make('archive')
225
                     Tables\Actions\Action::make('archive')
173
                         ->label('Archive')
226
                         ->label('Archive')
174
                         ->icon('heroicon-m-archive-box')
227
                         ->icon('heroicon-m-archive-box')
178
                                 ->label('Reason for archiving')
231
                                 ->label('Reason for archiving')
179
                                 ->maxLength(255),
232
                                 ->maxLength(255),
180
                         ])
233
                         ])
234
+                        ->databaseTransaction()
235
+                        ->successNotificationTitle('Adjustment archived')
236
+                        ->failureNotificationTitle('Failed to archive adjustment')
181
                         ->visible(fn (Adjustment $record) => $record->canBeArchived())
237
                         ->visible(fn (Adjustment $record) => $record->canBeArchived())
182
-                        ->action(function (Adjustment $record, array $data) {
238
+                        ->action(function (Adjustment $record, array $data, Tables\Actions\Action $action) {
183
                             $reason = $data['status_reason'] ?? null;
239
                             $reason = $data['status_reason'] ?? null;
184
                             $record->archive($reason);
240
                             $record->archive($reason);
241
+
242
+                            $action->success();
185
                         }),
243
                         }),
186
                 ]),
244
                 ]),
187
             ])
245
             ])

Loading…
Cancel
Save