浏览代码

add reorderable and clonable line items

3.x
Andrew Wallo 5 个月前
父节点
当前提交
ce7e7469e3

+ 2
- 1
app/Concerns/ManagesLineItems.php 查看文件

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

+ 1
- 0
app/Filament/Company/Resources/Sales/InvoiceResource.php 查看文件

195
                             ->saveRelationshipsUsing(null)
195
                             ->saveRelationshipsUsing(null)
196
                             ->dehydrated(true)
196
                             ->dehydrated(true)
197
                             ->reorderable()
197
                             ->reorderable()
198
+                            ->orderColumn('line_number')
198
                             ->reorderAtStart()
199
                             ->reorderAtStart()
199
                             ->cloneable()
200
                             ->cloneable()
200
                             ->addActionLabel('Add an item')
201
                             ->addActionLabel('Add an item')

+ 54
- 0
app/Filament/Forms/Components/CustomTableRepeater.php 查看文件

12
 
12
 
13
     protected bool | Closure $reorderAtStart = false;
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
     public function spreadsheet(bool | Closure $condition = true): static
27
     public function spreadsheet(bool | Closure $condition = true): static
16
     {
28
     {
17
         $this->spreadsheet = $condition;
29
         $this->spreadsheet = $condition;
36
         return $this->evaluate($this->reorderAtStart) && $this->isReorderable();
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
     protected function setUp(): void
69
     protected function setUp(): void
40
     {
70
     {
41
         parent::setUp();
71
         parent::setUp();
59
 
89
 
60
             return $action;
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
     public function getView(): string
118
     public function getView(): string

+ 1
- 1
app/Models/Accounting/Document.php 查看文件

27
 
27
 
28
     public function lineItems(): MorphMany
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
     public function hasLineItems(): bool
33
     public function hasLineItems(): bool

+ 1
- 0
app/Models/Accounting/DocumentLineItem.php 查看文件

36
         'unit_price',
36
         'unit_price',
37
         'tax_total',
37
         'tax_total',
38
         'discount_total',
38
         'discount_total',
39
+        'line_number',
39
         'created_by',
40
         'created_by',
40
         'updated_by',
41
         'updated_by',
41
     ];
42
     ];

+ 28
- 0
database/migrations/2025_05_05_211551_add_sort_order_to_document_line_items.php 查看文件

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
+};

正在加载...
取消
保存