Andrew Wallo 3 months ago
parent
commit
7956ff2ad1

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

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

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

@@ -29,7 +29,6 @@ use App\Models\Accounting\DocumentLineItem;
29 29
 use App\Models\Accounting\Estimate;
30 30
 use App\Models\Common\Client;
31 31
 use App\Models\Common\Offering;
32
-use App\Services\CompanySettingsService;
33 32
 use App\Utilities\Currency\CurrencyAccessor;
34 33
 use App\Utilities\Currency\CurrencyConverter;
35 34
 use App\Utilities\RateCalculator;
@@ -92,8 +91,7 @@ class EstimateResource extends Resource
92 91
                                     Forms\Components\DatePicker::make('date')
93 92
                                         ->label('Estimate date')
94 93
                                         ->live()
95
-                                        ->default(now())
96
-                                        ->timezone(CompanySettingsService::getDefaultTimezone())
94
+                                        ->default(company_today()->toDateString())
97 95
                                         ->columnSpan(2)
98 96
                                         ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
99 97
                                             $date = Carbon::parse($state)->toDateString();
@@ -139,11 +137,10 @@ class EstimateResource extends Resource
139 137
                                 Forms\Components\DatePicker::make('expiration_date')
140 138
                                     ->label('Expiration date')
141 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 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 145
                                     ->live()
149 146
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {

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

@@ -29,7 +29,6 @@ use App\Models\Accounting\DocumentLineItem;
29 29
 use App\Models\Accounting\Invoice;
30 30
 use App\Models\Common\Client;
31 31
 use App\Models\Common\Offering;
32
-use App\Services\CompanySettingsService;
33 32
 use App\Utilities\Currency\CurrencyAccessor;
34 33
 use App\Utilities\Currency\CurrencyConverter;
35 34
 use App\Utilities\RateCalculator;
@@ -96,8 +95,7 @@ class InvoiceResource extends Resource
96 95
                                     Forms\Components\DatePicker::make('date')
97 96
                                         ->label('Invoice date')
98 97
                                         ->live()
99
-                                        ->default(now())
100
-                                        ->timezone(CompanySettingsService::getDefaultTimezone())
98
+                                        ->default(company_today()->toDateString())
101 99
                                         ->disabled(function (?Invoice $record) {
102 100
                                             return $record?->hasPayments();
103 101
                                         })
@@ -147,11 +145,10 @@ class InvoiceResource extends Resource
147 145
                                 Forms\Components\DatePicker::make('due_date')
148 146
                                     ->label('Payment due')
149 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 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 153
                                     ->live()
157 154
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {

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

@@ -3,7 +3,9 @@
3 3
 use App\Enums\Accounting\AdjustmentComputation;
4 4
 use App\Enums\Setting\NumberFormat;
5 5
 use App\Models\Setting\Localization;
6
+use App\Services\CompanySettingsService;
6 7
 use Filament\Support\RawJs;
8
+use Illuminate\Support\Carbon;
7 9
 
8 10
 if (! function_exists('generateJsCode')) {
9 11
     function generateJsCode(string $precision, ?string $currency = null): string
@@ -129,3 +131,17 @@ if (! function_exists('is_demo_environment')) {
129 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,21 +357,32 @@ class MacroServiceProvider extends ServiceProvider
357 357
             return $this;
358 358
         });
359 359
 
360
+        // In your macro - simpler logic
360 361
         TextColumn::macro('asRelativeDay', function (?string $timezone = null): static {
361 362
             $this->formatStateUsing(function (TextColumn $column, mixed $state) use ($timezone) {
362 363
                 if (blank($state)) {
363 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 373
                 if ($date->isToday()) {
370 374
                     return 'Today';
375
+                } elseif ($date->isTomorrow()) {
376
+                    return 'Tomorrow';
377
+                } elseif ($date->isYesterday()) {
378
+                    return 'Yesterday';
371 379
                 }
372 380
 
373 381
                 return $date->diffForHumans([
374 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,15 +395,25 @@ class MacroServiceProvider extends ServiceProvider
384 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 404
                 if ($date->isToday()) {
391 405
                     return 'Today';
406
+                } elseif ($date->isTomorrow()) {
407
+                    return 'Tomorrow';
408
+                } elseif ($date->isYesterday()) {
409
+                    return 'Yesterday';
392 410
                 }
393 411
 
394 412
                 return $date->diffForHumans([
395 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,9 +494,8 @@ class MacroServiceProvider extends ServiceProvider
473 494
         Carbon::macro('toDefaultDateFormat', function () {
474 495
             $companyId = auth()->user()?->current_company_id;
475 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 501
         ExportColumn::macro('money', function () {

Loading…
Cancel
Save