<?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_receipt_orders')) {
            Schema::create('dberp_receipt_orders', function (Blueprint $table): void {
                $table->id();
                $table->string('code', 64)->comment('收款单号');
                $table->foreignId('customer_id');
                $table->foreign('customer_id', 'fk_dberp_receipt_customer')->references('id')->on('dberp_customers')->restrictOnDelete();
                $table->foreignId('sales_order_id')->nullable();
                $table->foreign('sales_order_id', 'fk_dberp_receipt_sales')->references('id')->on('dberp_sales_orders')->nullOnDelete();
                $table->foreignId('payment_method_item_id')->nullable();
                $table->foreign('payment_method_item_id', 'fk_dberp_receipt_method')->references('id')->on('dberp_dictionary_items')->nullOnDelete();
                $table->foreignId('handler_employee_id')->nullable();
                $table->foreign('handler_employee_id', 'fk_dberp_receipt_handler')->references('id')->on('dberp_employees')->nullOnDelete();
                $table->string('receipt_type', 32)->default('standard')->comment('收款类型');
                $table->string('status', 32)->default('draft')->comment('单据状态');
                $table->string('write_off_status', 32)->default('unwritten')->comment('核销状态');
                $table->dateTime('business_time')->comment('收款日期');
                $table->dateTime('submitted_at')->nullable()->comment('提交时间');
                $table->decimal('amount', 18, 4)->default(0)->comment('收款金额');
                $table->decimal('written_off_amount', 18, 4)->default(0)->comment('已核销金额');
                $table->decimal('available_amount', 18, 4)->default(0)->comment('可用金额');
                $table->decimal('occupied_amount', 18, 4)->default(0)->comment('已占用金额');
                $table->text('attachment_paths')->nullable()->comment('附件或凭证基础位');
                $table->dateTime('effective_at')->nullable()->comment('生效时间');
                $table->dateTime('voided_at')->nullable()->comment('作废时间');
                $table->text('remark')->nullable()->comment('备注');
                $table->string('source_system', 32)->nullable()->comment('来源系统');
                $table->string('external_document_no', 128)->nullable()->comment('外部单号');
                $table->string('external_reference_id', 128)->nullable()->comment('外部关联 ID');
                $table->timestamps();

                $table->unique('code', 'uq_dberp_receipt_code');
                $table->index(['receipt_type', 'status', 'business_time'], 'idx_dberp_receipt_type_status');
                $table->index(['write_off_status', 'business_time'], 'idx_dberp_receipt_writeoff_status');
                $table->index(['customer_id', 'status', 'business_time'], 'idx_dberp_receipt_customer_status');
                $table->index(['sales_order_id', 'status', 'business_time'], 'idx_dberp_receipt_sales_status');
                $table->index(['handler_employee_id', 'status'], 'idx_dberp_receipt_handler_status');
            });
        }

        if (! Schema::hasTable('dberp_payment_orders')) {
            Schema::create('dberp_payment_orders', function (Blueprint $table): void {
                $table->id();
                $table->string('code', 64)->comment('付款单号');
                $table->foreignId('supplier_id');
                $table->foreign('supplier_id', 'fk_dberp_payment_supplier')->references('id')->on('dberp_suppliers')->restrictOnDelete();
                $table->foreignId('purchase_order_id')->nullable();
                $table->foreign('purchase_order_id', 'fk_dberp_payment_purchase')->references('id')->on('dberp_purchase_orders')->nullOnDelete();
                $table->foreignId('payment_method_item_id')->nullable();
                $table->foreign('payment_method_item_id', 'fk_dberp_payment_method')->references('id')->on('dberp_dictionary_items')->nullOnDelete();
                $table->foreignId('handler_employee_id')->nullable();
                $table->foreign('handler_employee_id', 'fk_dberp_payment_handler')->references('id')->on('dberp_employees')->nullOnDelete();
                $table->string('payment_type', 32)->default('standard')->comment('付款类型');
                $table->string('status', 32)->default('draft')->comment('单据状态');
                $table->string('write_off_status', 32)->default('unwritten')->comment('核销状态');
                $table->dateTime('business_time')->comment('付款日期');
                $table->dateTime('submitted_at')->nullable()->comment('提交时间');
                $table->decimal('amount', 18, 4)->default(0)->comment('付款金额');
                $table->decimal('written_off_amount', 18, 4)->default(0)->comment('已核销金额');
                $table->decimal('available_amount', 18, 4)->default(0)->comment('可用金额');
                $table->decimal('occupied_amount', 18, 4)->default(0)->comment('已占用金额');
                $table->text('attachment_paths')->nullable()->comment('附件或凭证基础位');
                $table->dateTime('effective_at')->nullable()->comment('生效时间');
                $table->dateTime('voided_at')->nullable()->comment('作废时间');
                $table->text('remark')->nullable()->comment('备注');
                $table->string('source_system', 32)->nullable()->comment('来源系统');
                $table->string('external_document_no', 128)->nullable()->comment('外部单号');
                $table->string('external_reference_id', 128)->nullable()->comment('外部关联 ID');
                $table->timestamps();

                $table->unique('code', 'uq_dberp_payment_code');
                $table->index(['payment_type', 'status', 'business_time'], 'idx_dberp_payment_type_status');
                $table->index(['write_off_status', 'business_time'], 'idx_dberp_payment_writeoff_status');
                $table->index(['supplier_id', 'status', 'business_time'], 'idx_dberp_payment_supplier_status');
                $table->index(['purchase_order_id', 'status', 'business_time'], 'idx_dberp_payment_purchase_status');
                $table->index(['handler_employee_id', 'status'], 'idx_dberp_payment_handler_status');
            });
        }

        if (! Schema::hasTable('dberp_settlement_entries')) {
            Schema::create('dberp_settlement_entries', function (Blueprint $table): void {
                $table->id();
                $table->string('counterparty_type', 16)->comment('往来对象类型');
                $table->unsignedBigInteger('counterparty_id')->comment('往来对象');
                $table->string('settlement_side', 16)->comment('应收/应付');
                $table->string('change_type', 32)->comment('变动类型');
                $table->string('source_type', 191)->nullable()->comment('来源模型');
                $table->unsignedBigInteger('source_id')->nullable()->comment('来源主键');
                $table->string('document_number', 64)->nullable()->comment('来源单号');
                $table->decimal('change_amount', 18, 4)->default(0)->comment('本次变动金额');
                $table->decimal('balance_after', 18, 4)->default(0)->comment('变动后欠款余额');
                $table->dateTime('business_time')->comment('业务时间');
                $table->text('remark')->nullable()->comment('备注');
                $table->timestamps();

                $table->index(['counterparty_type', 'counterparty_id', 'business_time'], 'idx_dberp_settle_counterparty_time');
                $table->index(['settlement_side', 'change_type', 'business_time'], 'idx_dberp_settle_side_change_time');
                $table->index(['source_type', 'source_id'], 'idx_dberp_settle_source');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('dberp_settlement_entries');
        Schema::dropIfExists('dberp_payment_orders');
        Schema::dropIfExists('dberp_receipt_orders');
    }
};
