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