229 lines
6.3 KiB
PHP
229 lines
6.3 KiB
PHP
<?php
|
|
|
|
class Zend_Controller_Action_Helper_Globalfunction extends Zend_Controller_Action_Helper_Abstract
|
|
{
|
|
|
|
protected $_configs;
|
|
protected $_dataHelper;
|
|
protected $_genericModel;
|
|
|
|
public function init()
|
|
{
|
|
|
|
$this->_configs = new App_Service_ConfigManager();
|
|
$this->_dataHelper = $this->getActionController()->getHelper('ExtractData');
|
|
$this->_genericModel = new Application_Model_Generic();
|
|
}
|
|
|
|
public function direct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* =====================================================================================================================
|
|
* ========= FUNZIONI RELATIVE ALLA SCADENZA
|
|
* ===========================================
|
|
* =====================================================================================================================
|
|
*/
|
|
|
|
/**
|
|
* Funzione che restituisce il numero fattura successivo per l'inserimento di una nuova fattura
|
|
* in base alla data scelta
|
|
*
|
|
* @param string $data (YYYY-mm-dd)
|
|
* @return int $num
|
|
*/
|
|
public function getNumFatturaSucc($data,$serie = null)
|
|
{
|
|
$num = $this->_genericModel->getNumFatturaSucc($data,$serie);
|
|
if ($num == NULL || $num == '' || $num == 0 || $num == FALSE)
|
|
return 1;
|
|
return $num;
|
|
}
|
|
|
|
/**
|
|
* Funzione che restituisce il numero fattura successivo per l'inserimento di una nuova fattura
|
|
* in base alla data scelta
|
|
* @return int $num
|
|
*/
|
|
public function getProgressivoInvioSucc()
|
|
{
|
|
|
|
$lastProgressivo = strtoupper($this->_genericModel->getLastProgressivoInvio());
|
|
|
|
if(!$lastProgressivo) $lastProgressivo = $this->_configs->get('lettera_progressivo_invio');
|
|
|
|
$pad = 5;
|
|
|
|
$char = preg_replace("/[0-9]/",'',$lastProgressivo);
|
|
if(strlen($char) > 0) $pad -= strlen($char);
|
|
|
|
$num = preg_replace("/[A-Za-z]/",'',$lastProgressivo);
|
|
|
|
$num = intval($num);
|
|
if (!$num) $num = 0;
|
|
|
|
$maxNum = intval(str_repeat("9", $pad));
|
|
|
|
$num += 1;
|
|
if($num > $maxNum){
|
|
//Incremento dell'ultimo carattere
|
|
if(!$char)
|
|
$char = 'A';
|
|
else
|
|
++$char;
|
|
|
|
$pad = 5 - strlen($char);
|
|
$num = 1;
|
|
}
|
|
|
|
$num = str_pad($num, $pad, "0", STR_PAD_LEFT);
|
|
if($pad < 5) $num = $char.$num;
|
|
|
|
return $num;
|
|
}
|
|
|
|
public function formattaNumeroFatturaXml($numero, $decimali = 2)
|
|
{
|
|
if ($decimali < 2) $decimali = 2;
|
|
return number_format($numero, $decimali, '.', '');
|
|
}
|
|
|
|
/**
|
|
* Funzione di validazione di una partita iva
|
|
* @param $pi
|
|
* @return string
|
|
*/
|
|
public function ControllaPIVA($pi)
|
|
{
|
|
if ($pi === '')
|
|
return '';
|
|
if (strlen($pi) != 11)
|
|
return "La lunghezza della partita IVA non è\n" . "corretta: la partita IVA dovrebbe essere lunga\n" . "esattamente 11 caratteri.\n";
|
|
if (preg_match("/^[0-9]+\$/", $pi) != 1)
|
|
return "La partita IVA contiene dei caratteri non ammessi:\n" . "la partita IVA dovrebbe contenere solo cifre.\n";
|
|
$s = 0;
|
|
for ($i = 0; $i <= 9; $i += 2)
|
|
$s += ord($pi[$i]) - ord('0');
|
|
for ($i = 1; $i <= 9; $i += 2) {
|
|
$c = 2 * (ord($pi[$i]) - ord('0'));
|
|
if ($c > 9)
|
|
$c = $c - 9;
|
|
$s += $c;
|
|
}
|
|
if ((10 - $s % 10) % 10 != ord($pi[10]) - ord('0'))
|
|
return "La partita IVA non è valida:\n" . "il codice di controllo non corrisponde.";
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* @param $value
|
|
* @return string
|
|
* Funzione ricorsiva per codificare un array di valori in Json in modo sicuro comreso i caratteri utf-8
|
|
*/
|
|
public function safeJsonEncode($value)
|
|
{
|
|
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
|
|
$encoded = json_encode($value,JSON_PRETTY_PRINT);
|
|
} else {
|
|
$encoded = json_encode($value);
|
|
}
|
|
switch (json_last_error()) {
|
|
case JSON_ERROR_NONE:
|
|
return $encoded;
|
|
case JSON_ERROR_DEPTH:
|
|
return 'Maximum stack depth exceeded';
|
|
case JSON_ERROR_STATE_MISMATCH:
|
|
return 'Underflow or the modes mismatch';
|
|
case JSON_ERROR_CTRL_CHAR:
|
|
return 'Unexpected control character found';
|
|
case JSON_ERROR_SYNTAX:
|
|
return 'Syntax error, malformed JSON';
|
|
case JSON_ERROR_UTF8:
|
|
$clean = $this->utf8ize($value);
|
|
return $this->safeJsonEncode($clean);
|
|
default:
|
|
return 'Unknown error';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $value
|
|
* @return mixed|string
|
|
* Funzione ricorsiva per convertire in modo sicuro compreso i caratetteri utf-8 dal formato Json
|
|
*/
|
|
public function safeJsonDecode($value)
|
|
{
|
|
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
|
|
$encoded = json_decode($value,JSON_PRETTY_PRINT);
|
|
} else {
|
|
$encoded = json_decode($value);
|
|
}
|
|
switch (json_last_error()) {
|
|
case JSON_ERROR_NONE:
|
|
return $encoded;
|
|
case JSON_ERROR_DEPTH:
|
|
return 'Maximum stack depth exceeded';
|
|
case JSON_ERROR_STATE_MISMATCH:
|
|
return 'Underflow or the modes mismatch';
|
|
case JSON_ERROR_CTRL_CHAR:
|
|
return 'Unexpected control character found';
|
|
case JSON_ERROR_SYNTAX:
|
|
return 'Syntax error, malformed JSON';
|
|
case JSON_ERROR_UTF8:
|
|
$clean = $this->utf8decodize($value);
|
|
return $this->safeJsonDecode($clean);
|
|
default:
|
|
return 'Unknown error';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $mixed
|
|
* @return array|string
|
|
* Funzione ricorsiva per codificare un array di valori in utf-8
|
|
*/
|
|
public function utf8ize($mixed)
|
|
{
|
|
if (is_array($mixed)) {
|
|
foreach ($mixed as $key => $value) {
|
|
$mixed[$key] = $this->utf8ize($value);
|
|
}
|
|
} else if (is_string($mixed)) {
|
|
return utf8_encode($mixed);
|
|
}
|
|
return $mixed;
|
|
}
|
|
|
|
/**
|
|
* @param $mixed
|
|
* @return array|string
|
|
* Funzione ricorsiva per decodificare un array di valori in utf-8
|
|
*/
|
|
public function utf8decodize($mixed)
|
|
{
|
|
if (is_array($mixed)) {
|
|
foreach ($mixed as $key => $value) {
|
|
$mixed[$key] = $this->utf8decodize($value);
|
|
}
|
|
} else if (is_string($mixed)) {
|
|
return utf8_decode($mixed);
|
|
}
|
|
return $mixed;
|
|
}
|
|
|
|
/**
|
|
* @param $array
|
|
* @param $key
|
|
* @return array Questa funzione passato un array lo restituisce ragruppato per la chiave desiderata
|
|
*/
|
|
function groupArrayByKey($array, $key) {
|
|
$return = array();
|
|
foreach($array as $val) {
|
|
$return[$val[$key]][] = $val;
|
|
}
|
|
return $return;
|
|
}
|
|
|
|
}
|