瀏覽代碼

wip: Accounting Module

3.x
wallo 2 年之前
父節點
當前提交
eb16b90a61

+ 0
- 1
app/Filament/Resources/AccountResource.php 查看文件

17
 use Filament\Resources\Resource;
17
 use Filament\Resources\Resource;
18
 use Filament\Resources\Table;
18
 use Filament\Resources\Table;
19
 use Filament\Tables;
19
 use Filament\Tables;
20
-use Illuminate\Database\Eloquent\Builder;
21
 use Illuminate\Support\Collection;
20
 use Illuminate\Support\Collection;
22
 use Illuminate\Support\Facades\Auth;
21
 use Illuminate\Support\Facades\Auth;
23
 use Illuminate\Support\Facades\DB;
22
 use Illuminate\Support\Facades\DB;

+ 6
- 56
app/Filament/Resources/AccountResource/Pages/EditAccount.php 查看文件

4
 
4
 
5
 use App\Filament\Resources\AccountResource;
5
 use App\Filament\Resources\AccountResource;
6
 use App\Models\Banking\Account;
6
 use App\Models\Banking\Account;
7
+use App\Traits\HandlesResourceRecordUpdate;
7
 use Filament\Notifications\Notification;
8
 use Filament\Notifications\Notification;
8
 use Filament\Pages\Actions;
9
 use Filament\Pages\Actions;
9
 use Filament\Resources\Pages\EditRecord;
10
 use Filament\Resources\Pages\EditRecord;
13
 
14
 
14
 class EditAccount extends EditRecord
15
 class EditAccount extends EditRecord
15
 {
16
 {
17
+    use HandlesResourceRecordUpdate;
18
+
16
     protected static string $resource = AccountResource::class;
19
     protected static string $resource = AccountResource::class;
17
 
20
 
18
     protected function getActions(): array
21
     protected function getActions(): array
24
 
27
 
25
     protected function getRedirectUrl(): string
28
     protected function getRedirectUrl(): string
26
     {
29
     {
27
-        return $this->getResource()::getUrl('index');
30
+        return $this->previousUrl;
28
     }
31
     }
29
 
32
 
30
     protected function mutateFormDataBeforeSave(array $data): array
33
     protected function mutateFormDataBeforeSave(array $data): array
36
         return $data;
39
         return $data;
37
     }
40
     }
38
 
41
 
39
-    protected function handleRecordUpdate(Model|Account $record, array $data): Model|Account
40
-    {
41
-        return DB::transaction(function () use ($record, $data) {
42
-            $currentCompanyId = auth()->user()->currentCompany->id;
43
-            $recordId = $record->id;
44
-            $enabled = (bool)($data['enabled'] ?? false);
45
-
46
-            // If the record is enabled, disable all other records for the same company
47
-            if ($enabled === true) {
48
-                $this->disableExistingRecord($currentCompanyId, $recordId);
49
-            }
50
-            // If the record is disabled, ensure at least one record remains enabled
51
-            elseif ($enabled === false) {
52
-                $this->ensureAtLeastOneEnabled($currentCompanyId, $recordId, $enabled);
53
-            }
54
-
55
-            $data['enabled'] = $enabled;
56
-
57
-            return parent::handleRecordUpdate($record, $data);
58
-        });
59
-    }
60
-
61
-    protected function disableExistingRecord(int $companyId, int $recordId): void
62
-    {
63
-        $existingEnabledAccount = Account::where('company_id', $companyId)
64
-            ->where('enabled', true)
65
-            ->where('id', '!=', $recordId)
66
-            ->first();
67
-
68
-        if ($existingEnabledAccount !== null) {
69
-            $existingEnabledAccount->enabled = false;
70
-            $existingEnabledAccount->save();
71
-            $this->defaultAccountChanged();
72
-        }
73
-    }
74
-
75
-    protected function ensureAtLeastOneEnabled(int $companyId, int $recordId, bool &$enabled): void
76
-    {
77
-        $enabledAccountsCount = Account::where('company_id', $companyId)
78
-            ->where('enabled', true)
79
-            ->where('id', '!=', $recordId)
80
-            ->count();
81
-
82
-        if ($enabledAccountsCount === 0) {
83
-            $enabled = true;
84
-        }
85
-    }
86
-
87
-    protected function defaultAccountChanged(): void
42
+    protected function handleRecordUpdate(Model $record, array $data): Model
88
     {
43
     {
89
-        Notification::make()
90
-            ->warning()
91
-            ->title('Default account updated')
92
-            ->body('Your default account has been updated. Please check your account settings to review this change and ensure it is correct.')
93
-            ->persistent()
94
-            ->send();
44
+        return $this->handleRecordUpdateWithUniqueField($record, $data);
95
     }
45
     }
