Andrew Wallo 6 months ago
parent
commit
7eacd9e29e
1 changed files with 25 additions and 3 deletions
  1. 25
    3
      app/Filament/Forms/Components/CreateAdjustmentSelect.php

+ 25
- 3
app/Filament/Forms/Components/CreateAdjustmentSelect.php View File

18
 use Filament\Forms\Get;
18
 use Filament\Forms\Get;
19
 use Filament\Support\Enums\MaxWidth;
19
 use Filament\Support\Enums\MaxWidth;
20
 use Illuminate\Database\Eloquent\Builder;
20
 use Illuminate\Database\Eloquent\Builder;
21
+use Illuminate\Database\Eloquent\Model;
21
 use Illuminate\Support\Facades\DB;
22
 use Illuminate\Support\Facades\DB;
22
 
23
 
23
 class CreateAdjustmentSelect extends Select
24
 class CreateAdjustmentSelect extends Select
28
 
29
 
29
     protected bool $includeInactive = false;
30
     protected bool $includeInactive = false;
30
 
31
 
32
+    protected string $adjustmentsRelationship = 'adjustments';
33
+
31
     public function category(AdjustmentCategory $category): static
34
     public function category(AdjustmentCategory $category): static
32
     {
35
     {
33
         $this->category = $category;
36
         $this->category = $category;
49
         return $this;
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
     public function getCategory(): ?AdjustmentCategory
62
     public function getCategory(): ?AdjustmentCategory
53
     {
63
     {
54
         return $this->category;
64
         return $this->category;
64
         return $this->includeInactive;
74
         return $this->includeInactive;
65
     }
75
     }
66
 
76
 
77
+    public function getAdjustmentsRelationship(): string
78
+    {
79
+        return $this->adjustmentsRelationship;
80
+    }
81
+
67
     protected function setUp(): void
82
     protected function setUp(): void
68
     {
83
     {
69
         parent::setUp();
84
         parent::setUp();
75
             ->createOptionAction(fn (Action $action) => $this->createAdjustmentAction($action));
90
             ->createOptionAction(fn (Action $action) => $this->createAdjustmentAction($action));
76
 
91
 
77
         $this->relationship(
92
         $this->relationship(
78
-            name: 'adjustments',
93
+            name: $this->getAdjustmentsRelationship(),
79
             titleAttribute: 'name',
94
             titleAttribute: 'name',
80
-            modifyQueryUsing: function (Builder $query) {
95
+            modifyQueryUsing: function (Builder $query, ?Model $record) {
81
                 if ($this->getCategory()) {
96
                 if ($this->getCategory()) {
82
                     $query->where('category', $this->getCategory());
97
                     $query->where('category', $this->getCategory());
83
                 }
98
                 }
87
                 }
102
                 }
88
 
103
 
89
                 if (! $this->includesInactive()) {
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
                 return $query->orderBy('name');
115
                 return $query->orderBy('name');

Loading…
Cancel
Save