Files
star-erp/app/Modules/Inventory/Models/InventoryAdjustDoc.php

82 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 InventoryAdjustDoc extends Model
{
use HasFactory;
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');
$prefix = 'ADJ-' . $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 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');
}
}