|
|
@@ -3,20 +3,23 @@
|
|
3
|
3
|
namespace App\Filament\Company\Resources\Sales\RecurringInvoiceResource\Pages;
|
|
4
|
4
|
|
|
5
|
5
|
use App\Enums\Accounting\DayOfMonth;
|
|
6
|
|
-use App\Enums\Accounting\DayOfWeek;
|
|
7
|
|
-use App\Enums\Accounting\EndType;
|
|
8
|
|
-use App\Enums\Accounting\Frequency;
|
|
9
|
|
-use App\Enums\Accounting\IntervalType;
|
|
10
|
|
-use App\Enums\Accounting\Month;
|
|
|
6
|
+use App\Enums\Accounting\DocumentType;
|
|
|
7
|
+use App\Filament\Company\Resources\Sales\ClientResource;
|
|
11
|
8
|
use App\Filament\Company\Resources\Sales\RecurringInvoiceResource;
|
|
12
|
|
-use App\Filament\Forms\Components\LabeledField;
|
|
13
|
|
-use App\Models\Setting\CompanyProfile;
|
|
14
|
|
-use App\Utilities\Localization\Timezone;
|
|
15
|
|
-use Filament\Forms;
|
|
16
|
|
-use Filament\Forms\Form;
|
|
|
9
|
+use App\Filament\Infolists\Components\DocumentPreview;
|
|
|
10
|
+use App\Models\Accounting\RecurringInvoice;
|
|
|
11
|
+use CodeWithDennis\SimpleAlert\Components\Infolists\SimpleAlert;
|
|
|
12
|
+use Filament\Actions;
|
|
|
13
|
+use Filament\Infolists\Components\Actions\Action;
|
|
|
14
|
+use Filament\Infolists\Components\Grid;
|
|
|
15
|
+use Filament\Infolists\Components\Section;
|
|
|
16
|
+use Filament\Infolists\Components\TextEntry;
|
|
|
17
|
+use Filament\Infolists\Infolist;
|
|
17
|
18
|
use Filament\Resources\Pages\ViewRecord;
|
|
|
19
|
+use Filament\Support\Enums\FontWeight;
|
|
|
20
|
+use Filament\Support\Enums\IconPosition;
|
|
|
21
|
+use Filament\Support\Enums\IconSize;
|
|
18
|
22
|
use Filament\Support\Enums\MaxWidth;
|
|
19
|
|
-use Guava\FilamentClusters\Forms\Cluster;
|
|
20
|
23
|
|
|
21
|
24
|
class ViewRecurringInvoice extends ViewRecord
|
|
22
|
25
|
{
|
|
|
@@ -35,285 +38,89 @@ class ViewRecurringInvoice extends ViewRecord
|
|
35
|
38
|
return MaxWidth::SixExtraLarge;
|
|
36
|
39
|
}
|
|
37
|
40
|
|
|
38
|
|
- public function form(Form $form): Form
|
|
|
41
|
+ protected function getHeaderActions(): array
|
|
39
|
42
|
{
|
|
40
|
|
- return $form
|
|
41
|
|
- ->disabled(false)
|
|
|
43
|
+ return [
|
|
|
44
|
+ Actions\ActionGroup::make([
|
|
|
45
|
+ Actions\EditAction::make(),
|
|
|
46
|
+ Actions\DeleteAction::make(),
|
|
|
47
|
+ RecurringInvoice::getUpdateScheduleAction(),
|
|
|
48
|
+ RecurringInvoice::getApproveDraftAction(),
|
|
|
49
|
+ ])
|
|
|
50
|
+ ->label('Actions')
|
|
|
51
|
+ ->button()
|
|
|
52
|
+ ->outlined()
|
|
|
53
|
+ ->dropdownPlacement('bottom-end')
|
|
|
54
|
+ ->icon('heroicon-c-chevron-down')
|
|
|
55
|
+ ->iconSize(IconSize::Small)
|
|
|
56
|
+ ->iconPosition(IconPosition::After),
|
|
|
57
|
+ ];
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ public function infolist(Infolist $infolist): Infolist
|
|
|
61
|
+ {
|
|
|
62
|
+ return $infolist
|
|
42
|
63
|
->schema([
|
|
43
|
|
- Forms\Components\Section::make('Scheduling')
|
|
|
64
|
+ SimpleAlert::make('scheduleIsNotSet')
|
|
|
65
|
+ ->info()
|
|
|
66
|
+ ->title('Schedule Not Set')
|
|
|
67
|
+ ->description('The schedule for this recurring invoice has not been set. You must set a schedule before you can approve this draft and start creating invoices.')
|
|
|
68
|
+ ->visible(fn (RecurringInvoice $record) => ! $record->hasSchedule())
|
|
|
69
|
+ ->columnSpanFull()
|
|
|
70
|
+ ->actions([
|
|
|
71
|
+ RecurringInvoice::getUpdateScheduleAction(Action::class)
|
|
|
72
|
+ ->outlined(),
|
|
|
73
|
+ ]),
|
|
|
74
|
+ Section::make('Invoice Details')
|
|
|
75
|
+ ->columns(4)
|
|
44
|
76
|
->schema([
|
|
45
|
|
- Forms\Components\Group::make([
|
|
46
|
|
- Forms\Components\Select::make('frequency')
|
|
47
|
|
- ->label('Repeat this invoice')
|
|
48
|
|
- ->inlineLabel()
|
|
49
|
|
- ->options(Frequency::class)
|
|
50
|
|
- ->softRequired()
|
|
51
|
|
- ->live()
|
|
52
|
|
- ->afterStateUpdated(function (Forms\Set $set, $state) {
|
|
53
|
|
- $frequency = Frequency::parse($state);
|
|
54
|
|
-
|
|
55
|
|
- if ($frequency->isDaily()) {
|
|
56
|
|
- $set('interval_value', null);
|
|
57
|
|
- $set('interval_type', null);
|
|
58
|
|
- }
|
|
59
|
|
-
|
|
60
|
|
- if ($frequency->isWeekly()) {
|
|
61
|
|
- $currentDayOfWeek = now()->dayOfWeek;
|
|
62
|
|
- $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
|
|
63
|
|
- $set('day_of_week', $currentDayOfWeek);
|
|
64
|
|
- $set('interval_value', null);
|
|
65
|
|
- $set('interval_type', null);
|
|
66
|
|
- }
|
|
67
|
|
-
|
|
68
|
|
- if ($frequency->isMonthly()) {
|
|
69
|
|
- $set('day_of_month', DayOfMonth::First);
|
|
70
|
|
- $set('interval_value', null);
|
|
71
|
|
- $set('interval_type', null);
|
|
72
|
|
- }
|
|
73
|
|
-
|
|
74
|
|
- if ($frequency->isYearly()) {
|
|
75
|
|
- $currentMonth = now()->month;
|
|
76
|
|
- $currentMonth = Month::parse($currentMonth);
|
|
77
|
|
- $set('month', $currentMonth);
|
|
78
|
|
-
|
|
79
|
|
- $currentDay = now()->dayOfMonth;
|
|
80
|
|
- $currentDay = DayOfMonth::parse($currentDay);
|
|
81
|
|
- $set('day_of_month', $currentDay);
|
|
82
|
|
-
|
|
83
|
|
- $set('interval_value', null);
|
|
84
|
|
- $set('interval_type', null);
|
|
85
|
|
- }
|
|
86
|
|
-
|
|
87
|
|
- if ($frequency->isCustom()) {
|
|
88
|
|
- $set('interval_value', 1);
|
|
89
|
|
- $set('interval_type', IntervalType::Month);
|
|
90
|
|
-
|
|
91
|
|
- $currentDay = now()->dayOfMonth;
|
|
92
|
|
- $currentDay = DayOfMonth::parse($currentDay);
|
|
93
|
|
- $set('day_of_month', $currentDay);
|
|
94
|
|
- }
|
|
95
|
|
- }),
|
|
96
|
|
-
|
|
97
|
|
- // Custom frequency fields
|
|
98
|
|
-
|
|
99
|
|
- LabeledField::make()
|
|
100
|
|
- ->prefix('every')
|
|
101
|
|
- ->schema([
|
|
102
|
|
- Cluster::make([
|
|
103
|
|
- Forms\Components\TextInput::make('interval_value')
|
|
104
|
|
- ->label('every')
|
|
105
|
|
- ->numeric()
|
|
106
|
|
- ->default(1),
|
|
107
|
|
- Forms\Components\Select::make('interval_type')
|
|
108
|
|
- ->label('Interval Type')
|
|
109
|
|
- ->options(IntervalType::class)
|
|
110
|
|
- ->softRequired()
|
|
111
|
|
- ->default(IntervalType::Month)
|
|
112
|
|
- ->live()
|
|
113
|
|
- ->afterStateUpdated(function (Forms\Set $set, $state) {
|
|
114
|
|
- $intervalType = IntervalType::parse($state);
|
|
115
|
|
-
|
|
116
|
|
- if ($intervalType->isWeek()) {
|
|
117
|
|
- $currentDayOfWeek = now()->dayOfWeek;
|
|
118
|
|
- $currentDayOfWeek = DayOfWeek::parse($currentDayOfWeek);
|
|
119
|
|
- $set('day_of_week', $currentDayOfWeek);
|
|
120
|
|
- }
|
|
121
|
|
-
|
|
122
|
|
- if ($intervalType->isMonth()) {
|
|
123
|
|
- $currentDay = now()->dayOfMonth;
|
|
124
|
|
- $currentDay = DayOfMonth::parse($currentDay);
|
|
125
|
|
- $set('day_of_month', $currentDay);
|
|
126
|
|
- }
|
|
127
|
|
-
|
|
128
|
|
- if ($intervalType->isYear()) {
|
|
129
|
|
- $currentMonth = now()->month;
|
|
130
|
|
- $currentMonth = Month::parse($currentMonth);
|
|
131
|
|
- $set('month', $currentMonth);
|
|
132
|
|
-
|
|
133
|
|
- $currentDay = now()->dayOfMonth;
|
|
134
|
|
- $currentDay = DayOfMonth::parse($currentDay);
|
|
135
|
|
- $set('day_of_month', $currentDay);
|
|
136
|
|
- }
|
|
137
|
|
- }),
|
|
138
|
|
- ])
|
|
139
|
|
- ->live()
|
|
140
|
|
- ->hiddenLabel(),
|
|
141
|
|
- ])
|
|
142
|
|
- ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))->isCustom()),
|
|
143
|
|
-
|
|
144
|
|
- LabeledField::make()
|
|
145
|
|
- ->prefix(function (Forms\Get $get) {
|
|
146
|
|
- $frequency = Frequency::parse($get('frequency'));
|
|
147
|
|
- $intervalType = IntervalType::parse($get('interval_type'));
|
|
148
|
|
-
|
|
149
|
|
- if ($frequency->isYearly()) {
|
|
150
|
|
- return 'every';
|
|
151
|
|
- }
|
|
152
|
|
-
|
|
153
|
|
- if ($frequency->isCustom() && $intervalType?->isYear()) {
|
|
154
|
|
- return 'in';
|
|
155
|
|
- }
|
|
156
|
|
-
|
|
157
|
|
- return null;
|
|
158
|
|
- })
|
|
159
|
|
- ->schema([
|
|
160
|
|
- Forms\Components\Select::make('month')
|
|
161
|
|
- ->hiddenLabel()
|
|
162
|
|
- ->options(Month::class)
|
|
163
|
|
- ->live()
|
|
164
|
|
- ->softRequired(),
|
|
165
|
|
- ])
|
|
166
|
|
- ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))->isYearly() || IntervalType::parse($get('interval_type'))?->isYear()),
|
|
167
|
|
-
|
|
168
|
|
- LabeledField::make()
|
|
169
|
|
- ->prefix('on the')
|
|
170
|
|
- ->suffix(function (Forms\Get $get) {
|
|
171
|
|
- $frequency = Frequency::parse($get('frequency'));
|
|
172
|
|
- $intervalType = IntervalType::parse($get('interval_type'));
|
|
173
|
|
-
|
|
174
|
|
- if ($frequency->isMonthly()) {
|
|
175
|
|
- return 'day of every month';
|
|
176
|
|
- }
|
|
177
|
|
-
|
|
178
|
|
- if ($frequency->isYearly() || ($frequency->isCustom() && $intervalType->isMonth()) || ($frequency->isCustom() && $intervalType->isYear())) {
|
|
179
|
|
- return 'day of the month';
|
|
180
|
|
- }
|
|
181
|
|
-
|
|
182
|
|
- return null;
|
|
183
|
|
- })
|
|
184
|
|
- ->schema([
|
|
185
|
|
- Forms\Components\Select::make('day_of_month')
|
|
186
|
|
- ->hiddenLabel()
|
|
187
|
|
- ->inlineLabel()
|
|
188
|
|
- ->options(DayOfMonth::class)
|
|
189
|
|
- ->live()
|
|
190
|
|
- ->softRequired(),
|
|
191
|
|
- ])
|
|
192
|
|
- ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isMonthly() || Frequency::parse($get('frequency'))?->isYearly() || IntervalType::parse($get('interval_type'))?->isMonth() || IntervalType::parse($get('interval_type'))?->isYear()),
|
|
193
|
|
-
|
|
194
|
|
- LabeledField::make()
|
|
195
|
|
- ->prefix(function (Forms\Get $get) {
|
|
196
|
|
- $frequency = Frequency::parse($get('frequency'));
|
|
197
|
|
- $intervalType = IntervalType::parse($get('interval_type'));
|
|
198
|
|
-
|
|
199
|
|
- if ($frequency->isWeekly()) {
|
|
200
|
|
- return 'every';
|
|
201
|
|
- }
|
|
202
|
|
-
|
|
203
|
|
- if ($frequency->isCustom() && $intervalType->isWeek()) {
|
|
204
|
|
- return 'on';
|
|
205
|
|
- }
|
|
206
|
|
-
|
|
207
|
|
- return null;
|
|
208
|
|
- })
|
|
209
|
|
- ->schema([
|
|
210
|
|
- Forms\Components\Select::make('day_of_week')
|
|
211
|
|
- ->hiddenLabel()
|
|
212
|
|
- ->options(DayOfWeek::class)
|
|
213
|
|
- ->live()
|
|
214
|
|
- ->softRequired(),
|
|
215
|
|
- ])
|
|
216
|
|
- ->visible(fn (Forms\Get $get) => Frequency::parse($get('frequency'))?->isWeekly() || IntervalType::parse($get('interval_type'))?->isWeek()),
|
|
217
|
|
- ])->columns(2),
|
|
218
|
|
-
|
|
219
|
|
- Forms\Components\Group::make([
|
|
220
|
|
- Forms\Components\DatePicker::make('start_date')
|
|
221
|
|
- ->label('Create first invoice on')
|
|
222
|
|
- ->inlineLabel()
|
|
223
|
|
- ->softRequired(),
|
|
224
|
|
-
|
|
225
|
|
- LabeledField::make()
|
|
226
|
|
- ->prefix('and end')
|
|
227
|
|
- ->suffix(function (Forms\Get $get) {
|
|
228
|
|
- $endType = EndType::parse($get('end_type'));
|
|
229
|
|
-
|
|
230
|
|
- if ($endType->isAfter()) {
|
|
231
|
|
- return 'invoices';
|
|
232
|
|
- }
|
|
233
|
|
-
|
|
234
|
|
- return null;
|
|
235
|
|
- })
|
|
236
|
|
- ->schema(function (Forms\Get $get) {
|
|
237
|
|
- $components = [];
|
|
238
|
|
-
|
|
239
|
|
- $components[] = Forms\Components\Select::make('end_type')
|
|
240
|
|
- ->hiddenLabel()
|
|
241
|
|
- ->options(EndType::class)
|
|
242
|
|
- ->softRequired()
|
|
243
|
|
- ->live()
|
|
244
|
|
- ->afterStateUpdated(function (Forms\Set $set, $state) {
|
|
245
|
|
- $endType = EndType::parse($state);
|
|
246
|
|
-
|
|
247
|
|
- if ($endType->isNever()) {
|
|
248
|
|
- $set('max_occurrences', null);
|
|
249
|
|
- $set('end_date', null);
|
|
250
|
|
- }
|
|
251
|
|
-
|
|
252
|
|
- if ($endType->isAfter()) {
|
|
253
|
|
- $set('max_occurrences', 1);
|
|
254
|
|
- $set('end_date', null);
|
|
255
|
|
- }
|
|
256
|
|
-
|
|
257
|
|
- if ($endType->isOn()) {
|
|
258
|
|
- $set('max_occurrences', null);
|
|
259
|
|
- $set('end_date', now()->addMonth()->startOfMonth());
|
|
260
|
|
- }
|
|
261
|
|
- });
|
|
262
|
|
-
|
|
263
|
|
- $endType = EndType::parse($get('end_type'));
|
|
264
|
|
-
|
|
265
|
|
- if ($endType->isAfter()) {
|
|
266
|
|
- $components[] = Forms\Components\TextInput::make('max_occurrences')
|
|
267
|
|
- ->numeric()
|
|
268
|
|
- ->live();
|
|
269
|
|
- }
|
|
270
|
|
-
|
|
271
|
|
- if ($endType->isOn()) {
|
|
272
|
|
- $components[] = Forms\Components\DatePicker::make('end_date')
|
|
273
|
|
- ->live();
|
|
274
|
|
- }
|
|
275
|
|
-
|
|
276
|
|
- return [
|
|
277
|
|
- Cluster::make($components)
|
|
278
|
|
- ->hiddenLabel(),
|
|
279
|
|
- ];
|
|
280
|
|
- }),
|
|
281
|
|
- ])->columns(2),
|
|
282
|
|
-
|
|
283
|
|
- Forms\Components\Group::make([
|
|
284
|
|
- LabeledField::make()
|
|
285
|
|
- ->prefix('Create in')
|
|
286
|
|
- ->suffix('time zone')
|
|
287
|
|
- ->schema([
|
|
288
|
|
- Forms\Components\Select::make('timezone')
|
|
289
|
|
- ->softRequired()
|
|
290
|
|
- ->hiddenLabel()
|
|
291
|
|
- ->options(Timezone::getTimezoneOptions(CompanyProfile::first()->country))
|
|
292
|
|
- ->searchable(),
|
|
293
|
|
- ])
|
|
294
|
|
- ->columns(1),
|
|
295
|
|
- ])->columns(2),
|
|
296
|
|
- ])
|
|
297
|
|
- ->headerActions([
|
|
298
|
|
- Forms\Components\Actions\Action::make('save')
|
|
299
|
|
- ->label('Submit and Approve')
|
|
300
|
|
- ->button()
|
|
301
|
|
- ->successNotificationTitle('Scheduling saved')
|
|
302
|
|
- ->action(function (Forms\Components\Actions\Action $action) {
|
|
303
|
|
- $this->save();
|
|
304
|
|
-
|
|
305
|
|
- $action->success();
|
|
306
|
|
- }),
|
|
|
77
|
+ Grid::make(1)
|
|
|
78
|
+ ->schema([
|
|
|
79
|
+ TextEntry::make('status')
|
|
|
80
|
+ ->badge(),
|
|
|
81
|
+ TextEntry::make('client.name')
|
|
|
82
|
+ ->label('Client')
|
|
|
83
|
+ ->color('primary')
|
|
|
84
|
+ ->weight(FontWeight::SemiBold)
|
|
|
85
|
+ ->url(static fn (RecurringInvoice $record) => ClientResource::getUrl('edit', ['record' => $record->client_id])),
|
|
|
86
|
+ TextEntry::make('last_date')
|
|
|
87
|
+ ->label('Last Invoice')
|
|
|
88
|
+ ->date()
|
|
|
89
|
+ ->placeholder('Not Created'),
|
|
|
90
|
+ TextEntry::make('next_date')
|
|
|
91
|
+ ->label('Next Invoice')
|
|
|
92
|
+ ->placeholder('Not Scheduled')
|
|
|
93
|
+ ->date(),
|
|
|
94
|
+ TextEntry::make('schedule')
|
|
|
95
|
+ ->label('Schedule')
|
|
|
96
|
+ ->getStateUsing(function (RecurringInvoice $record) {
|
|
|
97
|
+ return $record->getScheduleDescription();
|
|
|
98
|
+ })
|
|
|
99
|
+ ->helperText(function (RecurringInvoice $record) {
|
|
|
100
|
+ return $record->getTimelineDescription();
|
|
|
101
|
+ }),
|
|
|
102
|
+ TextEntry::make('occurrences_count')
|
|
|
103
|
+ ->label('Invoices Created')
|
|
|
104
|
+ ->visible(fn (RecurringInvoice $record) => $record->occurrences_count > 0),
|
|
|
105
|
+ TextEntry::make('end_date')
|
|
|
106
|
+ ->label('Ends On')
|
|
|
107
|
+ ->date()
|
|
|
108
|
+ ->visible(fn (RecurringInvoice $record) => $record->end_type?->isOn()),
|
|
|
109
|
+ TextEntry::make('approved_at')
|
|
|
110
|
+ ->label('Approved At')
|
|
|
111
|
+ ->placeholder('Not Approved')
|
|
|
112
|
+ ->date(),
|
|
|
113
|
+ TextEntry::make('ended_at')
|
|
|
114
|
+ ->label('Ended At')
|
|
|
115
|
+ ->date()
|
|
|
116
|
+ ->visible(fn (RecurringInvoice $record) => $record->ended_at),
|
|
|
117
|
+ TextEntry::make('total')
|
|
|
118
|
+ ->label('Invoice Amount')
|
|
|
119
|
+ ->currency(static fn (RecurringInvoice $record) => $record->currency_code),
|
|
|
120
|
+ ])->columnSpan(1),
|
|
|
121
|
+ DocumentPreview::make()
|
|
|
122
|
+ ->type(DocumentType::RecurringInvoice),
|
|
307
|
123
|
]),
|
|
308
|
124
|
]);
|
|
309
|
125
|
}
|
|
310
|
|
-
|
|
311
|
|
- public function save(): void
|
|
312
|
|
- {
|
|
313
|
|
- $state = $this->form->getState();
|
|
314
|
|
-
|
|
315
|
|
- $this->getRecord()->update($state);
|
|
316
|
|
-
|
|
317
|
|
- $this->getRecord()->markAsApproved();
|
|
318
|
|
- }
|
|
319
|
126
|
}
|