refactor: streamline material validation and stock management in create and update methods of CuttingService
This commit is contained in:
parent
aaafbb92a2
commit
71128002d7
@ -145,23 +145,23 @@ public function getForEdit(Cutting $cutting): array
|
||||
|
||||
public function create(array $data): Cutting
|
||||
{
|
||||
foreach ($data['materials'] as $materialData) {
|
||||
$usage = (int) ($materialData['material_usage'] ?? 0);
|
||||
if ($usage <= 0) {
|
||||
continue;
|
||||
}
|
||||
$price = RawMaterialPrice::find($materialData['raw_material_price_id']);
|
||||
if (! $price) {
|
||||
continue;
|
||||
}
|
||||
if ($usage > $price->stock) {
|
||||
throw ValidationException::withMessages([
|
||||
'materials' => "Stok {$price->variant} tidak mencukupi. Tersedia: {$price->stock}, dibutuhkan: {$usage}.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($data) {
|
||||
foreach ($data['materials'] as $materialData) {
|
||||
$usage = (int) ($materialData['material_usage'] ?? 0);
|
||||
if ($usage <= 0) {
|
||||
continue;
|
||||
}
|
||||
$price = RawMaterialPrice::find($materialData['raw_material_price_id']);
|
||||
if (! $price) {
|
||||
continue;
|
||||
}
|
||||
if ($usage > $price->stock) {
|
||||
throw ValidationException::withMessages([
|
||||
'materials' => "Stok {$price->variant} tidak mencukupi. Tersedia: {$price->stock}, dibutuhkan: {$usage}.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$cutting = Cutting::create([
|
||||
'created_by_id' => auth()->id(),
|
||||
'status' => 'in_progress',
|
||||
@ -247,31 +247,31 @@ public function create(array $data): Cutting
|
||||
|
||||
public function update(Cutting $cutting, array $data): Cutting
|
||||
{
|
||||
$cutting->load(['cuttingMaterials.rawMaterialPrice']);
|
||||
|
||||
foreach ($cutting->cuttingMaterials as $oldMaterial) {
|
||||
if ($oldMaterial->material_usage > 0 && $oldMaterial->rawMaterialPrice) {
|
||||
$oldMaterial->rawMaterialPrice->increment('stock', (int) $oldMaterial->material_usage);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data['materials'] as $materialData) {
|
||||
$usage = (int) ($materialData['material_usage'] ?? 0);
|
||||
if ($usage <= 0) {
|
||||
continue;
|
||||
}
|
||||
$price = RawMaterialPrice::find($materialData['raw_material_price_id']);
|
||||
if (! $price) {
|
||||
continue;
|
||||
}
|
||||
if ($usage > $price->stock) {
|
||||
throw ValidationException::withMessages([
|
||||
'materials' => "Stok {$price->variant} tidak mencukupi. Tersedia: {$price->stock}, dibutuhkan: {$usage}.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($cutting, $data) {
|
||||
$cutting->load(['cuttingMaterials.rawMaterialPrice']);
|
||||
|
||||
foreach ($cutting->cuttingMaterials as $oldMaterial) {
|
||||
if ($oldMaterial->material_usage > 0 && $oldMaterial->rawMaterialPrice) {
|
||||
$oldMaterial->rawMaterialPrice->increment('stock', (int) $oldMaterial->material_usage);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data['materials'] as $materialData) {
|
||||
$usage = (int) ($materialData['material_usage'] ?? 0);
|
||||
if ($usage <= 0) {
|
||||
continue;
|
||||
}
|
||||
$price = RawMaterialPrice::find($materialData['raw_material_price_id']);
|
||||
if (! $price) {
|
||||
continue;
|
||||
}
|
||||
if ($usage > $price->stock) {
|
||||
throw ValidationException::withMessages([
|
||||
'materials' => "Stok {$price->variant} tidak mencukupi. Tersedia: {$price->stock}, dibutuhkan: {$usage}.",
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$cutting->load(['cuttingMaterials', 'cuttingMaterialCombinations', 'cuttingResults']);
|
||||
|
||||
$cutting->cuttingResults()->delete();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user