Andrew Wallo 3 months ago
parent
commit
7956ff2ad1

+ 5
- 9
app/Filament/Company/Resources/Purchases/BillResource.php View File

28
 use App\Models\Banking\BankAccount;
28
 use App\Models\Banking\BankAccount;
29
 use App\Models\Common\Offering;
29
 use App\Models\Common\Offering;
30
 use App\Models\Common\Vendor;
30
 use App\Models\Common\Vendor;
31
-use App\Services\CompanySettingsService;
32
 use App\Utilities\Currency\CurrencyAccessor;
31
 use App\Utilities\Currency\CurrencyAccessor;
33
 use App\Utilities\Currency\CurrencyConverter;
32
 use App\Utilities\Currency\CurrencyConverter;
34
 use App\Utilities\RateCalculator;
33
 use App\Utilities\RateCalculator;
89
                                     Forms\Components\DatePicker::make('date')
88
                                     Forms\Components\DatePicker::make('date')
90
                                         ->label('Bill date')
89
                                         ->label('Bill date')
91
                                         ->live()
90
                                         ->live()
92
-                                        ->default(now())
93
-                                        ->timezone(CompanySettingsService::getDefaultTimezone())
91
+                                        ->default(company_today()->toDateString())
94
                                         ->disabled(function (?Bill $record) {
92
                                         ->disabled(function (?Bill $record) {
95
                                             return $record?->hasPayments();
93
                                             return $record?->hasPayments();
96
                                         })
94
                                         })
139
                                     ->columns(3),
137
                                     ->columns(3),
140
                                 Forms\Components\DatePicker::make('due_date')
138
                                 Forms\Components\DatePicker::make('due_date')
141
                                     ->label('Due date')
139
                                     ->label('Due date')
142
-                                    ->default(function () use ($company) {
143
-                                        return now()->addDays($company->defaultBill->payment_terms->getDays());
140
+                                    ->default(function () use ($settings) {
141
+                                        return company_today()->addDays($settings->payment_terms->getDays())->toDateString();
144
                                     })
142
                                     })
145
-                                    ->timezone(CompanySettingsService::getDefaultTimezone())
146
                                     ->required()
143
                                     ->required()
147
                                     ->live()
144
                                     ->live()
148
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
145
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
453
                             })
450
                             })
454
                             ->mountUsing(function (Bill $record, Form $form) {
451
                             ->mountUsing(function (Bill $record, Form $form) {
455
                                 $form->fill([
452
                                 $form->fill([
456
-                                    'posted_at' => now(),
453
+                                    'posted_at' => company_today()->toDateString(),
457
                                     'amount' => $record->amount_due,
454
                                     'amount' => $record->amount_due,
458
                                 ]);
455
                                 ]);
459
                             })
456
                             })
461
                             ->successNotificationTitle('Payment recorded')
458
                             ->successNotificationTitle('Payment recorded')
