/** * MergeAffiliation constructor. * * @param Affiliation $mainAffiliation * @param Affiliation $affiliation */ public function __invoke(Affiliation $mainAffiliation, Affiliation $affiliation) { die("This is not working yet, effort is not correctly transferred"); $this->setMainAffiliation($mainAffiliation); $this->setAffiliation($affiliation); //The merge is handled in several steps //Move the cost foreach ($affiliation->getCost() as $cost) { //We need to check if the $mainAffiliation has already a cost in the given period $found = false; foreach ($mainAffiliation->getCost() as $mainCost) { if ($cost->getDateStart() == $mainCost->getDateStart() && $cost->getDateEnd() == $mainCost->getDateEnd()) { //We have a cost in the main affiliation, so we need to add the cost of the $cost $mainCost->setCosts($mainCost->getCosts() + $cost->getCosts()); $this->getProjectService()->updateEntity($mainCost); $this->getProjectService()->removeEntity($cost); $found = true; } } //Not found in the original table, do a move if (!$found) { //We have no costs in the main affiliation, replace the affiliation $cost->setAffiliation($mainAffiliation); $this->getProjectService()->updateEntity($cost); } } //Move the effort foreach ($affiliation->getEffort() as $effort) { //We need to check if the $mainAffiliation has already a effort in the given period $found = false; foreach ($mainAffiliation->getEffort() as $mainEffort) { if ($effort->getDateStart() == $mainEffort->getDateStart() && $effort->getDateEnd() == $mainEffort->getDateEnd() && $effort->getWorkpackage()->getId() == $mainEffort->getWorkpackage()->getId()) { //We have a effort in the main affiliation, so we need to add the effort of the $effort $originalEffort = $mainEffort->getEffort(); $mainEffort->setEffort($originalEffort + $effort->getEffort()); $this->getProjectService()->updateEntity($mainEffort); $this->getProjectService()->removeEntity($effort); $found = true; $this->debug[] = sprintf("effort found and added %s + %s = %s", $originalEffort, $effort->getEffort(), $mainEffort->getEffort()); } } //Not found in the original table, do a move if (!$found) { //We have no efforts in the main affiliation, replace the affiliation $effort->setAffiliation($mainAffiliation); $this->getProjectService()->updateEntity($effort); $this->debug[] = sprintf("effort not found and added moved from %s to %s", $affiliation->getId(), $mainAffiliation->getId()); } } //We need to find the suited mainAffiliationVersion, which is the instance of the mainAffiliation for the given version $newAffiliationVersionList = []; foreach ($affiliation->getVersion() as $affiliationVersion) { //Affiliation is active in some versions //We have the version now, check which affiliation are also active in this version foreach ($affiliationVersion->getVersion()->getAffiliationVersion() as $affiliationVersionResult) { if ($affiliationVersionResult->getAffiliation()->getId() === $mainAffiliation->getId()) { $newAffiliationVersionList[$affiliationVersion->getVersion()->getId()] = $affiliationVersionResult; } } } //Check if all versions are present (if the affiliation has all the partners) and if not, create it foreach ($affiliation->getVersion() as $affiliationVersion) { if (!array_key_exists($affiliationVersion->getVersion()->getId(), $newAffiliationVersionList)) { $mainAffiliationVersion = new Version(); $mainAffiliationVersion->setAffiliation($mainAffiliation); $mainAffiliationVersion->setContact($mainAffiliation->getContact()); $mainAffiliationVersion->setVersion($affiliationVersion->getVersion()); $mainAffiliationVersion = $this->getAffiliationService()->newEntity($mainAffiliationVersion); $newAffiliationVersionList[$affiliationVersion->getVersion()->getId()] = $mainAffiliationVersion; } } //Move the cost in the version foreach ($affiliation->getVersion() as $affiliationVersion) { // var_dump($affiliationVersion->getId()); // var_dump($affiliationVersion->getAffiliation()->getId()); // var_dump($affiliationVersion->getVersion()->getId()); // var_dump($affiliationVersion->getCostVersion()->count()); /** @var Version $mainAffiliationVersion */ $mainAffiliationVersion = $newAffiliationVersionList[$affiliationVersion->getVersion()->getId()]; foreach ($affiliationVersion->getCostVersion() as $cost) { //We need to check if the $mainAffiliation has already a cost in the given period $found = false; foreach ($mainAffiliationVersion->getCostVersion() as $mainCost) { if ($cost->getDateStart() == $mainCost->getDateStart() && $cost->getDateEnd() == $mainCost->getDateEnd()) { //We have a cost in the main affiliation, so we need to add the cost of the $cost $mainCost->setCosts($mainCost->getCosts() + $cost->getCosts()); $this->getProjectService()->updateEntity($mainCost); $this->getProjectService()->removeEntity($cost); $found = true; } } if (!$found) { //We have no costs in the main affiliation, replace the affiliation $cost->setAffiliationVersion($mainAffiliationVersion); $this->getProjectService()->updateEntity($cost); } } } //Move the effort in the version foreach ($affiliation->getVersion() as $affiliationVersion) { /** @var Version $mainAffiliationVersion */ $mainAffiliationVersion = $newAffiliationVersionList[$affiliationVersion->getVersion()->getId()]; foreach ($affiliationVersion->getEffortVersion() as $effort) { //We need to check if the $mainAffiliation has already a effort in the given period $found = false; foreach ($mainAffiliationVersion->getEffortVersion() as $mainEffort) { if ($effort->getDateStart() == $mainEffort->getDateStart() && $effort->getDateEnd() == $mainEffort->getDateEnd() && $effort->getWorkpackage()->getId() == $mainEffort->getWorkpackage()->getId()) { //We have a effort in the main affiliation, so we need to add the effort of the $effort $originalEffort = $mainEffort->getEffort(); $mainEffort->setEffort($originalEffort + $effort->getEffort()); $this->getProjectService()->updateEntity($mainEffort); $this->getProjectService()->removeEntity($effort); $found = true; $this->debug[] = sprintf("version-effort found and added %s + %s = %s", $originalEffort, $effort->getEffort(), $mainEffort->getEffort()); } } if (!$found) { //We have no efforts in the main affiliation, replace the affiliation $effort->setAffiliationVersion($mainAffiliationVersion); $this->getProjectService()->updateEntity($effort); $this->debug[] = sprintf("version-effort not found and added moved from %s to %s", $effort->getAffiliationVersion()->getId(), $mainAffiliationVersion->getId()); } } } //Remove the versions foreach ($affiliation->getVersion() as $version) { $this->getAffiliationService()->removeEntity($version); } //move the achievements foreach ($affiliation->getAchievement() as $achievement) { $achievement->addAffiliation($mainAffiliation); $achievement->removeAffiliation($affiliation); $this->getProjectService()->updateEntity($achievement); } //move the cost changes foreach ($affiliation->getChangerequestCostChange() as $costChange) { $costChange->setAffiliation($mainAffiliation); $this->getProjectService()->updateEntity($costChange); } //move the effort spent foreach ($affiliation->getSpent() as $effortSpent) { $effortSpent->setAffiliation($mainAffiliation); $this->getProjectService()->updateEntity($effortSpent); } //move the effort spent from the PPR foreach ($affiliation->getProjectReportEffortSpent() as $reportEffortSpent) { $reportEffortSpent->setAffiliation($mainAffiliation); $this->getProjectService()->updateEntity($reportEffortSpent); } //move the dedicated project logs foreach ($affiliation->getProjectLog() as $projectLog) { $projectLog->getAffiliation()->add($mainAffiliation); $projectLog->getAffiliation()->removeElement($affiliation); $this->getProjectService()->updateEntity($projectLog); } //Move the invoices foreach ($affiliation->getInvoice() as $invoice) { $invoice->setAffiliation($mainAffiliation); $this->getAffiliationService()->updateEntity($invoice); } //Move the associates foreach ($affiliation->getAssociate() as $associate) { if (!$mainAffiliation->getAssociate()->contains($associate)) { $mainAffiliation->getAssociate()->add($associate); $this->getAffiliationService()->updateEntity($mainAffiliation); } } $this->getAffiliationService()->removeEntity($affiliation); }
/** * @return Version; */ public function createVersion() { set_time_limit(0); /* * Depending on some settings, we are doing different things. * a PO or FPP can always be overwritten, so we will throw the previous version away. * * This will give the impression that the previous version is overwritten, but we actually will * create a new version and delete the previous one (only for PO and FPP) */ switch ($this->getVersionType()->getId()) { case VersionType::TYPE_PO: //PO == FPP == CR //PO == FPP == CR case VersionType::TYPE_FPP: //FPP == PO == CR $previousVersion = $this->getProjectService()->getLatestProjectVersion($this->getVersionType(), null, true); if (!is_null($previousVersion)) { $this->getVersionService()->removeEntity($previousVersion); } break; case VersionType::TYPE_CR: break; case VersionType::TYPE_SR: break; } /* * Create the version first */ $version = new Version(); $version->setProject($this->getProject()); $version->setVersionType($this->getVersionType()); $version->setApproved(Version::STATUS_SELECTED); $authentication = $this->getServiceLocator()->getServiceLocator()->get('zfcuser_auth_service'); $version->setContact($authentication->getIdentity()); $version->setDateStart($this->getProject()->getDateStart()); $version->setDateEnd($this->getProject()->getDateEnd()); /* * Auto submit always the created version */ switch ($this->getVersionType()->getId()) { case VersionType::TYPE_PO: //PO == FPP == CR //PO == FPP == CR case VersionType::TYPE_FPP: //PO == FPP == CR //PO == FPP == CR case VersionType::TYPE_CR: $version->setDateSubmitted(new \DateTime()); break; case VersionType::TYPE_SR: break; } /* * Transfer the documents to the version */ foreach ($this->getVersionDocuments() as $document) { /* * When the document is if the correct type, we allow to merge it */ try { if ($version->getVersionType()->getId() !== VersionType::TYPE_CR && $this->getDocumentService()->setDocument($document)->canBeMerged()) { $mergedVersionDocument = new Document(); $mergedVersionDocument->setVersion($version); $mergedVersionDocument->setContentType($document->getContentType()); /* * Create the document by referencing to the corresponding plugin */ $mergedDocument = $this->getController()->createMergedDocument()->setDocument($document)->createProposal(); /* * Determine the type and select the name and document type */ switch ($version->getVersionType()->getId()) { case VersionType::TYPE_PO: $mergedVersionDocument->setType($this->getDocumentService()->getDocumentTypeByType(Type::TYPE_NAME_PO_DOCUMENT)); $mergedVersionDocument->setDocument('Auto Generated Merged PO'); break; case VersionType::TYPE_FPP: case VersionType::TYPE_CR: $mergedVersionDocument->setType($this->getDocumentService()->getDocumentTypeByType(Type::TYPE_NAME_FPP_DOCUMENT)); $mergedVersionDocument->setDocument('Auto Generated Merged FPP'); break; default: throw new \InvalidArgumentException(sprintf("Documents if this type %s cannot be merged for version %s", $this->getDocumentService()->getDocument()->getType(), $version->getVersionType())); break; } $mergedVersionDocument->setSize(strlen($mergedDocument->getMergedDocument())); /* * Create the object and save it */ $mergedVersionDocumentObject = new DocumentObject(); $mergedVersionDocumentObject->setObject($mergedDocument->getMergedDocument()); $mergedVersionDocumentObject->setDocument($mergedVersionDocument); $this->getVersionService()->newEntity($mergedVersionDocumentObject); } } catch (\Exception $exception) { //Save at least the version because this is now not handled by the new document $version = $this->getVersionService()->newEntity($version); $logger = $this->getServiceLocator()->getServiceLocator()->get('ZendLog'); $logger->Error(sprintf("Exception: Merge conflict during version create - documentId %s", $document->getId())); $logger->Info(sprintf("Message %s. Trace %s", $exception->getMessage(), $exception->getTraceAsString())); } /* * Save the document finally to avoid confusion with the merged documents which holds references * to the used objects */ /* * Save the original document here in the version */ $object = $document->getObject()[0]->getObject(); //Rewind to have the stream back rewind($object); $versionDocument = new Document(); $versionDocument->setVersion($version); $versionDocument->setDocument($document->getDocument()); $versionDocument->setType($document->getType()); $versionDocument->setSize($document->getSize()); $versionDocument->setContentType($document->getContentType()); $versionDocumentObject = new DocumentObject(); $versionDocumentObject->setObject($object); $versionDocumentObject->setDocument($versionDocument); $this->getVersionService()->newEntity($versionDocumentObject); } /* * Copy the cost and effort in the corresponding effortVersion and costEffort tables */ foreach ($this->getAffiliationService()->findAffiliationByProjectAndWhich($this->getProject()) as $affiliationService) { /* * Create the affiliationVersion. * * @var Affiliation */ $affiliation = $affiliationService->getAffiliation(); $affiliationVersion = new AffiliationVersion(); $affiliationVersion->setAffiliation($affiliation); $affiliationVersion->setContact($affiliation->getContact()); $affiliationVersion->setVersion($version); $affiliationVersion = $this->getAffiliationService()->newEntity($affiliationVersion); /* * Include the cost */ $costPerAffiliation = $this->getProjectService()->findCostByAffiliation($affiliation); foreach ($costPerAffiliation as $cost) { $costVersion = new Cost\Version(); $costVersion->setAffiliationVersion($affiliationVersion); $costVersion->setCosts($cost->getCosts()); $costVersion->setDateStart($cost->getDateStart()); $costVersion->setDateEnd($cost->getDateEnd()); $this->getProjectService()->newEntity($costVersion); } /* * Include the effort */ foreach ($this->getProject()->getWorkpackage() as $workpackage) { /* * Include the cost */ $effortPerAffiliationAndWorkpackage = $this->getProjectService()->findEffortByWorkpackageAndAffiliation($workpackage, $affiliation); foreach ($effortPerAffiliationAndWorkpackage as $effort) { $effortVersion = new Effort\Version(); $effortVersion->setAffiliationVersion($affiliationVersion); $effortVersion->setWorkpackage($effort->getWorkpackage()); $effortVersion->setEffort($effort->getEffort()); $effortVersion->setDateStart($effort->getDateStart()); $effortVersion->setDateEnd($effort->getDateEnd()); $this->getProjectService()->newEntity($effortVersion); } } } /* * Update the project */ $this->getProject()->setMode(Project::MODE_DEFAULT); $this->getProjectService()->updateEntity($this->getProject()); return $version; }