Преглед изворни кода

add reorderable and clonable line items

3.x
Andrew Wallo пре 5 месеци
родитељ
комит
ce7e7469e3

+ 2
- 1
app/Concerns/ManagesLineItems.php Прегледај датотеку

@@ -16,7 +16,7 @@ trait ManagesLineItems
16 16
 {
17 17
     protected function handleLineItems(Model $record, Collection $lineItems): void
18 18
     {
19
-        foreach ($lineItems as $itemData) {
19
+        foreach ($lineItems as $index => $itemData) {
20 20
             $lineItem = isset($itemData['id'])
21 21
                 ? $record->lineItems->find($itemData['id'])
22 22
                 : $record->lineItems()->make();
@@ -26,6 +26,7 @@ trait ManagesLineItems
26 26
                 'description' => $itemData['description'],
27 27
                 'quantity' => $itemData['quantity'],
28 28
                 'unit_price' => $itemData['unit_price'],
29
+                'line_number' => $index + 1,
29 30
             ]);
30 31
 
31 32
             if (! $lineItem->exists) {

+ 1
- 0
app/Filament/Company/Resources/Sales/InvoiceResource.php Прегледај датотеку

@@ -195,6 +195,7 @@ class InvoiceResource extends Resource
195 195
                             ->saveRelationshipsUsing(null)
196 196
                             ->dehydrated(true)
197 197
                             ->reorderable()
198
+                            ->orderColumn('line_number')
198 199
                             ->reorderAtStart()
199 200
                             ->cloneable()
200 201
                             ->addActionLabel('Add an item')

+ 54
- 0
app/Filament/Forms/Components/CustomTableRepeater.php Прегледај датотеку

@@ -12,6 +12,18 @@ class CustomTableRepeater extends TableRepeater
12 12
 
13 13
     protected bool | Closure $reorderAtStart = false;
14 14
 
15
+    /**
16
+     * @var array<string> | Closure | null
17
+     */
18
+    protected array | Closure | null $excludedAttributesForCloning = [
19
+        'id',
20
+        'line_number',
21
+        'created_by',
22
+        'updated_by',
23
+        'created_at',
24
+        'updated_at',
25
+    ];
26
+
15 27
     public function spreadsheet(bool | Closure $condition = true): static
16 28
     {
17 29
         $this->spreadsheet = $condition;
@@ -36,6 +48,24 @@ class CustomTableRepeater extends TableRepeater
36 48
         return $this->evaluate($this->reorderAtStart) && $this->isReorderable();
37 49
     }
38 50
 
51
+    /**
52
+     * @param  array<string> | Closure | null  $attributes
53
+     */
54
+    public function excludeAttributesForCloning(array | Closure | null $attributes): static
55
+    {
56
+        $this->excludedAttributesForCloning = $attributes;
57
+
58
+        return $this;
59
+    }
60
+
61
+    /**
62
+     * @return array<string> | null
63
+     */
64
+    public function getExcludedAttributesForCloning(): ?array
65
+    {
66
+        return $this->evaluate($this->excludedAttributesForCloning);
67
+    }
68
+
39 69
     protected function setUp(): void
40 70
     {
41 71
         parent::setUp();
@@ -59,6 +89,30 @@ class CustomTableRepeater extends TableRepeater
59 89
 
60 90
             return $action;
61 91
         });
92
+
93
+        $this->cloneAction(function (Action $action) {
94
+            return $action
95
+                ->action(function (array $arguments, CustomTableRepeater $component): void {
96
+                    $newUuid = $component->generateUuid();
97
+                    $items = $component->getState();
98
+
99
+                    $clone = $items[$arguments['item']];
100
+
101
+                    foreach ($component->getExcludedAttributesForCloning() as $attribute) {
102
+                        unset($clone[$attribute]);
103
+                    }
104
+
105
+                    if ($newUuid) {
106
+                        $items[$newUuid] = $clone;
107
+                    } else {
108
+                        $items[] = $clone;
109
+                    }
110
+
111
+                    $component->state($items);
112
+                    $component->collapsed(false, shouldMakeComponentCollapsible: false);
113
+                    $component->callAfterStateUpdated();
114
+                });
115
+        });
62 116
     }
63 117
 
64 118
     public function getView(): string

+ 1
- 1
app/Models/Accounting/Document.php Прегледај датотеку

@@ -27,7 +27,7 @@ abstract class Document extends Model
27 27
 
28 28
     public function lineItems(): MorphMany
29 29
     {
30
-        return $this->morphMany(DocumentLineItem::class, 'documentable');
30
+        return $this->morphMany(DocumentLineItem::class, 'documentable')->orderBy('line_number');
31 31
     }
32 32
 
33 33
     public function hasLineItems(): bool

+ 1
- 0
app/Models/Accounting/DocumentLineItem.php Прегледај датотеку

@@ -36,6 +36,7 @@ class DocumentLineItem extends Model
36 36
         'unit_price',
37 37
         'tax_total',
38 38
         'discount_total',
39
+        'line_number',
39 40
         'created_by',
40 41
         'updated_by',
41 42
     ];

+ 28
- 0
database/migrations/2025_05_05_211551_add_sort_order_to_document_line_items.php Прегледај датотеку

@@ -0,0 +1,28 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    /**
10
+     * Run the migrations.
11
+     */
12
+    public function up(): void
13
+    {
14
+        Schema::table('document_line_items', function (Blueprint $table) {
15
+            $table->integer('line_number')->nullable()->after('documentable_id');
16
+        });
17
+    }
18
+
19
+    /**
20
+     * Reverse the migrations.
21
+     */
22
+    public function down(): void
23
+    {
24
+        Schema::table('document_line_items', function (Blueprint $table) {
25
+            $table->dropColumn('line_number');
26
+        });
27
+    }
28
+};

Loading…
Откажи
Сачувај