feat(order): mencatat perubahan stok saat order dan memperbaiki kesalahan logika (terbaik) antara order dan hapus order
This commit is contained in:
parent
c435d58f89
commit
3982f7ed70
@ -19,7 +19,7 @@ class Index extends Component
|
|||||||
public function delete(Order $order)
|
public function delete(Order $order)
|
||||||
{
|
{
|
||||||
foreach ($order->items as $item) {
|
foreach ($order->items as $item) {
|
||||||
$this->decreaseOutletStock($order->outlet, $item);
|
$this->increaseOutletStock($order->outlet, $item);
|
||||||
}
|
}
|
||||||
|
|
||||||
$order->delete();
|
$order->delete();
|
||||||
|
|||||||
@ -111,16 +111,14 @@ protected function handleRefillPerfume()
|
|||||||
$perfume = Perfume::find($this->form->perfume_id);
|
$perfume = Perfume::find($this->form->perfume_id);
|
||||||
$quality = Formula::find($this->form->quality_id);
|
$quality = Formula::find($this->form->quality_id);
|
||||||
|
|
||||||
$size = $this->form->bottle_size;
|
$quantity = $quality->volume;
|
||||||
|
|
||||||
if (! $perfume || ! $quality || $size <= 0) {
|
if (! $perfume || ! $quality || $quantity <= 0) {
|
||||||
$this->toast('Parfum, ukuran, atau kualitas tidak valid.', 'Gagal', 'danger');
|
$this->toast('Parfum, ukuran, atau kualitas tidak valid.', 'Gagal', 'danger');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$quantity = $size;
|
|
||||||
|
|
||||||
$this->addOrUpdateOrderItem($perfume, $quantity);
|
$this->addOrUpdateOrderItem($perfume, $quantity);
|
||||||
|
|
||||||
$this->toast('Parfum isi ulang ditambahkan ke keranjang.', 'Berhasil');
|
$this->toast('Parfum isi ulang ditambahkan ke keranjang.', 'Berhasil');
|
||||||
|
|||||||
@ -12,7 +12,7 @@ public function increaseOutletStock($outlet, $item)
|
|||||||
{
|
{
|
||||||
$quantity = $item->quantity;
|
$quantity = $item->quantity;
|
||||||
|
|
||||||
switch ($item->purchasable_type) {
|
switch ($item->orderable_type) {
|
||||||
case Perfume::class:
|
case Perfume::class:
|
||||||
$relation = 'perfumes';
|
$relation = 'perfumes';
|
||||||
break;
|
break;
|
||||||
@ -26,25 +26,40 @@ public function increaseOutletStock($outlet, $item)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$existing = $outlet->{$relation}()->where("{$relation}.id", $item->purchasable_id)->first();
|
$existing = $outlet->{$relation}()->withPivot('stock')->where("{$relation}.id", $item->orderable_id)->first();
|
||||||
|
|
||||||
if ($existing) {
|
if ($existing) {
|
||||||
$currentStock = $existing->pivot->stock ?? 0;
|
$currentStock = $existing->pivot->stock ?? 0;
|
||||||
$outlet->{$relation}()->updateExistingPivot($item->purchasable_id, [
|
$outlet->{$relation}()->updateExistingPivot($item->orderable_id, [
|
||||||
'stock' => $currentStock + $quantity,
|
'stock' => $currentStock + $quantity,
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
$outlet->{$relation}()->attach($item->purchasable_id, [
|
$outlet->{$relation}()->attach($item->orderable_id, [
|
||||||
'stock' => $quantity,
|
'stock' => $quantity,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
activity('Order')
|
||||||
|
->performedOn($item->orderable)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->withProperties([
|
||||||
|
'orderable_id' => $item->orderable_id,
|
||||||
|
'orderable_type' => $item->orderable_type,
|
||||||
|
'outlet_id' => $outlet->id,
|
||||||
|
'quantity_change' => $quantity,
|
||||||
|
'previous_stock' => $currentStock,
|
||||||
|
'new_stock' => $currentStock + $quantity,
|
||||||
|
'purchase_id' => $item->purchase_id ?? null,
|
||||||
|
])
|
||||||
|
->event('Menambah')
|
||||||
|
->log('Stok '.$item->orderable->name.' di '.$outlet->name.' bertambah '.currency($quantity));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function decreaseOutletStock($outlet, $item)
|
public function decreaseOutletStock($outlet, $item)
|
||||||
{
|
{
|
||||||
$quantity = $item->quantity;
|
$quantity = $item->quantity;
|
||||||
|
|
||||||
switch ($item->purchasable_type) {
|
switch ($item->orderable_type) {
|
||||||
case Perfume::class:
|
case Perfume::class:
|
||||||
$relation = 'perfumes';
|
$relation = 'perfumes';
|
||||||
break;
|
break;
|
||||||
@ -58,14 +73,29 @@ public function decreaseOutletStock($outlet, $item)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$existing = $outlet->{$relation}()->where("{$relation}.id", $item->purchasable_id)->first();
|
$existing = $outlet->{$relation}()->withPivot('stock')->where("{$relation}.id", $item->orderable_id)->first();
|
||||||
|
|
||||||
if ($existing) {
|
if ($existing) {
|
||||||
$currentStock = $existing->pivot->stock ?? 0;
|
$currentStock = $existing->pivot->stock ?? 0;
|
||||||
$newStock = max(0, $currentStock - $quantity);
|
$newStock = max(0, $currentStock - $quantity);
|
||||||
$outlet->{$relation}()->updateExistingPivot($item->purchasable_id, [
|
$outlet->{$relation}()->updateExistingPivot($item->orderable_id, [
|
||||||
'stock' => $newStock,
|
'stock' => $newStock,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
activity('Order')
|
||||||
|
->performedOn($item->orderable)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->withProperties([
|
||||||
|
'orderable_id' => $item->orderable_id,
|
||||||
|
'orderable_type' => $item->orderable_type,
|
||||||
|
'outlet_id' => $outlet->id,
|
||||||
|
'quantity_change' => $quantity,
|
||||||
|
'previous_stock' => $currentStock,
|
||||||
|
'new_stock' => $currentStock - $quantity,
|
||||||
|
'purchase_id' => $item->purchase_id ?? null,
|
||||||
|
])
|
||||||
|
->event('Mengurangi')
|
||||||
|
->log('Stok '.$item->orderable->name.' di '.$outlet->name.' berkurang '.currency($quantity));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user