<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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('dberp_transfer_orders', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('调拨单号');
            $table->foreignId('from_warehouse_id')->constrained('dberp_warehouses')->restrictOnDelete();
            $table->string('from_warehouse_contact_phone_snapshot', 32)->nullable()->comment('调出仓联系电话快照');
            $table->string('from_warehouse_country_code_snapshot', 32)->nullable()->comment('调出仓国家快照');
            $table->string('from_warehouse_region_name_snapshot', 255)->nullable()->comment('调出仓地区路径快照');
            $table->string('from_warehouse_address_snapshot', 255)->nullable()->comment('调出仓详细地址快照');
            $table->foreignId('to_warehouse_id')->constrained('dberp_warehouses')->restrictOnDelete();
            $table->string('to_warehouse_contact_phone_snapshot', 32)->nullable()->comment('调入仓联系电话快照');
            $table->string('to_warehouse_country_code_snapshot', 32)->nullable()->comment('调入仓国家快照');
            $table->string('to_warehouse_region_name_snapshot', 255)->nullable()->comment('调入仓地区路径快照');
            $table->string('to_warehouse_address_snapshot', 255)->nullable()->comment('调入仓详细地址快照');
            $table->string('reason', 255)->comment('调拨原因');
            $table->string('status', 32)->default('draft')->comment('单据状态');
            $table->dateTime('business_time')->comment('业务时间');
            $table->dateTime('effective_at')->nullable()->comment('生效时间');
            $table->dateTime('completed_at')->nullable()->comment('完成时间');
            $table->dateTime('voided_at')->nullable()->comment('作废时间');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_transfer_order_code');
            $table->index(['from_warehouse_id', 'status', 'business_time'], 'idx_dberp_transfer_from_status_time');
            $table->index(['to_warehouse_id', 'status', 'business_time'], 'idx_dberp_transfer_to_status_time');
        });

        Schema::create('dberp_transfer_order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained('dberp_transfer_orders')->cascadeOnDelete();
            $table->foreignId('product_id')->constrained('dberp_products')->restrictOnDelete();
            $table->decimal('quantity', 18, 4)->comment('调拨数量');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->index(['order_id', 'sort_order'], 'idx_dberp_transfer_item_order_sort');
            $table->index(['product_id'], 'idx_dberp_transfer_item_product');
        });

        Schema::create('dberp_stocktake_orders', function (Blueprint $table) {
            $table->id();
            $table->string('code', 64)->comment('盘点单号');
            $table->foreignId('warehouse_id')->constrained('dberp_warehouses')->restrictOnDelete();
            $table->string('scope_type', 32)->default('all')->comment('盘点范围');
            $table->foreignId('scope_category_id')->nullable()->constrained('dberp_product_categories')->nullOnDelete();
            $table->foreignId('scope_product_id')->nullable()->constrained('dberp_products')->nullOnDelete();
            $table->foreignId('stocktake_employee_id')->nullable()->constrained('dberp_employees')->nullOnDelete();
            $table->string('status', 32)->default('draft')->comment('单据状态');
            $table->dateTime('business_time')->comment('盘点时间');
            $table->dateTime('effective_at')->nullable()->comment('生效时间');
            $table->dateTime('voided_at')->nullable()->comment('作废时间');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_stocktake_order_code');
            $table->index(['warehouse_id', 'status', 'business_time'], 'idx_dberp_stocktake_wh_status_time');
            $table->index(['scope_type', 'scope_category_id'], 'idx_dberp_stocktake_scope_cat');
            $table->index(['scope_type', 'scope_product_id'], 'idx_dberp_stocktake_scope_prod');
        });

        Schema::create('dberp_stocktake_order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained('dberp_stocktake_orders')->cascadeOnDelete();
            $table->foreignId('product_id')->constrained('dberp_products')->restrictOnDelete();
            $table->decimal('book_quantity', 18, 4)->default(0)->comment('账面数量');
            $table->decimal('actual_quantity', 18, 4)->default(0)->comment('实盘数量');
            $table->decimal('difference_quantity', 18, 4)->default(0)->comment('差异数量');
            $table->string('result_type', 32)->default('balanced')->comment('盘点结果');
            $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->index(['order_id', 'sort_order'], 'idx_dberp_stocktake_item_order_sort');
            $table->index(['product_id', 'result_type'], 'idx_dberp_stocktake_item_prod_result');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('dberp_stocktake_order_items');
        Schema::dropIfExists('dberp_stocktake_orders');
        Schema::dropIfExists('dberp_transfer_order_items');
        Schema::dropIfExists('dberp_transfer_orders');
    }
};
