<?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('purchase_invoice_makloon_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('purchase_invoice_makloon_id');
            $table->unsignedBigInteger('prev_item_id');
            $table->unsignedBigInteger('item_id');
            $table->decimal('qty', 25, 3);
            $table->unsignedBigInteger('unit');
            $table->unsignedBigInteger('warehouse_id');
            $table->float('price');
            $table->decimal('price_base', 20, 2)->nullable();
            $table->float('disc_percent');
            $table->decimal('disc_nominal',20,2);
            $table->decimal('disc_header',20,2);
            $table->decimal('nominal',20,2);
            $table->decimal('nominal_base', 20, 2)->nullable();
            $table->decimal('base_qty',20,3);
            $table->decimal('add_tax_tariff_detail',20,2)->nullable();
            $table->decimal('add_tax_detail',20,2)->nullable();
            $table->string('base_unit');
            $table->string('description')->nullable();
            $table->unsignedBigInteger('purchase_order_makloon_id')->nullable();
            $table->unsignedBigInteger('good_receipt_makloon_id')->nullable();
            $table->unsignedBigInteger('good_receipt_makloon_detail_id')->nullable();
            $table->unsignedBigInteger('purchase_requisition_makloon_detail_id')->nullable();
            $table->decimal('qty_left',25,3);
            $table->decimal('base_qty_left',25,3);
            $table->unsignedBigInteger('grade_item_id')->nullable();
            $table->string('company_code')->unsigned();
            $table->unsignedBigInteger('department_id');
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->date('exp_date')->nullable();
            $table->decimal('currency_base', 20, 2)->nullable();
            $table->timestamps();

            $table->foreign('warehouse_id')->references('id')->on('warehouse')->onDelete('restrict');
            $table->foreign('purchase_invoice_makloon_id')->references('id')->on('purchase_invoice_makloons')->onDelete('restrict');
            $table->foreign('item_id')->references('id')->on('items')->onDelete('restrict');
            $table->foreign('prev_item_id')->references('id')->on('items')->onDelete('restrict');
            $table->foreign('grade_item_id')->references('id')->on('items')->onDelete('restrict');
            $table->foreign('unit')->references('id')->on('item_units')->onDelete('restrict');
            $table->foreign(['item_id','base_qty'])->references(['item_id','conversion'])->on('item_details')->onDelete('restrict');
            $table->foreign('purchase_order_makloon_id')->references('id')->on('purchase_order_makloons')->onDelete('restrict');
            $table->foreign('good_receipt_makloon_id')->references('id')->on('good_receipt_makloons')->onDelete('restrict');
            $table->foreign('good_receipt_makloon_detail_id')->references('id')->on('good_receipt_makloon_details')->onDelete('restrict');
            $table->foreign('department_id')->references('id')->on('department')->onDelete('restrict');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('restrict');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('restrict');
        });
    }

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