fix handle error and add note for shippings receive approve and reject mutations
This commit is contained in:
@@ -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