<?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('stock_ledgers', function (Blueprint $table) {
            $table->id();
            $table->date('transaction_date');
            $table->foreignId('yarn_id')->constrained('yarns')->cascadeOnDelete();
            $table->string('lot_no')->nullable();
            $table->enum('type', ['purchase', 'sale', 'purchase_return', 'sale_return', 'adjustment'])->index();
            $table->string('reference_type')->nullable(); // purchase, sale etc
            $table->unsignedBigInteger('reference_id')->nullable();
            $table->string('voucher_no')->nullable();
            $table->decimal('in_qty', 15, 2)->default(0);
            $table->decimal('out_qty', 15, 2)->default(0);
            $table->decimal('balance_qty', 15, 2)->default(0);
            $table->decimal('rate', 15, 2)->default(0);
            $table->decimal('value', 15, 2)->default(0);
            $table->text('remarks')->nullable();
            $table->timestamps();
            $table->index(['transaction_date', 'yarn_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('stock_ledgers');
    }
};
