瀏覽代碼

cleanup

3.x
Andrew Wallo 4 月之前
父節點
當前提交
3e8c63714c

+ 8
- 2
app/Concerns/HasTransactionAction.php 查看文件

11
 use App\Utilities\Currency\CurrencyAccessor;
11
 use App\Utilities\Currency\CurrencyAccessor;
12
 use App\Utilities\Currency\CurrencyConverter;
12
 use App\Utilities\Currency\CurrencyConverter;
13
 use Awcodes\TableRepeater\Header;
13
 use Awcodes\TableRepeater\Header;
14
+use Closure;
14
 use Filament\Forms;
15
 use Filament\Forms;
15
 use Filament\Forms\Components\Actions\Action as FormAction;
16
 use Filament\Forms\Components\Actions\Action as FormAction;
16
 use Filament\Forms\Form;
17
 use Filament\Forms\Form;
21
 {
22
 {
22
     use HasJournalEntryActions;
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
         $this->transactionType = $type;
29
         $this->transactionType = $type;
29
 
30
 
30
         return $this;
31
         return $this;
31
     }
32
     }
32
 
33
 
34
+    public function getTransactionType(): ?TransactionType
35
+    {
36
+        return $this->evaluate($this->transactionType);
37
+    }
38
+
33
     protected function getFormDefaultsForType(TransactionType $type): array
39
     protected function getFormDefaultsForType(TransactionType $type): array
34
     {
40
     {
35
         $commonDefaults = [
41
         $commonDefaults = [

+ 11
- 14
app/Filament/Actions/CreateTransactionAction.php 查看文件

25
         $this->slideOver();
25
         $this->slideOver();
26
 
26
 
27
         $this->modalWidth(function (): MaxWidth {
27
         $this->modalWidth(function (): MaxWidth {
28
-            return match ($this->transactionType) {
28
+            return match ($this->getTransactionType()) {
29
                 TransactionType::Journal => MaxWidth::Screen,
29
                 TransactionType::Journal => MaxWidth::Screen,
30
                 default => MaxWidth::ThreeExtraLarge,
30
                 default => MaxWidth::ThreeExtraLarge,
31
             };
31
             };
32
         });
32
         });
33
 
33
 
34
         $this->extraModalWindowAttributes(function (): array {
34
         $this->extraModalWindowAttributes(function (): array {
35
-            if ($this->transactionType === TransactionType::Journal) {
35
+            if ($this->getTransactionType() === TransactionType::Journal) {
36
                 return ['class' => 'journal-transaction-modal'];
36
                 return ['class' => 'journal-transaction-modal'];
37
             }
37
             }
38
 
38
 
40
         });
40
         });
41
 
41
 
42
         $this->modalHeading(function (): string {
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
         $this->form(function (Form $form) {
51
         $this->form(function (Form $form) {
55
-            return match ($this->transactionType) {
52
+            return match ($this->getTransactionType()) {
56
                 TransactionType::Transfer => $this->transferForm($form),
53
                 TransactionType::Transfer => $this->transferForm($form),
57
                 TransactionType::Journal => $this->journalTransactionForm($form),
54
                 TransactionType::Journal => $this->journalTransactionForm($form),
58
                 default => $this->transactionForm($form),
55
                 default => $this->transactionForm($form),
60
         });
57
         });
61
 
58
 
62
         $this->afterFormFilled(function () {
59
         $this->afterFormFilled(function () {
63
-            if ($this->transactionType === TransactionType::Journal) {
60
+            if ($this->getTransactionType() === TransactionType::Journal) {
64
                 $this->resetJournalEntryAmounts();
61
                 $this->resetJournalEntryAmounts();
65
             }
62
             }
66
         });
63
         });
67
 
64
 
68
         $this->modalSubmitAction(function (StaticAction $action) {
65
         $this->modalSubmitAction(function (StaticAction $action) {
69
-            if ($this->transactionType === TransactionType::Journal) {
66
+            if ($this->getTransactionType() === TransactionType::Journal) {
70
                 $action->disabled(! $this->isJournalEntryBalanced());
67
                 $action->disabled(! $this->isJournalEntryBalanced());
71
             }
68
             }
72
 
69
 
74
         });
71
         });
75
 
72
 
76
         $this->after(function (Transaction $transaction) {
73
         $this->after(function (Transaction $transaction) {
77
-            if ($this->transactionType === TransactionType::Journal) {
74
+            if ($this->getTransactionType() === TransactionType::Journal) {
78
                 $transaction->updateAmountIfBalanced();
75
                 $transaction->updateAmountIfBalanced();
79
             }
76
             }
80
         });
77
         });
81
 
78
 
82
         $this->mutateFormDataUsing(function (array $data) {
79
         $this->mutateFormDataUsing(function (array $data) {
83
-            if ($this->transactionType === TransactionType::Journal) {
80
+            if ($this->getTransactionType() === TransactionType::Journal) {
84
                 $data['type'] = TransactionType::Journal;
81
                 $data['type'] = TransactionType::Journal;
85
             }
82
             }
86
 
83
 

+ 10
- 19
app/Filament/Actions/EditTransactionAction.php 查看文件

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

+ 1
- 8
app/Filament/Company/Resources/Accounting/TransactionResource.php 查看文件

172
                     ->action(fn (Transaction $transaction) => $transaction->update(['reviewed' => ! $transaction->reviewed])),
172
                     ->action(fn (Transaction $transaction) => $transaction->update(['reviewed' => ! $transaction->reviewed])),
173
                 Tables\Actions\ActionGroup::make([
173
                 Tables\Actions\ActionGroup::make([
174
                     Tables\Actions\ActionGroup::make([
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
                         Tables\Actions\ReplicateAction::make()
176
                         Tables\Actions\ReplicateAction::make()
184
                             ->excludeAttributes(['created_by', 'updated_by', 'created_at', 'updated_at'])
177
                             ->excludeAttributes(['created_by', 'updated_by', 'created_at', 'updated_at'])
185
                             ->modal(false)
178
                             ->modal(false)

+ 16
- 15
app/Filament/Tables/Actions/EditTransactionAction.php 查看文件

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

+ 69
- 0
app/Policies/TransactionPolicy.php 查看文件

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…
取消
儲存