chore: update document approval workflow and bug fixes
This commit is contained in:
76
Modules/MaterialLogistics/app/Models/MaterialDeployment.php
Normal file
76
Modules/MaterialLogistics/app/Models/MaterialDeployment.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\MaterialLogistics\Models;
|
||||
use Modules\MasterData\Models\Material;
|
||||
use Modules\MasterData\Models\MaterialGroup;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Traits\HasPublicIdentifier;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Modules\MaterialLogistics\Enums\DeploymentStatus;
|
||||
use Modules\ApprovalWorkflow\Traits\HasApprovable;
|
||||
use Modules\ProjectManagement\Models\Project;
|
||||
|
||||
class MaterialDeployment extends Model
|
||||
{
|
||||
use HasApprovable, HasPublicIdentifier;
|
||||
|
||||
protected $fillable = [
|
||||
'project_id', 'material_requisition_id', 'material_id', 'requested_by',
|
||||
'quantity', 'status', 'notes',
|
||||
'dispatched_at', 'delivered_at', 'consumed_at',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'status' => DeploymentStatus::class,
|
||||
'quantity' => 'decimal:2',
|
||||
'dispatched_at' => 'datetime',
|
||||
'delivered_at' => 'datetime',
|
||||
'consumed_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public function material(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Material::class);
|
||||
}
|
||||
|
||||
public function requisition(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MaterialRequisition::class, 'material_requisition_id');
|
||||
}
|
||||
|
||||
public function requester(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'requested_by');
|
||||
}
|
||||
|
||||
public function transitionTo(DeploymentStatus $newStatus): void
|
||||
{
|
||||
$allowed = $this->status->allowedTransitions();
|
||||
if (!in_array($newStatus, $allowed)) {
|
||||
throw new \InvalidArgumentException(
|
||||
"Cannot transition deployment from {$this->status->label()} to {$newStatus->label()}"
|
||||
);
|
||||
}
|
||||
|
||||
$updates = ['status' => $newStatus];
|
||||
match ($newStatus) {
|
||||
DeploymentStatus::InTransit => $updates['dispatched_at'] = now(),
|
||||
DeploymentStatus::Delivered => $updates['delivered_at'] = now(),
|
||||
DeploymentStatus::Consumed => $updates['consumed_at'] = now(),
|
||||
default => null,
|
||||
};
|
||||
|
||||
$this->update($updates);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user