71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Modules\Production\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use App\Modules\Inventory\Models\Product;
|
||
|
|
use App\Modules\Inventory\Models\Warehouse;
|
||
|
|
use App\Modules\Core\Models\User;
|
||
|
|
|
||
|
|
class ProductionOrder extends Model
|
||
|
|
{
|
||
|
|
/** @use HasFactory<\Database\Factories\ProductionOrderFactory> */
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'code',
|
||
|
|
'product_id',
|
||
|
|
'warehouse_id',
|
||
|
|
'output_quantity',
|
||
|
|
'output_batch_number',
|
||
|
|
'output_box_count',
|
||
|
|
'production_date',
|
||
|
|
'expiry_date',
|
||
|
|
'user_id',
|
||
|
|
'status',
|
||
|
|
'remark',
|
||
|
|
];
|
||
|
|
|
||
|
|
public static function generateCode()
|
||
|
|
{
|
||
|
|
$prefix = 'PO' . now()->format('Ymd');
|
||
|
|
$lastOrder = self::where('code', 'like', $prefix . '%')->latest()->first();
|
||
|
|
if ($lastOrder) {
|
||
|
|
$lastSequence = intval(substr($lastOrder->code, -3));
|
||
|
|
$sequence = str_pad($lastSequence + 1, 3, '0', STR_PAD_LEFT);
|
||
|
|
} else {
|
||
|
|
$sequence = '001';
|
||
|
|
}
|
||
|
|
return $prefix . $sequence;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'order_date' => 'date',
|
||
|
|
'start_date' => 'datetime',
|
||
|
|
'completion_date' => 'datetime',
|
||
|
|
'quantity' => 'decimal:2',
|
||
|
|
'produced_quantity' => 'decimal:2',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Product::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function warehouse(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Warehouse::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function items(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||
|
|
{
|
||
|
|
return $this->hasMany(ProductionOrderItem::class);
|
||
|
|
}
|
||
|
|
}
|