chore: update document approval workflow and bug fixes
This commit is contained in:
100
Modules/MaterialLogistics/app/Models/PurchaseOrder.php
Normal file
100
Modules/MaterialLogistics/app/Models/PurchaseOrder.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?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\MaterialLogistics\Enums\PaymentStatus;
|
||||
use Modules\ProjectManagement\Models\Project;
|
||||
|
||||
class PurchaseOrder extends Model
|
||||
{
|
||||
use HasPublicIdentifier, HasApprovable;
|
||||
|
||||
protected $fillable = [
|
||||
'document_number', 'supplier', 'status', 'target_warehouse_id',
|
||||
'payment_status', 'paid_at', 'receipt_path', 'receipt_original_name',
|
||||
'requested_by', 'approved_by', 'approved_at', 'notes',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'payment_status' => PaymentStatus::class,
|
||||
'approved_at' => 'datetime',
|
||||
'paid_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function isPaid(): bool
|
||||
{
|
||||
return $this->payment_status === PaymentStatus::Paid;
|
||||
}
|
||||
|
||||
public function isApproved(): bool
|
||||
{
|
||||
return $this->status === 'approved';
|
||||
}
|
||||
|
||||
public function hasReceipt(): bool
|
||||
{
|
||||
return !empty($this->receipt_path);
|
||||
}
|
||||
|
||||
|
||||
public function targetWarehouse(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Warehouse::class, 'target_warehouse_id');
|
||||
}
|
||||
|
||||
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(PurchaseOrderItem::class);
|
||||
}
|
||||
|
||||
public function requisitions(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(MaterialRequisition::class, 'purchase_order_requisitions')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function getTotalCostAttribute(): float
|
||||
{
|
||||
return $this->items->sum(fn (PurchaseOrderItem $item) => $item->line_total);
|
||||
}
|
||||
|
||||
public function scopeApproved($query)
|
||||
{
|
||||
return $query->where('status', 'approved');
|
||||
}
|
||||
|
||||
public function onApprovalCompleted($chain)
|
||||
{
|
||||
$this->update(['status' => $chain->status->value]);
|
||||
|
||||
if ($chain->status->value === 'approved') {
|
||||
$lastStep = $chain->steps()->where('status', 'approved')->orderByDesc('acted_at')->first();
|
||||
$this->update([
|
||||
'approved_by' => $lastStep ? $lastStep->approver_id : null,
|
||||
'approved_at' => $lastStep ? $lastStep->acted_at : now(),
|
||||
]);
|
||||
} elseif ($chain->status->value === 'rejected') {
|
||||
$this->requisitions()->detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user