/**
* @return JsonModel
*/
public function checkVatAction()
{
$financialId = (int) $this->getEvent()->getRequest()->getPost()->get('financialId');
/**
* @var $financial Financial
*/
$financial = $this->getOrganisationService()->findEntityById('financial', $financialId);
if (is_null($financial->getVat()) && is_null($this->getEvent()->getRequest()->getPost()->get('vat'))) {
return new JsonModel(['success' => 'error', 'result' => $this->translate("txt-vat-number-empty")]);
}
//Overrule the vat when a VAT number is sent via the URL
if (!is_null($this->getEvent()->getRequest()->getPost()->get('vat'))) {
$vat = $this->getEvent()->getRequest()->getPost()->get('vat');
} else {
$vat = $financial->getVat();
}
$vies = new Vies();
if (false === $vies->getHeartBeat()->isAlive()) {
return new JsonModel(['success' => 'error', 'result' => 'Service is not available at the moment, please try again later.']);
} else {
try {
$result = $vies->validateVat($financial->getOrganisation()->getCountry()->getCd(), trim(str_replace($financial->getOrganisation()->getCountry()->getCd(), '', $vat)));
if ($result->isValid()) {
//Update the financial
$financial->setVatStatus(Financial::VAT_STATUS_VALID);
$financial->setDateVat(new \DateTime());
$this->getOrganisationService()->updateEntity($financial);
return new JsonModel(['success' => 'success', 'result' => 'Valid', 'status' => Financial::VAT_STATUS_VALID]);
} else {
//Update the financial
$financial->setVatStatus(Financial::VAT_STATUS_INVALID);
$financial->setDateVat(new \DateTime());
$this->getOrganisationService()->updateEntity($financial);
return new JsonModel(['success' => 'error', 'result' => 'Invalid', 'status' => Financial::VAT_STATUS_INVALID]);
}
} catch (\Exception $e) {
return new JsonModel(['success' => 'error', 'result' => $e->getMessage(), 'status' => Financial::VAT_STATUS_UNDEFINED]);
}
}
}