- 優化採購單更新與刪除的活動紀錄邏輯 (PurchaseOrderController) - 整合更新異動為單一紀錄,包含品項差異 - 刪除時記錄當下品項快照 - 統一採購單刪除確認介面,使用 AlertDialog 取代原生 confirm (PurchaseOrderActions) - Refactor: 將 ActivityDetailDialog 移至 Components/ActivityLog 並優化樣式與大數據顯示 - 調整 UI 文字:將「總金額」統一為「小計」 - 其他模型與 Controller 的活動紀錄支援更新
93 lines
2.8 KiB
PHP
93 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Inventory extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\InventoryFactory> */
|
|
use HasFactory;
|
|
use \Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
protected $fillable = [
|
|
'warehouse_id',
|
|
'product_id',
|
|
'quantity',
|
|
'safety_stock',
|
|
'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'] ?? [];
|
|
$snapshot = $properties['snapshot'] ?? [];
|
|
|
|
// Always snapshot names for context, even if IDs didn't change
|
|
// $this refers to the Inventory model instance
|
|
$snapshot['warehouse_name'] = $this->warehouse ? $this->warehouse->name : ($snapshot['warehouse_name'] ?? null);
|
|
$snapshot['product_name'] = $this->product ? $this->product->name : ($snapshot['product_name'] ?? null);
|
|
|
|
// Capture the reason if set
|
|
if ($this->activityLogReason) {
|
|
$attributes['_reason'] = $this->activityLogReason;
|
|
}
|
|
|
|
$properties['attributes'] = $attributes;
|
|
$properties['snapshot'] = $snapshot;
|
|
$activity->properties = $properties;
|
|
}
|
|
|
|
public function warehouse(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Warehouse::class);
|
|
}
|
|
|
|
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function transactions(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(InventoryTransaction::class);
|
|
}
|
|
|
|
public function lastOutgoingTransaction()
|
|
{
|
|
return $this->hasOne(InventoryTransaction::class)->ofMany([
|
|
'actual_time' => 'max',
|
|
'id' => 'max',
|
|
], function ($query) {
|
|
$query->where('quantity', '<', 0);
|
|
});
|
|
}
|
|
|
|
public function lastIncomingTransaction()
|
|
{
|
|
return $this->hasOne(InventoryTransaction::class)->ofMany([
|
|
'actual_time' => 'max',
|
|
'id' => 'max',
|
|
], function ($query) {
|
|
$query->where('quantity', '>', 0);
|
|
});
|
|
}
|
|
}
|