<?php

declare(strict_types=1);

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

namespace App\Business\Settlement;

use App\Models\DberpDictionaryType;
use App\Models\DberpSystemProfile;

final class SettlementRuleService
{
    public function profile(): DberpSystemProfile
    {
        return DberpSystemProfile::query()->firstOrCreate(
            ['id' => 1],
            DberpSystemProfile::defaultAttributes(),
        );
    }

    public function receiptOverLimitPolicy(): string
    {
        return (string) $this->profile()->receipt_over_limit_policy;
    }

    public function paymentOverLimitPolicy(): string
    {
        return (string) $this->profile()->payment_over_limit_policy;
    }

    public function allowReceiptOverLimit(): bool
    {
        return $this->receiptOverLimitPolicy() !== 'forbid';
    }

    public function allowPaymentOverLimit(): bool
    {
        return $this->paymentOverLimitPolicy() !== 'forbid';
    }

    public function allowSettlementOnClosedDocument(): bool
    {
        return (bool) $this->profile()->allow_settlement_on_closed_document;
    }

    public function settlementVoidPolicy(): string
    {
        return (string) $this->profile()->settlement_void_policy;
    }

    public function allowsSettlementVoid(): bool
    {
        return $this->settlementVoidPolicy() !== 'forbid';
    }

    public function autoRollbackSettlementVoid(): bool
    {
        return $this->settlementVoidPolicy() === 'rollback';
    }

    public function receiptMethodTypeId(): ?int
    {
        return DberpDictionaryType::query()->where('code', 'receipt_method')->value('id');
    }

    public function paymentMethodTypeId(): ?int
    {
        return DberpDictionaryType::query()->where('code', 'payment_method')->value('id');
    }
}
