feat: Implement stock activity logging when outlets are detached from items and add a DELETED status to AuditStockStatus.
This commit is contained in:
parent
4aad6aa730
commit
5d39f002f1
@ -12,6 +12,7 @@ enum AuditStockStatus: string
|
||||
case INCREASED = 'Menambah';
|
||||
case DECREASED = 'Mengurangi';
|
||||
case UPDATED = 'Mengubah';
|
||||
case DELETED = 'Menghapus';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
@ -19,6 +20,7 @@ public function label(): string
|
||||
self::INCREASED => 'Menambah',
|
||||
self::DECREASED => 'Mengurangi',
|
||||
self::UPDATED => 'Mengubah',
|
||||
self::DELETED => 'Menghapus',
|
||||
};
|
||||
}
|
||||
|
||||
@ -26,8 +28,9 @@ public function color(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::INCREASED => 'emerald',
|
||||
self::DECREASED => 'red',
|
||||
self::DECREASED => 'orange',
|
||||
self::UPDATED => 'yellow',
|
||||
self::DELETED => 'red',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,9 @@
|
||||
namespace App\Livewire\Forms\Studio\Catalog;
|
||||
|
||||
use App\Models\Bottle;
|
||||
use App\Models\Outlet;
|
||||
use App\Rules\UnsignedInteger;
|
||||
use App\Services\StockActivityLogService;
|
||||
use App\Traits\Media\WithMediaHandler;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
@ -90,8 +92,43 @@ public function update(): void
|
||||
DB::transaction(function () {
|
||||
$this->bottle->update($this->prepareSavedData());
|
||||
|
||||
// Get old outlet IDs before sync
|
||||
$oldOutlets = $this->bottle->outlets()
|
||||
->withPivot('stock')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$oldOutletIds = $oldOutlets->keys()->toArray();
|
||||
|
||||
// Sync outlets
|
||||
$this->bottle->outlets()->sync($this->outlet_ids);
|
||||
|
||||
// Outlet yang ter-detach
|
||||
$detachedOutletIds = array_diff($oldOutletIds, $this->outlet_ids);
|
||||
|
||||
foreach ($detachedOutletIds as $outletId) {
|
||||
$outlet = $oldOutlets[$outletId];
|
||||
$previousStock = $outlet->pivot->stock ?? 0;
|
||||
|
||||
if ($previousStock > 0) {
|
||||
StockActivityLogService::log(
|
||||
logName: 'Update Outlet',
|
||||
event: 'Menghapus',
|
||||
description: 'Outlet '.$outlet->name.' dihapus dari bottle '.$this->bottle->name,
|
||||
performedOn: $this->bottle,
|
||||
outlet: $outlet,
|
||||
quantityChange: $previousStock,
|
||||
previousStock: $previousStock,
|
||||
newStock: 0,
|
||||
extraProperties: [
|
||||
'action' => 'detach_outlet',
|
||||
'item_type' => 'bottle',
|
||||
'item_id' => $this->bottle->id,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->syncMedia($this->image, $this->bottle, 'image');
|
||||
$this->uploadMedia($this->image, $this->bottle, 'image');
|
||||
});
|
||||
|
||||
@ -3,8 +3,10 @@
|
||||
namespace App\Livewire\Forms\Studio\Catalog;
|
||||
|
||||
use App\Enums\Concentration;
|
||||
use App\Models\Outlet;
|
||||
use App\Models\Perfume;
|
||||
use App\Rules\UnsignedInteger;
|
||||
use App\Services\StockActivityLogService;
|
||||
use App\Traits\Media\WithMediaHandler;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
@ -133,8 +135,43 @@ public function update(): void
|
||||
|
||||
$this->perfume->categories()->sync($this->category_ids);
|
||||
|
||||
// Get old outlet IDs before sync
|
||||
$oldOutlets = $this->perfume->outlets()
|
||||
->withPivot('stock')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$oldOutletIds = $oldOutlets->keys()->toArray();
|
||||
|
||||
// Sync outlets
|
||||
$this->perfume->outlets()->sync($this->outlet_ids);
|
||||
|
||||
// Outlet yang ter-detach
|
||||
$detachedOutletIds = array_diff($oldOutletIds, $this->outlet_ids);
|
||||
|
||||
foreach ($detachedOutletIds as $outletId) {
|
||||
$outlet = $oldOutlets[$outletId];
|
||||
$previousStock = $outlet->pivot->stock ?? 0;
|
||||
|
||||
if ($previousStock > 0) {
|
||||
StockActivityLogService::log(
|
||||
logName: 'Update Outlet',
|
||||
event: 'Menghapus',
|
||||
description: 'Outlet '.$outlet->name.' dihapus dari perfume '.$this->perfume->name,
|
||||
performedOn: $this->perfume,
|
||||
outlet: $outlet,
|
||||
quantityChange: $previousStock,
|
||||
previousStock: $previousStock,
|
||||
newStock: 0,
|
||||
extraProperties: [
|
||||
'action' => 'detach_outlet',
|
||||
'item_type' => 'perfume',
|
||||
'item_id' => $this->perfume->id,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->syncMedia($data['image'], $this->perfume, 'image');
|
||||
$this->uploadMedia($data['image'], $this->perfume, 'image');
|
||||
});
|
||||
|
||||
@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Livewire\Forms\Studio\Catalog;
|
||||
|
||||
use App\Models\Outlet;
|
||||
use App\Models\Product;
|
||||
use App\Rules\UnsignedInteger;
|
||||
use App\Services\StockActivityLogService;
|
||||
use App\Traits\Media\WithMediaHandler;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
@ -100,8 +102,43 @@ public function update(): void
|
||||
DB::transaction(function () use ($data) {
|
||||
$this->product->update($data);
|
||||
|
||||
// Get old outlet IDs before sync
|
||||
$oldOutlets = $this->product->outlets()
|
||||
->withPivot('stock')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$oldOutletIds = $oldOutlets->keys()->toArray();
|
||||
|
||||
// Sync outlets
|
||||
$this->product->outlets()->sync($this->outlet_ids);
|
||||
|
||||
// Outlet yang ter-detach
|
||||
$detachedOutletIds = array_diff($oldOutletIds, $this->outlet_ids);
|
||||
|
||||
foreach ($detachedOutletIds as $outletId) {
|
||||
$outlet = $oldOutlets[$outletId];
|
||||
$previousStock = $outlet->pivot->stock ?? 0;
|
||||
|
||||
if ($previousStock > 0) {
|
||||
StockActivityLogService::log(
|
||||
logName: 'Update Outlet',
|
||||
event: 'Menghapus',
|
||||
description: 'Outlet '.$outlet->name.' dihapus dari product '.$this->product->name,
|
||||
performedOn: $this->product,
|
||||
outlet: $outlet,
|
||||
quantityChange: $previousStock,
|
||||
previousStock: $previousStock,
|
||||
newStock: 0,
|
||||
extraProperties: [
|
||||
'action' => 'detach_outlet',
|
||||
'item_type' => 'product',
|
||||
'item_id' => $this->product->id,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->syncMedia($data['image'], $this->product, 'image');
|
||||
$this->uploadMedia($data['image'], $this->product, 'image');
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user