Make sorting of invoice items reliable.
Some checks failed
Build project image / Build-and-release-image (push) Failing after 3m1s

This commit is contained in:
2025-08-02 12:12:57 +02:00
parent e1f579c7fd
commit 0a4089fe14
6 changed files with 33 additions and 2 deletions

View File

@@ -32,7 +32,7 @@ class InvoiceitemController extends Controller
'name' => 'required|string',
'description' => 'nullable|string',
'article_number' => 'nullable|string',
'sort' => 'required|numeric|min:0',
]);
$item = $invoice->items()->create($itemData);

View File

@@ -104,7 +104,7 @@ class Invoice extends Model
*/
public function items(): HasMany
{
return $this->hasMany(Invoiceitem::class);
return $this->hasMany(Invoiceitem::class)->orderBy('sort');
}
/**

View File

@@ -22,6 +22,7 @@ class Invoiceitem extends Model
'total',
'name',
'description',
'sort'
];
}

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('invoiceitems', function (Blueprint $table) {
$table->integer('sort')->default(0)->after('invoice_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('invoiceitems', function (Blueprint $table) {
$table->dropColumn('sort');
});
}
};

View File

@@ -655,6 +655,7 @@
for (let i = 0; i < self.items.length; i++) {
let pos = sort_flipped[i];
let item = self.items[pos];
item.sort = i;
const result = axios.post('invoice/' + self.invoice_id + '/item', item)
.catch(function (error) {
self.error = true;

View File

@@ -662,6 +662,7 @@
for (let i = 0; i < self.items.length; i++) {
let pos = sort_flipped[i];
let item = self.items[pos];
item.sort = i;
const result = axios.post('invoice/' + self.invoice.id + '/item', item)
.catch(function (error) {
self.error = true;