You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListCompanyCurrencies.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Livewire\Company\Service\LiveCurrency;
  3. use App\Models\Setting\Currency;
  4. use Filament\Forms\Concerns\InteractsWithForms;
  5. use Filament\Forms\Contracts\HasForms;
  6. use Filament\Notifications\Notification;
  7. use Filament\Support\Enums\FontWeight;
  8. use Filament\Support\Enums\IconPosition;
  9. use Filament\Tables;
  10. use Filament\Tables\Concerns\InteractsWithTable;
  11. use Filament\Tables\Contracts\HasTable;
  12. use Filament\Tables\Table;
  13. use Illuminate\Contracts\View\View;
  14. use Illuminate\Database\Eloquent\Collection;
  15. use Livewire\Component;
  16. class ListCompanyCurrencies extends Component implements HasForms, HasTable
  17. {
  18. use InteractsWithForms;
  19. use InteractsWithTable;
  20. protected static ?string $tableModelLabel = 'Currency';
  21. public function getTableModelLabel(): ?string
  22. {
  23. return static::$tableModelLabel;
  24. }
  25. public function table(Table $table): Table
  26. {
  27. return $table
  28. ->query(Currency::query())
  29. ->modelLabel($this->getTableModelLabel())
  30. ->columns([
  31. Tables\Columns\TextColumn::make('code')
  32. ->localizeLabel()
  33. ->weight(FontWeight::Medium)
  34. ->icon(static fn (Currency $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  35. ->tooltip(function (Currency $record) {
  36. $tooltipMessage = translate('Default :Record', [
  37. 'Record' => $this->getTableModelLabel(),
  38. ]);
  39. if ($record->isEnabled()) {
  40. return $tooltipMessage;
  41. }
  42. return null;
  43. })
  44. ->iconPosition(IconPosition::After)
  45. ->sortable()
  46. ->searchable(),
  47. Tables\Columns\TextColumn::make('name')
  48. ->localizeLabel()
  49. ->sortable()
  50. ->searchable(),
  51. Tables\Columns\TextColumn::make('rate')
  52. ->localizeLabel()
  53. ->sortable()
  54. ->searchable(),
  55. Tables\Columns\TextColumn::make('live_rate')
  56. ->localizeLabel()
  57. ->sortable()
  58. ->searchable(),
  59. ])
  60. ->filters([
  61. //
  62. ])
  63. ->actions([
  64. Tables\Actions\Action::make('update_rate')
  65. ->label('Update Rate')
  66. ->icon('heroicon-o-arrow-path')
  67. ->hidden(static fn (Currency $record): bool => $record->isEnabled() || ($record->rate === $record->live_rate))
  68. ->requiresConfirmation()
  69. ->action(static function (Currency $record): void {
  70. if (($record->rate !== $record->live_rate) && $record->isDisabled()) {
  71. $record->update([
  72. 'rate' => $record->live_rate,
  73. ]);
  74. Notification::make()
  75. ->success()
  76. ->title('Exchange Rate Updated')
  77. ->body(__('The exchange rate for :currency has been updated to reflect the current market rate.', [
  78. 'currency' => $record->name,
  79. ]))
  80. ->send();
  81. }
  82. }),
  83. ])
  84. ->bulkActions([
  85. Tables\Actions\BulkAction::make('update_rate')
  86. ->label('Update Rate')
  87. ->icon('heroicon-o-arrow-path')
  88. ->requiresConfirmation()
  89. ->deselectRecordsAfterCompletion()
  90. ->action(function (Collection $records): void {
  91. $updatedCurrencies = [];
  92. $records->each(function (Currency $record) use (&$updatedCurrencies): void {
  93. if (($record->rate !== $record->live_rate) && $record->isDisabled()) {
  94. $record->update([
  95. 'rate' => $record->live_rate,
  96. ]);
  97. $updatedCurrencies[] = $record->name;
  98. }
  99. });
  100. if (filled($updatedCurrencies)) {
  101. $currencyList = implode('<br>', array_map(static function ($currency) {
  102. return '&bull; ' . $currency;
  103. }, $updatedCurrencies));
  104. $message = __('The exchange rate for the following currencies has been updated to reflect the current market rate:') . '<br><br>';
  105. $message .= $currencyList;
  106. Notification::make()
  107. ->success()
  108. ->title('Exchange Rates Updated')
  109. ->body($message)
  110. ->send();
  111. }
  112. }),
  113. ])
  114. ->checkIfRecordIsSelectableUsing(static function (Currency $record): bool {
  115. return ($record->rate !== $record->live_rate) && $record->isDisabled();
  116. });
  117. }
  118. public function render(): View
  119. {
  120. return view('livewire.company.service.live-currency.list-company-currencies');
  121. }
  122. }