Andrew Wallo 4 kuukautta sitten
vanhempi
commit
3e8c63714c

+ 8
- 2
app/Concerns/HasTransactionAction.php Näytä tiedosto

@@ -11,6 +11,7 @@ use App\Models\Banking\BankAccount;
11 11
 use App\Utilities\Currency\CurrencyAccessor;
12 12
 use App\Utilities\Currency\CurrencyConverter;
13 13
 use Awcodes\TableRepeater\Header;
14
+use Closure;
14 15
 use Filament\Forms;
15 16
 use Filament\Forms\Components\Actions\Action as FormAction;
16 17
 use Filament\Forms\Form;
@@ -21,15 +22,20 @@ trait HasTransactionAction
21 22
 {
22 23
     use HasJournalEntryActions;
23 24
 
24
-    protected ?TransactionType $transactionType = null;
25
+    protected TransactionType | Closure | null $transactionType = null;
25 26
 
26
-    public function type(TransactionType $type): static
27
+    public function type(TransactionType | Closure | null $type = null): static
27 28
     {
28 29
         $this->transactionType = $type;
29 30
 
30 31
         return $this;
31 32
     }
32 33
 
34
+    public function getTransactionType(): ?TransactionType
35
+    {
36
+        return $this->evaluate($this->transactionType);
37
+    }
38
+
33 39
     protected function getFormDefaultsForType(TransactionType $type): array
34 40
     {
35 41
         $commonDefaults = [

+ 11
- 14
app/Filament/Actions/CreateTransactionAction.php Näytä tiedosto

@@ -25,14 +25,14 @@ class CreateTransactionAction extends CreateAction
25 25
         $this->slideOver();
26 26
 
27 27
         $this->modalWidth(function (): MaxWidth {
28
-            return match ($this->transactionType) {
28
+            return match ($this->getTransactionType()) {
29 29
                 TransactionType::Journal => MaxWidth::Screen,
30 30
                 default => MaxWidth::ThreeExtraLarge,
31 31
             };
32 32
         });
33 33
 
34 34
         $this->extraModalWindowAttributes(function (): array {
35
-            if ($this->transactionType === TransactionType::Journal) {
35
+            if ($this->getTransactionType() === TransactionType::Journal) {
36 36
                 return ['class' => 'journal-transaction-modal'];
37 37
             }
38 38
 
@@ -40,19 +40,16 @@ class CreateTransactionAction extends CreateAction
40 40
         });
41 41
 
42 42
         $this->modalHeading(function (): string {
43
-            return match ($this->transactionType) {
44
-                TransactionType::Journal => 'Journal Entry',
45
-                TransactionType::Deposit => 'Add Income',
46
-                TransactionType::Withdrawal => 'Add Expense',
47
-                TransactionType::Transfer => 'Add Transfer',
48
-                default => 'Add Transaction',
43
+            return match ($this->getTransactionType()) {
44
+                TransactionType::Journal => 'Create journal entry',
45
+                default => 'Create transaction',
49 46
             };
50 47
         });
51 48
 
52
-        $this->fillForm(fn (): array => $this->getFormDefaultsForType($this->transactionType));
49
+        $this->fillForm(fn (): array => $this->getFormDefaultsForType($this->getTransactionType()));
53 50
 
54 51
         $this->form(function (Form $form) {
55
-            return match ($this->transactionType) {
52
+            return match ($this->getTransactionType()) {
56 53
                 TransactionType::Transfer => $this->transferForm($form),
57 54
                 TransactionType::Journal => $this->journalTransactionForm($form),
58 55
                 default => $this->transactionForm($form),
@@ -60,13 +57,13 @@ class CreateTransactionAction extends CreateAction
60 57
         });
61 58
 
62 59
         $this->afterFormFilled(function () {
63
-            if ($this->transactionType === TransactionType::Journal) {
60
+            if ($this->getTransactionType() === TransactionType::Journal) {
64 61
                 $this->resetJournalEntryAmounts();
65 62
             }
66 63
         });
67 64
 
68 65
         $this->modalSubmitAction(function (StaticAction $action) {
69
-            if ($this->transactionType === TransactionType::Journal) {
66
+            if ($this->getTransactionType() === TransactionType::Journal) {
70 67
                 $action->disabled(! $this->isJournalEntryBalanced());
71 68
             }
72 69
 
@@ -74,13 +71,13 @@ class CreateTransactionAction extends CreateAction
74 71
         });
75 72
 
76 73
         $this->after(function (Transaction $transaction) {
77
-            if ($this->transactionType === TransactionType::Journal) {
74
+            if ($this->getTransactionType() === TransactionType::Journal) {
78 75
                 $transaction->updateAmountIfBalanced();
79 76
             }
80 77
         });
81 78
 
82 79
         $this->mutateFormDataUsing(function (array $data) {
83
-            if ($this->transactionType === TransactionType::Journal) {
80
+            if ($this->getTransactionType() === TransactionType::Journal) {
84 81
                 $data['type'] = TransactionType::Journal;
85 82
             }
86 83
 

+ 10
- 19
app/Filament/Actions/EditTransactionAction.php Näytä tiedosto

@@ -18,11 +18,12 @@ class EditTransactionAction extends EditAction
18 18
     {
19 19
         parent::setUp();
20 20
 
21
-        $this->transactionType = $this->getRecord()->type;
21
+        $this->type(static function (Transaction $record) {
22
+            return $record->type;
23
+        });
22 24
 
23 25
         $this->label(function () {
24
-            return match ($this->transactionType) {
25
-                TransactionType::Transfer => 'Edit transfer',
26
+            return match ($this->getTransactionType()) {
26 27
                 TransactionType::Journal => 'Edit journal entry',
27 28
                 default => 'Edit transaction',
28 29
             };
@@ -30,33 +31,23 @@ class EditTransactionAction extends EditAction
30 31
 
31 32
         $this->slideOver();
32 33
 
33
-        $this->visible(static fn (Transaction $transaction) => ! $transaction->transactionable_id);
34
-
35 34
         $this->modalWidth(function (): MaxWidth {
36
-            return match ($this->transactionType) {
35
+            return match ($this->getTransactionType()) {
37 36
                 TransactionType::Journal => MaxWidth::Screen,
38 37
                 default => MaxWidth::ThreeExtraLarge,
39 38
             };
40 39
         });
41 40
 
42 41
         $this->extraModalWindowAttributes(function (): array {
43
-            if ($this->transactionType === TransactionType::Journal) {
42
+            if ($this->getTransactionType() === TransactionType::Journal) {
44 43
                 return ['class' => 'journal-transaction-modal'];
45 44
             }
46 45
 
47 46
             return [];
48 47
         });
49 48
 
50
-        $this->modalHeading(function (): string {
51
-            return match ($this->transactionType) {
52
-                TransactionType::Journal => 'Journal Entry',
53
-                TransactionType::Transfer => 'Edit Transfer',
54
-                default => 'Edit Transaction',
55
-            };
56
-        });
57
-
58 49
         $this->form(function (Form $form) {
59
-            return match ($this->transactionType) {
50
+            return match ($this->getTransactionType()) {
60 51
                 TransactionType::Transfer => $this->transferForm($form),
61 52
                 TransactionType::Journal => $this->journalTransactionForm($form),
62 53
                 default => $this->transactionForm($form),
@@ -64,7 +55,7 @@ class EditTransactionAction extends EditAction
64 55
         });
65 56
 
66 57
         $this->afterFormFilled(function (Transaction $record) {
67
-            if ($this->transactionType === TransactionType::Journal) {
58
+            if ($this->getTransactionType() === TransactionType::Journal) {
68 59
                 $debitAmounts = $record->journalEntries->sumDebits()->getAmount();
69 60
                 $creditAmounts = $record->journalEntries->sumCredits()->getAmount();
70 61
 
@@ -74,7 +65,7 @@ class EditTransactionAction extends EditAction
74 65
         });
75 66
 
76 67
         $this->modalSubmitAction(function (StaticAction $action) {
77
-            if ($this->transactionType === TransactionType::Journal) {
68
+            if ($this->getTransactionType() === TransactionType::Journal) {
78 69
                 $action->disabled(! $this->isJournalEntryBalanced());
79 70
             }
80 71
 
@@ -82,7 +73,7 @@ class EditTransactionAction extends EditAction
82 73
         });
83 74
 
84 75
         $this->after(function (Transaction $transaction) {
85
-            if ($this->transactionType === TransactionType::Journal) {
76
+            if ($this->getTransactionType() === TransactionType::Journal) {
86 77
                 $transaction->updateAmountIfBalanced();
87 78
             }
88 79
         });

+ 1
- 8
app/Filament/Company/Resources/Accounting/TransactionResource.php Näytä tiedosto

@@ -172,14 +172,7 @@ class TransactionResource extends Resource
172 172
                     ->action(fn (Transaction $transaction) => $transaction->update(['reviewed' => ! $transaction->reviewed])),
173 173
                 Tables\Actions\ActionGroup::make([
174 174
                     Tables\Actions\ActionGroup::make([
175
-                        EditTransactionAction::make('editTransaction')
176
-                            ->visible(static fn (Transaction $transaction) => $transaction->type->isStandard() && ! $transaction->transactionable_id),
177
-                        EditTransactionAction::make('editTransfer')
178
-                            ->type(TransactionType::Transfer)
179
-                            ->visible(static fn (Transaction $transaction) => $transaction->type->isTransfer()),
180
-                        EditTransactionAction::make('editJournalEntry')
181
-                            ->type(TransactionType::Journal)
182
-                            ->visible(static fn (Transaction $transaction) => $transaction->type->isJournal() && ! $transaction->transactionable_id),
175
+                        EditTransactionAction::make(),
183 176
                         Tables\Actions\ReplicateAction::make()
184 177
                             ->excludeAttributes(['created_by', 'updated_by', 'created_at', 'updated_at'])
185 178
                             ->modal(false)

+ 16
- 15
app/Filament/Tables/Actions/EditTransactionAction.php Näytä tiedosto

@@ -18,35 +18,36 @@ class EditTransactionAction extends EditAction
18 18
     {
19 19
         parent::setUp();
20 20
 
21
-        $this->label(null);
21
+        $this->type(static function (Transaction $record) {
22
+            return $record->type;
23
+        });
24
+
25
+        $this->label(function () {
26
+            return match ($this->getTransactionType()) {
27
+                TransactionType::Journal => 'Edit journal entry',
28
+                default => 'Edit transaction',
29
+            };
30
+        });
22 31
 
23 32
         $this->slideOver();
24 33
 
25 34
         $this->modalWidth(function (): MaxWidth {
26
-            return match ($this->transactionType) {
35
+            return match ($this->getTransactionType()) {
27 36
                 TransactionType::Journal => MaxWidth::Screen,
28 37
                 default => MaxWidth::ThreeExtraLarge,
29 38
             };
30 39
         });
31 40
 
32 41
         $this->extraModalWindowAttributes(function (): array {
33
-            if ($this->transactionType === TransactionType::Journal) {
42
+            if ($this->getTransactionType() === TransactionType::Journal) {
34 43
                 return ['class' => 'journal-transaction-modal'];
35 44
             }
36 45
 
37 46
             return [];
38 47
         });
39 48
 
40
-        $this->modalHeading(function (): string {
41
-            return match ($this->transactionType) {
42
-                TransactionType::Journal => 'Journal Entry',
43
-                TransactionType::Transfer => 'Edit Transfer',
44
-                default => 'Edit Transaction',
45
-            };
46
-        });
47
-
48 49
         $this->form(function (Form $form) {
49
-            return match ($this->transactionType) {
50
+            return match ($this->getTransactionType()) {
50 51
                 TransactionType::Transfer => $this->transferForm($form),
51 52
                 TransactionType::Journal => $this->journalTransactionForm($form),
52 53
                 default => $this->transactionForm($form),
@@ -54,7 +55,7 @@ class EditTransactionAction extends EditAction
54 55
         });
55 56
 
56 57
         $this->afterFormFilled(function (Transaction $record) {
57
-            if ($this->transactionType === TransactionType::Journal) {
58
+            if ($this->getTransactionType() === TransactionType::Journal) {
58 59
                 $debitAmounts = $record->journalEntries->sumDebits()->getAmount();
59 60
                 $creditAmounts = $record->journalEntries->sumCredits()->getAmount();
60 61
 
@@ -64,7 +65,7 @@ class EditTransactionAction extends EditAction
64 65
         });
65 66
 
66 67
         $this->modalSubmitAction(function (StaticAction $action) {
67
-            if ($this->transactionType === TransactionType::Journal) {
68
+            if ($this->getTransactionType() === TransactionType::Journal) {
68 69
                 $action->disabled(! $this->isJournalEntryBalanced());
69 70
             }
70 71
 
@@ -72,7 +73,7 @@ class EditTransactionAction extends EditAction
72 73
         });
73 74
 
74 75
         $this->after(function (Transaction $transaction) {
75
-            if ($this->transactionType === TransactionType::Journal) {
76
+            if ($this->getTransactionType() === TransactionType::Journal) {
76 77
                 $transaction->updateAmountIfBalanced();
77 78
             }
78 79
         });

+ 69
- 0
app/Policies/TransactionPolicy.php Näytä tiedosto

@@ -0,0 +1,69 @@
1
+<?php
2
+
3
+namespace App\Policies;
4
+
5
+use App\Models\Accounting\Transaction;
6
+use App\Models\User;
7
+
8
+class TransactionPolicy
9
+{
10
+    /**
11
+     * Determine whether the user can view any models.
12
+     */
13
+    public function viewAny(User $user): bool
14
+    {
15
+        return true;
16
+    }
17
+
18
+    /**
19
+     * Determine whether the user can view the model.
20
+     */
21
+    public function view(User $user, Transaction $transaction): bool
22
+    {
23
+        return true;
24
+    }
25
+
26
+    /**
27
+     * Determine whether the user can create models.
28
+     */
29
+    public function create(User $user): bool
30
+    {
31
+        return true;
32
+    }
33
+
34
+    /**
35
+     * Determine whether the user can update the model.
36
+     */
37
+    public function update(User $user, Transaction $transaction): bool
38
+    {
39
+        if ($transaction->transactionable_id) {
40
+            return false;
41
+        }
42
+
43
+        return true;
44
+    }
45
+
46
+    /**
47
+     * Determine whether the user can delete the model.
48
+     */
49
+    public function delete(User $user, Transaction $transaction): bool
50
+    {
51
+        return true;
52
+    }
53
+
54
+    /**
55
+     * Determine whether the user can restore the model.
56
+     */
57
+    public function restore(User $user, Transaction $transaction): bool
58
+    {
59
+        return true;
60
+    }
61
+
62
+    /**
63
+     * Determine whether the user can permanently delete the model.
64
+     */
65
+    public function forceDelete(User $user, Transaction $transaction): bool
66
+    {
67
+        return true;
68
+    }
69
+}

Loading…
Peruuta
Tallenna