462
                             ->form([
459
                             ->form([
463
                                 Forms\Components\DatePicker::make('posted_at')
460
                                 Forms\Components\DatePicker::make('posted_at')
464
-                                    ->label('Date')
465
-                                    ->timezone(CompanySettingsService::getDefaultTimezone()),
461
+                                    ->label('Date'),
466
                                 Forms\Components\Grid::make()
462
                                 Forms\Components\Grid::make()
467
                                     ->schema([
463
                                     ->schema([
468
                                         Forms\Components\Select::make('bank_account_id')
464
                                         Forms\Components\Select::make('bank_account_id')

+ 3
- 6
app/Filament/Company/Resources/Sales/EstimateResource.php View File

29
 use App\Models\Accounting\Estimate;
29
 use App\Models\Accounting\Estimate;
30
 use App\Models\Common\Client;
30
 use App\Models\Common\Client;
31
 use App\Models\Common\Offering;
31
 use App\Models\Common\Offering;
32
-use App\Services\CompanySettingsService;
33
 use App\Utilities\Currency\CurrencyAccessor;
32
 use App\Utilities\Currency\CurrencyAccessor;
34
 use App\Utilities\Currency\CurrencyConverter;
33
 use App\Utilities\Currency\CurrencyConverter;
35
 use App\Utilities\RateCalculator;
34
 use App\Utilities\RateCalculator;
92
                                     Forms\Components\DatePicker::make('date')
91
                                     Forms\Components\DatePicker::make('date')
93
                                         ->label('Estimate date')
92
                                         ->label('Estimate date')
94
                                         ->live()
93
                                         ->live()
95
-                                        ->default(now())
96
-                                        ->timezone(CompanySettingsService::getDefaultTimezone())
94
+                                        ->default(company_today()->toDateString())
97
                                         ->columnSpan(2)
95
                                         ->columnSpan(2)
98
                                         ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
96
                                         ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
99
                                             $date = Carbon::parse($state)->toDateString();
97
                                             $date = Carbon::parse($state)->toDateString();
139
                                 Forms\Components\DatePicker::make('expiration_date')
137
                                 Forms\Components\DatePicker::make('expiration_date')
140
                                     ->label('Expiration date')
138
                                     ->label('Expiration date')
141
                                     ->default(function () use ($settings) {
139
                                     ->default(function () use ($settings) {
142
-                                        return now()->addDays($settings->payment_terms->getDays());
140
+                                        return company_today()->addDays($settings->payment_terms->getDays())->toDateString();
143
                                     })
141
                                     })
144
-                                    ->timezone(CompanySettingsService::getDefaultTimezone())
145
                                     ->minDate(static function (Forms\Get $get) {
142
                                     ->minDate(static function (Forms\Get $get) {
146
-                                        return Carbon::parse($get('date'))->toDateString() ?? now(CompanySettingsService::getDefaultTimezone())->toDateString();
143
+                                        return Carbon::parse($get('date'))->toDateString() ?? company_today()->toDateString();
147
                                     })
144
                                     })
148
                                     ->live()
145
                                     ->live()
149
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
146
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {

+ 3
- 6
app/Filament/Company/Resources/Sales/InvoiceResource.php View File

29
 use App\Models\Accounting\Invoice;
29
 use App\Models\Accounting\Invoice;
30
 use App\Models\Common\Client;
30
 use App\Models\Common\Client;
31
 use App\Models\Common\Offering;
31
 use App\Models\Common\Offering;
32
-use App\Services\CompanySettingsService;
33
 use App\Utilities\Currency\CurrencyAccessor;
32
 use App\Utilities\Currency\CurrencyAccessor;
34
 use App\Utilities\Currency\CurrencyConverter;
33
 use App\Utilities\Currency\CurrencyConverter;
35
 use App\Utilities\RateCalculator;
34
 use App\Utilities\RateCalculator;
96
                                     Forms\Components\DatePicker::make('date')
95
                                     Forms\Components\DatePicker::make('date')
97
                                         ->label('Invoice date')
96
                                         ->label('Invoice date')
98
                                         ->live()
97
                                         ->live()
99
-                                        ->default(now())
100
-                                        ->timezone(CompanySettingsService::getDefaultTimezone())
98
+                                        ->default(company_today()->toDateString())
101
                                         ->disabled(function (?Invoice $record) {
99
                                         ->disabled(function (?Invoice $record) {
102
                                             return $record?->hasPayments();
100
                                             return $record?->hasPayments();
103
                                         })
101
                                         })
147
                                 Forms\Components\DatePicker::make('due_date')
145
                                 Forms\Components\DatePicker::make('due_date')
148
                                     ->label('Payment due')
146
                                     ->label('Payment due')
149
                                     ->default(function () use ($settings) {
147
                                     ->default(function () use ($settings) {
150
-                                        return now()->addDays($settings->payment_terms->getDays());
148
+                                        return company_today()->addDays($settings->payment_terms->getDays())->toDateString();
151
                                     })
149
                                     })
152
-                                    ->timezone(CompanySettingsService::getDefaultTimezone())
153
                                     ->minDate(static function (Forms\Get $get) {
150
                                     ->minDate(static function (Forms\Get $get) {
154
-                                        return Carbon::parse($get('date'))->toDateString() ?? now(CompanySettingsService::getDefaultTimezone())->toDateString();
151
+                                        return Carbon::parse($get('date'))->toDateString() ?? company_today()->toDateString();
155
                                     })
152
                                     })
156
                                     ->live()
153
                                     ->live()
157
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
154
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {

+ 16
- 0
app/Helpers/helpers.php View File

3
 use App\Enums\Accounting\AdjustmentComputation;
3
 use App\Enums\Accounting\AdjustmentComputation;
4
 use App\Enums\Setting\NumberFormat;
4
 use App\Enums\Setting\NumberFormat;
5
 use App\Models\Setting\Localization;
5
 use App\Models\Setting\Localization;
6
+use App\Services\CompanySettingsService;
6
 use Filament\Support\RawJs;
7
 use Filament\Support\RawJs;
8
+use Illuminate\Support\Carbon;
7
 
9
 
8
 if (! function_exists('generateJsCode')) {
10
 if (! function_exists('generateJsCode')) {
9
     function generateJsCode(string $precision, ?string $currency = null): string
11
     function generateJsCode(string $precision, ?string $currency = null): string
129
         return app()->environment('demo');
131
         return app()->environment('demo');
130
     }
132
     }
131
 }
133
 }
134
+
135
+if (! function_exists('company_today')) {
136
+    function company_today(): Carbon
137
+    {
138
+        return today(CompanySettingsService::getDefaultTimezone());
139
+    }
140
+}
141
+
142
+if (! function_exists('company_now')) {
143
+    function company_now(): Carbon
144
+    {
145
+        return now(CompanySettingsService::getDefaultTimezone());
146
+    }
147
+}

+ 26
- 6
app/Providers/MacroServiceProvider.php View File

357
             return $this;
357
             return $this;
358
         });
358
         });
359
 
359
 
360
+        // In your macro - simpler logic
360
         TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
361
         TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
361
             $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
362
             $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
362
                 if (blank($state)) {
363
                 if (blank($state)) {
363
                     return null;
364
                     return null;
364
                 }
365
                 }
365
 
366
 
366
-                $date = Carbon::parse($state)
367
-                    ->setTimezone($timezone ?? $column->getTimezone());
367
+                $timezone ??= $column->getTimezone() ?? CompanySettingsService::getDefaultTimezone();
368
+
369
+                // Use shiftTimezone to shift UTC calendar date to the specified timezone
370
+                // Using setTimezone would convert which is wrong for calendar dates
371
+                $date = Carbon::parse($state)->shiftTimezone($timezone);
368
 
372
 
369
                 if ($date->isToday()) {
373
                 if ($date->isToday()) {
370
                     return 'Today';
374
                     return 'Today';
375
+                } elseif ($date->isTomorrow()) {
376
+                    return 'Tomorrow';
377
+                } elseif ($date->isYesterday()) {
378
+                    return 'Yesterday';
371
                 }
379
                 }
372
 
380
 
373
                 return $date->diffForHumans([
381
                 return $date->diffForHumans([
374
                     'options' => CarbonInterface::ONE_DAY_WORDS,
382
                     'options' => CarbonInterface::ONE_DAY_WORDS,
383
+                    'skip' => ['month', 'week'], // Skip larger units, force days and years only
384
+                    'parts' => 2,
385
+                    'join' => ', ',
375
                 ]);
386
                 ]);
376
             });
387
             });