96
 }
46
 }

+ 6
- 67
app/Filament/Resources/CategoryResource/Pages/EditCategory.php 查看文件

3
 namespace App\Filament\Resources\CategoryResource\Pages;
3
 namespace App\Filament\Resources\CategoryResource\Pages;
4
 
4
 
5
 use App\Filament\Resources\CategoryResource;
5
 use App\Filament\Resources\CategoryResource;
6
-use App\Models\Setting\Category;
6
+use App\Traits\HandlesResourceRecordUpdate;
7
 use Filament\Pages\Actions;
7
 use Filament\Pages\Actions;
8
 use Filament\Resources\Pages\EditRecord;
8
 use Filament\Resources\Pages\EditRecord;
9
 use Illuminate\Database\Eloquent\Model;
9
 use Illuminate\Database\Eloquent\Model;
10
 use Illuminate\Support\Facades\Auth;
10
 use Illuminate\Support\Facades\Auth;
11
-use Illuminate\Support\Facades\DB;
12
 
11
 
13
 class EditCategory extends EditRecord
12
 class EditCategory extends EditRecord
14
 {
13
 {
14
+    use HandlesResourceRecordUpdate;
15
+
15
     protected static string $resource = CategoryResource::class;
16
     protected static string $resource = CategoryResource::class;
16
 
17
 
17
     protected function getActions(): array
18
     protected function getActions(): array
23
 
24
 
24
     protected function getRedirectUrl(): string
25
     protected function getRedirectUrl(): string
25
     {
26
     {
26
-        return $this->getResource()::getUrl('index');
27
+        return $this->previousUrl;
27
     }
28
     }
28
 
29
 
29
     protected function mutateFormDataBeforeSave(array $data): array
30
     protected function mutateFormDataBeforeSave(array $data): array
35
         return $data;
36
         return $data;
36
     }
37
     }
37
 
38
 
