Hey,
I’m trying to develop my first module to SuiteCRM 8, however I’m getting some trouble trying to add some functionality to an input…
My objective is to have an input for the user to insert the VAT Number and then communicate with the VIES API and get some data from there.
My problem is that I’m adding a button to the edit view and embedding a js to the page, however it seems that suitecrm is ignoring my parameters, because the vardefs field appear and is working normally, only this is not populating, also I need some help to understand how can I call my module controller
My manifest.json is:
<?php
$manifest = array(
'id' => 'VATValidationPackage',
'name' => 'VAT Validation Module',
'description' => 'Adiciona o campo VAT_NUMBER ao módulo Accounts e permite a busca de informações.',
'version' => '1.0',
'author' => 'Controlink',
'acceptable_sugar_flavors' => array('CE'),
'acceptable_sugar_versions' => array(
'exact_matches' => array('6.5.25'),
),
'is_uninstallable' => true,
'published_date' => '2024-11-04',
'type' => 'module',
);
$installdefs = array(
'id' => 'VATValidationPackage',
// Copia o diretĂłrio VATValidationPackage para o sistema
'copy' => array(
array(
'from' => '<basepath>/VATValidationPackage',
'to' => 'custom/modules/VATValidationPackage',
),
),
// Configurações de idioma para pt_PT e en_us no módulo Accounts
'language' => array(
array(
'from' => '<basepath>/VATValidationPackage/language/en_us.lang.php',
'to_module' => 'Accounts',
'language' => 'en_us',
),
array(
'from' => '<basepath>/VATValidationPackage/language/pt_PT.lang.php',
'to_module' => 'Accounts',
'language' => 'pt_PT',
),
),
// Define o campo VAT_NUMBER através de vardefs
'vardefs' => array(
array(
'from' => '<basepath>/VATValidationPackage/vardefs/vat_number_field.php',
'to_module' => 'Accounts',
),
),
// Adiciona o layout de botão à visualização de detalhes do Accounts
'layoutdefs' => array(
array(
'from' => '<basepath>/VATValidationPackage/metadata/editviewdefs.php',
'to_module' => 'Accounts',
),
),
);
My metadata/editviewdefs.php:
<?php
$viewdefs['Accounts']['EditView']['templateMeta']['form']['buttons'][] = array(
'customCode' => '<input type="button" id="vat_search_button" value="{$MOD.LBL_VAT_SEARCH}" title="{$MOD.LBL_VAT_SEARCH_DESC}" class="button" />',
);
$viewdefs['Accounts']['EditView']['templateMeta']['includes'][] = array(
'file' => 'custom/modules/VATValidationPackage/javascript/vat_search.js',
);
My js file:
document.addEventListener('DOMContentLoaded', function() {
console.log('VAT Search loaded');
const vatSearchButton = document.getElementById('vat_search_button');
if (vatSearchButton) {
vatSearchButton.addEventListener('click', function() {
const vatNumber = document.getElementById('vat_number').value;
if (vatNumber) {
fetch(`index.php?module=VATValidation&action=validateVat&vat_number=${vatNumber}`)
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById('name').value = data.name;
document.getElementById('billing_address_street').value = data.address;
document.getElementById('billing_address_country').value = data.country_code;
} else {
alert(data.message);
}
})
.catch(error => console.error('Erro:', error));
} else {
alert('Insira um NIF para obter informações.');
}
});
}
});
And finally my controller:
<?php
use DragonBe\Vies\Vies;
use DragonBe\Vies\ViesException;
class VATValidationController extends SugarController
{
public function action_validateVat(): void
{
$vatNumber = $_GET['vat_number'] ?? '';
if (!empty($vatNumber)) {
$vies = new Vies();
try {
$countryCode = substr($vatNumber, 0, 2);
$vatId = substr($vatNumber, 2);
$result = $vies->validateVat($countryCode, $vatId);
if ($result->isValid()) {
echo json_encode([
'success' => true,
'name' => $result->getName(),
'address' => $result->getAddress(),
'country_code' => $result->getCountryCode(),
]);
} else {
echo json_encode(['success' => false, 'message' => 'Número de IVA inválido']);
}
} catch (ViesException $e) {
echo json_encode(['success' => false, 'message' => 'Erro VIES: ' . $e->getMessage()]);
}
} else {
echo json_encode(['success' => false, 'message' => 'NĂşmero de IVA nĂŁo fornecido']);
}
sugar_cleanup(true);
}
}
Can anyone help me figure this out?
I believe that this fetch will not work using the SuiteCRM 8, because the routing stuff, however what should be the new uri?
Best Regards,
Fairydx