Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PaymentsRelationManager.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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(BankAccount::query()
  82. ->get()
  83. ->pluck('account.name', 'id'))
  84. ->searchable(),
  85. Forms\Components\Textarea::make('notes')
  86. ->label('Notes'),
  87. ]);
  88. }
  89. public function table(Table $table): Table
  90. {
  91. return $table
  92. ->recordTitleAttribute('description')
  93. ->columns([
  94. Tables\Columns\TextColumn::make('posted_at')
  95. ->label('Date')
  96. ->sortable()
  97. ->defaultDateFormat(),
  98. Tables\Columns\TextColumn::make('type')
  99. ->label('Type')
  100. ->sortable()
  101. ->toggleable(isToggledHiddenByDefault: true),
  102. Tables\Columns\TextColumn::make('description')
  103. ->label('Description')
  104. ->limit(30)
  105. ->toggleable(),
  106. Tables\Columns\TextColumn::make('bankAccount.account.name')
  107. ->label('Account')
  108. ->toggleable(),
  109. Tables\Columns\TextColumn::make('amount')
  110. ->label('Amount')
  111. ->weight(static fn (Transaction $transaction) => $transaction->reviewed ? null : FontWeight::SemiBold)
  112. ->color(
  113. static fn (Transaction $transaction) => match ($transaction->type) {
  114. TransactionType::Deposit => Color::rgb('rgb(' . Color::Green[700] . ')'),
  115. TransactionType::Journal => 'primary',
  116. default => null,
  117. }
  118. )
  119. ->sortable()
  120. ->currency(static fn (Transaction $transaction) => $transaction->bankAccount?->account->currency_code ?? CurrencyAccessor::getDefaultCurrency(), true),
  121. ])
  122. ->filters([
  123. //
  124. ])
  125. ->headerActions([
  126. Tables\Actions\CreateAction::make()
  127. ->label('Record payment')
  128. ->modalHeading(fn (Tables\Actions\CreateAction $action) => $action->getLabel())
  129. ->modalWidth(MaxWidth::TwoExtraLarge)
  130. ->visible(function () {
  131. return $this->getOwnerRecord()->canRecordPayment();
  132. })
  133. ->mountUsing(function (Form $form) {
  134. $record = $this->getOwnerRecord();
  135. $form->fill([
  136. 'posted_at' => now(),
  137. 'amount' => $record->amount_due,
  138. ]);
  139. })
  140. ->databaseTransaction()
  141. ->successNotificationTitle('Payment recorded')
  142. ->action(function (Tables\Actions\CreateAction $action, array $data) {
  143. /** @var Bill $record */
  144. $record = $this->getOwnerRecord();
  145. $record->recordPayment($data);
  146. $action->success();
  147. $this->dispatch('refresh');
  148. }),
  149. ])
  150. ->actions([
  151. Tables\Actions\EditAction::make()
  152. ->modalWidth(MaxWidth::TwoExtraLarge)
  153. ->after(fn () => $this->dispatch('refresh')),
  154. Tables\Actions\DeleteAction::make()
  155. ->after(fn () => $this->dispatch('refresh')),
  156. ])
  157. ->bulkActions([
  158. Tables\Actions\BulkActionGroup::make([
  159. Tables\Actions\DeleteBulkAction::make(),
  160. ]),
  161. ]);
  162. }
  163. }