public function applicationSignAction(Request $request)
 {
     /** @var ClientAccount $account */
     $account = $this->em->getRepository('WealthbotClientBundle:ClientAccount')->find($request->get('account_id'));
     if (!$account) {
         throw $this->createNotFoundException();
     }
     $signature = $this->signatureManager->findActiveDocumentSignatureBySourceIdAndType($account->getId(), DocumentSignature::TYPE_OPEN_OR_TRANSFER_ACCOUNT);
     if (!$signature) {
         throw $this->createNotFoundException();
     }
     if (null === $signature->getDocusignEnvelopeId()) {
         $this->electronicSignature->sendEnvelopeForApplication($account);
         $this->em->refresh($signature);
     }
     $envelopeId = $signature->getDocusignEnvelopeId();
     $primaryApplicant = $account->getPrimaryApplicant();
     if ($primaryApplicant instanceof AccountOwnerInterface) {
         $recipient = new AccountOwnerRecipientAdapter($primaryApplicant);
         $returnUrl = $this->generateUrl('wealthbot_docusign_application_sign_callback', array('envelope_id' => $envelopeId, 'application_id' => $account->getId()), true);
         $embeddedUrl = $this->api->getEmbeddedSigningUrl($envelopeId, $recipient, $returnUrl);
         if ($embeddedUrl) {
             return $this->render('WealthbotSignatureBundle:Default:application_sign_iframe.html.twig', array('url' => $embeddedUrl));
         }
     }
     return $this->render('WealthbotSignatureBundle:Default:application_sign_error.html.twig', array('message' => 'An error has occurred. Please try again later.'));
 }
 /**
  * Update account and account owners signatures
  *
  * @param ClientAccount $account
  * @param string $type
  * @return \Wealthbot\SignatureBundle\Entity\DocumentSignature
  */
 public function updateAccountSignatureStatusByAccountAndType(ClientAccount $account, $type)
 {
     $signature = $this->signatureManager->findActiveDocumentSignatureBySourceIdAndType($account->getId(), $type);
     if ($signature) {
         $this->updateDocumentSignaturesStatusByEnvelopeId($signature->getDocusignEnvelopeId(), array($signature));
     }
     return $signature;
 }