38
-    protected function handleRecordUpdate(Model|Category $record, array $data): Model|Category
39
-    {
40
-        return DB::transaction(function () use ($record, $data) {
41
-            $currentCompanyId = auth()->user()->currentCompany->id;
42
-            $recordId = $record->id;
43
-            $oldType = $record->type;
44
-            $newType = $data['type'];
45
-            $enabled = (bool)($data['enabled'] ?? false);
46
-
47
-            // If the record type has changed and it was previously enabled
48
-            if ($oldType !== $newType && $record->enabled) {
49
-                $this->changeRecordType($currentCompanyId, $recordId, $oldType);
50
-            }
51
-
52
-            if ($enabled === true) {
53
-                $this->disableExistingRecord($currentCompanyId, $recordId, $newType);
54
-            } elseif ($enabled === false) {
55
-                $this->ensureAtLeastOneEnabled($currentCompanyId, $recordId, $newType, $enabled);
56
-            }
57
-
58
-            $data['enabled'] = $enabled;
59
-
60
-            return parent::handleRecordUpdate($record, $data);
61
-        });
62
-    }
63
-
64
-    protected function changeRecordType(int $companyId, int $recordId, string $oldType): void
65
-    {
66
-        $oldTypeRecord = $this->getCompanyRecord($companyId, $oldType, $recordId);
67
-
68
-        if ($oldTypeRecord) {
69
-            $oldTypeRecord->enabled = true;
70
-            $oldTypeRecord->save();
71
-        }
72
-    }
73
-
74
-    protected function disableExistingRecord(int $companyId, int $recordId, string $newType): void
75
-    {
76
-        $existingEnabledRecord = $this->getCompanyRecord($companyId, $newType, $recordId);
77
-
78
-        if ($existingEnabledRecord !== null) {
79
-            $existingEnabledRecord->enabled = false;
80
-            $existingEnabledRecord->save();
81
-        }
82
-    }
83
-
84
-    protected function ensureAtLeastOneEnabled(int $companyId, int $recordId, string $newType, bool &$enabled): void
85
-    {
86
-        $otherEnabledRecords = Category::where('company_id', $companyId)
87
-            ->where('enabled', true)
88
-            ->where('type', $newType)
89
-            ->where('id', '!=', $recordId)
90
-            ->count();
91
-
92
-        if ($otherEnabledRecords === 0) {
93
-            $enabled = true;
94
-        }
95
-    }
96
-
97
-    protected function getCompanyRecord(int $companyId, string $type, int $recordId): ?Category
39
+    protected function handleRecordUpdate(Model $record, array $data): Model
98
     {
40
     {
99
-        return Category::where('company_id', $companyId)
100
-            ->where('type', $type)
101
-            ->where('id', '!=', $recordId)
102
-            ->first();
41
+        return $this->handleRecordUpdateWithUniqueField($record, $data, 'type');
103
     }
42
     }
104
 }
43
 }

+ 5
- 43
app/Filament/Resources/CurrencyResource/Pages/EditCurrency.php 查看文件

5
 use App\Filament\Resources\CurrencyResource;
5
 use App\Filament\Resources\CurrencyResource;
6
 use App\Models\Banking\Account;
6
 use App\Models\Banking\Account;
7
 use App\Models\Setting\Currency;
7
 use App\Models\Setting\Currency;
8
+use App\Traits\HandlesResourceRecordUpdate;
8
 use Filament\Pages\Actions;
9
 use Filament\Pages\Actions;
9
 use Filament\Resources\Pages\EditRecord;
10
 use Filament\Resources\Pages\EditRecord;
10
 use Illuminate\Database\Eloquent\Model;
11
 use Illuminate\Database\Eloquent\Model;
13
 
14
 
14
 class EditCurrency extends EditRecord
15
 class EditCurrency extends EditRecord
