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.

CurrencyResource.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Resources;
  3. use App\Concerns\ChecksForeignKeyConstraints;
  4. use App\Concerns\NotifiesOnDelete;
  5. use App\Facades\Forex;
  6. use App\Filament\Company\Clusters\Settings;
  7. use App\Filament\Company\Clusters\Settings\Resources\CurrencyResource\Pages;
  8. use App\Models\Accounting\Account;
  9. use App\Models\Setting\Currency;
  10. use App\Models\Setting\Currency as CurrencyModel;
  11. use App\Utilities\Currency\CurrencyAccessor;
  12. use Closure;
  13. use Filament\Forms;
  14. use Filament\Forms\Form;
  15. use Filament\Resources\Resource;
  16. use Filament\Support\Enums\FontWeight;
  17. use Filament\Tables;
  18. use Filament\Tables\Table;
  19. use Illuminate\Database\Eloquent\Collection;
  20. class CurrencyResource extends Resource
  21. {
  22. use ChecksForeignKeyConstraints;
  23. use NotifiesOnDelete;
  24. protected static ?string $model = CurrencyModel::class;
  25. protected static ?string $modelLabel = 'Currency';
  26. protected static ?string $cluster = Settings::class;
  27. public static function getModelLabel(): string
  28. {
  29. $modelLabel = static::$modelLabel;
  30. return translate($modelLabel);
  31. }
  32. public static function form(Form $form): Form
  33. {
  34. return $form
  35. ->schema([
  36. Forms\Components\Section::make('General')
  37. ->schema([
  38. Forms\Components\Select::make('code')
  39. ->options(CurrencyAccessor::getAvailableCurrencies())
  40. ->searchable()
  41. ->live()
  42. ->required()
  43. ->localizeLabel()
  44. ->afterStateUpdated(static function (Forms\Set $set, $state) {
  45. $fields = ['name', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator'];
  46. if ($state === null) {
  47. array_walk($fields, static fn ($field) => $set($field, null));
  48. return;
  49. }
  50. $currencyDetails = CurrencyAccessor::getAllCurrencies()[$state] ?? [];
  51. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  52. $exchangeRate = Forex::getCachedExchangeRate($defaultCurrencyCode, $state);
  53. if ($exchangeRate !== null) {
  54. $set('rate', $exchangeRate);
  55. }
  56. array_walk($fields, static fn ($field) => $set($field, $currencyDetails[$field] ?? null));
  57. }),
  58. Forms\Components\TextInput::make('name')
  59. ->localizeLabel()
  60. ->maxLength(50)
  61. ->required(),
  62. Forms\Components\TextInput::make('rate')
  63. ->numeric()
  64. ->rule('gt:0')
  65. ->live()
  66. ->localizeLabel()
  67. ->required(),
  68. Forms\Components\Select::make('precision')
  69. ->localizeLabel()
  70. ->options(['0', '1', '2', '3', '4'])
  71. ->required(),
  72. Forms\Components\TextInput::make('symbol')
  73. ->localizeLabel()
  74. ->maxLength(5)
  75. ->required(),
  76. Forms\Components\Select::make('symbol_first')
  77. ->localizeLabel('Symbol Position')
  78. ->boolean(translate('Before Amount'), translate('After Amount'), translate('Select a symbol position'))
  79. ->required(),
  80. Forms\Components\TextInput::make('decimal_mark')
  81. ->localizeLabel('Decimal Separator')
  82. ->maxLength(1)
  83. ->rule(static function (Forms\Get $get): Closure {
  84. return static function ($attribute, $value, Closure $fail) use ($get) {
  85. if ($value === $get('thousands_separator')) {
  86. $fail(translate('Separators must be unique.'));
  87. }
  88. };
  89. })
  90. ->required(),
  91. Forms\Components\TextInput::make('thousands_separator')
  92. ->localizeLabel()
  93. ->maxLength(1)
  94. ->rule(static function (Forms\Get $get): Closure {
  95. return static function ($attribute, $value, Closure $fail) use ($get) {
  96. if ($value === $get('decimal_mark')) {
  97. $fail(translate('Separators must be unique.'));
  98. }
  99. };
  100. })
  101. ->nullable(),
  102. ])->columns(),
  103. ]);
  104. }
  105. public static function table(Table $table): Table
  106. {
  107. return $table
  108. ->columns([
  109. Tables\Columns\TextColumn::make('name')
  110. ->localizeLabel()
  111. ->weight(FontWeight::Medium)
  112. ->icon(static fn (CurrencyModel $record) => $record->isEnabled() ? 'heroicon-o-lock-closed' : null)
  113. ->tooltip(static function (CurrencyModel $record) {
  114. $tooltipMessage = translate('Default :Record', [
  115. 'Record' => static::getModelLabel(),
  116. ]);
  117. return $record->isEnabled() ? $tooltipMessage : null;
  118. })
  119. ->iconPosition('after')
  120. ->searchable()
  121. ->sortable(),
  122. Tables\Columns\TextColumn::make('code')
  123. ->localizeLabel()
  124. ->searchable()
  125. ->sortable(),
  126. Tables\Columns\TextColumn::make('symbol')
  127. ->localizeLabel()
  128. ->searchable()
  129. ->sortable(),
  130. Tables\Columns\TextColumn::make('rate')
  131. ->localizeLabel()
  132. ->searchable()
  133. ->sortable(),
  134. ])
  135. ->filters([
  136. //
  137. ])
  138. ->actions([
  139. Tables\Actions\EditAction::make(),
  140. Tables\Actions\DeleteAction::make()
  141. ->before(function (Tables\Actions\DeleteAction $action, Currency $record) {
  142. $modelsToCheck = [
  143. Account::class,
  144. ];
  145. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  146. if ($isUsed) {
  147. $reason = 'in use';
  148. self::notifyBeforeDelete($record, $reason);
  149. $action->cancel();
  150. }
  151. }),
  152. ])
  153. ->bulkActions([
  154. Tables\Actions\BulkActionGroup::make([
  155. Tables\Actions\DeleteBulkAction::make()
  156. ->before(static function (Tables\Actions\DeleteBulkAction $action, Collection $records) {
  157. foreach ($records as $record) {
  158. $modelsToCheck = [
  159. Account::class,
  160. ];
  161. $isUsed = self::isForeignKeyUsed('currency_code', $record->code, $modelsToCheck);
  162. if ($isUsed) {
  163. $reason = 'in use';
  164. self::notifyBeforeDelete($record, $reason);
  165. $action->cancel();
  166. }
  167. }
  168. })
  169. ->hidden(function (Table $table) {
  170. return $table->getAllSelectableRecordsCount() === 0;
  171. }),
  172. ]),
  173. ])
  174. ->checkIfRecordIsSelectableUsing(static function (CurrencyModel $record) {
  175. return $record->isDisabled();
  176. });
  177. }
  178. public static function getPages(): array
  179. {
  180. return [
  181. 'index' => Pages\ListCurrencies::route('/'),
  182. 'create' => Pages\CreateCurrency::route('/create'),
  183. 'edit' => Pages\EditCurrency::route('/{record}/edit'),
  184. ];
  185. }
  186. }