public function beneficiariesAction(Request $request)
 {
     /** @var $em EntityManager */
     /** @var $repo ClientAccountRepository  */
     $em = $this->get('doctrine.orm.entity_manager');
     $repo = $em->getRepository('WealthbotClientBundle:ClientAccount');
     /** @var $client User */
     $client = $this->getUser();
     $profile = $client->getProfile();
     /** @var $account ClientAccount */
     $account = $repo->findOneBy(array('id' => $request->get('account_id'), 'client_id' => $client->getId()));
     if (!$account) {
         $this->createNotFoundException('You have not account with id: ' . $request->get('account_id') . '.');
     }
     $this->denyAccessForCurrentRetirementAccount($account);
     $beneficiaries = $account->getBeneficiaries();
     if (!$beneficiaries->count()) {
         if ($profile->getMaritalStatus() == Profile::CLIENT_MARITAL_STATUS_MARRIED) {
             $stepActionsKeys = array_flip(array_keys(ClientAccount::getStepActionChoices()));
             if ($stepActionsKeys[$account->getStepAction()] < $stepActionsKeys[ClientAccount::STEP_ACTION_BENEFICIARIES]) {
                 $beneficiary = $this->buildBeneficiaryByClient($client);
             }
         }
         if (!isset($beneficiary)) {
             $beneficiary = new Beneficiary();
         }
         $beneficiary->setAccount($account);
         $beneficiaries->add($beneficiary);
     }
     $isPreSaved = $request->isXmlHttpRequest();
     $form = $this->createForm(new BeneficiariesCollectionFormType($isPreSaved));
     $form->get('beneficiaries')->setData($beneficiaries);
     $originalBeneficiaries = array();
     foreach ($beneficiaries as $item) {
         $originalBeneficiaries[] = $item;
     }
     if ($request->isMethod('post')) {
         $form->bind($request);
         if ($form->isValid()) {
             $beneficiaries = $form['beneficiaries']->getData();
             foreach ($beneficiaries as $beneficiary) {
                 $beneficiary->setAccount($account);
                 $em->persist($beneficiary);
                 foreach ($originalBeneficiaries as $key => $toDel) {
                     if ($beneficiary->getId() === $toDel->getId()) {
                         unset($originalBeneficiaries[$key]);
                     }
                 }
             }
             foreach ($originalBeneficiaries as $beneficiary) {
                 $account->removeBeneficiarie($beneficiary);
                 $em->remove($beneficiary);
             }
             $em->flush();
             $em->clear();
             $account->setStepAction(ClientAccount::STEP_ACTION_BENEFICIARIES);
             $account->setIsPreSaved($isPreSaved);
             $em->persist($account);
             $em->flush();
             $redirectUrl = $this->getRedirectUrl($account, ClientAccount::STEP_ACTION_BENEFICIARIES);
             if ($isPreSaved) {
                 return $this->getJsonResponse(array('status' => 'success', 'redirect_url' => $redirectUrl));
             }
             return $this->redirect($redirectUrl);
         } else {
             if ($isPreSaved) {
                 return $this->getJsonResponse(array('status' => 'error'));
             }
         }
     }
     return $this->render($this->getTemplate('beneficiaries.html.twig'), array('client' => $client, 'account' => $account, 'form' => $form->createView()));
 }
 private function createClientBeneficiary(array $data, ClientAccount $account)
 {
     $beneficiary = new Beneficiary();
     $beneficiary->setAccount($account);
     $beneficiary->setType($data['type']);
     $beneficiary->setState($this->getReference('state-' . $data['state']));
     $beneficiary->setFirstName($data['first_name']);
     $beneficiary->setLastName($data['last_name']);
     $beneficiary->setMiddleName($data['middle_name']);
     $beneficiary->setSsn($data['ssn']);
     $beneficiary->setBirthDate(new \DateTime($data['birth_date']));
     $beneficiary->setStreet($data['street']);
     $beneficiary->setCity($data['city']);
     $beneficiary->setZip($data['zip']);
     $beneficiary->setRelationship($data['relationship']);
     $beneficiary->setShare($data['share']);
     return $beneficiary;
 }
 public function addBeneficiaryAction(Request $request)
 {
     /** @var $em EntityManager */
     /** @var $repo SystemAccountRepository */
     /** @var $beneficiaryRepo BeneficiaryRepository */
     $em = $this->get('doctrine.orm.entity_manager');
     $repo = $em->getRepository('WealthbotClientBundle:SystemAccount');
     $beneficiaryRepo = $em->getRepository('WealthbotClientBundle:Beneficiary');
     $signatureManager = $this->get('wealthbot_docusign.document_signature.manager');
     $client = $this->getUser();
     /** @var $account SystemAccount */
     $account = $repo->find($request->get('account_id'));
     if (!$account || $account->getClientId() != $client->getId()) {
         return $this->getJsonResponse(array('status' => 'error', 'message' => sprintf('You have not account id: %s.', $account->getId())));
     }
     $clientAccount = $account->getClientAccount();
     $beneficiary = new Beneficiary();
     $beneficiary->setAccount($clientAccount);
     $form = $this->createForm(new BeneficiaryFormType(false, true), $beneficiary);
     if ($request->isMethod('post')) {
         $form->bind($request);
         if ($form->isValid()) {
             /** @var Beneficiary $beneficiary */
             $beneficiary = $form->getData();
             $shareSum = $beneficiaryRepo->getBeneficiariesShareForAccount($clientAccount, $beneficiary->getType());
             if (round($beneficiary->getShare()) + $shareSum > 100) {
                 $form->get('share')->addError(new FormError('Beneficiary share can not be more then 100%.'));
             } else {
                 $em->persist($beneficiary);
                 $em->flush();
                 $this->get('wealthbot_docusign.document_signature.manager')->createSignature($beneficiary);
                 $event = new WorkflowEvent($client, $beneficiary, Workflow::TYPE_PAPERWORK);
                 $this->get('event_dispatcher')->dispatch(ClientEvents::CLIENT_WORKFLOW, $event);
                 $this->dispatchHistoryEvent($client, 'Created beneficiary');
                 return $this->getJsonResponse(array('status' => 'success', 'form' => $this->renderView('WealthbotClientBundle:Dashboard:_beneficiaries_sign.html.twig', array('signatures' => $signatureManager->findChangeBeneficiaryByClientAccount($clientAccount), 'account' => $account)), 'content' => $this->renderView('WealthbotClientBundle:Dashboard:_beneficiary_row.html.twig', array('account' => $account, 'beneficiary' => $beneficiary))));
             }
         }
         return $this->getJsonResponse(array('status' => 'error', 'content' => $this->renderView('WealthbotClientBundle:Dashboard:_beneficiary_form.html.twig', array('form' => $form->createView(), 'account' => $account))));
     }
     return $this->getJsonResponse(array('status' => 'success', 'content' => $this->renderView('WealthbotClientBundle:Dashboard:_beneficiary_form.html.twig', array('form' => $form->createView(), 'account' => $account))));
 }