15
 {
16
 {
17
+    use HandlesResourceRecordUpdate;
18
+
16
     protected static string $resource = CurrencyResource::class;
19
     protected static string $resource = CurrencyResource::class;
17
 
20
 
18
     protected function getActions(): array
21
     protected function getActions(): array
24
 
27
 
25
     protected function getRedirectUrl(): string
28
     protected function getRedirectUrl(): string
26
     {
29
     {
27
-        return $this->getResource()::getUrl('index');
30
+        return $this->previousUrl;
28
     }
31
     }
29
 
32
 
30
     protected function mutateFormDataBeforeSave(array $data): array
33
     protected function mutateFormDataBeforeSave(array $data): array
38
 
41
 
39
     protected function handleRecordUpdate(Model $record, array $data): Model
42
     protected function handleRecordUpdate(Model $record, array $data): Model
40
     {
43
     {
41
-        return DB::transaction(function () use ($record, $data) {
42
-            $currentCompanyId = auth()->user()->currentCompany->id;
43
-            $recordId = $record->id;
44
-            $enabled = (bool)($data['enabled'] ?? false);
45
-
46
-            // If the record is enabled, disable all other records for the same company
47
-            if ($enabled === true) {
48
-                $this->disableExistingRecord($currentCompanyId, $recordId);
49
-            }
50
-            // If the record is disabled, ensure at least one record remains enabled
51
-            elseif ($enabled === false) {
52
-                $this->ensureAtLeastOneEnabled($currentCompanyId, $recordId, $enabled);
53
-            }
54
-
55
-            $data['enabled'] = $enabled;
56
-
57
-            return parent::handleRecordUpdate($record, $data);
58
-        });
44
+        return $this->handleRecordUpdateWithUniqueField($record, $data);
59
     }
45
     }
60
 
46
 
61
-    protected function disableExistingRecord(int $companyId, int $recordId): void
62
-    {
63
-        $existingEnabledAccount = Currency::where('company_id', $companyId)
64
-            ->where('enabled', true)
65
-            ->where('id', '!=', $recordId)
66
-            ->first();
67
-
68
-        if ($existingEnabledAccount !== null) {
69
-            $existingEnabledAccount->enabled = false;
70
-            $existingEnabledAccount->save();
71
-        }
72
-    }
73
-
74
-    protected function ensureAtLeastOneEnabled(int $companyId, int $recordId, bool &$enabled): void
75
-    {
76
-        $enabledAccountsCount = Currency::where('company_id', $companyId)
77
-            ->where('enabled', true)
78
-            ->where('id', '!=', $recordId)
79
-            ->count();
80
-
81
-        if ($enabledAccountsCount === 0) {
82
-            $enabled = true;
83
-        }
84
-    }
85
 }
47
 }

+ 1
- 1
app/Filament/Resources/CustomerResource/Pages/CreateCustomer.php 查看文件

14
 
14
 
15
     protected function getRedirectUrl(): string
15
     protected function getRedirectUrl(): string
16
     {
16
     {
17
-        return $this->getResource()::getUrl('index');
17
+        return $this->previousUrl;
18
     }
18
     }
19
 
19
 
20
     protected function mutateFormDataBeforeCreate(array $data): array
20
     protected function mutateFormDataBeforeCreate(array $data): array

+ 14
- 0
app/Filament/Resources/DiscountResource/Pages/EditDiscount.php 查看文件

3
 namespace App\Filament\Resources\DiscountResource\Pages;
3
 namespace App\Filament\Resources\DiscountResource\Pages;
4
 
4
 
5
 use App\Filament\Resources\DiscountResource;
5
 use App\Filament\Resources\DiscountResource;
6
+use App\Traits\HandlesResourceRecordUpdate;
6
 use Filament\Pages\Actions;
7
 use Filament\Pages\Actions;
7
 use Filament\Resources\Pages\EditRecord;
8
 use Filament\Resources\Pages\EditRecord;
9
+use Illuminate\Database\Eloquent\Model;
8
 
10
 
9
 class EditDiscount extends EditRecord
11
 class EditDiscount extends EditRecord
10
 {
12
 {
13
+    use HandlesResourceRecordUpdate;
14
+
11
     protected static string $resource = DiscountResource::class;
15
     protected static string $resource = DiscountResource::class;
12
 
16
 
13
     protected function getActions(): array
17
     protected function getActions(): array
16
             Actions\DeleteAction::make(),
20
             Actions\DeleteAction::make(),
17
         ];
21
         ];
18
     }
22
     }
23
+
24
+    protected function getRedirectUrl(): string
25
+    {
26
+        return $this->previousUrl;
27
+    }
28
+
29
+    protected function handleRecordUpdate(Model $record, array $data): Model
30
+    {
31
+        return $this->handleRecordUpdateWithUniqueField($record, $data, 'type');
32
+    }
19
 }
33
 }

+ 5
- 0
app/Filament/Resources/InvoiceResource/Pages/CreateInvoice.php 查看文件

