<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('good_receipt_makloon_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('good_receipt_makloon_id');
            $table->foreign('good_receipt_makloon_id')->references('id')->on('good_receipt_makloons')->onDelete('restrict');
            $table->unsignedBigInteger('prev_item_id');
            $table->foreign('prev_item_id')->references('id')->on('items')->onDelete('restrict');
            $table->unsignedBigInteger('item_id');
            $table->foreign('item_id')->references('id')->on('items')->onDelete('restrict');
            $table->decimal('qty', 25, 3);
            $table->unsignedBigInteger('unit');
            $table->foreign('unit')->references('id')->on('item_units')->onDelete('restrict');
            $table->unsignedBigInteger('warehouse_id');
            $table->decimal('price', 20, 2);
            $table->decimal('price_base', 20, 2)->nullable();
            $table->decimal('discount', 20, 2);
            $table->decimal('subtotal', 20, 2);
            $table->decimal('nominal_base', 20, 2)->nullable();
            $table->integer('number_row')->nullable();
            $table->decimal('base_qty', 20, 3);
            $table->string('base_unit');
            $table->unsignedBigInteger('department_id');
            $table->unsignedBigInteger('purchase_order_makloon_id')->nullable();
            $table->unsignedBigInteger('purchase_order_makloon_detail_id')->nullable();
            $table->unsignedBigInteger('purchase_requisition_makloon_detail_id')->nullable();
            $table->unsignedBigInteger('supplier_id');
            $table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('restrict');
            $table->text('description')->nullable();
            $table->decimal('qty_left', 25, 3);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->foreign('warehouse_id')->references('id')->on('warehouse')->onDelete('restrict');
            $table->foreign('department_id')->references('id')->on('department')->onDelete('restrict');
            $table->foreign('purchase_order_makloon_id')->references('id')->on('purchase_order_makloons')->onDelete('restrict');
            $table->foreign('purchase_order_makloon_detail_id')->references('id')->on('purchase_order_makloon_details')->onDelete('restrict');
            $table->foreign('purchase_requisition_makloon_detail_id')->references('id')->on('purchase_requisition_makloon_details')->onDelete('restrict');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('restrict');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('restrict');
            $table->foreign(['item_id','base_qty'])->references(['item_id','conversion'])->on('item_details')->onDelete('restrict');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('good_receipt_makloon_details');
    }
};
