Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AccountResource.php 17KB

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