/** * @param Listing $baseListing * @param Listing $listingToMerge * @param array $selectedCollisionItems * @param User $ownerOfOutputListing * @return Listing * @throws NoCollisionListingItemSelectedException * @throws \Exception */ public function mergeListings(Listing $baseListing, Listing $listingToMerge, array $selectedCollisionItems = [], User $ownerOfOutputListing) { if (!$this->haveListingsSamePeriod($baseListing, $listingToMerge)) { throw new RuntimeException('Given Listings must have same Period(Year and Month).'); } try { $this->em->beginTransaction(); $items = $this->itemsService->getMergedListOfItems($this->listingItemsReader->findListingItems($baseListing->getId()), $this->listingItemsReader->findListingItems($listingToMerge->getId()), $selectedCollisionItems); $newListing = new Listing($baseListing->year, $baseListing->month, $ownerOfOutputListing); $this->em->persist($newListing); foreach ($items as $item) { /** @var ListingItem $item */ $item->setListing($newListing); $this->em->persist($item); } $this->em->flush(); $this->em->commit(); return $newListing; } catch (\Exception $e) { $this->em->rollback(); $this->em->close(); $this->onError('Merging of listings #id(' . $baseListing->getId() . ') and #id(' . $listingToMerge->getId() . ') failed.', $e, self::class); throw $e; } }
/** * @param Listing $baseListing * @param Listing $listing * @return array */ public function getMergedListingsItemsForEntireTable(Listing $baseListing, Listing $listing) { if (!$this->haveListingsSamePeriod($baseListing, $listing)) { throw new InvalidArgumentException('Given Listings must have same Period(Year and Month).'); } $items = $this->itemsService->mergeListingItems($this->listingItemsReader->findListingItems($baseListing->getId()), $this->listingItemsReader->findListingItems($listing->getId())); $days = $baseListing->getNumberOfDaysInMonth(); $result = array(); for ($day = 1; $day <= $days; $day++) { if (!array_key_exists($day, $items)) { $result[$day][] = new FillingItem(new \DateTime($baseListing->year . '-' . $baseListing->month . '-' . $day)); } else { foreach ($items[$day] as $key => $item) { $itemDec = new MergeableListingItem($item); $itemDec->setAsItemFromBaseListing(true); if ($key != 0) { $itemDec->setAsItemFromBaseListing(false); } $result[$day][] = $itemDec; } } } return $result; }
/** * @param Listing $listing * @return IDisplayableItem[] */ public function generateEntireTable(Listing $listing) { $listingItems = $this->listingItemsReader->findListingItems($listing); $displayableItems = $this->prepareDisplayableItemsCollection($listingItems); return $this->itemsService->generateEntireTable($displayableItems, $listing->getPeriod()); }