|
@@ -15,7 +15,9 @@ use Filament\Forms\Form;
|
15
|
15
|
use Filament\Notifications\Notification;
|
16
|
16
|
use Filament\Resources\Resource;
|
17
|
17
|
use Filament\Tables;
|
|
18
|
+use Filament\Tables\Filters\Indicator;
|
18
|
19
|
use Filament\Tables\Table;
|
|
20
|
+use Illuminate\Database\Eloquent\Builder;
|
19
|
21
|
use Illuminate\Database\Eloquent\Collection;
|
20
|
22
|
|
21
|
23
|
class AdjustmentResource extends Resource
|
|
@@ -120,15 +122,54 @@ class AdjustmentResource extends Resource
|
120
|
122
|
->toggleable(isToggledHiddenByDefault: true),
|
121
|
123
|
])
|
122
|
124
|
->filters([
|
123
|
|
- Tables\Filters\TernaryFilter::make('archived')
|
124
|
|
- ->label('Archived')
|
125
|
|
- ->default(false)
|
126
|
|
- ->attribute('archived_at')
|
127
|
|
- ->nullable(),
|
128
|
125
|
Tables\Filters\SelectFilter::make('status')
|
129
|
126
|
->label('Status')
|
130
|
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
|
173
|
Tables\Filters\SelectFilter::make('category')
|
133
|
174
|
->label('Category')
|
134
|
175
|
->native(false)
|
|
@@ -157,18 +198,30 @@ class AdjustmentResource extends Resource
|
157
|
198
|
->label('Reason for pausing')
|
158
|
199
|
->maxLength(255),
|
159
|
200
|
])
|
|
201
|
+ ->databaseTransaction()
|
|
202
|
+ ->successNotificationTitle('Adjustment paused')
|
|
203
|
+ ->failureNotificationTitle('Failed to pause adjustment')
|
160
|
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
|
206
|
$pausedUntil = $data['paused_until'] ?? null;
|
163
|
207
|
$reason = $data['status_reason'] ?? null;
|
164
|
208
|
$record->pause($reason, $pausedUntil);
|
|
209
|
+
|
|
210
|
+ $action->success();
|
165
|
211
|
}),
|
166
|
212
|
Tables\Actions\Action::make('resume')
|
167
|
213
|
->label('Resume')
|
168
|
214
|
->icon('heroicon-m-play')
|
169
|
215
|
->requiresConfirmation()
|
|
216
|
+ ->databaseTransaction()
|
|
217
|
+ ->successNotificationTitle('Adjustment resumed')
|
|
218
|
+ ->failureNotificationTitle('Failed to resume adjustment')
|
170
|
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
|
225
|
Tables\Actions\Action::make('archive')
|
173
|
226
|
->label('Archive')
|
174
|
227
|
->icon('heroicon-m-archive-box')
|
|
@@ -178,10 +231,15 @@ class AdjustmentResource extends Resource
|
178
|
231
|
->label('Reason for archiving')
|
179
|
232
|
->maxLength(255),
|
180
|
233
|
])
|
|
234
|
+ ->databaseTransaction()
|
|
235
|
+ ->successNotificationTitle('Adjustment archived')
|
|
236
|
+ ->failureNotificationTitle('Failed to archive adjustment')
|
181
|
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
|
239
|
$reason = $data['status_reason'] ?? null;
|
184
|
240
|
$record->archive($reason);
|
|
241
|
+
|
|
242
|
+ $action->success();
|
185
|
243
|
}),
|
186
|
244
|
]),
|
187
|
245
|
])
|