/** * @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; } }
public function processSaveItem(Form $form, $values) { try { $workedHours = new Entities\WorkedHours($values['workStart'], $values['workEnd'], $values['lunch'], $values['otherHours']); $this->itemFacade->setupWorkedHoursEntity($workedHours); $locality = new Entities\Locality($values['locality']); $this->itemFacade->setupLocalityEntity($locality); if (is_null($this->listingItem)) { $this->listingItem = new Entities\ListingItem($this->date->format('j'), $this->listing, $workedHours, $locality, $values['description'], $values['descOtherHours']); } else { $this->listingItem->setWorkedTime($workedHours, $values['descOtherHours']); $this->listingItem->setLocality($locality); } $this->listingItem->description = $values['description']; $listingItem = $this->itemFacade->saveListingItem($this->listingItem); } catch (Runtime\OtherHoursZeroTimeException $zt) { $form->addError(ItemUpdateFormFactory::OTHER_HOURS_ZERO_TIME_ERROR_MSG); return; } catch (Runtime\NegativeResultOfTimeCalcException $b) { $form->addError('Položku nelze uložit. Musíte mít odpracováno více hodin, než kolik strávíte obědem.'); return; } catch (Runtime\ShiftEndBeforeStartException $c) { $form->addError('Nelze skončit směnu dřív než začne. Zkontrolujte si začátek a konec směny.'); return; } catch (Runtime\ListingItemDayAlreadyExistsException $d) { $form->addError('Položku nelze uložit, protože výčetka již obsahuje záznam z tohoto dne.'); return; } catch (\DibiException $e) { $form->addError('Položka nebyla uložena. Zkuste akci opakovat později.'); return; } $this->flashMessage('Položka byla uložena.', 'success'); $this->redirect('Listing:detail#' . $listingItem->listingItemID, ['id' => $this->listing->listingID]); }