79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?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;
|
|
|
|
class InventoryCountDoc extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'doc_no',
|
|
'warehouse_id',
|
|
'status',
|
|
'snapshot_date',
|
|
'completed_at',
|
|
'remarks',
|
|
'created_by',
|
|
'updated_by',
|
|
'completed_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'snapshot_date' => 'datetime',
|
|
'completed_at' => 'datetime',
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($model) {
|
|
if (empty($model->doc_no)) {
|
|
$today = date('Ymd');
|
|
$prefix = 'CNT-' . $today . '-';
|
|
|
|
// 查詢當天編號最大的單據
|
|
$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 items(): HasMany
|
|
{
|
|
return $this->hasMany(InventoryCountItem::class, 'count_doc_id');
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function completedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'completed_by');
|
|
}
|
|
}
|