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

AccountResource.php 16KB

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