1. 新增 config/locales.php:定義 11 種系統支援語系白名單(原文+中文註記)、每台機台上限 5 種與 fallback (zh_TW)。 2. Company 新增 activeLocalesFor()/activeLocales():計算公司所有機台已開語系的聯集(去重、依白名單排序、保底 zh_TW),加快取與 forgetActiveLocales() 失效機制。 3. products 新增 spec_dictionary_key 欄位(migration);Product 新增 localized_spec accessor 與 specTranslations 關聯,商品規格比照名稱支援多語系。 4. ProductController 商品建立/更新改收 names 與 specs 物件,寫入 translations(group=product/product_spec),既有商品首次編輯補建 spec key,刪除時一併清除規格翻譯。 5. ProductCatalogService(B012)保留既有 t060v01_en/_jp 欄位,新增 t060v01_i18n/t060v03_i18n locale map,依公司語系聯集輸出並回退 zh_TW,確保線上舊 App 相容。 6. 機台系統設定(updateSystemSettings)新增 languages 顯示語系驗證與寫入(最多 5 種、白名單、去重,僅系統管理員),異動時失效語系聯集快取並重建商品目錄。 7. B014(getSettings)新增 LangSet 下發機台顯示語系(Languages 有序清單+Default 預設)。 8. 商品建立/編輯頁名稱與規格改為多語系 Tab(讀公司機台語系聯集、完成度標記),新增共用元件 product-locale-tabs;機台系統設定彈窗新增顯示語系勾選 UI(限 5 選、預設標記)。 9. 三語系 JSON(zh_TW/en/ja)新增 5 組對齊鍵並維持字母排序。 10. 同步更新文件:docs/API/product_multilingual_spec.md(規格書)、api-technical-specs SKILL.md(B012 i18n/B014 LangSet)、config/api-docs.php(B012/B014 範例與欄位)。 11. 系統設定同步:新增 sync-settings 路由與 update_settings 指令類型,ProcessCommandAck 與遠端指令歷史(remote index/tab-history)支援 update_settings 標籤,機台系統設定頁新增「同步設定」操作按鈕,並補 B016 回寫測試。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
142 lines
3.9 KiB
PHP
142 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Product;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use App\Services\Machine\MachineService;
|
|
use App\Traits\TenantScoped;
|
|
|
|
class Product extends Model
|
|
{
|
|
use HasFactory, SoftDeletes, TenantScoped;
|
|
|
|
protected static function booted()
|
|
{
|
|
static::updated(function (Product $product) {
|
|
if ($product->wasChanged(['track_limit', 'spring_limit'])) {
|
|
app(MachineService::class)->syncProductSlotMaxStock($product);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Scope a query to only include active products.
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_active', true);
|
|
}
|
|
|
|
protected $fillable = [
|
|
'company_id',
|
|
'category_id',
|
|
'name',
|
|
'name_dictionary_key',
|
|
'barcode',
|
|
'spec',
|
|
'spec_dictionary_key',
|
|
'manufacturer',
|
|
'description',
|
|
'price',
|
|
'member_price',
|
|
'cost',
|
|
'track_limit',
|
|
'spring_limit',
|
|
'type',
|
|
'image_url',
|
|
'status',
|
|
'is_active',
|
|
'metadata',
|
|
];
|
|
|
|
protected $casts = [
|
|
'price' => 'decimal:2',
|
|
'member_price' => 'decimal:2',
|
|
'cost' => 'decimal:2',
|
|
'track_limit' => 'integer',
|
|
'spring_limit' => 'integer',
|
|
'is_active' => 'boolean',
|
|
'metadata' => 'array',
|
|
];
|
|
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(ProductCategory::class, 'category_id');
|
|
}
|
|
|
|
/**
|
|
* 自動附加到 JSON/陣列輸出的屬性(供 Alpine.js 等前端使用)
|
|
*/
|
|
protected $appends = ['localized_name', 'localized_spec'];
|
|
|
|
/**
|
|
* 取得當前語系的商品名稱。
|
|
* 回退順序:當前語系 → zh_TW → name 欄位
|
|
*/
|
|
public function getLocalizedNameAttribute(): string
|
|
{
|
|
if ($this->relationLoaded('translations') && $this->translations->isNotEmpty()) {
|
|
$locale = app()->getLocale();
|
|
// 先找當前語系
|
|
$translation = $this->translations->firstWhere('locale', $locale);
|
|
if ($translation) {
|
|
return $translation->value;
|
|
}
|
|
// 回退至 zh_TW
|
|
$fallback = $this->translations->firstWhere('locale', 'zh_TW');
|
|
if ($fallback) {
|
|
return $fallback->value;
|
|
}
|
|
}
|
|
return $this->name ?? '';
|
|
}
|
|
|
|
/**
|
|
* 取得當前語系的商品規格。
|
|
* 回退順序:當前語系 → zh_TW → spec 欄位(與名稱同邏輯)
|
|
*/
|
|
public function getLocalizedSpecAttribute(): string
|
|
{
|
|
if ($this->relationLoaded('specTranslations') && $this->specTranslations->isNotEmpty()) {
|
|
$locale = app()->getLocale();
|
|
$translation = $this->specTranslations->firstWhere('locale', $locale);
|
|
if ($translation) {
|
|
return $translation->value;
|
|
}
|
|
$fallback = $this->specTranslations->firstWhere('locale', 'zh_TW');
|
|
if ($fallback) {
|
|
return $fallback->value;
|
|
}
|
|
}
|
|
return $this->spec ?? '';
|
|
}
|
|
|
|
/**
|
|
* Get the translations for the product name.
|
|
*/
|
|
public function translations()
|
|
{
|
|
return $this->hasMany(\App\Models\System\Translation::class, 'key', 'name_dictionary_key')
|
|
->where('group', 'product');
|
|
}
|
|
|
|
/**
|
|
* Get the translations for the product spec.
|
|
*/
|
|
public function specTranslations()
|
|
{
|
|
return $this->hasMany(\App\Models\System\Translation::class, 'key', 'spec_dictionary_key')
|
|
->where('group', 'product_spec');
|
|
}
|
|
|
|
/**
|
|
* 倉庫庫存紀錄
|
|
*/
|
|
public function stocks()
|
|
{
|
|
return $this->hasMany(\App\Models\Warehouse\WarehouseStock::class);
|
|
}
|
|
}
|