2018-03-01 20:35:25 +01:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-03-01 20:35:25 +01:00
|
|
|
|
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;
|
2018-06-26 14:26:40 +02:00
|
|
|
use Respect\Validation\Validator as v;
|
|
|
|
|
2018-03-01 20:35:25 +01:00
|
|
|
/**
|
|
|
|
* Classe per la gestione delle funzioni VALIDATE richiamabili del progetto.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
2018-03-02 19:01:30 +01:00
|
|
|
class Validate
|
2018-03-01 20:35:25 +01:00
|
|
|
{
|
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
|
2020-10-29 16:48:37 +01:00
|
|
|
if (string_starts_with($vat_number, 'IT') && !static::vatCheckIT($vat_number)) {
|
2019-09-19 10:42:20 +02:00
|
|
|
$result['valid-format'] = false;
|
2019-09-19 17:50:10 +02:00
|
|
|
|
|
|
|
return $result;
|
2018-12-07 11:36:21 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 10:42:20 +02: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-06-26 14:26:40 +02:00
|
|
|
}
|
2018-12-12 18:42:41 +01:00
|
|
|
} */
|
2018-06-26 14:26:40 +02:00
|
|
|
|
2018-06-27 10:50:41 +02:00
|
|
|
// Controllo attraverso apilayer
|
2019-09-19 10:42:20 +02:00
|
|
|
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
|
|
|
|
2019-09-19 10:42:20 +02: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
|
|
|
|
2019-09-19 10:42:20 +02:00
|
|
|
$fields = [];
|
|
|
|
// Ragione sociale
|
|
|
|
$fields['ragione_sociale'] = $data['result']['company_name'];
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2019-09-19 10:42:20 +02:00
|
|
|
// Indirizzo
|
|
|
|
$address = $data['result']['company_address'];
|
|
|
|
$info = explode(PHP_EOL, $address);
|
|
|
|
$fields['indirizzo'] = $info[0];
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2019-09-19 10:42:20 +02:00
|
|
|
$info = explode(' ', $info[1]);
|
2019-10-11 14:47:23 +02:00
|
|
|
|
2019-09-19 10:42:20 +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
|
|
|
|
2019-09-19 10:42:20 +02:00
|
|
|
$result['fields'] = $fields;
|
2018-03-02 19:01:30 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-01 22:05:54 +01:00
|
|
|
|
2019-09-19 10:42:20 +02:00
|
|
|
return $result;
|
2018-03-01 22:05:54 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:35:25 +01:00
|
|
|
/**
|
|
|
|
* Controlla se l'email inserita è valida.
|
|
|
|
*
|
|
|
|
* @param string $email
|
|
|
|
*
|
2019-09-19 10:42:20 +02:00
|
|
|
* @return array
|
2018-03-01 20:35:25 +01:00
|
|
|
*/
|
2019-09-19 10:42:20 +02:00
|
|
|
public static function isValidEmail($email)
|
2018-03-01 20:35:25 +01:00
|
|
|
{
|
2019-09-19 10:42:20 +02:00
|
|
|
$result = [];
|
2019-11-19 16:08:40 +01:00
|
|
|
$result['valid-format'] = true;
|
|
|
|
|
2018-06-26 14:26:40 +02:00
|
|
|
if (!v::email()->validate($email)) {
|
2019-09-19 10:42:20 +02:00
|
|
|
$result['valid-format'] = false;
|
2019-09-19 17:50:10 +02:00
|
|
|
|
|
|
|
return $result;
|
2018-06-26 14:26:40 +02:00
|
|
|
}
|
2018-03-01 20:35:25 +01:00
|
|
|
|
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);
|
2018-06-08 01:19:20 +02:00
|
|
|
|
2019-09-19 10:42:20 +02:00
|
|
|
if (!empty($data['result'])) {
|
|
|
|
$result['valid-format'] = $data['result']['format_valid'];
|
|
|
|
$result['smtp-check'] = $data['result']['smtp_check'];
|
|
|
|
}
|
2018-06-26 14:26:40 +02:00
|
|
|
}
|
2018-06-08 01:19:20 +02:00
|
|
|
|
2019-09-19 10:42:20 +02:00
|
|
|
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();
|
|
|
|
}
|
2018-03-01 20:35:25 +01:00
|
|
|
}
|