Пример #1
1
 /**
  * @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]);
 }
Пример #2
0
 /**
  * @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();
     }
     $projectService = $this->getProjectService()->setProject($affiliationService->getAffiliation()->getProject());
     if ($projectService->isEmpty()) {
         return $this->notFoundAction();
     }
     $organisationService = $this->getOrganisationService()->setOrganisation($affiliationService->getAffiliation()->getOrganisation());
     if ($organisationService->isEmpty()) {
         return $this->notFoundAction();
     }
     $formData = [];
     $branch = null;
     $financialAddress = null;
     if (!is_null($affiliationService->getAffiliation()->getFinancial())) {
         $branch = $affiliationService->getAffiliation()->getFinancial()->getBranch();
         $formData['attention'] = $affiliationService->getAffiliation()->getFinancial()->getContact()->getDisplayName();
         $contactService = clone $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 = $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();
         /**
          * This form is a aggregation of multiple form elements, so we treat it step by step
          */
         /**
          * If the organisation or country has changed or is not set, find the new
          */
         if ($formData['organisation'] !== $organisationService->parseOrganisationWithBranch($branch) || is_null($financialAddress) || intval($formData['country']) !== $financialAddress->getCountry()->getId() || intval($formData['contact']) !== $financialAddress->getContact()->getId()) {
             /**
              * The organisation, or country has changed, so try to find this country in the database
              */
             $organisation = $this->getOrganisationService()->findOrganisationByNameCountry(trim($formData['organisation']), $this->getGeneralService()->findEntityById('Country', $formData['country']));
             /**
              * If the organisation is 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 OrganisationType
                  */
                 $organisationType = $this->getOrganisationService()->getEntityManager()->getReference('Organisation\\Entity\\Type', 0);
                 $organisation->setType($organisationType);
             }
             $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);
         } else {
             $affiliationFinancial = $this->getAffiliationService()->getAffiliation()->getFinancial();
         }
         /**
          * The presence of a VAT number triggers the creation of a financial organisation
          */
         if (!empty($formData['vat'])) {
             if (is_null($affiliationService->getAffiliation()->getOrganisation()->getFinancial())) {
                 $organisationFinancial = new \Organisation\Entity\Financial();
             } else {
                 $organisationFinancial = $affiliationService->getAffiliation()->getOrganisation()->getFinancial();
             }
             $organisationFinancial->setOrganisation($affiliationService->getAffiliation()->getOrganisation());
             $organisationFinancial->setVat($formData['vat']);
             $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(_("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]);
 }