/** * @param ObjectManager $om */ protected function persistDemoCalls(ObjectManager $om) { $accounts = $om->getRepository('OroCRMAccountBundle:Account')->findAll(); $contacts = $om->getRepository('OroCRMContactBundle:Contact')->findAll(); $directions = array('incoming' => $om->getRepository('OroCRMCallBundle:CallDirection')->findOneBy(array('name' => 'incoming')), 'outgoing' => $om->getRepository('OroCRMCallBundle:CallDirection')->findOneBy(array('name' => 'outgoing'))); $contactCount = count($contacts); $accountCount = count($accounts); for ($i = 0; $i < 100; ++$i) { $contactRandom = rand(0, $contactCount - 1); $accountRandom = rand(0, $accountCount - 1); /** @var Contact $contact */ $contact = $contacts[$contactRandom]; /** @var Account $account */ $account = $accounts[$accountRandom]; $call = new Call(); $call->setOrganization($this->organization); $call->setOwner($contact->getOwner()); $call->setSubject($this->subjects[array_rand($this->subjects)]); $call->setDuration(new \DateTime(rand(0, 1) . ':' . rand(0, 59) . ':' . rand(0, 59), new \DateTimeZone('UTC'))); $randomPath = rand(1, 10); if ($randomPath > 2) { $call->setRelatedContact($contact); $call->setContactPhoneNumber($contact->getPrimaryPhone()); $call->setDirection($directions['outgoing']); } if ($randomPath > 3) { if ($call->getRelatedContact()) { $call->setRelatedAccount($call->getRelatedContact()->getAccounts()[0]); } else { $call->setRelatedAccount($account); } } if (is_null($call->getContactPhoneNumber())) { $phone = rand(1000000000, 9999999999); $phone = sprintf("%s-%s-%s", substr($phone, 0, 3), substr($phone, 3, 3), substr($phone, 6)); $call->setPhoneNumber($phone); $call->setDirection($directions['incoming']); } $om->persist($call); } }
/** * @param int|null $contactId * @param int|null $accountId * @return Call * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function initEntity($contactId = null, $accountId = null) { $entity = new Call(); $callStatus = $this->getDoctrine()->getRepository('OroCRMCallBundle:CallStatus')->findOneByName('completed'); $entity->setCallStatus($callStatus); $callDirection = $this->getDoctrine()->getRepository('OroCRMCallBundle:CallDirection')->findOneByName('outgoing'); $entity->setDirection($callDirection); if ($contactId) { $repository = $this->getDoctrine()->getRepository('OroCRMContactBundle:Contact'); $contact = $repository->find($contactId); if ($contact) { $entity->setRelatedContact($contact); $entity->setContactPhoneNumber($contact->getPrimaryPhone()); } else { throw new NotFoundHttpException(sprintf('Contact with ID %s is not found', $contactId)); } } if ($accountId) { $repository = $this->getDoctrine()->getRepository('OroCRMAccountBundle:Account'); /** @var Account $account */ $account = $repository->find($accountId); if ($account) { $entity->setRelatedAccount($account); } else { throw new NotFoundHttpException(sprintf('Account with ID %s is not found', $accountId)); } } return $entity; }