Files
Toiminnanohjaus/database/migrations/2026_11_26_140647_create_stock_tables.php
T
2026-11-26 16:18:59 +02:00

79 lines
3.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Stock accounting (spec 19.1–19.2): item groups, items with their
* moving average cost, and movements, whose sum is an item's balance.
* Quantities are in thousandths of the unit, unit costs in hundredths
* of a cent and values in cents. The links to machines, quick sale
* order lines, maintenance records and stock counts get their foreign
* keys with those tables.
*/
public function up(): void
{
Schema::create('stock_item_groups', function (Blueprint $table) {
$table->id();
$table->string('name', 150);
$table->unsignedInteger('sort_order');
$table->boolean('is_active')->default(true);
$table->timestamps();
});
Schema::create('stock_items', function (Blueprint $table) {
$table->id();
$table->foreignId('stock_item_group_id')->constrained()->restrictOnDelete();
$table->string('name', 150);
$table->string('unit', 20);
$table->string('code', 150)->nullable();
$table->string('supplier', 150)->nullable();
$table->bigInteger('reorder_level_milli')->nullable();
$table->bigInteger('average_cost_hundredths')->default(0);
$table->string('image_path')->nullable();
$table->string('thumbnail_path')->nullable();
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->index('code');
});
Schema::create('stock_movements', function (Blueprint $table) {
$table->id();
$table->foreignId('stock_item_id')->constrained()->restrictOnDelete();
$table->string('kind', 20);
$table->bigInteger('quantity_milli');
$table->bigInteger('unit_cost_hundredths');
$table->bigInteger('value_cents');
$table->date('occurred_on');
$table->timestamp('recorded_at');
$table->foreignId('user_id')->constrained()->restrictOnDelete();
$table->foreignId('task_id')->nullable()->constrained()->restrictOnDelete();
$table->foreignId('customer_job_id')->nullable()->constrained()->restrictOnDelete();
$table->unsignedBigInteger('machine_id')->nullable();
$table->unsignedBigInteger('quick_sale_order_line_id')->nullable();
$table->unsignedBigInteger('maintenance_record_id')->nullable();
$table->unsignedBigInteger('inventory_count_id')->nullable();
$table->foreignId('reverses_movement_id')->nullable()->unique()->constrained('stock_movements')->restrictOnDelete();
$table->string('reason')->nullable();
$table->string('supplier')->nullable();
$table->string('document_number')->nullable();
$table->timestamps();
$table->index(['stock_item_id', 'id']);
$table->index(['occurred_on', 'kind']);
$table->index('machine_id');
});
}
public function down(): void
{
Schema::dropIfExists('stock_movements');
Schema::dropIfExists('stock_items');
Schema::dropIfExists('stock_item_groups');
}
};