|
|
@@ -6,12 +6,21 @@ use App\Filament\Company\Resources\Sales\ClientResource\Pages;
|
|
6
|
6
|
use App\Filament\Forms\Components\CreateCurrencySelect;
|
|
7
|
7
|
use App\Filament\Forms\Components\CustomSection;
|
|
8
|
8
|
use App\Filament\Forms\Components\PhoneBuilder;
|
|
|
9
|
+use App\Filament\Tables\Columns;
|
|
|
10
|
+use App\Models\Common\Address;
|
|
9
|
11
|
use App\Models\Common\Client;
|
|
|
12
|
+use App\Models\Locale\Country;
|
|
|
13
|
+use App\Models\Locale\State;
|
|
|
14
|
+use App\Utilities\Currency\CurrencyConverter;
|
|
10
|
15
|
use Filament\Forms;
|
|
11
|
16
|
use Filament\Forms\Form;
|
|
|
17
|
+use Filament\Forms\Get;
|
|
|
18
|
+use Filament\Forms\Set;
|
|
12
|
19
|
use Filament\Resources\Resource;
|
|
13
|
20
|
use Filament\Tables;
|
|
14
|
21
|
use Filament\Tables\Table;
|
|
|
22
|
+use Illuminate\Database\Eloquent\Builder;
|
|
|
23
|
+use Illuminate\Support\HtmlString;
|
|
15
|
24
|
|
|
16
|
25
|
class ClientResource extends Resource
|
|
17
|
26
|
{
|
|
|
@@ -170,6 +179,8 @@ class ClientResource extends Resource
|
|
170
|
179
|
CreateCurrencySelect::make('currency_code'),
|
|
171
|
180
|
CustomSection::make('Billing Address')
|
|
172
|
181
|
->relationship('billingAddress')
|
|
|
182
|
+ ->saveRelationshipsUsing(null)
|
|
|
183
|
+ ->dehydrated(true)
|
|
173
|
184
|
->contained(false)
|
|
174
|
185
|
->schema([
|
|
175
|
186
|
Forms\Components\Hidden::make('type')
|
|
|
@@ -181,34 +192,42 @@ class ClientResource extends Resource
|
|
181
|
192
|
Forms\Components\TextInput::make('address_line_2')
|
|
182
|
193
|
->label('Address Line 2')
|
|
183
|
194
|
->maxLength(255),
|
|
|
195
|
+ Forms\Components\Select::make('country')
|
|
|
196
|
+ ->searchable()
|
|
|
197
|
+ ->localizeLabel()
|
|
|
198
|
+ ->live()
|
|
|
199
|
+ ->options(Country::getAvailableCountryOptions())
|
|
|
200
|
+ ->afterStateUpdated(static function (Set $set) {
|
|
|
201
|
+ $set('state_id', null);
|
|
|
202
|
+ })
|
|
|
203
|
+ ->required(),
|
|
|
204
|
+ Forms\Components\Select::make('state_id')
|
|
|
205
|
+ ->localizeLabel('State / Province')
|
|
|
206
|
+ ->searchable()
|
|
|
207
|
+ ->options(static fn (Get $get) => State::getStateOptions($get('country')))
|
|
|
208
|
+ ->nullable(),
|
|
184
|
209
|
Forms\Components\TextInput::make('city')
|
|
185
|
210
|
->label('City')
|
|
186
|
211
|
->required()
|
|
187
|
212
|
->maxLength(255),
|
|
188
|
|
- Forms\Components\TextInput::make('state')
|
|
189
|
|
- ->label('State')
|
|
190
|
|
- ->required()
|
|
191
|
|
- ->maxLength(255),
|
|
192
|
213
|
Forms\Components\TextInput::make('postal_code')
|
|
193
|
214
|
->label('Postal Code / Zip Code')
|
|
194
|
215
|
->required()
|
|
195
|
216
|
->maxLength(255),
|
|
196
|
|
- Forms\Components\TextInput::make('country')
|
|
197
|
|
- ->label('Country')
|
|
198
|
|
- ->required()
|
|
199
|
|
- ->maxLength(255),
|
|
200
|
217
|
])->columns(),
|
|
201
|
218
|
])
|
|
202
|
219
|
->columns(1),
|
|
203
|
220
|
Forms\Components\Section::make('Shipping')
|
|
204
|
221
|
->relationship('shippingAddress')
|
|
|
222
|
+ ->saveRelationshipsUsing(null)
|
|
|
223
|
+ ->dehydrated(true)
|
|
205
|
224
|
->schema([
|
|
|
225
|
+ Forms\Components\Hidden::make('type')
|
|
|
226
|
+ ->default('shipping'),
|
|
206
|
227
|
Forms\Components\TextInput::make('recipient')
|
|
207
|
228
|
->label('Recipient')
|
|
208
|
229
|
->required()
|
|
209
|
230
|
->maxLength(255),
|
|
210
|
|
- Forms\Components\Hidden::make('type')
|
|
211
|
|
- ->default('shipping'),
|
|
212
|
231
|
Forms\Components\TextInput::make('phone')
|
|
213
|
232
|
->label('Phone')
|
|
214
|
233
|
->required()
|
|
|
@@ -216,29 +235,70 @@ class ClientResource extends Resource
|
|
216
|
235
|
CustomSection::make('Shipping Address')
|
|
217
|
236
|
->contained(false)
|
|
218
|
237
|
->schema([
|
|
219
|
|
- Forms\Components\TextInput::make('address_line_1')
|
|
220
|
|
- ->label('Address Line 1')
|
|
221
|
|
- ->required()
|
|
222
|
|
- ->maxLength(255),
|
|
223
|
|
- Forms\Components\TextInput::make('address_line_2')
|
|
224
|
|
- ->label('Address Line 2')
|
|
225
|
|
- ->maxLength(255),
|
|
226
|
|
- Forms\Components\TextInput::make('city')
|
|
227
|
|
- ->label('City')
|
|
228
|
|
- ->required()
|
|
229
|
|
- ->maxLength(255),
|
|
230
|
|
- Forms\Components\TextInput::make('state')
|
|
231
|
|
- ->label('State')
|
|
232
|
|
- ->required()
|
|
233
|
|
- ->maxLength(255),
|
|
234
|
|
- Forms\Components\TextInput::make('postal_code')
|
|
235
|
|
- ->label('Postal Code / Zip Code')
|
|
236
|
|
- ->required()
|
|
237
|
|
- ->maxLength(255),
|
|
238
|
|
- Forms\Components\TextInput::make('country')
|
|
239
|
|
- ->label('Country')
|
|
240
|
|
- ->required()
|
|
241
|
|
- ->maxLength(255),
|
|
|
238
|
+ Forms\Components\Checkbox::make('same_as_billing')
|
|
|
239
|
+ ->label('Same as Billing Address')
|
|
|
240
|
+ ->live()
|
|
|
241
|
+ ->afterStateHydrated(function (?Address $record, Forms\Components\Checkbox $component) {
|
|
|
242
|
+ if (! $record || $record->parent_address_id) {
|
|
|
243
|
+ return $component->state(true);
|
|
|
244
|
+ }
|
|
|
245
|
+
|
|
|
246
|
+ return $component->state(false);
|
|
|
247
|
+ })
|
|
|
248
|
+ ->afterStateUpdated(static function (Get $get, Set $set, $state) {
|
|
|
249
|
+ if ($state) {
|
|
|
250
|
+ return;
|
|
|
251
|
+ }
|
|
|
252
|
+
|
|
|
253
|
+ $billingAddress = $get('../billingAddress');
|
|
|
254
|
+
|
|
|
255
|
+ $fieldsToSync = [
|
|
|
256
|
+ 'address_line_1',
|
|
|
257
|
+ 'address_line_2',
|
|
|
258
|
+ 'country',
|
|
|
259
|
+ 'state_id',
|
|
|
260
|
+ 'city',
|
|
|
261
|
+ 'postal_code',
|
|
|
262
|
+ ];
|
|
|
263
|
+
|
|
|
264
|
+ foreach ($fieldsToSync as $field) {
|
|
|
265
|
+ $set($field, $billingAddress[$field]);
|
|
|
266
|
+ }
|
|
|
267
|
+ })
|
|
|
268
|
+ ->columnSpanFull(),
|
|
|
269
|
+ Forms\Components\Grid::make()
|
|
|
270
|
+ ->schema([
|
|
|
271
|
+ Forms\Components\TextInput::make('address_line_1')
|
|
|
272
|
+ ->label('Address Line 1')
|
|
|
273
|
+ ->required()
|
|
|
274
|
+ ->maxLength(255),
|
|
|
275
|
+ Forms\Components\TextInput::make('address_line_2')
|
|
|
276
|
+ ->label('Address Line 2')
|
|
|
277
|
+ ->maxLength(255),
|
|
|
278
|
+ Forms\Components\Select::make('country')
|
|
|
279
|
+ ->searchable()
|
|
|
280
|
+ ->localizeLabel()
|
|
|
281
|
+ ->live()
|
|
|
282
|
+ ->options(Country::getAvailableCountryOptions())
|
|
|
283
|
+ ->afterStateUpdated(static function (Set $set) {
|
|
|
284
|
+ $set('state_id', null);
|
|
|
285
|
+ })
|
|
|
286
|
+ ->required(),
|
|
|
287
|
+ Forms\Components\Select::make('state_id')
|
|
|
288
|
+ ->localizeLabel('State / Province')
|
|
|
289
|
+ ->searchable()
|
|
|
290
|
+ ->options(static fn (Get $get) => State::getStateOptions($get('country')))
|
|
|
291
|
+ ->nullable(),
|
|
|
292
|
+ Forms\Components\TextInput::make('city')
|
|
|
293
|
+ ->label('City')
|
|
|
294
|
+ ->required()
|
|
|
295
|
+ ->maxLength(255),
|
|
|
296
|
+ Forms\Components\TextInput::make('postal_code')
|
|
|
297
|
+ ->label('Postal Code / Zip Code')
|
|
|
298
|
+ ->required()
|
|
|
299
|
+ ->maxLength(255),
|
|
|
300
|
+ ])
|
|
|
301
|
+ ->visible(fn (Get $get) => ! $get('same_as_billing')),
|
|
242
|
302
|
Forms\Components\Textarea::make('notes')
|
|
243
|
303
|
->label('Delivery Instructions')
|
|
244
|
304
|
->maxLength(255)
|
|
|
@@ -252,15 +312,57 @@ class ClientResource extends Resource
|
|
252
|
312
|
{
|
|
253
|
313
|
return $table
|
|
254
|
314
|
->columns([
|
|
|
315
|
+ Columns::id(),
|
|
255
|
316
|
Tables\Columns\TextColumn::make('name')
|
|
256
|
317
|
->searchable()
|
|
|
318
|
+ ->sortable()
|
|
257
|
319
|
->description(fn (Client $client) => $client->primaryContact->full_name),
|
|
|
320
|
+ Tables\Columns\TextColumn::make('account_number')
|
|
|
321
|
+ ->label('Account Number')
|
|
|
322
|
+ ->searchable()
|
|
|
323
|
+ ->sortable(),
|
|
258
|
324
|
Tables\Columns\TextColumn::make('primaryContact.email')
|
|
259
|
325
|
->label('Email')
|
|
260
|
|
- ->searchable(),
|
|
|
326
|
+ ->searchable()
|
|
|
327
|
+ ->toggleable(),
|
|
261
|
328
|
Tables\Columns\TextColumn::make('primaryContact.phones')
|
|
262
|
329
|
->label('Phone')
|
|
|
330
|
+ ->toggleable()
|
|
263
|
331
|
->state(fn (Client $client) => $client->primaryContact->first_available_phone),
|
|
|
332
|
+ Tables\Columns\TextColumn::make('billingAddress.address_string')
|
|
|
333
|
+ ->label('Billing Address')
|
|
|
334
|
+ ->searchable()
|
|
|
335
|
+ ->toggleable()
|
|
|
336
|
+ ->listWithLineBreaks(),
|
|
|
337
|
+ Tables\Columns\TextColumn::make('balance')
|
|
|
338
|
+ ->label('Balance')
|
|
|
339
|
+ ->getStateUsing(function (Client $client) {
|
|
|
340
|
+ return $client->invoices()
|
|
|
341
|
+ ->unpaid()
|
|
|
342
|
+ ->get()
|
|
|
343
|
+ ->sumMoneyInDefaultCurrency('amount_due');
|
|
|
344
|
+ })
|
|
|
345
|
+ ->description(function (Client $client) {
|
|
|
346
|
+ $overdue = $client->invoices()
|
|
|
347
|
+ ->overdue()
|
|
|
348
|
+ ->get()
|
|
|
349
|
+ ->sumMoneyInDefaultCurrency('amount_due');
|
|
|
350
|
+
|
|
|
351
|
+ if ($overdue <= 0) {
|
|
|
352
|
+ return null;
|
|
|
353
|
+ }
|
|
|
354
|
+
|
|
|
355
|
+ $formattedOverdue = CurrencyConverter::formatCentsToMoney($overdue);
|
|
|
356
|
+
|
|
|
357
|
+ return new HtmlString("<span class='text-danger-700 dark:text-danger-400'>Overdue: {$formattedOverdue}</span>");
|
|
|
358
|
+ })
|
|
|
359
|
+ ->sortable(query: function (Builder $query, string $direction) {
|
|
|
360
|
+ return $query
|
|
|
361
|
+ ->withSum(['invoices' => fn (Builder $query) => $query->unpaid()], 'amount_due')
|
|
|
362
|
+ ->orderBy('invoices_sum_amount_due', $direction);
|
|
|
363
|
+ })
|
|
|
364
|
+ ->currency(fn (Client $client) => $client->currency_code, false)
|
|
|
365
|
+ ->alignEnd(),
|
|
264
|
366
|
])
|
|
265
|
367
|
->filters([
|
|
266
|
368
|
//
|