Introduce spice_stock_movements ledger for traceable stock changes
Deploy / deploy (push) Successful in 16s

Every delivery and batch now writes a signed movement entry instead of
directly incrementing/decrementing spice.quantity. The balance is always
the sum of all movements. Existing data is backfilled in the migration.
Rohstoff edit page shows the full movement history (Lagerbewegungen tab).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Malek TellissiandClaude Sonnet 4.6 committed 2026-09-12 22:34:10 +02:00
1 parent 7545aed494
commit fb19e9a579
9 files changed
+179 -43

No files matched your search

@@ -0,0 +1,67 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('spice_stock_movements', function (Blueprint $table) {
$table->id();
$table->foreignId('spice_id')->constrained('spices')->cascadeOnDelete();
$table->decimal('amount', 10, 2); // positiv = Zugang, negativ = Abgang
$table->enum('source_type', ['delivery', 'batch']);
$table->unsignedBigInteger('source_id');
$table->string('description');
$table->timestamps();
$table->index(['spice_id', 'created_at']);
});
// Bestehende Lieferungen als Bewegungen einlesen
DB::table('spices_suppliers')->orderBy('id')->each(function ($row) {
DB::table('spice_stock_movements')->insert([
'spice_id' => $row->spice_id,
'amount' => $row->amount,
'source_type' => 'delivery',
'source_id' => $row->id,
'description' => 'Lieferung',
'created_at' => $row->created_at,
'updated_at' => $row->updated_at,
]);
});
// Bestehende Chargen als Bewegungen einlesen
DB::table('batches')->orderBy('id')->each(function ($batch) {
$spices = DB::table('recipes_spices')->where('recipe_id', $batch->recipe_id)->get();
foreach ($spices as $pivot) {
$grams = $batch->quantity_g * $pivot->amount_in_percent / 100;
DB::table('spice_stock_movements')->insert([
'spice_id' => $pivot->spice_id,
'amount' => -$grams,
'source_type' => 'batch',
'source_id' => $batch->id,
'description' => 'Charge ' . $batch->lot_number,
'created_at' => $batch->created_at,
'updated_at' => $batch->updated_at,
]);
}
});
// Bestand aller Rohstoffe aus den Bewegungen neu berechnen
DB::table('spices')->orderBy('id')->each(function ($spice) {
$balance = DB::table('spice_stock_movements')
->where('spice_id', $spice->id)
->sum('amount');
DB::table('spices')->where('id', $spice->id)->update(['quantity' => $balance]);
});
}
public function down(): void
{
Schema::dropIfExists('spice_stock_movements');
}
};