11
 {
11
 {
12
     protected static string $resource = InvoiceResource::class;
12
     protected static string $resource = InvoiceResource::class;
13
 
13
 
14
+    protected function getRedirectUrl(): string
15
+    {
16
+        return $this->previousUrl;
17
+    }
18
+
14
     protected function mutateFormDataBeforeCreate(array $data): array
19
     protected function mutateFormDataBeforeCreate(array $data): array
15
     {
20
     {
16
         $data['company_id'] = Auth::user()->currentCompany->id;
21
         $data['company_id'] = Auth::user()->currentCompany->id;

+ 1
- 1
app/Filament/Resources/InvoiceResource/Pages/EditInvoice.php 查看文件

20
 
20
 
21
     protected function getRedirectUrl(): string
21
     protected function getRedirectUrl(): string
22
     {
22
     {
23
-        return $this->getResource()::getUrl('index');
23
+        return $this->previousUrl;
24
     }
24
     }
25
 
25
 
26
     protected function mutateFormDataBeforeSave(array $data): array
26
     protected function mutateFormDataBeforeSave(array $data): array

+ 6
- 67
app/Filament/Resources/TaxResource/Pages/EditTax.php 查看文件

4
 
4
 
5
 use App\Filament\Resources\TaxResource;
5
 use App\Filament\Resources\TaxResource;
6
 use App\Models\Setting\Tax;
6
 use App\Models\Setting\Tax;
7
+use App\Traits\HandlesResourceRecordUpdate;
7
 use Filament\Pages\Actions;
8
 use Filament\Pages\Actions;
8
 use Filament\Resources\Pages\EditRecord;
9
 use Filament\Resources\Pages\EditRecord;
9
 use Illuminate\Database\Eloquent\Model;
10
 use Illuminate\Database\Eloquent\Model;
12
 
13
 
13
 class EditTax extends EditRecord
14
 class EditTax extends EditRecord
14
 {
15
 {
16
+    use HandlesResourceRecordUpdate;
17
+
15
     protected static string $resource = TaxResource::class;
18
     protected static string $resource = TaxResource::class;
16
 
19
 
17
     protected function getActions(): array
20
     protected function getActions(): array
23
 
26
 
24
     protected function getRedirectUrl(): string
27
     protected function getRedirectUrl(): string
25
     {
28
     {
26
-        return $this->getResource()::getUrl('index');
29
+        return $this->previousUrl;
27
     }
30
     }
28
 
31
 
29
     protected function mutateFormDataBeforeUpdate(array $data): array
32
     protected function mutateFormDataBeforeUpdate(array $data): array
35
         return $data;
38
         return $data;
36
     }
39
     }
37
 
40
 
38
-    protected function handleRecordUpdate(Model|Tax $record, array $data): Model|Tax
39
-    {
40
-        return DB::transaction(function () use ($record, $data) {
41
-            $currentCompanyId = auth()->user()->currentCompany->id;
42
-            $recordId = $record->id;
43
-            $oldType = $record->type;
44
-            $newType = $data['type'];
45
-            $enabled = (bool)($data['enabled'] ?? false);
46
-
47
-            // If the record type has changed and it was previously enabled
48
-            if ($oldType !== $newType && $record->enabled) {
49
-                $this->changeRecordType($currentCompanyId, $recordId, $oldType);
50
-            }
51
-
52
-            if ($enabled === true) {
53
-                $this->disableExistingRecord($currentCompanyId, $recordId, $newType);
54
-            } elseif ($enabled === false) {
55
-                $this->ensureAtLeastOneEnabled($currentCompanyId, $recordId, $newType, $enabled);
56
-            }
57
-
58
-            $data['enabled'] = $enabled;
59
-
60
-            return parent::handleRecordUpdate($record, $data);
61
-        });
62
-    }
63
-
64
-    protected function changeRecordType(int $companyId, int $recordId, string $oldType): void
65
-    {
66
-        $oldTypeRecord = $this->getCompanyCategoryRecord($companyId, $oldType, $recordId);
67
-
68
-        if ($oldTypeRecord) {
69
-            $oldTypeRecord->enabled = true;
70
-            $oldTypeRecord->save();
71
-        }
72
-    }
73
-
74
-    protected function disableExistingRecord(int $companyId, int $recordId, string $newType): void
75
-    {
76
-        $existingEnabledRecord = $this->getCompanyCategoryRecord($companyId, $newType, $recordId);
77
-
78
-        if ($existingEnabledRecord !== null) {
79
-            $existingEnabledRecord->enabled = false;
80
-            $existingEnabledRecord->save();
81
-        }
82
-    }
83
-
84
-    protected function ensureAtLeastOneEnabled(int $companyId, int $recordId, string $newType, bool &$enabled): void
41
+    protected function handleRecordUpdate(Model $record, array $data): Model
85
     {
42
     {
86
-        $otherEnabledRecords = Tax::where('company_id', $companyId)
87
-            ->where('enabled', true)
88
-            ->where('type', $newType)
89
-            ->where('id', '!=', $recordId)
90
-            ->count();
91
-
92
-        if ($otherEnabledRecords === 0) {
93
-            $enabled = true;
94
-        }
43
+        return $this->handleRecordUpdateWithUniqueField($record, $data, 'type');
95
     }
44
     }
96
-
97
-    protected function getCompanyCategoryRecord(int $companyId, string $type, int $recordId): ?Tax
98
-    {
99
-        return Tax::where('company_id', $companyId)
100
-            ->where('type', $type)
101
-            ->where('id', '!=', $recordId)
102
-            ->first();
103
-    }
104
-
105
-
106
 }
45
 }

+ 31
- 2
app/Observers/CompanyObserver.php 查看文件

3
 namespace App\Observers;
3
 namespace App\Observers;
4
 
4
 
5
 use App\Models\Company;
5
 use App\Models\Company;
6
+use App\Models\Setting\Category;
6
 use App\Models\Setting\Currency;
7
 use App\Models\Setting\Currency;
7
 use App\Models\Setting\Discount;
8
 use App\Models\Setting\Discount;
8
 use App\Models\Setting\DocumentDefault;
9
 use App\Models\Setting\DocumentDefault;
9
 use App\Models\Setting\Tax;
10
 use App\Models\Setting\Tax;
10
-use Database\Factories\CategoryFactory;
11
-use Illuminate\Support\Carbon;
12
 
11
 
13
 class CompanyObserver
12
 class CompanyObserver
14
 {
13
 {
17
      */
16
      */
18
     public function created(Company $company): void
17
     public function created(Company $company): void
19
     {
18
     {
19
+        $incomeCategories = ['Salary', 'Bonus', 'Interest', 'Dividends', 'Rentals'];
20
+        $expenseCategories = ['Rent', 'Utilities', 'Food', 'Transportation', 'Entertainment'];
21
+
22
+        $shuffledCategories = [
23
+            ...array_map(static fn ($name) => ['name' => $name, 'type' => 'income'], $incomeCategories),
24
+            ...array_map(static fn ($name) => ['name' => $name, 'type' => 'expense'], $expenseCategories),
25
+        ];
26
+
27
+        shuffle($shuffledCategories);
28
+
29
+        $incomeEnabled = $expenseEnabled = false;
30
+
31
+        foreach ($shuffledCategories as $category) {
32
+            $enabled = false;
33
+            if (!$incomeEnabled && $category['type'] === 'income') {
34
+                $enabled = $incomeEnabled = true;
35
+            } elseif (!$expenseEnabled && $category['type'] === 'expense') {
36
+                $enabled = $expenseEnabled = true;
37
+            }
38
+
39
+            Category::factory()->create([
40
+                'company_id' => $company->id,
41
+                'name' => $category['name'],
42
+                'type' => $category['type'],
43
+                'enabled' => $enabled,
44
+                'created_by' => $company->user_id,
45
+            ]);
46
+        }
47
+
48
+
20
         DocumentDefault::factory()->invoice()->create([
49
         DocumentDefault::factory()->invoice()->create([
21
             'company_id' => $company->id,
50
             'company_id' => $company->id,
22
         ]);
51
         ]);

+ 82
- 0
app/Traits/HandlesResourceRecordUpdate.php 查看文件

1
+<?php
2
+
3
+namespace App\Traits;
4
+
5
+use Illuminate\Database\Eloquent\Model;
6
+use Illuminate\Support\Facades\Auth;
7
+use Illuminate\Support\Facades\DB;
8
+
9
+trait HandlesResourceRecordUpdate
10
+{
11
+    protected function handleRecordUpdateWithUniqueField(Model $record, array $data, string|null $uniqueField = null): Model
12
+    {
13
+        return DB::transaction(function () use ($uniqueField, $record, $data) {
14
+            $companyId = Auth::user()->currentCompany->id;
15
+            $oldValue = $uniqueField ? $record->{$uniqueField} : null;
16
+            $newValue = $uniqueField ? $data[$uniqueField] : null;
17
+            $enabled = (bool)($data['enabled'] ?? false);
18
+
19
+            if ($oldValue !== $newValue && $record->enabled) {
20
+                $this->enableAnotherOfSameValue($companyId, $record, $uniqueField, $oldValue);
21
+            }
22
+
23
+            if ($enabled === true) {
24
+                $this->disableOthersOfSameValue($companyId, $record, $uniqueField, $newValue);
25
+            } elseif ($enabled === false) {
26
+                $this->ensureAtLeastOneEnabled($companyId, $record, $uniqueField, $newValue, $enabled);
27
+            }
28
+
29
+            $data['enabled'] = $enabled;
30
+
31
+            return tap($record)->update($data);
32
+        });
33
+    }
34
+
35
+    protected function enableAnotherOfSameValue(int $companyId, Model $record, ?string $uniqueField, $value): void
36
+    {
37
+        $query = $record::where('company_id', $companyId)
38
+            ->where('id', '!=', $record->id)
39
+            ->where('enabled', false);
40
+
41
+        if($uniqueField){
42
+            $query->where($uniqueField, $value);
43
+        }
44
+
45
+        $otherRecord = $query->first();
46
+
47
+        if ($otherRecord) {
48
+            $otherRecord->enabled = true;
49
+            $otherRecord->save();
50
+        }
51
+    }
52
+
53
+    protected function disableOthersOfSameValue(int $companyId, Model $record, ?string $uniqueField, $value): void
54
+    {
55
+        $query = $record::where('company_id', $companyId)
56
+            ->where('id', '!=', $record->id)
57
+            ->where('enabled', true);
58
+
59
+        if($uniqueField){
60
+            $query->where($uniqueField, $value);
61
+        }
62
+
63
+        $query->update(['enabled' => false]);
64
+    }
65
+
66
+    protected function ensureAtLeastOneEnabled(int $companyId, Model $record, ?string $uniqueField, $value, bool &$enabled): void
67
+    {
68
+        $query = $record::where('company_id', $companyId)
69
+            ->where('id', '!=', $record->id)
70
+            ->where('enabled', true);
71
+
72
+        if($uniqueField){
73
+            $query->where($uniqueField, $value);
74
+        }
75
+
76
+        $enabledCount = $query->count();
77
+
78
+        if ($enabledCount === 0) {
79
+            $enabled = true;
80
+        }
81
+    }
82
+}

+ 6
- 6
composer.lock 查看文件

4379
         },
4379
         },
4380
         {
4380
         {
4381
             "name": "psy/psysh",
4381
             "name": "psy/psysh",
4382
-            "version": "v0.11.18",
4382
+            "version": "v0.11.19",
4383
             "source": {
4383
             "source": {
4384
                 "type": "git",
4384
                 "type": "git",
4385
                 "url": "https://github.com/bobthecow/psysh.git",
4385
                 "url": "https://github.com/bobthecow/psysh.git",
4386
-                "reference": "4f00ee9e236fa6a48f4560d1300b9c961a70a7ec"
4386
+                "reference": "1724ceff278daeeac5a006744633bacbb2dc4706"
4387
             },
4387
             },
4388
             "dist": {
4388
             "dist": {
4389
                 "type": "zip",
4389
                 "type": "zip",
4390
-                "url": "https://api.github.com/repos/bobthecow/psysh/zipball/4f00ee9e236fa6a48f4560d1300b9c961a70a7ec",
4391
-                "reference": "4f00ee9e236fa6a48f4560d1300b9c961a70a7ec",
4390
+                "url": "https://api.github.com/repos/bobthecow/psysh/zipball/1724ceff278daeeac5a006744633bacbb2dc4706",
4391
+                "reference": "1724ceff278daeeac5a006744633bacbb2dc4706",
4392
                 "shasum": ""
4392
                 "shasum": ""
4393
             },
4393
             },
4394
             "require": {
4394
             "require": {
4449
             ],
4449
             ],
4450
             "support": {
4450
             "support": {
4451
                 "issues": "https://github.com/bobthecow/psysh/issues",
4451
                 "issues": "https://github.com/bobthecow/psysh/issues",
4452
-                "source": "https://github.com/bobthecow/psysh/tree/v0.11.18"
4452
+                "source": "https://github.com/bobthecow/psysh/tree/v0.11.19"
4453
             },
4453
             },
4454
-            "time": "2023-05-23T02:31:11+00:00"
4454
+            "time": "2023-07-15T19:42:19+00:00"
4455
         },
4455
         },
4456
         {
4456
         {
4457
             "name": "ralouphie/getallheaders",
4457
             "name": "ralouphie/getallheaders",

+ 9
- 8
database/factories/CategoryFactory.php 查看文件

25
     public function definition(): array
25
     public function definition(): array
26
     {
26
     {
27
         return [
27
         return [
28
+            'name' => $this->faker->word,
29
+            'type' => $this->faker->randomElement(['income', 'expense']),
28
             'color' => $this->faker->hexColor,
30
             'color' => $this->faker->hexColor,
31
+            'enabled' => false,
29
         ];
32
         ];
30
     }
33
     }
31
 
34
 
32
     /**
35
     /**
33
      * Indicate that the category is of income type.
36
      * Indicate that the category is of income type.
34
-     *
35
-     * @return Factory<Category>
36
      */
37
      */
37
-    public function income(): Factory
38
+    public function incomeCategory(string $name): self
38
     {
39
     {
39
-        return $this->state(function (array $attributes) {
40
+        return $this->state(function (array $attributes) use ($name) {
40
             return [
41
             return [
42
+                'name' => $name,
41
                 'type' => 'income',
43
                 'type' => 'income',
42
             ];
44
             ];
43
         });
45
         });
45
 
47
 
46
     /**
48
     /**
47
      * Indicate that the category is of expense type.
49
      * Indicate that the category is of expense type.
48
-     *
49
-     * @return Factory<Category>
50
      */
50
      */
51
-    public function expense(): Factory
51
+    public function expenseCategory(string $name): self
52
     {
52
     {
53
-        return $this->state(function (array $attributes) {
53
+        return $this->state(function (array $attributes) use ($name) {
54
             return [
54
             return [
55
+                'name' => $name,
55
                 'type' => 'expense',
56
                 'type' => 'expense',
56
             ];
57
             ];
57
         });
58
         });

+ 1
- 1
database/seeders/DatabaseSeeder.php 查看文件

103
 
103
 
104
         $this->call([
104
         $this->call([
105
             //CurrencySeeder::class,
105
             //CurrencySeeder::class,
106
-            CategorySeeder::class,
106
+            //CategorySeeder::class,
107
             //TaxSeeder::class,
107
             //TaxSeeder::class,
108
             //DiscountSeeder::class,
108
             //DiscountSeeder::class,
109
         ]);
109
         ]);

Loading…
取消
儲存