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;
|
2026-01-26 10:37:47 +08:00
|
|
|
use App\Modules\Procurement\Models\PurchaseOrder; // Cross-module dependency
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
class Inventory extends Model
|
|
|
|
|
{
|
|
|
|
|
/** @use HasFactory<\Database\Factories\InventoryFactory> */
|
|
|
|
|
use HasFactory;
|
2026-01-22 15:39:35 +08:00
|
|
|
use \Illuminate\Database\Eloquent\SoftDeletes;
|
2026-01-19 11:47:10 +08:00
|
|
|
use \Spatie\Activitylog\Traits\LogsActivity;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'warehouse_id',
|
|
|
|
|
'product_id',
|
|
|
|
|
'quantity',
|
|
|
|
|
'location',
|
2026-01-21 17:19:36 +08:00
|
|
|
// 批號追溯欄位
|
|
|
|
|
'batch_number',
|
|
|
|
|
'box_number',
|
|
|
|
|
'origin_country',
|
|
|
|
|
'arrival_date',
|
|
|
|
|
'expiry_date',
|
|
|
|
|
'source_purchase_order_id',
|
|
|
|
|
'quality_status',
|
|
|
|
|
'quality_remark',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'arrival_date' => 'date:Y-m-d',
|
|
|
|
|
'expiry_date' => 'date:Y-m-d',
|
2025-12-30 15:03:19 +08:00
|
|
|
];
|
|
|
|
|
|
2026-01-19 11:47:10 +08:00
|
|
|
/**
|
|
|
|
|
* 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'] ?? [];
|
2026-01-19 15:32:41 +08:00
|
|
|
$snapshot = $properties['snapshot'] ?? [];
|
2026-01-19 11:47:10 +08:00
|
|
|
|
|
|
|
|
// Always snapshot names for context, even if IDs didn't change
|
|
|
|
|
// $this refers to the Inventory model instance
|
2026-01-19 15:32:41 +08:00
|
|
|
$snapshot['warehouse_name'] = $this->warehouse ? $this->warehouse->name : ($snapshot['warehouse_name'] ?? null);
|
|
|
|
|
$snapshot['product_name'] = $this->product ? $this->product->name : ($snapshot['product_name'] ?? null);
|
2026-01-19 11:47:10 +08:00
|
|
|
|
|
|
|
|
// Capture the reason if set
|
|
|
|
|
if ($this->activityLogReason) {
|
|
|
|
|
$attributes['_reason'] = $this->activityLogReason;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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 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);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-01-21 17:19:36 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 來源採購單
|
|
|
|
|
*/
|
|
|
|
|
public function sourcePurchaseOrder(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(PurchaseOrder::class, 'source_purchase_order_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 產生批號
|
|
|
|
|
* 格式:{商品代號}-{來源國家}-{入庫日期}-{批次流水號}
|
|
|
|
|
*/
|
|
|
|
|
public static function generateBatchNumber(string $productCode, string $originCountry, string $arrivalDate): string
|
|
|
|
|
{
|
|
|
|
|
$dateFormatted = date('Ymd', strtotime($arrivalDate));
|
|
|
|
|
$prefix = "{$productCode}-{$originCountry}-{$dateFormatted}-";
|
|
|
|
|
|
2026-01-22 15:39:35 +08:00
|
|
|
// 加入 withTrashed() 確保流水號不會撞到已刪除的紀錄
|
|
|
|
|
$lastBatch = static::withTrashed()
|
|
|
|
|
->where('batch_number', 'like', "{$prefix}%")
|
2026-01-21 17:19:36 +08:00
|
|
|
->orderByDesc('batch_number')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($lastBatch) {
|
|
|
|
|
$lastNumber = (int) substr($lastBatch->batch_number, -2);
|
|
|
|
|
$nextNumber = str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT);
|
|
|
|
|
} else {
|
|
|
|
|
$nextNumber = '01';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $prefix . $nextNumber;
|
|
|
|
|
}
|
2025-12-30 15:03:19 +08:00
|
|
|
}
|