2026-01-28 18:04:45 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Modules\Inventory\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use App\Modules\Core\Models\User;
|
|
|
|
|
|
2026-02-04 16:56:08 +08:00
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
|
use Spatie\Activitylog\LogOptions;
|
|
|
|
|
|
2026-01-28 18:04:45 +08:00
|
|
|
class InventoryAdjustDoc extends Model
|
|
|
|
|
{
|
2026-02-04 16:56:08 +08:00
|
|
|
use HasFactory, LogsActivity;
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'doc_no',
|
|
|
|
|
'count_doc_id',
|
|
|
|
|
'warehouse_id',
|
|
|
|
|
'status',
|
|
|
|
|
'reason',
|
|
|
|
|
'remarks',
|
|
|
|
|
'posted_at',
|
|
|
|
|
'created_by',
|
|
|
|
|
'updated_by',
|
|
|
|
|
'posted_by',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'posted_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected static function boot()
|
|
|
|
|
{
|
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
|
|
static::creating(function ($model) {
|
|
|
|
|
if (empty($model->doc_no)) {
|
|
|
|
|
$today = date('Ymd');
|
2026-02-04 13:08:05 +08:00
|
|
|
$prefix = 'ADJ-' . $today . '-';
|
2026-01-28 18:04:45 +08:00
|
|
|
|
|
|
|
|
$lastDoc = static::where('doc_no', 'like', $prefix . '%')
|
|
|
|
|
->orderBy('doc_no', 'desc')
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($lastDoc) {
|
|
|
|
|
$lastNumber = substr($lastDoc->doc_no, -2);
|
|
|
|
|
$nextNumber = str_pad((int)$lastNumber + 1, 2, '0', STR_PAD_LEFT);
|
|
|
|
|
} else {
|
|
|
|
|
$nextNumber = '01';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$model->doc_no = $prefix . $nextNumber;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function warehouse(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Warehouse::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function countDoc(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(InventoryCountDoc::class, 'count_doc_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function items(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(InventoryAdjustItem::class, 'adjust_doc_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createdBy(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postedBy(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'posted_by');
|
|
|
|
|
}
|
2026-02-04 16:56:08 +08:00
|
|
|
|
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
|
|
|
{
|
|
|
|
|
return LogOptions::defaults()
|
|
|
|
|
->logFillable()
|
|
|
|
|
->dontSubmitEmptyLogs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
|
|
|
|
{
|
|
|
|
|
// 確保為陣列以進行修改
|
|
|
|
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
|
|
|
|
? $activity->properties->toArray()
|
|
|
|
|
: $activity->properties;
|
|
|
|
|
|
|
|
|
|
$snapshot = $properties['snapshot'] ?? [];
|
|
|
|
|
|
|
|
|
|
// Snapshot key information
|
|
|
|
|
$snapshot['doc_no'] = $this->doc_no;
|
|
|
|
|
$snapshot['warehouse_name'] = $this->warehouse ? $this->warehouse->name : null;
|
|
|
|
|
$snapshot['posted_at'] = $this->posted_at ? $this->posted_at->format('Y-m-d H:i:s') : null;
|
|
|
|
|
$snapshot['status'] = $this->status;
|
|
|
|
|
$snapshot['created_by_name'] = $this->createdBy ? $this->createdBy->name : null;
|
|
|
|
|
$snapshot['posted_by_name'] = $this->postedBy ? $this->postedBy->name : null;
|
|
|
|
|
|
|
|
|
|
$properties['snapshot'] = $snapshot;
|
|
|
|
|
|
|
|
|
|
// 全域 ID 轉名稱邏輯 (用於 attributes 與 old)
|
|
|
|
|
$convertIdsToNames = function (&$data) {
|
|
|
|
|
if (empty($data) || !is_array($data)) return;
|
|
|
|
|
|
|
|
|
|
// 倉庫 ID 轉換
|
|
|
|
|
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
|
|
|
|
|
$warehouse = \App\Modules\Inventory\Models\Warehouse::find($data['warehouse_id']);
|
|
|
|
|
if ($warehouse) {
|
|
|
|
|
$data['warehouse_id'] = $warehouse->name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用者 ID 轉換
|
|
|
|
|
$userFields = ['created_by', 'updated_by', 'posted_by'];
|
|
|
|
|
foreach ($userFields as $field) {
|
|
|
|
|
if (isset($data[$field]) && is_numeric($data[$field])) {
|
|
|
|
|
$user = \App\Modules\Core\Models\User::find($data[$field]);
|
|
|
|
|
if ($user) {
|
|
|
|
|
$data[$field] = $user->name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (isset($properties['attributes'])) {
|
|
|
|
|
$convertIdsToNames($properties['attributes']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($properties['old'])) {
|
|
|
|
|
$convertIdsToNames($properties['old']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$activity->properties = $properties;
|
|
|
|
|
}
|
2026-01-28 18:04:45 +08:00
|
|
|
}
|