openstamanager/src/Validate.php

163 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2019-07-19 17:58:03 +02:00
use API\Services;
2018-10-01 18:43:01 +02:00
use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException;
2018-12-29 12:03:22 +01:00
use Mpociot\VatCalculator\VatCalculator;
use Respect\Validation\Validator as v;
/**
* Classe per la gestione delle funzioni VALIDATE richiamabili del progetto.
*
* @since 2.4
*/
2018-03-02 19:01:30 +01:00
class Validate
{
2019-02-14 18:04:50 +01:00
public static function vatCheckIT($partita_iva)
2018-11-30 18:31:16 +01:00
{
if ($partita_iva === '') {
return true;
}
2018-12-12 18:42:41 +01:00
if (strlen($partita_iva) == 13) {
$partita_iva = substr($partita_iva, 2);
}
2018-11-30 18:31:16 +01:00
2018-12-23 14:01:59 +01:00
if (strlen($partita_iva) != 11 || preg_match('/^[0-9]+$/D', $partita_iva) != 1) {
2018-11-30 18:31:16 +01:00
return false;
}
$s = 0;
for ($i = 0; $i <= 9; $i += 2) {
$s += ord($partita_iva[$i]) - ord('0');
}
for ($i = 1; $i <= 9; $i += 2) {
2018-12-23 14:01:59 +01:00
$c = 2 * (ord($partita_iva[$i]) - ord('0'));
2018-11-30 18:31:16 +01:00
if ($c > 9) {
$c = $c - 9;
}
$s += $c;
}
2018-12-23 14:01:59 +01:00
if ((10 - $s % 10) % 10 != ord($partita_iva[10]) - ord('0')) {
2018-11-30 18:31:16 +01:00
return false;
}
return true;
}
2018-03-02 19:01:30 +01:00
/**
2018-03-01 22:05:54 +01:00
* Controlla se la partita iva inserita è valida.
*
* @param string $vat_number
*
2018-06-26 15:53:10 +02:00
* @return bool
2018-03-01 22:05:54 +01:00
*/
public static function isValidVatNumber($vat_number)
{
2019-12-03 20:13:29 +01:00
$result['valid-format'] = true;
2018-06-26 15:53:10 +02:00
if (empty($vat_number)) {
2019-12-03 20:13:29 +01:00
return $result;
2018-06-26 15:53:10 +02:00
}
2018-12-07 11:36:21 +01:00
// Controllo sulla sintassi
2019-02-14 18:04:50 +01:00
if (starts_with($vat_number, 'IT') && !static::vatCheckIT($vat_number)) {
$result['valid-format'] = false;
2019-09-19 17:50:10 +02:00
return $result;
2018-12-07 11:36:21 +01:00
}
/*
2018-06-27 10:50:41 +02:00
// Controllo con API europea ufficiale
if (extension_loaded('soap')) {
try {
$validator = new VatCalculator();
if (!$validator->isValidVATNumber($vat_number)) {
return false;
}
} catch (VATCheckUnavailableException $e) {
}
2018-12-12 18:42:41 +01:00
} */
2018-06-27 10:50:41 +02:00
// Controllo attraverso apilayer
if (Services::isEnabled()) {
$response = Services::request('post', 'check_iva', [
'partita_iva' => $vat_number,
]);
$data = Services::responseBody($response);
2018-03-01 22:05:54 +01:00
if (!empty($data['result'])) {
$result['valid-format'] = $data['result']['format_valid'];
$result['valid'] = $data['result']['valid'];
2018-03-01 22:05:54 +01:00
$fields = [];
// Ragione sociale
$fields['ragione_sociale'] = $data['result']['company_name'];
2018-03-01 22:05:54 +01:00
// Indirizzo
$address = $data['result']['company_address'];
$info = explode(PHP_EOL, $address);
$fields['indirizzo'] = $info[0];
2018-03-01 22:05:54 +01:00
$info = explode(' ', $info[1]);
2019-10-11 14:47:23 +02:00
$fields['cap'] = $info[0];
2019-10-11 14:47:23 +02:00
$fields['provincia'] = end($info);
$citta = array_slice($info, 1, -1);
$fields['citta'] = implode(' ', $citta);
2018-03-01 22:05:54 +01:00
$result['fields'] = $fields;
2018-03-02 19:01:30 +01:00
}
}
2018-03-01 22:05:54 +01:00
return $result;
2018-03-01 22:05:54 +01:00
}
/**
* Controlla se l'email inserita è valida.
*
* @param string $email
*
* @return array
*/
public static function isValidEmail($email)
{
$result = [];
2019-11-19 16:08:40 +01:00
$result['valid-format'] = true;
if (!v::email()->validate($email)) {
$result['valid-format'] = false;
2019-09-19 17:50:10 +02:00
return $result;
}
2018-06-27 10:50:41 +02:00
// Controllo attraverso apilayer
2019-07-19 17:58:03 +02:00
if (Services::isEnabled()) {
2019-09-18 18:29:00 +02:00
$response = Services::request('post', 'check_email', [
2019-07-19 17:58:03 +02:00
'email' => $email,
]);
$data = Services::responseBody($response);
if (!empty($data['result'])) {
$result['valid-format'] = $data['result']['format_valid'];
$result['smtp-check'] = $data['result']['smtp_check'];
}
}
return $result;
2018-03-02 19:01:30 +01:00
}
2018-11-16 19:14:21 +01:00
public static function isValidTaxCode($codice_fiscale)
{
2018-11-16 19:50:02 +01:00
if (empty($codice_fiscale)) {
return true;
}
2018-11-16 19:14:21 +01:00
$validator = new CodiceFiscale\Validator($codice_fiscale);
return $validator->isFormallyValid();
}
}