67 lines
1.6 KiB
PHP
67 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\MaterialLogistics\Models;
|
|
|
|
use App\Models\User;
|
|
use App\Traits\HasPublicIdentifier;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Modules\ApprovalWorkflow\Traits\HasApprovable;
|
|
use Modules\ProjectManagement\Models\Project;
|
|
|
|
class MaterialRequisition extends Model
|
|
{
|
|
use HasPublicIdentifier, HasApprovable;
|
|
|
|
protected $fillable = [
|
|
'document_number', 'status',
|
|
'requested_by', 'approved_by', 'approved_at', 'notes',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'approved_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
|
|
public function requester(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'requested_by');
|
|
}
|
|
|
|
public function approver(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'approved_by');
|
|
}
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(MaterialRequisitionItem::class);
|
|
}
|
|
|
|
public function purchaseOrders(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(PurchaseOrder::class, 'purchase_order_requisitions')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function getTotalCostAttribute(): float
|
|
{
|
|
return $this->items->sum(fn (MaterialRequisitionItem $item) => $item->line_total);
|
|
}
|
|
|
|
public function scopeDraft($query)
|
|
{
|
|
return $query->where('status', 'draft');
|
|
}
|
|
|
|
public function scopeApproved($query)
|
|
{
|
|
return $query->where('status', 'approved');
|
|
}
|
|
}
|