2025-12-30 15:03:19 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
|
namespace App\Modules\Inventory\Models;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-01-16 17:36:37 +08:00
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
|
|
use Spatie\Activitylog\LogOptions;
|
2026-01-26 14:59:24 +08:00
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
class Product extends Model
|
|
|
|
|
|
{
|
2026-01-16 17:36:37 +08:00
|
|
|
|
use HasFactory, LogsActivity, SoftDeletes;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
|
'code',
|
2026-01-29 16:13:56 +08:00
|
|
|
|
'barcode',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
'name',
|
2026-02-06 11:56:29 +08:00
|
|
|
|
'external_pos_id',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
'category_id',
|
|
|
|
|
|
'brand',
|
|
|
|
|
|
'specification',
|
2026-01-08 11:52:25 +08:00
|
|
|
|
'base_unit_id',
|
|
|
|
|
|
'large_unit_id',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
'conversion_rate',
|
2026-01-08 11:52:25 +08:00
|
|
|
|
'purchase_unit_id',
|
2026-02-03 13:17:46 +08:00
|
|
|
|
'location',
|
2026-02-05 11:45:08 +08:00
|
|
|
|
'cost_price',
|
|
|
|
|
|
'price',
|
|
|
|
|
|
'member_price',
|
|
|
|
|
|
'wholesale_price',
|
2026-02-05 16:15:06 +08:00
|
|
|
|
'is_active',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
|
'conversion_rate' => 'decimal:4',
|
2026-02-05 16:15:06 +08:00
|
|
|
|
'is_active' => 'boolean',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 14:59:24 +08:00
|
|
|
|
* 取得該商品所屬的分類。
|
2025-12-30 15:03:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function category(): BelongsTo
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->belongsTo(Category::class);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 11:52:25 +08:00
|
|
|
|
public function baseUnit(): BelongsTo
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->belongsTo(Unit::class, 'base_unit_id');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function largeUnit(): BelongsTo
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->belongsTo(Unit::class, 'large_unit_id');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function purchaseUnit(): BelongsTo
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->belongsTo(Unit::class, 'purchase_unit_id');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
public function inventories(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->hasMany(Inventory::class);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-16 17:36:37 +08:00
|
|
|
|
public function transactions(): HasMany
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->hasMany(InventoryTransaction::class);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
return LogOptions::defaults()
|
|
|
|
|
|
->logAll()
|
|
|
|
|
|
->logOnlyDirty()
|
|
|
|
|
|
->dontSubmitEmptyLogs();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 11:47:10 +08:00
|
|
|
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
|
|
|
|
|
{
|
|
|
|
|
|
$properties = $activity->properties;
|
|
|
|
|
|
$attributes = $properties['attributes'] ?? [];
|
2026-01-19 15:32:41 +08:00
|
|
|
|
$snapshot = $properties['snapshot'] ?? [];
|
2026-01-19 11:47:10 +08:00
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
// 處理分類名稱快照
|
2026-01-19 11:47:10 +08:00
|
|
|
|
if (isset($attributes['category_id'])) {
|
|
|
|
|
|
$category = Category::find($attributes['category_id']);
|
2026-01-19 15:32:41 +08:00
|
|
|
|
$snapshot['category_name'] = $category ? $category->name : null;
|
2026-01-19 11:47:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
// 處理單位名稱快照
|
2026-01-19 11:47:10 +08:00
|
|
|
|
$unitFields = ['base_unit_id', 'large_unit_id', 'purchase_unit_id'];
|
|
|
|
|
|
foreach ($unitFields as $field) {
|
|
|
|
|
|
if (isset($attributes[$field])) {
|
|
|
|
|
|
$unit = Unit::find($attributes[$field]);
|
|
|
|
|
|
$nameKey = str_replace('_id', '_name', $field);
|
2026-01-19 15:32:41 +08:00
|
|
|
|
$snapshot[$nameKey] = $unit ? $unit->name : null;
|
2026-01-19 11:47:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
// 始終對自身名稱進行快照以便於上下文顯示(這樣日誌總是顯示 "可樂")
|
2026-01-19 15:32:41 +08:00
|
|
|
|
$snapshot['name'] = $this->name;
|
2026-01-19 11:47:10 +08:00
|
|
|
|
|
|
|
|
|
|
$properties['attributes'] = $attributes;
|
2026-01-19 15:32:41 +08:00
|
|
|
|
$properties['snapshot'] = $snapshot;
|
2026-01-19 11:47:10 +08:00
|
|
|
|
$activity->properties = $properties;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
|
public function warehouses(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->belongsToMany(Warehouse::class, 'inventories')
|
|
|
|
|
|
->withPivot(['quantity', 'safety_stock', 'location'])
|
|
|
|
|
|
->withTimestamps();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|