feat: 優化操作紀錄顯示與邏輯 (恢復描述欄位、支援來源標記、改進快照)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 45s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-01-19 11:47:10 +08:00
parent 74417e2e31
commit 18edb3cb69
10 changed files with 333 additions and 74 deletions

View File

@@ -33,6 +33,8 @@ class ActivityLogController extends Controller
'App\Models\Category' => '商品分類',
'App\Models\Unit' => '單位',
'App\Models\PurchaseOrder' => '採購單',
'App\Models\Warehouse' => '倉庫',
'App\Models\Inventory' => '庫存',
];
$eventMap = [

View File

@@ -103,7 +103,8 @@ class InventoryController extends Controller
return \Illuminate\Support\Facades\DB::transaction(function () use ($validated, $warehouse) {
foreach ($validated['items'] as $item) {
// 取得或建立庫存紀錄
$inventory = $warehouse->inventories()->firstOrCreate(
// 取得或初始化庫存紀錄
$inventory = $warehouse->inventories()->firstOrNew(
['product_id' => $item['productId']],
['quantity' => 0, 'safety_stock' => null]
);
@@ -111,8 +112,9 @@ class InventoryController extends Controller
$currentQty = $inventory->quantity;
$newQty = $currentQty + $item['quantity'];
// 更新庫存
$inventory->update(['quantity' => $newQty]);
// 更新庫存並儲存 (新紀錄: Created, 舊紀錄: Updated)
$inventory->quantity = $newQty;
$inventory->save();
// 寫入異動紀錄
$inventory->transactions()->create([

View File

@@ -56,6 +56,9 @@ class TransferOrderController extends Controller
// 3. 執行庫存轉移 (扣除來源)
$oldSourceQty = $sourceInventory->quantity;
$newSourceQty = $oldSourceQty - $validated['quantity'];
// 設定活動紀錄原因
$sourceInventory->activityLogReason = "撥補出庫 至 {$targetWarehouse->name}";
$sourceInventory->update(['quantity' => $newSourceQty]);
// 記錄來源異動
@@ -72,6 +75,9 @@ class TransferOrderController extends Controller
// 4. 執行庫存轉移 (增加目標)
$oldTargetQty = $targetInventory->quantity;
$newTargetQty = $oldTargetQty + $validated['quantity'];
// 設定活動紀錄原因
$targetInventory->activityLogReason = "撥補入庫 來自 {$sourceWarehouse->name}";
$targetInventory->update(['quantity' => $newTargetQty]);
// 記錄目標異動

View File

@@ -36,4 +36,13 @@ class Category extends Model
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$properties = $activity->properties;
$attributes = $properties['attributes'] ?? [];
$attributes['name'] = $this->name;
$properties['attributes'] = $attributes;
$activity->properties = $properties;
}
}

View File

@@ -9,6 +9,7 @@ class Inventory extends Model
{
/** @use HasFactory<\Database\Factories\InventoryFactory> */
use HasFactory;
use \Spatie\Activitylog\Traits\LogsActivity;
protected $fillable = [
'warehouse_id',
@@ -18,6 +19,40 @@ class Inventory extends Model
'location',
];
/**
* Transient property to store the reason for the activity log (e.g., "Replenishment #123").
* This is not stored in the database column but used for logging context.
* @var string|null
*/
public $activityLogReason;
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
{
return \Spatie\Activitylog\LogOptions::defaults()
->logAll()
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$properties = $activity->properties;
$attributes = $properties['attributes'] ?? [];
// Always snapshot names for context, even if IDs didn't change
// $this refers to the Inventory model instance
$attributes['warehouse_name'] = $this->warehouse ? $this->warehouse->name : ($attributes['warehouse_name'] ?? null);
$attributes['product_name'] = $this->product ? $this->product->name : ($attributes['product_name'] ?? null);
// Capture the reason if set
if ($this->activityLogReason) {
$attributes['_reason'] = $this->activityLogReason;
}
$properties['attributes'] = $attributes;
$activity->properties = $properties;
}
public function warehouse(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Warehouse::class);

View File

@@ -76,6 +76,34 @@ class Product extends Model
->dontSubmitEmptyLogs();
}
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$properties = $activity->properties;
$attributes = $properties['attributes'] ?? [];
// Handle Category Name Snapshot
if (isset($attributes['category_id'])) {
$category = Category::find($attributes['category_id']);
$attributes['category_name'] = $category ? $category->name : null;
}
// Handle Unit Name Snapshots
$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);
$attributes[$nameKey] = $unit ? $unit->name : null;
}
}
// Always snapshot self name for context (so logs always show "Cola")
$attributes['name'] = $this->name;
$properties['attributes'] = $attributes;
$activity->properties = $properties;
}
public function warehouses(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Warehouse::class, 'inventories')

View File

@@ -23,4 +23,13 @@ class Unit extends Model
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$properties = $activity->properties;
$attributes = $properties['attributes'] ?? [];
$attributes['name'] = $this->name;
$properties['attributes'] = $attributes;
$activity->properties = $properties;
}
}

View File

@@ -9,6 +9,7 @@ class Warehouse extends Model
{
/** @use HasFactory<\Database\Factories\WarehouseFactory> */
use HasFactory;
use \Spatie\Activitylog\Traits\LogsActivity;
protected $fillable = [
'code',
@@ -17,6 +18,26 @@ class Warehouse extends Model
'description',
];
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
{
return \Spatie\Activitylog\LogOptions::defaults()
->logAll()
->logOnlyDirty()
->dontSubmitEmptyLogs();
}
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$properties = $activity->properties;
$attributes = $properties['attributes'] ?? [];
// Always snapshot name
$attributes['name'] = $this->name;
$properties['attributes'] = $attributes;
$activity->properties = $properties;
}
public function inventories(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Inventory::class);