您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AccountResource.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace App\Filament\Company\Resources\Banking;
  3. use App\Actions\OptionAction\CreateCurrency;
  4. use App\Filament\Company\Resources\Banking\AccountResource\Pages;
  5. use App\Models\Banking\Account;
  6. use App\Models\Setting\Currency;
  7. use App\Services\CurrencyService;
  8. use App\Utilities\CurrencyConverter;
  9. use Filament\Forms\Form;
  10. use Filament\Notifications\Notification;
  11. use Filament\Resources\Resource;
  12. use Filament\Tables\Table;
  13. use Filament\{Forms, Tables};
  14. use Illuminate\Support\Facades\{Auth, DB};
  15. use Illuminate\Validation\Rules\Unique;
  16. use Wallo\FilamentSelectify\Components\ToggleButton;
  17. class AccountResource extends Resource
  18. {
  19. protected static ?string $model = Account::class;
  20. protected static ?string $navigationIcon = 'heroicon-o-credit-card';
  21. protected static ?string $navigationGroup = 'Banking';
  22. public static function form(Form $form): Form
  23. {
  24. return $form
  25. ->schema([
  26. Forms\Components\Group::make()
  27. ->schema([
  28. Forms\Components\Section::make('Account Information')
  29. ->schema([
  30. Forms\Components\Select::make('type')
  31. ->label('Type')
  32. ->options(Account::getAccountTypes())
  33. ->searchable()
  34. ->default('checking')
  35. ->live()
  36. ->required(),
  37. Forms\Components\TextInput::make('name')
  38. ->label('Name')
  39. ->maxLength(100)
  40. ->required(),
  41. Forms\Components\TextInput::make('number')
  42. ->label('Account Number')
  43. ->unique(ignoreRecord: true, modifyRuleUsing: static function (Unique $rule, $state) {
  44. $companyId = Auth::user()->currentCompany->id;
  45. return $rule->where('company_id', $companyId)->where('number', $state);
  46. })
  47. ->maxLength(20)
  48. ->validationAttribute('account number')
  49. ->required(),
  50. ToggleButton::make('enabled')
  51. ->label('Default Account')
  52. ->hidden(static fn (Forms\Get $get) => $get('type') === 'credit_card')
  53. ->offColor('danger')
  54. ->onColor('primary'),
  55. ])->columns(),
  56. Forms\Components\Section::make('Currency & Balance')
  57. ->schema([
  58. Forms\Components\Select::make('currency_code')
  59. ->label('Currency')
  60. ->relationship('currency', 'name')
  61. ->default(Currency::getDefaultCurrencyCode())
  62. ->saveRelationshipsUsing(null)
  63. ->preload()
  64. ->searchable()
  65. ->live()
  66. ->afterStateUpdated(static function (Forms\Set $set, $state, $old, Forms\Get $get) {
  67. $opening_balance = CurrencyConverter::convertAndSet($state, $old, $get('opening_balance'));
  68. if ($opening_balance !== null) {
  69. $set('opening_balance', $opening_balance);
  70. }
  71. })
  72. ->required()
  73. ->createOptionForm([
  74. Forms\Components\Select::make('currency.code')
  75. ->label('Code')
  76. ->searchable()
  77. ->options(Currency::getAvailableCurrencyCodes())
  78. ->live()
  79. ->afterStateUpdated(static function (callable $set, $state) {
  80. if ($state === null) {
  81. return;
  82. }
  83. $currency_code = currency($state);
  84. $currencyService = app(CurrencyService::class);
  85. $defaultCurrencyCode = Currency::getDefaultCurrencyCode();
  86. $rate = 1;
  87. if ($defaultCurrencyCode !== null) {
  88. $rate = $currencyService->getCachedExchangeRate($defaultCurrencyCode, $state);
  89. }
  90. $set('currency.name', $currency_code->getName() ?? '');
  91. $set('currency.rate', $rate);
  92. })
  93. ->required(),
  94. Forms\Components\TextInput::make('currency.name')
  95. ->label('Name')
  96. ->maxLength(100)
  97. ->required(),
  98. Forms\Components\TextInput::make('currency.rate')
  99. ->label('Rate')
  100. ->numeric()
  101. ->required(),
  102. ])->createOptionAction(static function (Forms\Components\Actions\Action $action) {
  103. return $action
  104. ->label('Add Currency')
  105. ->modalHeading('Add Currency')
  106. ->modalSubmitActionLabel('Add')
  107. ->slideOver()
  108. ->action(static function (array $data) {
  109. return DB::transaction(static function () use ($data) {
  110. $code = $data['currency']['code'];
  111. $name = $data['currency']['name'];
  112. $rate = $data['currency']['rate'];
  113. return (new CreateCurrency())->create($code, $name, $rate);
  114. });
  115. });
  116. }),
  117. Forms\Components\TextInput::make('opening_balance')
  118. ->label('Opening Balance')
  119. ->required()
  120. ->currency(static fn (Forms\Get $get) => $get('currency_code')),
  121. ])->columns(),
  122. Forms\Components\Tabs::make('Account Specifications')
  123. ->tabs([
  124. Forms\Components\Tabs\Tab::make('Bank Information')
  125. ->icon('heroicon-o-credit-card')
  126. ->schema([
  127. Forms\Components\TextInput::make('bank_name')
  128. ->label('Bank Name')
  129. ->maxLength(100),
  130. Forms\Components\TextInput::make('bank_phone')
  131. ->label('Bank Phone')
  132. ->tel()
  133. ->maxLength(20),
  134. Forms\Components\Textarea::make('bank_address')
  135. ->label('Bank Address')
  136. ->columnSpanFull(),
  137. ])->columns(),
  138. Forms\Components\Tabs\Tab::make('Additional Information')
  139. ->icon('heroicon-o-information-circle')
  140. ->schema([
  141. Forms\Components\TextInput::make('description')
  142. ->label('Description')
  143. ->maxLength(100),
  144. Forms\Components\SpatieTagsInput::make('tags')
  145. ->label('Tags')
  146. ->placeholder('Enter tags...')
  147. ->type('statuses')
  148. ->suggestions([
  149. 'Business',
  150. 'Personal',
  151. 'College Fund',
  152. ]),
  153. Forms\Components\MarkdownEditor::make('notes')
  154. ->label('Notes')
  155. ->columnSpanFull(),
  156. ])->columns(),
  157. ]),
  158. ])->columnSpan(['lg' => 2]),
  159. Forms\Components\Group::make()
  160. ->schema([
  161. Forms\Components\Section::make('Routing Information')
  162. ->schema([
  163. Forms\Components\TextInput::make('aba_routing_number')
  164. ->label('ABA Number')
  165. ->integer()
  166. ->length(9),
  167. Forms\Components\TextInput::make('ach_routing_number')
  168. ->label('ACH Number')
  169. ->integer()
  170. ->length(9),
  171. ]),
  172. Forms\Components\Section::make('International Bank Information')
  173. ->schema([
  174. Forms\Components\TextInput::make('bic_swift_code')
  175. ->label('BIC/SWIFT Code')
  176. ->maxLength(11),
  177. Forms\Components\TextInput::make('iban')
  178. ->label('IBAN')
  179. ->maxLength(34),
  180. ]),
  181. ])->columnSpan(['lg' => 1]),
  182. ])->columns(3);
  183. }
  184. public static function table(Table $table): Table
  185. {
  186. return $table
  187. ->columns([
  188. Tables\Columns\TextColumn::make('name')
  189. ->label('Account')
  190. ->searchable()
  191. ->weight('semibold')
  192. ->icon(static fn (Account $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  193. ->tooltip(static fn (Account $record) => $record->enabled ? 'Default Account' : null)
  194. ->iconPosition('after')
  195. ->description(static fn (Account $record) => $record->number ?: 'N/A')
  196. ->sortable(),
  197. Tables\Columns\TextColumn::make('bank_name')
  198. ->label('Bank')
  199. ->placeholder('N/A')
  200. ->description(static fn (Account $record) => $record->bank_phone ?: 'N/A')
  201. ->searchable()
  202. ->sortable(),
  203. Tables\Columns\TextColumn::make('status')
  204. ->badge()
  205. ->label('Status')
  206. ->colors([
  207. 'primary' => 'open',
  208. 'success' => 'active',
  209. 'secondary' => 'dormant',
  210. 'warning' => 'restricted',
  211. 'danger' => 'closed',
  212. ])
  213. ->icons([
  214. 'heroicon-o-currency-dollar' => 'open',
  215. 'heroicon-o-clock' => 'active',
  216. 'heroicon-o-status-offline' => 'dormant',
  217. 'heroicon-o-exclamation' => 'restricted',
  218. 'heroicon-o-x-circle' => 'closed',
  219. ])
  220. ->sortable(),
  221. Tables\Columns\TextColumn::make('opening_balance')
  222. ->label('Current Balance')
  223. ->sortable()
  224. ->currency(static fn (Account $record) => $record->currency_code, true),
  225. ])
  226. ->filters([
  227. //
  228. ])
  229. ->actions([
  230. Tables\Actions\EditAction::make(),
  231. Tables\Actions\Action::make('update_balance')
  232. ->hidden(static fn (Account $record) => $record->currency_code === Currency::getDefaultCurrencyCode())
  233. ->label('Update Balance')
  234. ->icon('heroicon-o-currency-dollar')
  235. ->requiresConfirmation()
  236. ->modalDescription('Are you sure you want to update the balance with the latest exchange rate?')
  237. ->before(static function (Tables\Actions\Action $action, Account $record) {
  238. if ($record->currency_code !== Currency::getDefaultCurrencyCode()) {
  239. $currencyService = app(CurrencyService::class);
  240. $defaultCurrency = Currency::getDefaultCurrencyCode();
  241. $cachedExchangeRate = $currencyService->getCachedExchangeRate($defaultCurrency, $record->currency_code);
  242. $oldExchangeRate = $record->currency->rate;
  243. if ($cachedExchangeRate === $oldExchangeRate) {
  244. Notification::make()
  245. ->warning()
  246. ->title('Balance Already Up to Date')
  247. ->body(__('The :name account balance is already up to date.', ['name' => $record->name]))
  248. ->persistent()
  249. ->send();
  250. $action->cancel();
  251. }
  252. }
  253. })
  254. ->action(static function (Account $record) {
  255. if ($record->currency_code !== Currency::getDefaultCurrencyCode()) {
  256. $currencyService = app(CurrencyService::class);
  257. $defaultCurrency = Currency::getDefaultCurrencyCode();
  258. $cachedExchangeRate = $currencyService->getCachedExchangeRate($defaultCurrency, $record->currency_code);
  259. $oldExchangeRate = $record->currency->rate;
  260. if ($cachedExchangeRate !== $oldExchangeRate) {
  261. $scale = 10 ** $record->currency->precision;
  262. $cleanedBalance = (int) filter_var($record->opening_balance, FILTER_SANITIZE_NUMBER_INT);
  263. $newBalance = ($cachedExchangeRate / $oldExchangeRate) * $cleanedBalance;
  264. $newBalanceInt = (int) round($newBalance, $scale);
  265. $record->opening_balance = money($newBalanceInt, $record->currency_code)->getValue();
  266. $record->currency->rate = $cachedExchangeRate;
  267. $record->currency->save();
  268. $record->save();
  269. }
  270. Notification::make()
  271. ->success()
  272. ->title('Balance Updated Successfully')
  273. ->body(__('The :name account balance has been updated to reflect the current exchange rate.', ['name' => $record->name]))
  274. ->send();
  275. }
  276. }),
  277. ])
  278. ->bulkActions([
  279. Tables\Actions\BulkActionGroup::make([
  280. Tables\Actions\DeleteBulkAction::make(),
  281. ]),
  282. ])
  283. ->emptyStateActions([
  284. Tables\Actions\CreateAction::make(),
  285. ]);
  286. }
  287. public static function getRelations(): array
  288. {
  289. return [
  290. //
  291. ];
  292. }
  293. public static function getPages(): array
  294. {
  295. return [
  296. 'index' => Pages\ListAccounts::route('/'),
  297. 'create' => Pages\CreateAccount::route('/create'),
  298. 'edit' => Pages\EditAccount::route('/{record}/edit'),
  299. ];
  300. }
  301. }