DB::transaction(function () use ($record, $data) {
// Get the updated items from the modal
$updatedItems = $data['items'];
// Get the original items from the purchase order
$originalItems = $record->purchaseOrder->items;
// Initialize an array to hold the updated items for saving
$updatedItemsArray = [];
// Process the received quantities
foreach ($updatedItems as $updatedItem) {
// Find the matching original item in the purchase order
$matchingItem = collect($originalItems)->firstWhere('name', $updatedItem['name']);
if ($matchingItem) {
// Calculate the difference in received quantity
$previousReceivedQuantity = $matchingItem['received_quantity'];
$newReceivedQuantity = $updatedItem['received_quantity'];
$difference = $newReceivedQuantity - $previousReceivedQuantity;
// Update the received quantity in the original item
$matchingItem['received_quantity'] = $newReceivedQuantity;
// Increment or decrement inventory based on the difference
$product = Product::where('name', $updatedItem['name'])->first();
if ($product) {
if ($difference > 0) {
// If the received quantity increased, increment the inventory
$product->increment('inventory', $difference);
} elseif ($difference < 0) {
// If the received quantity decreased, decrement the inventory
$product->decrement('inventory', abs($difference));
}
}
// Add the updated item to the array
$updatedItemsArray[] = $matchingItem;
}
}
// Step 4: Save the updated items back to the purchase order
$record->purchaseOrder->items = $updatedItemsArray;
$record->purchaseOrder->save();
});
DB::transaction(function () use ($record, $data) {
// Get the updated items from the modal
$updatedItems = $data['items'];
// Get the original items from the purchase order
$originalItems = $record->purchaseOrder->items;
// Initialize an array to hold the updated items for saving
$updatedItemsArray = [];
// Process the received quantities
foreach ($updatedItems as $updatedItem) {
// Find the matching original item in the purchase order
$matchingItem = collect($originalItems)->firstWhere('name', $updatedItem['name']);
if ($matchingItem) {
// Calculate the difference in received quantity
$previousReceivedQuantity = $matchingItem['received_quantity'];
$newReceivedQuantity = $updatedItem['received_quantity'];
$difference = $newReceivedQuantity - $previousReceivedQuantity;
// Update the received quantity in the original item
$matchingItem['received_quantity'] = $newReceivedQuantity;
// Increment or decrement inventory based on the difference
$product = Product::where('name', $updatedItem['name'])->first();
if ($product) {
if ($difference > 0) {
// If the received quantity increased, increment the inventory
$product->increment('inventory', $difference);
} elseif ($difference < 0) {
// If the received quantity decreased, decrement the inventory
$product->decrement('inventory', abs($difference));
}
}
// Add the updated item to the array
$updatedItemsArray[] = $matchingItem;
}
}
// Step 4: Save the updated items back to the purchase order
$record->purchaseOrder->items = $updatedItemsArray;
$record->purchaseOrder->save();
});