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.

PaymentsRelationManager.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Filament\Company\Resources\Purchases\BillResource\RelationManagers;
  3. use App\Enums\Accounting\PaymentMethod;
  4. use App\Enums\Accounting\TransactionType;
  5. use App\Filament\Company\Resources\Purchases\BillResource\Pages\ViewBill;
  6. use App\Models\Accounting\Bill;
  7. use App\Models\Accounting\Transaction;
  8. use App\Models\Banking\BankAccount;
  9. use App\Utilities\Currency\CurrencyAccessor;
  10. use App\Utilities\Currency\CurrencyConverter;
  11. use Closure;
  12. use Filament\Forms;
  13. use Filament\Forms\Form;
  14. use Filament\Resources\RelationManagers\RelationManager;
  15. use Filament\Support\Colors\Color;
  16. use Filament\Support\Enums\FontWeight;
  17. use Filament\Support\Enums\MaxWidth;
  18. use Filament\Tables;
  19. use Filament\Tables\Table;
  20. use Illuminate\Database\Eloquent\Model;
  21. class PaymentsRelationManager extends RelationManager
  22. {
  23. protected static string $relationship = 'payments';
  24. protected static ?string $modelLabel = 'Payment';
  25. protected $listeners = [
  26. 'refresh' => '$refresh',
  27. ];
  28. public function isReadOnly(): bool
  29. {
  30. return false;
  31. }
  32. public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
  33. {
  34. return $pageClass === ViewBill::class;
  35. }
  36. public function form(Form $form): Form
  37. {
  38. return $form
  39. ->columns(1)
  40. ->schema([
  41. Forms\Components\DatePicker::make('posted_at')
  42. ->label('Date'),
  43. Forms\Components\TextInput::make('amount')
  44. ->label('Amount')
  45. ->required()
  46. ->money()
  47. ->live(onBlur: true)
  48. ->helperText(function (RelationManager $livewire, $state, ?Transaction $record) {
  49. if (! CurrencyConverter::isValidAmount($state)) {
  50. return null;
  51. }
  52. /** @var Bill $ownerRecord */
  53. $ownerRecord = $livewire->getOwnerRecord();
  54. $amountDue = $ownerRecord->getRawOriginal('amount_due');
  55. $amount = CurrencyConverter::convertToCents($state);
  56. if ($amount <= 0) {
  57. return 'Please enter a valid positive amount';
  58. }
  59. $currentPaymentAmount = $record?->getRawOriginal('amount') ?? 0;
  60. $newAmountDue = $amountDue - $amount + $currentPaymentAmount;
  61. return match (true) {
  62. $newAmountDue > 0 => 'Amount due after payment will be ' . CurrencyConverter::formatCentsToMoney($newAmountDue),
  63. $newAmountDue === 0 => 'Bill will be fully paid',
  64. default => 'Amount exceeds bill total by ' . CurrencyConverter::formatCentsToMoney(abs($newAmountDue)),
  65. };
  66. })
  67. ->rules([
  68. static fn (): Closure => static function (string $attribute, $value, Closure $fail) {
  69. if (! CurrencyConverter::isValidAmount($value)) {
  70. $fail('Please enter a valid amount');
  71. }
  72. },
  73. ]),
  74. Forms\Components\Select::make('payment_method')
  75. ->label('Payment method')
  76. ->required()
  77. ->options(PaymentMethod::class),
  78. Forms\Components\Select::make('bank_account_id')
  79. ->label('Account')
  80. ->required()
  81. ->options(function () {
  82. return BankAccount::query()
  83. ->join('accounts', 'bank_accounts.account_id', '=', 'accounts.id')
  84. ->select(['bank_accounts.id', 'accounts.name'])
  85. ->pluck('accounts.name', 'bank_accounts.id')
  86. ->toArray();
  87. })
  88. ->searchable(),
  89. Forms\Components\Textarea::make('notes')
  90. ->label('Notes'),
  91. ]);
  92. }
  93. public function table(Table $table): Table
  94. {
  95. return $table
  96. ->recordTitleAttribute('description')
  97. ->columns([
  98. Tables\Columns\TextColumn::make('posted_at')
  99. ->label('Date')
  100. ->sortable()
  101. ->defaultDateFormat(),
  102. Tables\Columns\TextColumn::make('type')
  103. ->label('Type')
  104. ->sortable()
  105. ->toggleable(isToggledHiddenByDefault: true),
  106. Tables\Columns\TextColumn::make('description')
  107. ->label('Description')
  108. ->limit(30)
  109. ->toggleable(),
  110. Tables\Columns\TextColumn::make('bankAccount.account.name')
  111. ->label('Account')
  112. ->toggleable(),
  113. Tables\Columns\TextColumn::make('amount')
  114. ->label('Amount')
  115. ->weight(static fn (Transaction $transaction) => $transaction->reviewed ? null : FontWeight::SemiBold)
  116. ->color(
  117. static fn (Transaction $transaction) => match ($transaction->type) {
  118. TransactionType::Deposit => Color::rgb('rgb(' . Color::Green[700] . ')'),
  119. TransactionType::Journal => 'primary',
  120. default => null,
  121. }
  122. )
  123. ->sortable()
  124. ->currency(static fn (Transaction $transaction) => $transaction->bankAccount?->account->currency_code ?? CurrencyAccessor::getDefaultCurrency(), true),
  125. ])
  126. ->filters([
  127. //
  128. ])
  129. ->headerActions([
  130. Tables\Actions\CreateAction::make()
  131. ->label('Record payment')
  132. ->modalHeading(fn (Tables\Actions\CreateAction $action) => $action->getLabel())
  133. ->modalWidth(MaxWidth::TwoExtraLarge)
  134. ->visible(function () {
  135. return $this->getOwnerRecord()->canRecordPayment();
  136. })
  137. ->mountUsing(function (Form $form) {
  138. $record = $this->getOwnerRecord();
  139. $form->fill([
  140. 'posted_at' => now(),
  141. 'amount' => $record->amount_due,
  142. ]);
  143. })
  144. ->databaseTransaction()
  145. ->successNotificationTitle('Payment recorded')
  146. ->action(function (Tables\Actions\CreateAction $action, array $data) {
  147. /** @var Bill $record */
  148. $record = $this->getOwnerRecord();
  149. $record->recordPayment($data);
  150. $action->success();
  151. $this->dispatch('refresh');
  152. }),
  153. ])
  154. ->actions([
  155. Tables\Actions\EditAction::make()
  156. ->modalWidth(MaxWidth::TwoExtraLarge)
  157. ->after(fn () => $this->dispatch('refresh')),
  158. Tables\Actions\DeleteAction::make()
  159. ->after(fn () => $this->dispatch('refresh')),
  160. ])
  161. ->bulkActions([
  162. Tables\Actions\BulkActionGroup::make([
  163. Tables\Actions\DeleteBulkAction::make(),
  164. ]),
  165. ]);
  166. }
  167. }