/**
  * @return \Zend\Http\Response|ViewModel
  *
  * @throws \Doctrine\ORM\ORMException
  */
 public function financialAction()
 {
     $affiliationService = $this->getAffiliationService()->setAffiliationId($this->getEvent()->getRouteMatch()->getParam('id'));
     if ($affiliationService->isEmpty()) {
         return $this->notFoundAction();
     }
     /** @var ProjectService $projectService */
     $projectService = $this->getProjectService()->setProject($affiliationService->getAffiliation()->getProject());
     if ($projectService->isEmpty()) {
         return $this->notFoundAction();
     }
     /** @var OrganisationService $organisationService */
     $organisationService = $this->getOrganisationService()->setOrganisation($affiliationService->getAffiliation()->getOrganisation());
     if ($organisationService->isEmpty()) {
         return $this->notFoundAction();
     }
     $formData = ['preferredDelivery' => \Organisation\Entity\Financial::EMAIL_DELIVERY, 'omitContact' => \Organisation\Entity\Financial::OMIT_CONTACT];
     $branch = null;
     $financialAddress = null;
     $organisationFinancial = null;
     if (!is_null($affiliationService->getAffiliation()->getFinancial())) {
         //We have a financial organisation, so populate the form with this data
         $organisationService = $this->getOrganisationService()->setOrganisation($affiliationService->getAffiliation()->getFinancial()->getOrganisation());
         $organisationFinancial = $affiliationService->getAffiliation()->getFinancial()->getOrganisation()->getFinancial();
         $branch = $affiliationService->getAffiliation()->getFinancial()->getBranch();
         $formData['attention'] = $affiliationService->getAffiliation()->getFinancial()->getContact()->getDisplayName();
         /** @var ContactService $contactService */
         $contactService = $this->getContactService()->setContact($affiliationService->getAffiliation()->getFinancial()->getContact());
         $formData['contact'] = $affiliationService->getAffiliation()->getFinancial()->getContact()->getId();
         if (!is_null($financialAddress = $contactService->getFinancialAddress())) {
             $financialAddress = $contactService->getFinancialAddress()->getAddress();
             $formData['address'] = $financialAddress->getAddress();
             $formData['zipCode'] = $financialAddress->getZipCode();
             $formData['city'] = $financialAddress->getCity();
             $formData['country'] = $financialAddress->getCountry()->getId();
         }
     }
     $formData['organisation'] = $organisationService->parseOrganisationWithBranch($branch);
     $formData['registeredCountry'] = $organisationService->getOrganisation()->getCountry()->getId();
     if (!is_null($organisationFinancial)) {
         $organisationFinancial = $affiliationService->getAffiliation()->getOrganisation()->getFinancial();
         $formData['preferredDelivery'] = $organisationFinancial->getEmail();
         $formData['vat'] = $organisationFinancial->getVat();
         $formData['omitContact'] = $organisationFinancial->getOmitContact();
     }
     $form = new FinancialForm($affiliationService, $this->getGeneralService());
     $form->setData($formData);
     if ($this->getRequest()->isPost() && $form->setData($_POST) && $form->isValid()) {
         $formData = $form->getData();
         //We need to find the organisation, first by tring the VAT, then via the name and country and then just create it
         $organisation = null;
         //Check if an organisation with the given VAT is already found
         $organisationFinancial = $this->getOrganisationService()->findFinancialOrganisationWithVAT($formData['vat']);
         //If the organisation is found, it has by default an organiation
         if (!is_null($organisationFinancial)) {
             $organisation = $organisationFinancial->getOrganisation();
         }
         //try to find the organisation based on te country and name
         if (is_null($organisation)) {
             $organisation = $this->getOrganisationService()->findOrganisationByNameCountry(trim($formData['organisation']), $this->getGeneralService()->findEntityById('Country', $formData['country']));
         }
         /**
          * If the organisation is still not found, create it
          */
         if (is_null($organisation)) {
             $organisation = new Organisation();
             $organisation->setOrganisation($formData['organisation']);
             $organisation->setCountry($this->getGeneralService()->findEntityById('Country', $formData['country']));
             /**
              * @var $organisationType Type
              */
             $organisationType = $this->getOrganisationService()->getEntityManager()->getReference('Organisation\\Entity\\Type', 0);
             $organisation->setType($organisationType);
         }
         /**
          *
          * Update the affiliationFinancial
          */
         $affiliationFinancial = $this->getAffiliationService()->getAffiliation()->getFinancial();
         if (is_null($affiliationFinancial)) {
             $affiliationFinancial = new Financial();
             $affiliationFinancial->setAffiliation($this->getAffiliationService()->getAffiliation());
         }
         $affiliationFinancial->setContact($this->getContactService()->setContactId($formData['contact'])->getContact());
         $affiliationFinancial->setOrganisation($organisation);
         $affiliationFinancial->setBranch(trim(substr($formData['organisation'], strlen($organisation->getOrganisation()))));
         $this->getAffiliationService()->updateEntity($affiliationFinancial);
         if (!is_null($affiliationService->getAffiliation()->getFinancial())) {
             $organisationFinancial = $affiliationService->getAffiliation()->getFinancial()->getOrganisation()->getFinancial();
         } else {
             $organisationFinancial = $affiliationService->getAffiliation()->getOrganisation()->getFinancial();
         }
         if (is_null($organisationFinancial)) {
             $organisationFinancial = new \Organisation\Entity\Financial();
         }
         $organisationFinancial->setOrganisation($organisation);
         /**
          * The presence of a VAT number triggers the creation of a financial organisation
          */
         if (!empty($formData['vat'])) {
             $organisationFinancial->setVat($formData['vat']);
             //Do an in-situ vat check
             $vies = new Vies();
             try {
                 $result = $vies->validateVat($organisationFinancial->getOrganisation()->getCountry()->getCd(), trim(str_replace($organisationFinancial->getOrganisation()->getCountry()->getCd(), '', $formData['vat'])));
                 if ($result->isValid()) {
                     $this->flashMessenger()->setNamespace('success')->addMessage(sprintf($this->translate("txt-vat-number-is-valid"), $affiliationService->getAffiliation()));
                     //Update the financial
                     $organisationFinancial->setVatStatus(\Organisation\Entity\Financial::VAT_STATUS_VALID);
                     $organisationFinancial->setDateVat(new \DateTime());
                 } else {
                     //Update the financial
                     $organisationFinancial->setVatStatus(\Organisation\Entity\Financial::VAT_STATUS_INVALID);
                     $organisationFinancial->setDateVat(new \DateTime());
                     $this->flashMessenger()->setNamespace('error')->addMessage(sprintf($this->translate("txt-vat-number-is-invalid"), $affiliationService->getAffiliation()));
                 }
             } catch (\Exception $e) {
                 $this->flashMessenger()->setNamespace('danger')->addMessage(sprintf($this->translate("txt-vat-information-could-not-be-verified"), $affiliationService->getAffiliation()));
             }
         } else {
             $organisationFinancial->setVat(null);
         }
         $organisationFinancial->setEmail($formData['preferredDelivery']);
         $organisationFinancial->setOmitContact($formData['omitContact']);
         $this->getOrganisationService()->updateEntity($organisationFinancial);
         /*
          * save the financial address
          */
         $contactService = clone $this->getContactService()->setContact($affiliationFinancial->getContact());
         if (!is_null($contactService->getFinancialAddress())) {
             $financialAddress = $contactService->getFinancialAddress()->getAddress();
         } else {
             $financialAddress = new Address();
             $financialAddress->setContact($affiliationService->getAffiliation()->getFinancial()->getContact());
             /**
              * @var $addressType AddressType
              */
             $addressType = $this->getContactService()->getEntityManager()->getReference('Contact\\Entity\\AddressType', AddressType::ADDRESS_TYPE_FINANCIAL);
             $financialAddress->setType($addressType);
         }
         $financialAddress->setAddress($formData['address']);
         $financialAddress->setZipCode($formData['zipCode']);
         $financialAddress->setCity($formData['city']);
         /**
          * @var Country $country
          */
         $country = $this->getContactService()->getEntityManager()->getReference('General\\Entity\\Country', $formData['country']);
         $financialAddress->setCountry($country);
         $this->getContactService()->updateEntity($financialAddress);
         $this->flashMessenger()->setNamespace('success')->addMessage(sprintf($this->translate("txt-affiliation-%s-has-successfully-been-updated"), $affiliationService->getAffiliation()));
         return $this->redirect()->toRoute('community/affiliation/affiliation', ['id' => $affiliationService->getAffiliation()->getId()], ['fragment' => 'invoicing']);
     }
     return new ViewModel(['affiliationService' => $affiliationService, 'projectService' => $projectService, 'form' => $form]);
 }
 /**
  * Edit a affiliation
  *
  * @return ViewModel
  */
 public function editAction()
 {
     $affiliationService = $this->getAffiliationService()->setAffiliationId($this->getEvent()->getRouteMatch()->getParam('id'));
     $projectService = $this->getProjectService()->setProject($affiliationService->getAffiliation()->getProject());
     $formData = [];
     $formData['affiliation'] = sprintf("%s|%s", $affiliationService->getAffiliation()->getOrganisation()->getId(), $affiliationService->getAffiliation()->getBranch());
     $formData['technical'] = $affiliationService->getAffiliation()->getContact()->getId();
     $formData['valueChain'] = $affiliationService->getAffiliation()->getValueChain();
     /**
      * Check if the organisation has a financial contact
      */
     if (!is_null($affiliationService->getAffiliation()->getOrganisation()->getFinancial())) {
         $formData['preferredDelivery'] = $affiliationService->getAffiliation()->getOrganisation()->getFinancial()->getEmail();
     }
     /**
      * Check if the organisation has a financial contact
      */
     if (!is_null($affiliationService->getAffiliation()->getFinancial())) {
         $formData['financial'] = $affiliationService->getAffiliation()->getFinancial()->getContact()->getId();
     }
     $form = new Affiliation($affiliationService);
     $form->setData($formData);
     if ($this->getRequest()->isPost() && $form->setData($_POST) && $form->isValid()) {
         $formData = $form->getData();
         $affiliation = $affiliationService->getAffiliation();
         /**
          * When the deactivate button is pressed, handle this in the service layer
          */
         if (!is_null($formData['deactivate'])) {
             $this->getAffiliationService()->deactivateAffiliation($affiliation);
             $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-affiliation-%s-has-successfully-been-deactivated"), $affiliationService->getAffiliation()));
             return $this->redirect()->toRoute('community/project/project/partners', ['docRef' => $projectService->getProject()->getDocRef()]);
         }
         /**
          * When the deactivate button is pressed, handle this in the service layer
          */
         if (!is_null($formData['reactivate'])) {
             $this->getAffiliationService()->reactivateAffiliation($affiliation);
             $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-affiliation-%s-has-successfully-been-reactivated"), $affiliationService->getAffiliation()));
             return $this->redirect()->toRoute('community/affiliation/affiliation', ['id' => $affiliationService->getAffiliation()->getId()]);
         }
         /**
          * Parse the organisation and branch
          */
         list($organisationId, $branch) = explode('|', $formData['affiliation']);
         $organisation = $this->getOrganisationService()->setOrganisationId($organisationId)->getOrganisation();
         $affiliation->setOrganisation($organisation);
         $affiliation->setContact($this->getContactService()->setContactId($formData['technical'])->getContact());
         $affiliation->setBranch($branch);
         $this->getAffiliationService()->updateEntity($affiliation);
         $affiliation->setValueChain($formData['valueChain']);
         /**
          * Handle the financial organisation
          */
         if (is_null($financial = $affiliation->getFinancial())) {
             $financial = new Entity\Financial();
         }
         $financial->setOrganisation($organisation);
         $financial->setAffiliation($affiliation);
         $financial->setBranch($branch);
         $financial->setContact($this->getContactService()->setContactId($formData['financial'])->getContact());
         $this->getAffiliationService()->updateEntity($financial);
         /**
          * Handle the preferred delivery for the organisation (OrganisationFinancial)
          */
         if (is_null($organisationFinancial = $affiliation->getOrganisation()->getFinancial())) {
             $organisationFinancial = new \Organisation\Entity\Financial();
             $organisationFinancial->setOrganisation($affiliation->getOrganisation());
         }
         $organisationFinancial->setEmail((bool) $formData['preferredDelivery']);
         $this->getOrganisationService()->updateEntity($organisationFinancial);
         $this->flashMessenger()->setNamespace('success')->addMessage(sprintf(_("txt-affiliation-%s-has-successfully-been-updated"), $affiliationService->getAffiliation()));
         return $this->redirect()->toRoute('community/affiliation/affiliation', ['id' => $affiliationService->getAffiliation()->getId()]);
     }
     return new ViewModel(['affiliationService' => $affiliationService, 'projectService' => $projectService, 'form' => $form]);
 }