has('closeDate') && !$entity->get('closeDate') && $entity->get('stage') == 'Closed Won' && ( $entity->isAttributeChanged('stage') || $entity->isNew() ) ) { $entity->set('closeDate', date('Y-m-d')); } if ($entity->get('requestId') && $entity->get('propertyId')) { $request = $this->entityManager->getEntity('RealEstateRequest', $entity->get('requestId')); $property = $this->entityManager->getEntity('RealEstateProperty', $entity->get('propertyId')); if ($request && $property) { $name = $property->get('name') . ' - ' . $request->get('name'); $entity->set('name', $name); } } if (!$entity->get('name') && $entity->get('propertyId') && $entity->get('contactId')) { $property = $this->entityManager->getEntity('RealEstateProperty', $entity->get('propertyId')); $contact = $this->entityManager->getEntity('Contact', $entity->get('contactId')); $name = $property->get('name') . ' - ' . $contact->get('name'); $entity->set('name', $name); } if (!$entity->get('name')) { $entity->set('name', 'WARNING: Detail Missing'); } } public function afterSave(Entity $entity, array $options = []) { parent::afterSave($entity, $options); if ( $entity->get('stage') == 'Closed Won' && ( $entity->isAttributeChanged('stage') || $entity->isNew() ) ) { if ($entity->get('requestId') && $entity->get('propertyId')) { $request = $this->entityManager->getEntity('RealEstateRequest', $entity->get('requestId')); $request->set('status', 'Completed'); $this->entityManager->saveEntity($request); $property = $this->entityManager->getEntity('RealEstateProperty', $entity->get('propertyId')); $property->set('status', 'Completed'); $this->entityManager->saveEntity($property); $opportunityList = $this ->where([ 'requestId' => $entity->get('requestId'), 'propertyId' => $entity->get('propertyId'), ]) ->find(); foreach ($opportunityList as $opportunity) { if ($entity->getId() == $opportunity->getId()) { continue; } $opportunity->set('stage', 'Closed Lost'); $opportunity->set('closeDate', date('Y-m-d')); $this->save($opportunity); } } } if ($entity->isNew() && $entity->get('status') !== 'Closed Lost') { if ($entity->get('requestId') && $entity->get('propertyId')) { $note = $this->entityManager->getEntity('Note'); $note->set([ 'type' => 'CreateRelated', 'parentId' => $entity->get('propertyId'), 'parentType' => 'RealEstateProperty', 'data' => [ 'action' => 'created', ], 'relatedId' => $entity->getId(), 'relatedType' => 'Opportunity' ]); $this->entityManager->saveEntity($note); $note = $this->entityManager->getEntity('Note'); $note->set([ 'type' => 'CreateRelated', 'parentId' => $entity->get('requestId'), 'parentType' => 'RealEstateRequest', 'data' => [ 'action' => 'created', ], 'relatedId' => $entity->getId(), 'relatedType' => 'Opportunity' ]); $this->entityManager->saveEntity($note); } } if ($entity->isNew() || $entity->isAttributeChanged('stage')) { if ($entity->get('requestId') && $entity->get('propertyId')) { $property = $this->entityManager->getEntity('RealEstateProperty', $entity->get('propertyId')); if ($property) { if ($entity->get('stage') !== 'Closed Lost') { $this->entityManager ->getRDBRepository('RealEstateProperty') ->getRelation($property, 'requests') ->unrelateById($entity->get('requestId')); } else { $this->entityManager ->getRDBRepository('RealEstateProperty') ->getRelation($property, 'requests') ->relateById($entity->get('requestId'), ['interestDegree' => 0]); } } } } } }