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.

AccountResource.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Actions\OptionAction\CreateCurrency;
  4. use App\Filament\Resources\AccountResource\Pages;
  5. use App\Filament\Resources\AccountResource\RelationManagers;
  6. use App\Models\Banking\Account;
  7. use App\Models\Setting\Currency;
  8. use App\Services\CurrencyService;
  9. use Closure;
  10. use Exception;
  11. use Filament\Forms;
  12. use Filament\Forms\Components\TextInput\Mask;
  13. use Filament\Notifications\Notification;
  14. use Filament\Resources\Form;
  15. use Filament\Resources\Resource;
  16. use Filament\Resources\Table;
  17. use Filament\Tables;
  18. use Illuminate\Support\Collection;
  19. use Illuminate\Support\Facades\Auth;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Validation\Rules\Unique;
  22. use Wallo\FilamentSelectify\Components\ToggleButton;
  23. class AccountResource extends Resource
  24. {
  25. protected static ?string $model = Account::class;
  26. protected static ?string $navigationIcon = 'heroicon-o-credit-card';
  27. protected static ?string $navigationGroup = 'Banking';
  28. public static function form(Form $form): Form
  29. {
  30. return $form
  31. ->schema([
  32. Forms\Components\Group::make()
  33. ->schema([
  34. Forms\Components\Section::make('Account Information')
  35. ->schema([
  36. Forms\Components\Select::make('type')
  37. ->label('Type')
  38. ->options(Account::getAccountTypes())
  39. ->searchable()
  40. ->default('checking')
  41. ->reactive()
  42. ->disablePlaceholderSelection()
  43. ->required(),
  44. Forms\Components\TextInput::make('name')
  45. ->label('Name')
  46. ->maxLength(100)
  47. ->required(),
  48. Forms\Components\TextInput::make('number')
  49. ->label('Account Number')
  50. ->unique(callback: static function (Unique $rule, $state) {
  51. $companyId = Auth::user()->currentCompany->id;
  52. return $rule->where('company_id', $companyId)->where('number', $state);
  53. }, ignoreRecord: true)
  54. ->maxLength(20)
  55. ->validationAttribute('account number')
  56. ->required(),
  57. ToggleButton::make('enabled')
  58. ->label('Default Account')
  59. ->hidden(static fn (Closure $get) => $get('type') === 'credit_card')
  60. ->offColor('danger')
  61. ->onColor('primary'),
  62. ])->columns(),
  63. Forms\Components\Section::make('Currency & Balance')
  64. ->schema([
  65. Forms\Components\Select::make('currency_code')
  66. ->label('Currency')
  67. ->relationship('currency', 'name')
  68. ->preload()
  69. ->default(Currency::getDefaultCurrency())
  70. ->searchable()
  71. ->reactive()
  72. ->required()
  73. ->createOptionForm([
  74. Forms\Components\Select::make('currency.code')
  75. ->label('Code')
  76. ->searchable()
  77. ->options(Currency::getCurrencyCodes())
  78. ->reactive()
  79. ->afterStateUpdated(static function (callable $set, $state) {
  80. if ($state === null) {
  81. return;
  82. }
  83. $code = $state;
  84. $currencyConfig = config("money.{$code}", []);
  85. $currencyService = app(CurrencyService::class);
  86. $defaultCurrency = Currency::getDefaultCurrency();
  87. $rate = 1;
  88. if ($defaultCurrency !== null) {
  89. $rate = $currencyService->getCachedExchangeRate($defaultCurrency, $code);
  90. }
  91. $set('currency.name', $currencyConfig['name'] ?? '');
  92. $set('currency.rate', $rate);
  93. })
  94. ->required(),
  95. Forms\Components\TextInput::make('currency.name')
  96. ->label('Name')
  97. ->maxLength(100)
  98. ->required(),
  99. Forms\Components\TextInput::make('currency.rate')
  100. ->label('Rate')
  101. ->numeric()
  102. ->required(),
  103. ])->createOptionAction(static function (Forms\Components\Actions\Action $action) {
  104. return $action
  105. ->label('Add Currency')
  106. ->modalHeading('Add Currency')
  107. ->modalButton('Add')
  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. ->default('0')
  121. ->numeric()
  122. ->mask(static fn (Forms\Components\TextInput\Mask $mask, Closure $get) => $mask
  123. ->patternBlocks([
  124. 'money' => static fn (Mask $mask) => $mask
  125. ->numeric()
  126. ->decimalPlaces(config('money.' . $get('currency_code') . '.precision'))
  127. ->decimalSeparator(config('money.' . $get('currency_code') . '.decimal_mark'))
  128. ->thousandsSeparator(config('money.' . $get('currency_code') . '.thousands_separator'))
  129. ->signed()
  130. ->padFractionalZeros()
  131. ->normalizeZeros(),
  132. ])
  133. ->pattern(config('money.' . $get('currency_code') . '.symbol_first') ? config('money.' . $get('currency_code') . '.symbol') . 'money' : 'money' . config('money.' . $get('currency_code') . '.symbol'))
  134. ->lazyPlaceholder(false)),
  135. ])->columns(),
  136. Forms\Components\Tabs::make('Account Specifications')
  137. ->tabs([
  138. Forms\Components\Tabs\Tab::make('Bank Information')
  139. ->icon('heroicon-o-credit-card')
  140. ->schema([
  141. Forms\Components\TextInput::make('bank_name')
  142. ->label('Bank Name')
  143. ->maxLength(100),
  144. Forms\Components\TextInput::make('bank_phone')
  145. ->label('Bank Phone')
  146. ->tel()
  147. ->maxLength(20),
  148. Forms\Components\Textarea::make('bank_address')
  149. ->label('Bank Address')
  150. ->columnSpanFull(),
  151. ])->columns(),
  152. Forms\Components\Tabs\Tab::make('Additional Information')
  153. ->icon('heroicon-o-information-circle')
  154. ->schema([
  155. Forms\Components\TextInput::make('description')
  156. ->label('Description')
  157. ->maxLength(100),
  158. Forms\Components\SpatieTagsInput::make('tags')
  159. ->label('Tags')
  160. ->placeholder('Enter tags...')
  161. ->type('statuses')
  162. ->suggestions([
  163. 'Business',
  164. 'Personal',
  165. 'College Fund',
  166. ]),
  167. Forms\Components\MarkdownEditor::make('notes')
  168. ->label('Notes')
  169. ->columnSpanFull(),
  170. ])->columns(),
  171. ]),
  172. ])->columnSpan(['lg' => 2]),
  173. Forms\Components\Group::make()
  174. ->schema([
  175. Forms\Components\Section::make('Routing Information')
  176. ->schema([
  177. Forms\Components\TextInput::make('aba_routing_number')
  178. ->label('ABA Number')
  179. ->integer()
  180. ->length(9),
  181. Forms\Components\TextInput::make('ach_routing_number')
  182. ->label('ACH Number')
  183. ->integer()
  184. ->length(9),
  185. ]),
  186. Forms\Components\Section::make('International Bank Information')
  187. ->schema([
  188. Forms\Components\TextInput::make('bic_swift_code')
  189. ->label('BIC/SWIFT Code')
  190. ->maxLength(11),
  191. Forms\Components\TextInput::make('iban')
  192. ->label('IBAN')
  193. ->maxLength(34),
  194. ]),
  195. ])->columnSpan(['lg' => 1]),
  196. ])->columns(3);
  197. }
  198. /**
  199. * @throws Exception
  200. */
  201. public static function table(Table $table): Table
  202. {
  203. return $table
  204. ->columns([
  205. Tables\Columns\TextColumn::make('name')
  206. ->label('Account')
  207. ->searchable()
  208. ->weight('semibold')
  209. ->icon(static fn (Account $record) => $record->enabled ? 'heroicon-o-lock-closed' : null)
  210. ->tooltip(static fn (Account $record) => $record->enabled ? 'Default Account' : null)
  211. ->iconPosition('after')
  212. ->description(static fn (Account $record) => $record->number ?: 'N/A')
  213. ->sortable(),
  214. Tables\Columns\TextColumn::make('bank_name')
  215. ->label('Bank')
  216. ->placeholder('N/A')
  217. ->description(static fn (Account $record) => $record->bank_phone ?: 'N/A')
  218. ->searchable()
  219. ->sortable(),
  220. Tables\Columns\BadgeColumn::make('status')
  221. ->label('Status')
  222. ->colors([
  223. 'primary' => 'open',
  224. 'success' => 'active',
  225. 'secondary' => 'dormant',
  226. 'warning' => 'restricted',
  227. 'danger' => 'closed',
  228. ])
  229. ->icons([
  230. 'heroicon-o-cash' => 'open',
  231. 'heroicon-o-clock' => 'active',
  232. 'heroicon-o-status-offline' => 'dormant',
  233. 'heroicon-o-exclamation' => 'restricted',
  234. 'heroicon-o-x-circle' => 'closed',
  235. ])
  236. ->sortable(),
  237. Tables\Columns\TextColumn::make('opening_balance')
  238. ->label('Current Balance')
  239. ->sortable()
  240. ->money(static fn ($record) => $record->currency_code, true),
  241. ])
  242. ->filters([
  243. //
  244. ])
  245. ->actions([
  246. Tables\Actions\EditAction::make(),
  247. Tables\Actions\DeleteAction::make()
  248. ->before(static function (Tables\Actions\DeleteAction $action, Account $record) {
  249. if ($record->enabled) {
  250. Notification::make()
  251. ->danger()
  252. ->title('Action Denied')
  253. ->body(__('The :name account is currently set as your default account and cannot be deleted. Please set a different account as your default before attempting to delete this one.', ['name' => $record->name]))
  254. ->persistent()
  255. ->send();
  256. $action->cancel();
  257. }
  258. }),
  259. ])
  260. ->bulkActions([
  261. Tables\Actions\DeleteBulkAction::make()
  262. ->before(static function (Tables\Actions\DeleteBulkAction $action, Collection $records) {
  263. foreach ($records as $record) {
  264. if ($record->enabled) {
  265. Notification::make()
  266. ->danger()
  267. ->title('Action Denied')
  268. ->body(__('The :name account is currently set as your default account and cannot be deleted. Please set a different account as your default before attempting to delete this one.', ['name' => $record->name]))
  269. ->persistent()
  270. ->send();
  271. $action->cancel();
  272. }
  273. }
  274. }),
  275. ]);
  276. }
  277. public static function getRelations(): array
  278. {
  279. return [
  280. //
  281. ];
  282. }
  283. public static function getPages(): array
  284. {
  285. return [
  286. 'index' => Pages\ListAccounts::route('/'),
  287. 'create' => Pages\CreateAccount::route('/create'),
  288. 'edit' => Pages\EditAccount::route('/{record}/edit'),
  289. ];
  290. }
  291. }