fix handle error and add note for shippings receive approve and reject mutations
This commit is contained in:
@@ -20,16 +20,25 @@ class Mutation extends Model
|
||||
'requested_by',
|
||||
'approved_by',
|
||||
'approved_at',
|
||||
'approval_notes',
|
||||
'received_by',
|
||||
'received_at',
|
||||
'notes',
|
||||
'rejection_reason'
|
||||
'reception_notes',
|
||||
'shipping_notes',
|
||||
'rejection_reason',
|
||||
'rejected_by',
|
||||
'rejected_at',
|
||||
'cancelled_by',
|
||||
'cancelled_at',
|
||||
'cancellation_reason'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'status' => MutationStatus::class,
|
||||
'approved_at' => 'datetime',
|
||||
'received_at' => 'datetime'
|
||||
'received_at' => 'datetime',
|
||||
'rejected_at' => 'datetime',
|
||||
'cancelled_at' => 'datetime'
|
||||
];
|
||||
|
||||
protected static function booted()
|
||||
@@ -66,6 +75,16 @@ class Mutation extends Model
|
||||
return $this->belongsTo(User::class, 'received_by');
|
||||
}
|
||||
|
||||
public function rejectedBy()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'rejected_by');
|
||||
}
|
||||
|
||||
public function cancelledBy()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'cancelled_by');
|
||||
}
|
||||
|
||||
public function mutationDetails()
|
||||
{
|
||||
return $this->hasMany(MutationDetail::class);
|
||||
@@ -137,7 +156,7 @@ class Mutation extends Model
|
||||
}
|
||||
|
||||
// Receive mutation by destination dealer
|
||||
public function receive($userId)
|
||||
public function receive($userId, $receptionNotes = null)
|
||||
{
|
||||
if (!$this->canBeReceived()) {
|
||||
throw new \Exception('Mutasi tidak dapat diterima dalam status saat ini');
|
||||
@@ -146,14 +165,15 @@ class Mutation extends Model
|
||||
$this->update([
|
||||
'status' => MutationStatus::RECEIVED,
|
||||
'received_by' => $userId,
|
||||
'received_at' => now()
|
||||
'received_at' => now(),
|
||||
'reception_notes' => $receptionNotes
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Approve mutation
|
||||
public function approve($userId, $notes = null)
|
||||
public function approve($userId, $approvalNotes = null)
|
||||
{
|
||||
if (!$this->canBeApproved()) {
|
||||
throw new \Exception('Mutasi tidak dapat disetujui dalam status saat ini');
|
||||
@@ -163,7 +183,7 @@ class Mutation extends Model
|
||||
'status' => MutationStatus::APPROVED,
|
||||
'approved_by' => $userId,
|
||||
'approved_at' => now(),
|
||||
'notes' => $notes
|
||||
'approval_notes' => $approvalNotes
|
||||
]);
|
||||
|
||||
return $this;
|
||||
@@ -178,14 +198,31 @@ class Mutation extends Model
|
||||
|
||||
$this->update([
|
||||
'status' => MutationStatus::REJECTED,
|
||||
'approved_by' => $userId,
|
||||
'approved_at' => now(),
|
||||
'rejected_by' => $userId,
|
||||
'rejected_at' => now(),
|
||||
'rejection_reason' => $rejectionReason
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Cancel mutation
|
||||
public function cancel($userId, $cancellationReason = null)
|
||||
{
|
||||
if (!$this->canBeCancelled()) {
|
||||
throw new \Exception('Mutasi tidak dapat dibatalkan dalam status saat ini');
|
||||
}
|
||||
|
||||
$this->update([
|
||||
'status' => MutationStatus::CANCELLED,
|
||||
'cancelled_by' => $userId,
|
||||
'cancelled_at' => now(),
|
||||
'cancellation_reason' => $cancellationReason
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Complete mutation (actually move the stock)
|
||||
public function complete()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\OpnameStatus;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
@@ -22,14 +23,15 @@ class Opname extends Model
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'approved_at' => 'datetime'
|
||||
'approved_at' => 'datetime',
|
||||
'status' => OpnameStatus::class
|
||||
];
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::updated(function ($opname) {
|
||||
// Jika status berubah menjadi approved
|
||||
if ($opname->isDirty('status') && $opname->status === 'approved') {
|
||||
if ($opname->isDirty('status') && $opname->status === OpnameStatus::APPROVED) {
|
||||
// Update stock untuk setiap detail opname
|
||||
foreach ($opname->details as $detail) {
|
||||
$stock = Stock::firstOrCreate(
|
||||
@@ -74,11 +76,11 @@ class Opname extends Model
|
||||
// Method untuk approve opname
|
||||
public function approve(User $approver)
|
||||
{
|
||||
if ($this->status !== 'pending') {
|
||||
if ($this->status !== OpnameStatus::PENDING) {
|
||||
throw new \Exception('Only pending opnames can be approved');
|
||||
}
|
||||
|
||||
$this->status = 'approved';
|
||||
$this->status = OpnameStatus::APPROVED;
|
||||
$this->approved_by = $approver->id;
|
||||
$this->approved_at = now();
|
||||
$this->save();
|
||||
@@ -89,11 +91,11 @@ class Opname extends Model
|
||||
// Method untuk reject opname
|
||||
public function reject(User $rejector, string $note)
|
||||
{
|
||||
if ($this->status !== 'pending') {
|
||||
if ($this->status !== OpnameStatus::PENDING) {
|
||||
throw new \Exception('Only pending opnames can be rejected');
|
||||
}
|
||||
|
||||
$this->status = 'rejected';
|
||||
$this->status = OpnameStatus::REJECTED;
|
||||
$this->approved_by = $rejector->id;
|
||||
$this->approved_at = now();
|
||||
$this->rejection_note = $note;
|
||||
@@ -105,11 +107,11 @@ class Opname extends Model
|
||||
// Method untuk submit opname untuk approval
|
||||
public function submit()
|
||||
{
|
||||
if ($this->status !== 'draft') {
|
||||
if ($this->status !== OpnameStatus::DRAFT) {
|
||||
throw new \Exception('Only draft opnames can be submitted');
|
||||
}
|
||||
|
||||
$this->status = 'pending';
|
||||
$this->status = OpnameStatus::PENDING;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
|
||||
Reference in New Issue
Block a user