feat(inventory): add calculated availability and movement ledger
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?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 $withinTransaction = false;
|
||||
|
||||
private const FORMULA = 'CAST(real_stock AS SIGNED) - CAST(sold_units AS SIGNED) - CAST(reserved_stock AS SIGNED) - CAST(entry_reserved_stock AS SIGNED) + CAST(refunded_units AS SIGNED)';
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
$conversion = 'CAST(real_stock AS SIGNED) + CAST(sold_units AS SIGNED) - CAST(refunded_units AS SIGNED)';
|
||||
$this->assertNonNegative($conversion, 'la conversión produciría un stock real negativo');
|
||||
$this->assertNonNegative(
|
||||
"({$conversion}) - CAST(sold_units AS SIGNED) - CAST(reserved_stock AS SIGNED) - CAST(entry_reserved_stock AS SIGNED) + CAST(refunded_units AS SIGNED)",
|
||||
'el stock disponible actual es negativo',
|
||||
);
|
||||
|
||||
if (DB::getDriverName() === 'sqlite') {
|
||||
Schema::table('inventories', function (Blueprint $table): void {
|
||||
$table->bigInteger('available_stock')->virtualAs(self::FORMULA);
|
||||
});
|
||||
DB::table('inventories')->update(['real_stock' => DB::raw($conversion)]);
|
||||
} else {
|
||||
Schema::table('inventories', function (Blueprint $table): void {
|
||||
$table->unsignedBigInteger('real_stock')->default(0)->change();
|
||||
$table->bigInteger('available_stock')->storedAs(self::FORMULA);
|
||||
});
|
||||
DB::table('inventories')->update(['real_stock' => DB::raw($conversion)]);
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$conversion = 'CAST(real_stock AS SIGNED) - CAST(sold_units AS SIGNED) + CAST(refunded_units AS SIGNED)';
|
||||
$this->assertNonNegative($conversion, 'el rollback produciría un stock real negativo');
|
||||
|
||||
if (DB::getDriverName() === 'sqlite') {
|
||||
DB::table('inventories')->update(['real_stock' => DB::raw($conversion)]);
|
||||
Schema::table('inventories', fn (Blueprint $table) => $table->dropColumn('available_stock'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('inventories')->update(['real_stock' => DB::raw($conversion)]);
|
||||
Schema::table('inventories', fn (Blueprint $table) => $table->dropColumn('available_stock'));
|
||||
}
|
||||
|
||||
private function assertNonNegative(string $expression, string $reason): void
|
||||
{
|
||||
if (DB::table('inventories')->whereRaw("({$expression}) < 0")->exists()) {
|
||||
throw new RuntimeException("Hay inventarios inconsistentes: {$reason}.");
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('inventory_movements', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('inventory_id')->constrained('inventories')->restrictOnDelete();
|
||||
$table->string('operation', 40);
|
||||
$table->bigInteger('available_stock_delta');
|
||||
$table->bigInteger('available_stock_before');
|
||||
$table->bigInteger('available_stock_after');
|
||||
$table->json('counter_deltas');
|
||||
$table->foreignId('responsible_user_id')->nullable()->constrained('users')->restrictOnDelete();
|
||||
$table->uuid('idempotency_key')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['inventory_id', 'idempotency_key']);
|
||||
$table->index(['inventory_id', 'created_at']);
|
||||
$table->index(['operation', 'created_at']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('inventory_movements');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user