<?php

declare(strict_types=1);

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

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DberpInventoryTransaction extends Model
{
    protected $table = 'dberp_inventory_transactions';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'product_id',
        'product_sku_id',
        'product_code',
        'product_name',
        'sku_code',
        'spec_text',
        'unit_id',
        'unit_name',
        'warehouse_id',
        'direction',
        'change_quantity',
        'before_quantity',
        'after_quantity',
        'business_type',
        'business_number',
        'source_type',
        'source_id',
        'operator_user_id',
        'business_time',
        'remark',
        'idempotency_key',
        'document_input_quantity',
        'document_input_unit_id',
        'document_input_unit_name',
        'posting_base_unit_id',
        'posting_auxiliary_unit_id',
        'posting_is_auxiliary_unit',
        'posting_auxiliary_unit_quantity',
        'posting_base_unit_quantity',
    ];

    protected function casts(): array
    {
        return [
            'product_id' => 'integer',
            'product_sku_id' => 'integer',
            'unit_id' => 'integer',
            'warehouse_id' => 'integer',
            'change_quantity' => 'decimal:4',
            'before_quantity' => 'decimal:4',
            'after_quantity' => 'decimal:4',
            'source_id' => 'integer',
            'operator_user_id' => 'integer',
            'business_time' => 'datetime',
            'document_input_quantity' => 'decimal:4',
            'document_input_unit_id' => 'integer',
            'posting_base_unit_id' => 'integer',
            'posting_auxiliary_unit_id' => 'integer',
            'posting_is_auxiliary_unit' => 'boolean',
            'posting_auxiliary_unit_quantity' => 'decimal:6',
            'posting_base_unit_quantity' => 'decimal:6',
        ];
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(DberpProduct::class, 'product_id');
    }

    public function productSku(): BelongsTo
    {
        return $this->belongsTo(DberpProductSku::class, 'product_sku_id');
    }

    public function warehouse(): BelongsTo
    {
        return $this->belongsTo(DberpWarehouse::class, 'warehouse_id');
    }

    public function operator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'operator_user_id');
    }

    public function postingBaseUnit(): BelongsTo
    {
        return $this->belongsTo(DberpUnit::class, 'posting_base_unit_id');
    }

    public function postingAuxiliaryUnit(): BelongsTo
    {
        return $this->belongsTo(DberpUnit::class, 'posting_auxiliary_unit_id');
    }

    public function documentInputUnit(): BelongsTo
    {
        return $this->belongsTo(DberpUnit::class, 'document_input_unit_id');
    }
}
