Advertisement
eXistenZNL

Wat ChatGPT bedacht

Jun 16th, 2025
259
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1.  
  2.         $newEntries = [];
  3.  
  4.         foreach ($budgetData as $budgetDataForEmployee) {
  5.             $employeeId = $budgetDataForEmployee['employee'];
  6.  
  7.             foreach ($budgetDataForEmployee['hours'] as $post => $hours) {
  8.                 $newEntries[] = [
  9.                     'order_id' => $order->id,
  10.                     'post_id' => $post,
  11.                     'employee_id' => (int) $employeeId,
  12.                     'hours' => (int) $hours,
  13.                 ];
  14.             }
  15.         }
  16.  
  17.         // Get existing budget hours for this order
  18.         $existingEntries = Hour::where('order_id', $order->id)->get();
  19.  
  20.         // Index by composite key: postId_employeeId
  21.         $existingMap = $existingEntries->keyBy(fn ($item) => $item->post_id . '_' . $item->employee_id);
  22.         $newMap = collect($newEntries)->keyBy(fn ($item) => $item['post_id'] . '_' . $item['employee_id']);
  23.  
  24.         $toInsert = [];
  25.         $toUpdate = [];
  26.         $toDelete = [];
  27.  
  28.         foreach ($newMap as $key => $newItem) {
  29.             $newHours = $newItem['hours'];
  30.  
  31.             if ($newHours === 0) {
  32.                 // Mark for deletion if it exists
  33.                 if ($existingMap->has($key)) {
  34.                     $itemToDelete = $existingMap[$key];
  35.                     Assert::isInstanceOf($itemToDelete, Hour::class);
  36.                     $toDelete[] = $itemToDelete->id;
  37.                 }
  38.             } elseif (!$existingMap->has($key)) {
  39.                 $toInsert[] = $newItem;
  40.             } else {
  41.                 $existingItem = $existingMap[$key];
  42.                 Assert::isInstanceOf($existingItem, Hour::class);
  43.                 if ($existingItem->hours !== $newHours) {
  44.                     $existingItem->hours = $newHours;
  45.                     $toUpdate[] = $existingItem;
  46.                 }
  47.             }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement