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

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