<?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
    {
        if (! Schema::hasTable('dberp_sales_orders')) {
            Schema::create('dberp_sales_orders', function (Blueprint $table) {
                $table->id();
                $table->string('code', 64)->comment('销售订单号');
                $table->foreignId('customer_id')->constrained('dberp_customers')->restrictOnDelete();
                $table->string('customer_contact_person_snapshot', 120)->nullable()->comment('客户联系人快照');
                $table->string('customer_mobile_snapshot', 32)->nullable()->comment('客户手机快照');
                $table->string('customer_phone_snapshot', 32)->nullable()->comment('客户电话快照');
                $table->string('customer_country_code_snapshot', 32)->nullable()->comment('客户国家快照');
                $table->string('customer_region_name_snapshot', 255)->nullable()->comment('客户地区路径快照');
                $table->string('customer_address_snapshot', 255)->nullable()->comment('客户详细地址快照');
                $table->foreignId('handler_employee_id')->nullable()->constrained('dberp_employees')->nullOnDelete();
                $table->string('source_type', 32)->default('manual')->comment('来源类型');
                $table->string('status', 32)->default('draft')->comment('订单状态');
                $table->dateTime('business_time')->comment('业务日期');
                $table->decimal('discount_rate', 8, 4)->default(0)->comment('折扣率承载位');
                $table->decimal('total_quantity', 18, 4)->default(0)->comment('订单总数量');
                $table->decimal('total_amount', 18, 4)->default(0)->comment('订单总金额');
                $table->decimal('discount_amount', 18, 4)->default(0)->comment('优惠金额');
                $table->decimal('final_amount', 18, 4)->default(0)->comment('成交金额');
                $table->decimal('receivable_amount', 18, 4)->default(0)->comment('应收金额');
                $table->decimal('received_amount', 18, 4)->default(0)->comment('已收金额');
                $table->decimal('unreceived_amount', 18, 4)->default(0)->comment('未收金额');
                $table->string('receivable_status', 32)->default('uncollected')->comment('应收状态');
                $table->dateTime('confirmed_at')->nullable()->comment('确认时间');
                $table->dateTime('closed_at')->nullable()->comment('关闭时间');
                $table->dateTime('voided_at')->nullable()->comment('作废时间');
                $table->text('remark')->nullable()->comment('备注');
                $table->string('source_platform', 32)->nullable();
                $table->string('external_order_id', 128)->nullable();
                $table->string('external_order_no', 128)->nullable();
                $table->dateTime('imported_at')->nullable();
                $table->dateTime('last_sync_attempt_at')->nullable();
                $table->timestamps();

                $table->unique('code', 'uq_dberp_sales_order_code');
                $table->unique(['source_platform', 'external_order_no'], 'uq_dberp_so_extordno');
                $table->index(['source_platform', 'external_order_id'], 'idx_dberp_so_extordid');
                $table->index(['customer_id', 'status', 'business_time'], 'idx_dso_customer_status_time');
                $table->index(['customer_id', 'receivable_status', 'business_time'], 'idx_dso_receivable_status_time');
                $table->index(['handler_employee_id', 'status'], 'idx_dso_handler_status');
                $table->index(['source_type', 'status'], 'idx_dso_source_status');
            });
        }

        if (! Schema::hasTable('dberp_sales_order_items')) {
            Schema::create('dberp_sales_order_items', function (Blueprint $table) {
                $table->id();
                $table->foreignId('order_id')->constrained('dberp_sales_orders')->cascadeOnDelete();
                $table->foreignId('product_id')->constrained('dberp_products')->restrictOnDelete();
                $table->decimal('quantity', 18, 4)->comment('订购数量');
                $table->decimal('unit_price', 18, 4)->default(0)->comment('销售单价');
                $table->decimal('line_amount', 18, 4)->default(0)->comment('行金额');
                $table->decimal('discount_amount', 18, 4)->default(0)->comment('优惠金额');
                $table->decimal('final_amount', 18, 4)->default(0)->comment('成交金额');
                $table->decimal('shipped_quantity', 18, 4)->default(0)->comment('累计出库数量');
                $table->decimal('returned_quantity', 18, 4)->default(0)->comment('累计退货数量');
                $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
                $table->text('remark')->nullable()->comment('备注');
                $table->string('external_line_id', 128)->nullable();
                $table->string('external_product_id', 128)->nullable();
                $table->string('external_sku', 128)->nullable();
                $table->timestamps();

                $table->index(['order_id', 'sort_order'], 'idx_dsoi_order_sort');
                $table->index(['product_id'], 'idx_dsoi_product');
                $table->index(['external_product_id'], 'idx_dberp_soi_extprodid');
            });
        }

        if (! Schema::hasTable('dberp_sales_outbound_orders')) {
            Schema::create('dberp_sales_outbound_orders', function (Blueprint $table) {
                $table->id();
                $table->string('code', 64)->comment('销售出库单号');
                $table->foreignId('sales_order_id');
                $table->foreign('sales_order_id', 'fk_dsout_order_sales')->references('id')->on('dberp_sales_orders')->restrictOnDelete();
                $table->foreignId('customer_id');
                $table->foreign('customer_id', 'fk_dsout_order_customer')->references('id')->on('dberp_customers')->restrictOnDelete();
                $table->string('customer_contact_person_snapshot', 120)->nullable()->comment('客户联系人快照');
                $table->string('customer_mobile_snapshot', 32)->nullable()->comment('客户手机快照');
                $table->string('customer_phone_snapshot', 32)->nullable()->comment('客户电话快照');
                $table->string('customer_country_code_snapshot', 32)->nullable()->comment('客户国家快照');
                $table->string('customer_region_name_snapshot', 255)->nullable()->comment('客户地区路径快照');
                $table->string('customer_address_snapshot', 255)->nullable()->comment('客户详细地址快照');
                $table->foreignId('warehouse_id');
                $table->foreign('warehouse_id', 'fk_dsout_order_wh')->references('id')->on('dberp_warehouses')->restrictOnDelete();
                $table->string('warehouse_contact_phone_snapshot', 32)->nullable()->comment('仓库联系电话快照');
                $table->string('warehouse_country_code_snapshot', 32)->nullable()->comment('仓库国家快照');
                $table->string('warehouse_region_name_snapshot', 255)->nullable()->comment('仓库地区路径快照');
                $table->string('warehouse_address_snapshot', 255)->nullable()->comment('仓库详细地址快照');
                $table->foreignId('handler_employee_id')->nullable();
                $table->foreign('handler_employee_id', 'fk_dsout_order_handler')->references('id')->on('dberp_employees')->nullOnDelete();
                $table->string('source_type', 32)->default('manual')->comment('来源类型');
                $table->string('status', 32)->default('draft')->comment('单据状态');
                $table->dateTime('business_time')->comment('业务日期');
                $table->decimal('total_shipped_quantity', 18, 4)->default(0)->comment('出库总数量');
                $table->decimal('total_amount', 18, 4)->default(0)->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_sales_outbound_code');
                $table->index(['sales_order_id', 'status', 'business_time'], 'idx_dsout_order_status_time');
                $table->index(['customer_id', 'status', 'business_time'], 'idx_dsout_customer_status_time');
                $table->index(['warehouse_id', 'status'], 'idx_dsout_wh_status');
                $table->index(['handler_employee_id', 'status'], 'idx_dsout_handler_status');
                $table->index(['source_type', 'status'], 'idx_dsout_source_status');
            });
        }

        if (! Schema::hasTable('dberp_sales_outbound_order_items')) {
            Schema::create('dberp_sales_outbound_order_items', function (Blueprint $table) {
                $table->id();
                $table->foreignId('order_id');
                $table->foreign('order_id', 'fk_dsout_item_order')->references('id')->on('dberp_sales_outbound_orders')->cascadeOnDelete();
                $table->foreignId('sales_order_item_id');
                $table->foreign('sales_order_item_id', 'fk_dsout_item_sales')->references('id')->on('dberp_sales_order_items')->restrictOnDelete();
                $table->foreignId('product_id');
                $table->foreign('product_id', 'fk_dsout_item_product')->references('id')->on('dberp_products')->restrictOnDelete();
                $table->decimal('ship_quantity', 18, 4)->default(0)->comment('出库数量');
                $table->decimal('unit_price', 18, 4)->default(0)->comment('销售单价');
                $table->decimal('line_amount', 18, 4)->default(0)->comment('行金额');
                $table->decimal('returned_quantity', 18, 4)->default(0)->comment('累计退货数量');
                $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
                $table->text('remark')->nullable()->comment('备注');
                $table->timestamps();

                $table->index(['order_id', 'sort_order'], 'idx_dsouti_order_sort');
                $table->index(['sales_order_item_id'], 'idx_dsouti_sales_item');
                $table->index(['product_id'], 'idx_dsouti_product');
            });
        }

        if (! Schema::hasTable('dberp_sales_return_orders')) {
            Schema::create('dberp_sales_return_orders', function (Blueprint $table) {
                $table->id();
                $table->string('code', 64)->comment('销售退货单号');
                $table->foreignId('sales_order_id')->nullable();
                $table->foreign('sales_order_id', 'fk_dsreturn_sales')->references('id')->on('dberp_sales_orders')->nullOnDelete();
                $table->foreignId('sales_outbound_order_id')->nullable();
                $table->foreign('sales_outbound_order_id', 'fk_dsreturn_outbound')->references('id')->on('dberp_sales_outbound_orders')->nullOnDelete();
                $table->foreignId('customer_id');
                $table->foreign('customer_id', 'fk_dsreturn_customer')->references('id')->on('dberp_customers')->restrictOnDelete();
                $table->foreignId('warehouse_id');
                $table->foreign('warehouse_id', 'fk_dsreturn_wh')->references('id')->on('dberp_warehouses')->restrictOnDelete();
                $table->foreignId('handler_employee_id')->nullable();
                $table->foreign('handler_employee_id', 'fk_dsreturn_handler')->references('id')->on('dberp_employees')->nullOnDelete();
                $table->string('source_type', 32)->default('manual')->comment('来源类型');
                $table->string('status', 32)->default('draft')->comment('单据状态');
                $table->dateTime('business_time')->comment('业务日期');
                $table->decimal('total_return_quantity', 18, 4)->default(0)->comment('退货总数量');
                $table->decimal('total_amount', 18, 4)->default(0)->comment('退货总金额');
                $table->string('reason', 255)->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_sales_return_code');
                $table->index(['sales_order_id', 'status', 'business_time'], 'idx_dsret_sales_status_time');
                $table->index(['sales_outbound_order_id', 'status'], 'idx_dsret_outbound_status');
                $table->index(['customer_id', 'status', 'business_time'], 'idx_dsret_customer_status_time');
                $table->index(['handler_employee_id', 'status'], 'idx_dsret_handler_status');
                $table->index(['source_type', 'status'], 'idx_dsret_source_status');
            });
        }

        if (! Schema::hasTable('dberp_sales_return_order_items')) {
            Schema::create('dberp_sales_return_order_items', function (Blueprint $table) {
                $table->id();
                $table->foreignId('order_id');
                $table->foreign('order_id', 'fk_dsret_item_order')->references('id')->on('dberp_sales_return_orders')->cascadeOnDelete();
                $table->foreignId('sales_order_item_id');
                $table->foreign('sales_order_item_id', 'fk_dsret_item_sales')->references('id')->on('dberp_sales_order_items')->restrictOnDelete();
                $table->foreignId('sales_outbound_item_id')->nullable();
                $table->foreign('sales_outbound_item_id', 'fk_dsret_item_outbound')->references('id')->on('dberp_sales_outbound_order_items')->nullOnDelete();
                $table->foreignId('product_id');
                $table->foreign('product_id', 'fk_dsret_item_product')->references('id')->on('dberp_products')->restrictOnDelete();
                $table->decimal('return_quantity', 18, 4)->default(0)->comment('退货数量');
                $table->decimal('unit_price', 18, 4)->default(0)->comment('退货单价');
                $table->decimal('return_amount', 18, 4)->default(0)->comment('退货金额');
                $table->unsignedInteger('sort_order')->default(0)->comment('越小越靠前');
                $table->text('remark')->nullable()->comment('备注');
                $table->timestamps();

                $table->index(['order_id', 'sort_order'], 'idx_dsreti_order_sort');
                $table->index(['sales_order_item_id'], 'idx_dsreti_sales_item');
                $table->index(['sales_outbound_item_id'], 'idx_dsreti_outbound_item');
                $table->index(['product_id'], 'idx_dsreti_product');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('dberp_sales_return_order_items');
        Schema::dropIfExists('dberp_sales_return_orders');
        Schema::dropIfExists('dberp_sales_outbound_order_items');
        Schema::dropIfExists('dberp_sales_outbound_orders');
        Schema::dropIfExists('dberp_sales_order_items');
        Schema::dropIfExists('dberp_sales_orders');
    }
};
