Count stock and correct the differences on confirmation

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:34:08 +02:00
co-authored by Claude Sonnet 5.5
parent bca1eed166
commit 0de0e332b2
27 changed files with 1568 additions and 0 deletions
@@ -0,0 +1,62 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Stock counts (inventaario, spec 19.3). A count covers the whole stock
* or the chosen item groups; a line keeps the counted quantity and the
* book balance at the moment of counting, so use recorded during the
* count does not distort the difference. Confirming stores the average
* cost of each line and the stock value before and after.
*/
public function up(): void
{
Schema::create('inventory_counts', function (Blueprint $table) {
$table->id();
$table->date('counted_on');
$table->string('status', 20);
$table->json('group_ids')->nullable();
$table->foreignId('started_by')->constrained('users')->restrictOnDelete();
$table->foreignId('confirmed_by')->nullable()->constrained('users')->restrictOnDelete();
$table->timestamp('confirmed_at')->nullable();
$table->bigInteger('value_before_cents')->nullable();
$table->bigInteger('value_after_cents')->nullable();
$table->timestamps();
});
Schema::create('inventory_count_lines', function (Blueprint $table) {
$table->id();
$table->foreignId('inventory_count_id')->constrained()->cascadeOnDelete();
$table->foreignId('stock_item_id')->constrained()->restrictOnDelete();
$table->bigInteger('book_quantity_milli');
$table->bigInteger('counted_quantity_milli');
$table->bigInteger('unit_cost_hundredths')->nullable();
$table->foreignId('counted_by')->constrained('users')->restrictOnDelete();
$table->timestamp('counted_at');
$table->timestamps();
$table->unique(['inventory_count_id', 'stock_item_id']);
});
Schema::table('stock_movements', function (Blueprint $table) {
$table->foreign('inventory_count_id', 'stock_movements_count_foreign')->references('id')->on('inventory_counts')->restrictOnDelete();
});
}
public function down(): void
{
// SQLite (tests) cannot drop a foreign key; there the table is dropped by the earlier migration's rollback.
if (Schema::getConnection()->getDriverName() !== 'sqlite') {
Schema::table('stock_movements', function (Blueprint $table) {
$table->dropForeign('stock_movements_count_foreign');
});
}
Schema::dropIfExists('inventory_count_lines');
Schema::dropIfExists('inventory_counts');
}
};