Add stock items, groups and movements at a moving average cost

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:18:59 +02:00
co-authored by Claude Sonnet 5.5
parent 361a2974cf
commit 30132deb0e
81 changed files with 4968 additions and 115 deletions
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Database\Factories;
use App\Modules\Inventory\Domain\Enums\StockUnit;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<StockItemModel>
*/
class StockItemFactory extends Factory
{
protected $model = StockItemModel::class;
/**
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'stock_item_group_id' => StockItemGroupModel::factory(),
'name' => fake()->unique()->words(3, true),
'unit' => StockUnit::Piece,
'code' => null,
'supplier' => null,
'reorder_level_milli' => null,
'average_cost_hundredths' => 0,
'is_active' => true,
];
}
public function inactive(): static
{
return $this->state(fn (): array => ['is_active' => false]);
}
}
@@ -0,0 +1,26 @@
<?php
namespace Database\Factories;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<StockItemGroupModel>
*/
class StockItemGroupFactory extends Factory
{
protected $model = StockItemGroupModel::class;
/**
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->unique()->words(2, true),
'sort_order' => fake()->unique()->numberBetween(1, 100000),
'is_active' => true,
];
}
}
@@ -0,0 +1,78 @@
<?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');
}
};