Andrew Wallo 6 月之前
父節點
當前提交
7eacd9e29e
共有 1 個檔案被更改,包括 25 行新增3 行删除
  1. 25
    3
      app/Filament/Forms/Components/CreateAdjustmentSelect.php

+ 25
- 3
app/Filament/Forms/Components/CreateAdjustmentSelect.php 查看文件

@@ -18,6 +18,7 @@ use Filament\Forms\Components\TextInput;
18 18
 use Filament\Forms\Get;
19 19
 use Filament\Support\Enums\MaxWidth;
20 20
 use Illuminate\Database\Eloquent\Builder;
21
+use Illuminate\Database\Eloquent\Model;
21 22
 use Illuminate\Support\Facades\DB;
22 23
 
23 24
 class CreateAdjustmentSelect extends Select
@@ -28,6 +29,8 @@ class CreateAdjustmentSelect extends Select
28 29
 
29 30
     protected bool $includeInactive = false;
30 31
 
32
+    protected string $adjustmentsRelationship = 'adjustments';
33
+
31 34
     public function category(AdjustmentCategory $category): static
32 35
     {
33 36
         $this->category = $category;
@@ -49,6 +52,13 @@ class CreateAdjustmentSelect extends Select
49 52
         return $this;
50 53
     }
51 54
 
55
+    public function adjustmentsRelationship(string $relationship): static
56
+    {
57
+        $this->adjustmentsRelationship = $relationship;
58
+
59
+        return $this;
60
+    }
61
+
52 62
     public function getCategory(): ?AdjustmentCategory
53 63
     {
54 64
         return $this->category;
@@ -64,6 +74,11 @@ class CreateAdjustmentSelect extends Select
64 74
         return $this->includeInactive;
65 75
     }
66 76
 
77
+    public function getAdjustmentsRelationship(): string
78
+    {
79
+        return $this->adjustmentsRelationship;
80
+    }
81
+
67 82
     protected function setUp(): void
68 83
     {
69 84
         parent::setUp();
@@ -75,9 +90,9 @@ class CreateAdjustmentSelect extends Select
75 90
             ->createOptionAction(fn (Action $action) => $this->createAdjustmentAction($action));
76 91
 
77 92
         $this->relationship(
78
-            name: 'adjustments',
93
+            name: $this->getAdjustmentsRelationship(),
79 94
             titleAttribute: 'name',
80
-            modifyQueryUsing: function (Builder $query) {
95
+            modifyQueryUsing: function (Builder $query, ?Model $record) {
81 96
                 if ($this->getCategory()) {
82 97
                     $query->where('category', $this->getCategory());
83 98
                 }
@@ -87,7 +102,14 @@ class CreateAdjustmentSelect extends Select
87 102
                 }
88 103
 
89 104
                 if (! $this->includesInactive()) {
90
-                    $query->where('status', AdjustmentStatus::Active);
105
+                    $existingAdjustmentIds = $record?->{$this->getAdjustmentsRelationship()}()
106
+                        ->pluck('adjustments.id')
107
+                        ->toArray() ?? [];
108
+
109
+                    $query->where(function ($query) use ($existingAdjustmentIds) {
110
+                        $query->where('status', AdjustmentStatus::Active)
111
+                            ->orWhereIn('adjustments.id', $existingAdjustmentIds);
112
+                    });
91 113
                 }
92 114
 
93 115
                 return $query->orderBy('name');

Loading…
取消
儲存