|
@@ -34,7 +34,6 @@ use Awcodes\TableRepeater\Header;
|
34
|
34
|
use Closure;
|
35
|
35
|
use Filament\Forms;
|
36
|
36
|
use Filament\Forms\Form;
|
37
|
|
-use Filament\Notifications\Notification;
|
38
|
37
|
use Filament\Resources\Resource;
|
39
|
38
|
use Filament\Support\Enums\Alignment;
|
40
|
39
|
use Filament\Support\Enums\MaxWidth;
|
|
@@ -42,7 +41,6 @@ use Filament\Tables;
|
42
|
41
|
use Filament\Tables\Table;
|
43
|
42
|
use Guava\FilamentClusters\Forms\Cluster;
|
44
|
43
|
use Illuminate\Database\Eloquent\Builder;
|
45
|
|
-use Illuminate\Database\Eloquent\Collection;
|
46
|
44
|
use Illuminate\Support\Carbon;
|
47
|
45
|
use Illuminate\Support\Facades\Auth;
|
48
|
46
|
|
|
@@ -558,108 +556,6 @@ class BillResource extends Resource
|
558
|
556
|
'created_at',
|
559
|
557
|
'updated_at',
|
560
|
558
|
]),
|
561
|
|
- Tables\Actions\BulkAction::make('recordPayments')
|
562
|
|
- ->label('Record payments')
|
563
|
|
- ->icon('heroicon-o-credit-card')
|
564
|
|
- ->stickyModalHeader()
|
565
|
|
- ->stickyModalFooter()
|
566
|
|
- ->modalFooterActionsAlignment(Alignment::End)
|
567
|
|
- ->modalWidth(MaxWidth::TwoExtraLarge)
|
568
|
|
- ->databaseTransaction()
|
569
|
|
- ->successNotificationTitle('Payments recorded')
|
570
|
|
- ->failureNotificationTitle('Failed to record payments')
|
571
|
|
- ->deselectRecordsAfterCompletion()
|
572
|
|
- ->beforeFormFilled(function (Collection $records, Tables\Actions\BulkAction $action) {
|
573
|
|
- $isInvalid = $records->contains(fn (Bill $bill) => ! $bill->canRecordPayment());
|
574
|
|
-
|
575
|
|
- if ($isInvalid) {
|
576
|
|
- Notification::make()
|
577
|
|
- ->title('Payment recording failed')
|
578
|
|
- ->body('Bills that are either paid, voided, or are in a foreign currency cannot be processed through bulk payments. Please adjust your selection and try again.')
|
579
|
|
- ->persistent()
|
580
|
|
- ->danger()
|
581
|
|
- ->send();
|
582
|
|
-
|
583
|
|
- $action->cancel(true);
|
584
|
|
- }
|
585
|
|
- })
|
586
|
|
- ->mountUsing(function (Collection $records, Form $form) {
|
587
|
|
- $totalAmountDue = $records->sum('amount_due');
|
588
|
|
-
|
589
|
|
- $form->fill([
|
590
|
|
- 'posted_at' => now(),
|
591
|
|
- 'amount' => $totalAmountDue,
|
592
|
|
- ]);
|
593
|
|
- })
|
594
|
|
- ->form([
|
595
|
|
- Forms\Components\DatePicker::make('posted_at')
|
596
|
|
- ->label('Date'),
|
597
|
|
- Forms\Components\TextInput::make('amount')
|
598
|
|
- ->label('Amount')
|
599
|
|
- ->required()
|
600
|
|
- ->money()
|
601
|
|
- ->rules([
|
602
|
|
- static fn (): Closure => static function (string $attribute, $value, Closure $fail) {
|
603
|
|
- if (! CurrencyConverter::isValidAmount($value)) {
|
604
|
|
- $fail('Please enter a valid amount');
|
605
|
|
- }
|
606
|
|
- },
|
607
|
|
- ]),
|
608
|
|
- Forms\Components\Select::make('payment_method')
|
609
|
|
- ->label('Payment method')
|
610
|
|
- ->required()
|
611
|
|
- ->options(PaymentMethod::class),
|
612
|
|
- Forms\Components\Select::make('bank_account_id')
|
613
|
|
- ->label('Account')
|
614
|
|
- ->required()
|
615
|
|
- ->options(function () {
|
616
|
|
- return BankAccount::query()
|
617
|
|
- ->join('accounts', 'bank_accounts.account_id', '=', 'accounts.id')
|
618
|
|
- ->select(['bank_accounts.id', 'accounts.name'])
|
619
|
|
- ->pluck('accounts.name', 'bank_accounts.id')
|
620
|
|
- ->toArray();
|
621
|
|
- })
|
622
|
|
- ->searchable(),
|
623
|
|
- Forms\Components\Textarea::make('notes')
|
624
|
|
- ->label('Notes'),
|
625
|
|
- ])
|
626
|
|
- ->before(function (Collection $records, Tables\Actions\BulkAction $action, array $data) {
|
627
|
|
- $totalPaymentAmount = $data['amount'] ?? 0;
|
628
|
|
- $totalAmountDue = $records->sum('amount_due');
|
629
|
|
-
|
630
|
|
- if ($totalPaymentAmount > $totalAmountDue) {
|
631
|
|
- $formattedTotalAmountDue = CurrencyConverter::formatCentsToMoney($totalAmountDue);
|
632
|
|
-
|
633
|
|
- Notification::make()
|
634
|
|
- ->title('Excess payment amount')
|
635
|
|
- ->body("The payment amount exceeds the total amount due of {$formattedTotalAmountDue}. Please adjust the payment amount and try again.")
|
636
|
|
- ->persistent()
|
637
|
|
- ->warning()
|
638
|
|
- ->send();
|
639
|
|
-
|
640
|
|
- $action->halt(true);
|
641
|
|
- }
|
642
|
|
- })
|
643
|
|
- ->action(function (Collection $records, Tables\Actions\BulkAction $action, array $data) {
|
644
|
|
- $totalPaymentAmount = $data['amount'] ?? 0;
|
645
|
|
- $remainingAmount = $totalPaymentAmount;
|
646
|
|
-
|
647
|
|
- $records->each(function (Bill $record) use (&$remainingAmount, $data) {
|
648
|
|
- $amountDue = $record->amount_due;
|
649
|
|
-
|
650
|
|
- if ($amountDue <= 0 || $remainingAmount <= 0) {
|
651
|
|
- return;
|
652
|
|
- }
|
653
|
|
-
|
654
|
|
- $paymentAmount = min($amountDue, $remainingAmount);
|
655
|
|
- $data['amount'] = $paymentAmount;
|
656
|
|
-
|
657
|
|
- $record->recordPayment($data);
|
658
|
|
- $remainingAmount -= $paymentAmount;
|
659
|
|
- });
|
660
|
|
-
|
661
|
|
- $action->success();
|
662
|
|
- }),
|
663
|
559
|
]),
|
664
|
560
|
]);
|
665
|
561
|
}
|