예제 #1
0
 /**
  * @param Listing $listing
  * @param WorkedHours $workedHours
  * @param bool $createNewListing
  * @param array $selectedItemsIDsToChange
  * @return array
  * @throws \DibiException
  * @throws NegativeResultOfTimeCalcException
  */
 public function changeItemsInListing(Listing $listing, WorkedHours $workedHours, $createNewListing = true, array $selectedItemsIDsToChange)
 {
     $this->checkListingValidity($listing);
     Validators::assert($createNewListing, 'boolean');
     if (empty($selectedItemsIDsToChange)) {
         throw new InvalidArgumentException('Argument $selectedItemsIDsToChange must not be empty array!');
     }
     $listingItems = $listing->listingItems;
     $itemsToChange = [];
     $itemsToCopy = [];
     // items that won't change
     $selectedItemsToChange = array_flip($selectedItemsIDsToChange);
     foreach ($listingItems as $listingItem) {
         if (array_key_exists($listingItem->listingItemID, $selectedItemsToChange)) {
             $itemsToChange[] = $listingItem;
         } else {
             $itemsToCopy[] = $listingItem;
         }
     }
     try {
         $this->transaction->begin();
         // amount of Items is same all the time
         $numberOfItemsToChange = count($itemsToChange);
         if ($createNewListing === true) {
             $listing = $this->establishListingCopy($listing, false);
             if (count($itemsToCopy) > 0) {
                 $itemsToCopy = $this->getItemsCopies($listing, $itemsToCopy);
             }
             if ($numberOfItemsToChange > 0) {
                 $itemsToChange = $this->getItemsCopies($listing, $itemsToChange);
             }
         }
         if ($numberOfItemsToChange > 0) {
             $workedHours = $this->itemFacade->setupWorkedHoursEntity($workedHours);
             $whInSecs = $workedHours->otherHours->toSeconds();
             foreach ($itemsToChange as $item) {
                 $descOtherHours = null;
                 if (isset($item->descOtherHours) and $whInSecs > 0) {
                     $descOtherHours = $item->descOtherHours;
                 }
                 $item->setWorkedTime($workedHours, $descOtherHours);
             }
         }
         if ($createNewListing === true) {
             $allItems = array_merge($itemsToChange, $itemsToCopy);
             $this->listingItemRepository->saveListingItems($allItems);
         } else {
             $this->listingItemRepository->updateListingItemsWorkedHours($itemsToChange, $workedHours);
         }
         $this->transaction->commit();
         return ['listing' => $listing, 'changedItems' => $itemsToChange];
     } catch (\DibiException $e) {
         $this->transaction->rollback();
         Debugger::log($e, Debugger::ERROR);
         throw $e;
     }
 }