<?php

declare(strict_types=1);

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

namespace App\Models;

use App\Models\Concerns\HasDberpAuditTrail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DberpStocktakeDifference extends Model
{
    use HasDberpAuditTrail;

    protected $table = 'dberp_stocktake_differences';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'stocktake_order_id',
        'stocktake_order_item_id',
        'inventory_transaction_id',
        'product_id',
        'product_sku_id',
        'product_sku_code',
        'product_sku_name',
        'product_sku_spec',
        'warehouse_id',
        'book_quantity',
        'actual_quantity',
        'difference_quantity',
        'result_type',
        'status',
        'reason_code',
        'reason_detail',
        'resolution_type',
        'resolution_remark',
        'handled_by',
        'handled_at',
        'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'stocktake_order_id' => 'integer',
            'stocktake_order_item_id' => 'integer',
            'inventory_transaction_id' => 'integer',
            'product_id' => 'integer',
            'product_sku_id' => 'integer',
            'warehouse_id' => 'integer',
            'book_quantity' => 'decimal:4',
            'actual_quantity' => 'decimal:4',
            'difference_quantity' => 'decimal:4',
            'handled_by' => 'integer',
            'handled_at' => 'datetime',
            'sort_order' => 'integer',
        ];
    }

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

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

    public function stocktakeOrder(): BelongsTo
    {
        return $this->belongsTo(DberpStocktakeOrder::class, 'stocktake_order_id');
    }

    public function stocktakeOrderItem(): BelongsTo
    {
        return $this->belongsTo(DberpStocktakeOrderItem::class, 'stocktake_order_item_id');
    }

    public function inventoryTransaction(): BelongsTo
    {
        return $this->belongsTo(DberpInventoryTransaction::class, 'inventory_transaction_id');
    }

    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 handler(): BelongsTo
    {
        return $this->belongsTo(User::class, 'handled_by');
    }
}
