diff --git a/app/Models/ProductInco.php b/app/Models/ProductInco.php index e572d224..06b4ed8d 100644 --- a/app/Models/ProductInco.php +++ b/app/Models/ProductInco.php @@ -98,7 +98,7 @@ class ProductInco extends Model { public function getId(): IncoId { - return new IncoId($this->id); + return new IncoId($this->no_art); } /** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Product,$this> */ diff --git a/database/migrations/2026_11_02_111000_fix_inco_id_in_order_lines_product.php b/database/migrations/2026_11_02_111000_fix_inco_id_in_order_lines_product.php new file mode 100644 index 00000000..e339d605 --- /dev/null +++ b/database/migrations/2026_11_02_111000_fix_inco_id_in_order_lines_product.php @@ -0,0 +1,57 @@ +pluck('no_art', 'product_id') + ->all(); + + DB::table('order_lines') + ->whereNotNull('product') + ->orderBy('id') + ->chunk(500, function ($lines) use ($incoMap): void { + foreach ($lines as $line) { + $data = json_decode((string) $line->product, true); + if (! is_array($data) || ! isset($data['original_id'])) { + continue; + } + + $productId = (string) $data['original_id']; + if (! isset($incoMap[$productId])) { + continue; + } + + $expectedNoArt = (int) $incoMap[$productId]; + $currentIncoId = isset($data['inco_id']) ? (int) $data['inco_id'] : null; + + if ($currentIncoId !== $expectedNoArt) { + $data['inco_id'] = $expectedNoArt; + + DB::table('order_lines') + ->where('id', $line->id) + ->update([ + 'product' => json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES), + ]); + } + } + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // Data repair migration, no rollback. + } +}; diff --git a/tests/Integration/WithApp/App/Models/ProductIncoTest.php b/tests/Integration/WithApp/App/Models/ProductIncoTest.php new file mode 100644 index 00000000..585c7457 --- /dev/null +++ b/tests/Integration/WithApp/App/Models/ProductIncoTest.php @@ -0,0 +1,25 @@ +id = 258; + $inco->no_art = 140317; + + // WHEN + $incoId = $inco->getId(); + + // THEN + expect($incoId)->toBeInstanceOf(IncoId::class) + ->and($incoId->value)->toBe(140317) + ->and($incoId->value)->not->toBe(258); + }); +});