377
 
388
 
384
                     return null;
395
                     return null;
385
                 }
396
                 }
386
 
397
 
387
-                $date = Carbon::parse($state)
388
-                    ->setTimezone($timezone ?? $entry->getTimezone());
398
+                $timezone ??= $entry->getTimezone() ?? CompanySettingsService::getDefaultTimezone();
399
+
400
+                // Use shiftTimezone to shift UTC calendar date to the specified timezone
401
+                // Using setTimezone would convert which is wrong for calendar dates
402
+                $date = Carbon::parse($state)->shiftTimezone($timezone);
389
 
403
 
390
                 if ($date->isToday()) {
404
                 if ($date->isToday()) {
391
                     return 'Today';
405
                     return 'Today';
406
+                } elseif ($date->isTomorrow()) {
407
+                    return 'Tomorrow';
408
+                } elseif ($date->isYesterday()) {
409
+                    return 'Yesterday';
392
                 }
410
                 }
393
 
411
 
394
                 return $date->diffForHumans([
412
                 return $date->diffForHumans([
395
                     'options' => CarbonInterface::ONE_DAY_WORDS,
413
                     'options' => CarbonInterface::ONE_DAY_WORDS,
414
+                    'skip' => ['month', 'week'], // Skip larger units, force days and years only
415
+                    'parts' => 2,
416
+                    'join' => ', ',
396
                 ]);
417
                 ]);
397
             });
418
             });
398
 
419
 
473
         Carbon::macro('toDefaultDateFormat', function () {
494
         Carbon::macro('toDefaultDateFormat', function () {
474
             $companyId = auth()->user()?->current_company_id;
495
             $companyId = auth()->user()?->current_company_id;
475
             $dateFormat = CompanySettingsService::getDefaultDateFormat($companyId);
496
             $dateFormat = CompanySettingsService::getDefaultDateFormat($companyId);
476
-            $timezone = CompanySettingsService::getDefaultTimezone($companyId);
477
 
497
 
478
-            return $this->setTimezone($timezone)->format($dateFormat);
498
+            return $this->format($dateFormat);
479
         });
499
         });
480
 
500
 
481
         ExportColumn::macro('money', function () {
501
         ExportColumn::macro('money', function () {

Loading…
Cancel
Save