選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ListCompanyCurrencies.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. return $record->isEnabled() ? $tooltipMessage : null;
  40. })
  41. ->iconPosition(IconPosition::After)
  42. ->sortable()
  43. ->searchable(),
  44. Tables\Columns\TextColumn::make('name')
  45. ->localizeLabel()
  46. ->sortable()
  47. ->searchable(),
  48. Tables\Columns\TextColumn::make('rate')
  49. ->localizeLabel()
  50. ->sortable()
  51. ->searchable(),
  52. Tables\Columns\TextColumn::make('live_rate')
  53. ->localizeLabel()
  54. ->sortable()
  55. ->searchable(),
  56. ])
  57. ->filters([
  58. //
  59. ])
  60. ->actions([
  61. Tables\Actions\Action::make('update_rate')
  62. ->label('Update Rate')
  63. ->icon('heroicon-o-arrow-path')
  64. ->hidden(static fn (Currency $record): bool => $record->isEnabled() || ($record->rate === $record->live_rate))
  65. ->requiresConfirmation()
  66. ->action(static function (Currency $record): void {
  67. if (($record->rate !== $record->live_rate) && $record->isDisabled()) {
  68. $record->update([
  69. 'rate' => $record->live_rate,
  70. ]);
  71. Notification::make()
  72. ->success()
  73. ->title('Exchange Rate Updated')
  74. ->body(__('The exchange rate for :currency has been updated to reflect the current market rate.', [
  75. 'currency' => $record->name,
  76. ]))
  77. ->send();
  78. }
  79. }),
  80. ])
  81. ->bulkActions([
  82. Tables\Actions\BulkAction::make('update_rate')
  83. ->label('Update Rate')
  84. ->icon('heroicon-o-arrow-path')
  85. ->requiresConfirmation()
  86. ->deselectRecordsAfterCompletion()
  87. ->action(function (Collection $records): void {
  88. $updatedCurrencies = [];
  89. $records->each(function (Currency $record) use (&$updatedCurrencies): void {
  90. if (($record->rate !== $record->live_rate) && $record->isDisabled()) {
  91. $record->update([
  92. 'rate' => $record->live_rate,
  93. ]);
  94. $updatedCurrencies[] = $record->name;
  95. }
  96. });
  97. if (filled($updatedCurrencies)) {
  98. $currencyList = implode('<br>', array_map(static function ($currency) {
  99. return '&bull; ' . $currency;
  100. }, $updatedCurrencies));
  101. $message = __('The exchange rate for the following currencies has been updated to reflect the current market rate:') . '<br><br>';
  102. $message .= $currencyList;
  103. Notification::make()
  104. ->success()
  105. ->title('Exchange Rates Updated')
  106. ->body($message)
  107. ->send();
  108. }
  109. }),
  110. ])
  111. ->checkIfRecordIsSelectableUsing(static function (Currency $record): bool {
  112. return ($record->rate !== $record->live_rate) && $record->isDisabled();
  113. });
  114. }
  115. public function render(): View
  116. {
  117. return view('livewire.company.service.live-currency.list-company-currencies');
  118. }
  119. }