<?php

declare(strict_types=1);

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

namespace App\Models;

use App\Business\Settlement\Enums\SettlementDocumentStatus;
use App\Casts\DberpAttachmentPathsCast;
use App\Models\Concerns\GeneratesDberpCode;
use App\Models\Concerns\HasDberpAuditTrail;
use App\Models\Concerns\HasOptionalLightApprovalRelations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Validation\ValidationException;

class DberpReceiptOrder extends Model
{
    use GeneratesDberpCode;
    use HasDberpAuditTrail;
    use HasOptionalLightApprovalRelations;

    protected $table = 'dberp_receipt_orders';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'code',
        'customer_id',
        'receipt_type',
        'sales_order_id',
        'payment_method_item_id',
        'handler_employee_id',
        'status',
        'write_off_status',
        'business_time',
        'submitted_at',
        'amount',
        'written_off_amount',
        'available_amount',
        'occupied_amount',
        'attachment_paths',
        'effective_at',
        'voided_at',
        'remark',
        'source_system',
        'external_document_no',
        'external_reference_id',
    ];

    protected static function booted(): void
    {
        static::deleting(function (self $order): void {
            if ($order->status !== SettlementDocumentStatus::Draft->value) {
                throw ValidationException::withMessages([
                    'status' => __('dberp.errors.settlement_delete_only_draft'),
                ]);
            }
        });
    }

    protected function casts(): array
    {
        return [
            'attachment_paths' => DberpAttachmentPathsCast::class,
            'customer_id' => 'integer',
            'receipt_type' => 'string',
            'sales_order_id' => 'integer',
            'payment_method_item_id' => 'integer',
            'handler_employee_id' => 'integer',
            'business_time' => 'datetime',
            'submitted_at' => 'datetime',
            'amount' => 'decimal:4',
            'written_off_amount' => 'decimal:4',
            'available_amount' => 'decimal:4',
            'occupied_amount' => 'decimal:4',
            'effective_at' => 'datetime',
            'voided_at' => 'datetime',
        ];
    }

    public function getDberpNumberScope(): string
    {
        return 'receipt';
    }

    public function getDberpAuditActionPrefix(): string
    {
        return 'dberp.receipt_order';
    }

    /**
     * @return list<string>
     */
    public function getDberpAuditFields(): array
    {
        return $this->getFillable();
    }

    public function customer(): BelongsTo
    {
        return $this->belongsTo(DberpCustomer::class, 'customer_id');
    }

    public function salesOrder(): BelongsTo
    {
        return $this->belongsTo(DberpSalesOrder::class, 'sales_order_id');
    }

    public function paymentMethodItem(): BelongsTo
    {
        return $this->belongsTo(DberpDictionaryItem::class, 'payment_method_item_id');
    }

    public function handlerEmployee(): BelongsTo
    {
        return $this->belongsTo(DberpEmployee::class, 'handler_employee_id');
    }

    public function writeOffLines(): HasMany
    {
        return $this->hasMany(DberpSettlementWriteOffLine::class, 'receipt_order_id');
    }

    public function advanceFlows(): HasMany
    {
        return $this->hasMany(DberpAdvanceAccountFlow::class, 'receipt_order_id');
    }

    protected function lightApprovalDocumentType(): string
    {
        return 'receipt';
    }
}
