Advertisement
jacknpoe

Conversões para NFSE (em progresso)

Jul 3rd, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.38 KB | None | 0 0
  1. <?php
  2.     //***********************************************************************************************
  3.     // Ricardo Erick Rebêlo
  4.     // Objetivo: conversões em geral
  5.     // Versão Original: 02/07/2014 -
  6.     // Alterações:
  7.  
  8.     // namespace jacknpoe::nf_e; (usar quando for possível editar em 5.3.0 ou posterior)
  9.  
  10.     //* CONSTANTES **********************************************************************************
  11.     // constante indefinida para todos os tipos
  12.     define( "INDEFINIDO", 0);
  13.  
  14. // TIPOS DE CONVERSÕES COMUNS (HTML, ASCII, ETC)
  15.     define( "TIPO_CONV_PARA_HTML", 1);
  16.     define( "TIPO_CONV_DE_HTML", 2);
  17.     define( "TIPO_CONV_SO_ASCII", 3);
  18.  
  19. // TIPOS DE CONVERSÕES NUMÉRICAS
  20.     define( "TIPO_CONV_NUMERO", 100);
  21.     define( "TIPO_CONV_VALOR", 101);
  22.     define( "TIPO_CONV_ALIQUOTA", 102);
  23.  
  24. // TIPOS DE CONVERSÕES TÉCNICO-NUMÉRICAS
  25.     define( "TIPO_CONV_TECN_NUMERO", 200);
  26.     define( "TIPO_CONV_TECN_VALOR", 201);
  27.     define( "TIPO_CONV_TECN_ALIQUOTA", 202);
  28.  
  29. // TIPOS DE CONVERSÕES PARA NFS-e
  30.     define( "TIPO_CONV_NFSE_NUMEROLOTE", 3001);     // N 10
  31.     define( "TIPO_CONV_NFSE_STATUS", 3002);     // 0 Normal 1 Cancelado
  32.     define( "TIPO_CONV_NFSE_NATUREZAOPERACAO", 3003);       // N 3 (101, 111, 121, 201, 301, 501, 511, 541, 551, 601, 701)
  33.     define( "TIPO_CONV_NFSE_SIMNAO", 3004);     // 1 Sim 2 Não
  34.     define( "TIPO_CONV_NFSE_QUANTIDADERPS", 3005);      // N 4
  35.     define( "TIPO_CONV_NFSE_NUMERORPS", 3006);      // N 15
  36.     define( "TIPO_CONV_NFSE_SERIERPS", 3007);       // N 5
  37.     define( "TIPO_CONV_NFSE_TIPORPS", 3008);        // N 1 (1 RPS, 2 NF Conjugada (Mista), 3 Cupom)
  38.     define( "TIPO_CONV_NFSE_VALOR", 3009);      // N 15 2 (13.2)
  39.     define( "TIPO_CONV_NFSE_ITEMLISTASERVICO", 3010);       // C 5 (ex.: 14.01 = 1401, 7.02 = 702)
  40.     define( "TIPO_CONV_NFSE_ALIQUOTA", 3011);       // 5, 4 (1.4)
  41.     define( "TIPO_CONV_NFSE_DISCRIMINACAO", 3012);      // C 2000
  42.     define( "TIPO_CONV_NFSE_CODIGOMUNICIPIOIBGE", 3013);        // N 7
  43.     define( "TIPO_CONV_NFSE_CODIGOPAISBACEN", 3014);        // N 4
  44.     define( "TIPO_CONV_NFSE_RAZAOSOCIAL", 3015);        // C 115
  45.     define( "TIPO_CONV_NFSE_CNPJ", 3016);       // C 14 (N?)
  46.     define( "TIPO_CONV_NFSE_ENDERECO", 3017);       // C 125
  47.     define( "TIPO_CONV_NFSE_NUMEROENDERECO", 3018);     // C 10
  48.     define( "TIPO_CONV_NFSE_COMPLEMENTOENDERECO", 3019);        // C 60
  49.     define( "TIPO_CONV_NFSE_BAIRRO", 3020);     // C 60
  50.     define( "TIPO_CONV_NFSE_UF", 3021);     // C 2
  51.     define( "TIPO_CONV_NFSE_CEP", 3022);        // N 8
  52.     define( "TIPO_CONV_NFSE_EMAIL", 3023);      // C 80
  53.     define( "TIPO_CONV_NFSE_TELEFONE", 3024);       // C 11 (N?)
  54.     define( "TIPO_CONV_NFSE_CPF", 3025);        // C 11 (N?)
  55.     define( "TIPO_CONV_NFSE_IDTAG", 3026);      // C 255
  56.     define( "TIPO_CONV_NFSE_INSCRICAOMUNICIPAL", 3027);     // N 15
  57.     define( "TIPO_CONV_NFSE_NUMERONFSE", 3028);     // N 15 (4+11)
  58.     define( "TIPO_CONV_NFSE_CODIGOVERIFICACAO", 3029);      // N 9
  59.     define( "TIPO_CONV_NFSE_OUTRASINFORMACOES", 3030);      // C 255
  60.     define( "TIPO_CONV_NFSE_SITUACAOLOTERPS", 3031);    // N 1 (1 não recebido; 2 processado; 3 processado erro; 4 processado sucesso)
  61.     define( "TIPO_CONV_NFSE_CODIGOCANCELAMENTONFSE", 3032);     // C 4 (tabela)
  62.     define( "TIPO_CONV_NFSE_MOTIVOCANCELAMENTONFSE", 3033);     // C 255
  63.  
  64. //* CLASSE E MÉTODOS PÚBLICOS *******************************************************************
  65.  
  66.     class JacknpoeConversor
  67.     {
  68.         // CONSTANTES
  69.         const VERSAO = '1.0';       // versão atual da classe
  70.  
  71.         function __construct()
  72.         {
  73.             // NULO
  74.         }
  75.  
  76.     // TODAS as conversões que aceitam apenas um parâmetro podem ser generalizadas neste método
  77.         function converter( $entrada, $tipo)        // generalizador de conversão
  78.         {
  79.  
  80.             switch( $tipo)
  81.             {
  82.             // CONVERSÕES COMUNS (HTML, ASCII, ETC.)
  83.                 case TIPO_CONV_PARA_HTML:
  84.                     return $this->converter_para_html( $entrada);
  85.                 case TIPO_CONV_DE_HTML :
  86.                     return $this->converter_de_html( $entrada);
  87.                 case TIPO_CONV_SO_ASCII :
  88.                     return $this->converter_so_ascii( $entrada);
  89.  
  90.             // CONVERSÕES NUMÉRICAS
  91.                 case TIPO_CONV_NUMERO :
  92.                     return $this->converter_numero( $entrada);
  93.                 case TIPO_CONV_VALOR :
  94.                     return $this->converter_valor( $entrada);
  95.                 case TIPO_CONV_ALIQUOTA :
  96.                     return $this->converter_aliquota( $entrada);
  97.  
  98.             // CONVERSÕES NFS-e
  99.                 case TIPO_CONV_NFSE_NUMEROLOTE :
  100.                     return $this->converter_nfse_numero( $entrada, 10);
  101.                 case TIPO_CONV_NFSE_STATUS :
  102.                     return $this->converter_nfse_numero( $entrada, 1);
  103.                 case TIPO_CONV_NFSE_NATUREZAOPERACAO :
  104.                     return $this->converter_nfse_numero( $entrada, 3);
  105.                 case TIPO_CONV_NFSE_SIMNAO :
  106.                     return $this->converter_nfse_numero( $entrada, 1);
  107.                 case TIPO_CONV_NFSE_QUANTIDADERPS :
  108.                     return $this->converter_nfse_numero( $entrada, 4);
  109.                 case TIPO_CONV_NFSE_NUMERORPS :
  110.                     return $this->converter_nfse_numero( $entrada, 15);
  111.                 case TIPO_CONV_NFSE_SERIERPS :
  112.                     return $this->converter_nfse_numero( $entrada, 5);
  113.                 case TIPO_CONV_NFSE_TIPORPS :
  114.                     return $this->converter_nfse_numero( $entrada, 1);
  115.                 case TIPO_CONV_NFSE_VALOR :
  116.                     return $this->converter_nfse_valor( $entrada, 15, 2);
  117.                 case TIPO_CONV_NFSE_ITEMLISTASERVICO :
  118.                     return $this->converter_nfse_numero( $entrada, 5);
  119.                 case TIPO_CONV_NFSE_ALIQUOTA :
  120.                     return $this->converter_nfse_valor( $entrada, 5, 4);
  121.                 case TIPO_CONV_NFSE_DISCRIMINACAO :
  122.                     return $this->converter_nfse_texto( $entrada, 2000);
  123.                 case TIPO_CONV_NFSE_CODIGOMUNICIPIOIBGE :
  124.                     return $this->converter_nfse_numero( $entrada, 7);
  125.                 case TIPO_CONV_NFSE_CODIGOPAISBACEN :
  126.                     return $this->converter_nfse_numero( $entrada, 4);
  127.                 case TIPO_CONV_NFSE_RAZAOSOCIAL :
  128.                     return $this->converter_nfse_texto( $entrada, 115);
  129.                 case TIPO_CONV_NFSE_CNPJ :
  130.                     return $this->converter_nfse_numero( $entrada, 14);
  131.                 case TIPO_CONV_NFSE_ENDERECO :
  132.                     return $this->converter_nfse_texto( $entrada, 125);
  133.                 case TIPO_CONV_NFSE_NUMEROENDERECO :
  134.                     return $this->converter_nfse_texto( $entrada, 10);
  135.                 case TIPO_CONV_NFSE_COMPLEMENTOENDERECO :
  136.                     return $this->converter_nfse_texto( $entrada, 60);
  137.                 case TIPO_CONV_NFSE_BAIRRO :
  138.                     return $this->converter_nfse_texto( $entrada, 60);
  139.                 case TIPO_CONV_NFSE_UF :
  140.                     return $this->converter_nfse_texto( $entrada, 2);
  141.                 case TIPO_CONV_NFSE_CEP :
  142.                     return $this->converter_nfse_numero( $entrada, 8);
  143.                 case TIPO_CONV_NFSE_EMAIL :
  144.                     return $this->converter_nfse_texto( $entrada, 80);
  145.                 case TIPO_CONV_NFSE_TELEFONE :
  146.                     return $this->converter_nfse_numero( $entrada, 11);
  147.                 case TIPO_CONV_NFSE_CPF :
  148.                     return $this->converter_nfse_numero( $entrada, 11);
  149.                 case TIPO_CONV_NFSE_IDTAG :
  150.                     return $this->converter_nfse_texto( $entrada, 255);
  151.                 case TIPO_CONV_NFSE_INSCRICAOMUNICIPAL :
  152.                     return $this->converter_nfse_numero( $entrada, 15);
  153.                 case TIPO_CONV_NFSE_NUMERONFSE :
  154.                     return $this->converter_nfse_numero( $entrada, 15);
  155.                 case TIPO_CONV_NFSE_CODIGOVERIFICACAO :
  156.                     return $this->converter_nfse_numero( $entrada, 9);
  157.                 case TIPO_CONV_NFSE_OUTRASINFORMACOES :
  158.                     return $this->converter_nfse_texto( $entrada, 255);
  159.                 case TIPO_CONV_NFSE_SITUACAOLOTERPS :
  160.                     return $this->converter_nfse_numero( $entrada, 1);
  161.                 case TIPO_CONV_NFSE_CODIGOCANCELAMENTONFSE :
  162.                     return $this->converter_nfse_texto( $entrada, 4);
  163.                 case TIPO_CONV_NFSE_MOTIVOCANCELAMENTONFSE :
  164.                     return $this->converter_nfse_texto( $entrada, 255);
  165.             // ERRO(S)
  166.                 default:
  167.                     return( 'Erro');
  168.             }
  169.         }
  170.  
  171.     // CONVERSÕES COMUNS (HTML, ASCII, ETC.)
  172.         function converter_para_html( $entrada)
  173.         {
  174.             return htmlentities( $entrada, ENT_QUOTES | ENT_HTML401, "ISO-8859-1");
  175.         }
  176.  
  177.         function converter_de_html( $entrada)
  178.         {
  179.             return html_entity_decode( $entrada, ENT_QUOTES | ENT_HTML401, "ISO-8859-1");
  180.         }
  181.  
  182.         function converter_so_ascii( $entrada)
  183.         {
  184.             return $this->apenasASCII( $entrada, TRUE);
  185.         }
  186.  
  187.     // CONVERSÕES NUMÉRICAS
  188.         function converter_numero( $entrada, $decimais = 2, $pontodecimal = ",", $pontomilhar = ".")
  189.         {
  190.             return number_format( (float)$entrada , $decimais, $pontodecimal, $pontomilhar);
  191.         }
  192.  
  193.         function converter_valor( $entrada)
  194.         {
  195.             return number_format( (float)$entrada , 2, ",", "");
  196.         }
  197.  
  198.         function converter_aliquota( $entrada)
  199.         {
  200.             return number_format( (float)$entrada , 4, ",", "");
  201.         }
  202.  
  203.     // CONVERSÕES BASE PARA NF-e
  204.         // conversão para números SEM ponto decimal (inclui aceitar apenas números de uma string)
  205.         function converter_nfse_numero( $entrada, $tamanho)
  206.         {
  207.             $temp = $this->apenasNumeros( $entrada, FALSE); // aceita apenas números da string ou o que seja
  208.             return substr( (string)$temp, $tamanho * -1, $tamanho);     // e limita o tamanho pelo final da string
  209.         }
  210.  
  211.         // conversão para números COM ponto decimal (inclui aceitar apenas números, +, -, pontos e vírgulas de uma string)
  212.         function converter_nfse_valor( $entrada, $tamanho, $decimais)
  213.         {
  214.             $temp = $this->apenasNumerosEPontuacao( $entrada, FALSE);   // aceita apenas números, pontos, vírgulas, + e -
  215.             $temp = number_format( (float)$temp, $decimais, ".", "");   // converte o número em em string com o format XX.XX
  216.             $temp = substr( (string)$temp, ($tamanho + 1) * -1, $tamanho + 1);      // limita o tamanho pelo final da string
  217.  
  218.             // retira zeros no final e ponto desnecessários
  219.             while( strlen( $temp) > 0)
  220.             {
  221.                 if( $temp[ strlen( $temp) - 1 ] == '.' ) {  $temp = substr( $temp, 0, -1); break; }
  222.  
  223.                 if( $temp[ strlen( $temp) - 1 ] == '0' ) {  $temp = substr( $temp, 0, -1); }
  224.                     else { break; }
  225.             }
  226.  
  227.             return $temp;
  228.         }
  229.  
  230.         // conversão de texto apenas ASCII mas convertido o que é possível com limite de tamanho
  231.         function converter_nfse_texto( $entrada, $tamanho)
  232.         {
  233.             $temp = $this->apenasASCII( $entrada, TRUE);    // aceita apenas ASCII mas converte o que for possível
  234.             return substr( (string)$temp, 0, $tamanho);     // e limita o tamanho pelo início da string
  235.         }
  236.  
  237. //* MÉTODOS INTERNOS (PRIVATE) *****************************************************************
  238.  
  239.     // NÃO USADO ATÉ O MOMENTO
  240.         private function retornarSemSimbolos( $texto)   // retira espaços e símbolos (ver abaixo) do início e do final de um texto
  241.         {
  242.             $antiga = '';
  243.             $texto = trim( $texto);
  244.  
  245.             while( $antiga != $texto)
  246.             {
  247.                 $antiga = $texto;
  248.  
  249.                 if( strpos( '!@"#$%-=*&,.;:/?´^~_§°', $texto[ strlen( $texto)-1 ]) !== FALSE)
  250.                     $texto = substr( $texto, 0, -1);    //  $texto = substr( $texto, 0, strlen( $texto)-1);
  251.  
  252.                 $texto = trim( $texto);
  253.             }
  254.             return $texto;
  255.         }
  256.  
  257.     // TABELA PARA CONVERSÃO (FUNÇÃO LOGO ABAIXO)
  258.         private $_tabela_conversao = array(
  259.             'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'AE', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  260.             'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
  261.             'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
  262.             'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'ae', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
  263.             'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
  264.             'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y',
  265.             'þ'=>'b', 'ÿ'=>'y', '°'=>'o', 'º'=>'o', 'ª'=>'a'
  266.         ); // retirado do manual do PHP (allixsenos at gmail dot com)
  267.  
  268.     // CONVERTE CARACTERES PARA SEM ACENTOS
  269.         private function converterAcentosEEspeciais( $texto)
  270.         {
  271.             return strtr( $texto, $this->_tabela_conversao);    // retirado do manual do PHP (allixsenos at gmail dot com)
  272.         }
  273.  
  274.     // TABELA PARA CONVERSÃO DE VÍRGULA PRA PONTO (FUNÇÃO LOGO ABAIXO)
  275.         private $_tabela_conversao_vp = array( ','=>'.');
  276.  
  277.     // CONVERTE VÍRGULA PARA PONTO
  278.         private function converterVirgulaEmPonto( $texto)
  279.         {
  280.             return strtr( $texto, $this->_tabela_conversao_vp);
  281.         }
  282.  
  283.     // NÃO USADO ATÉ O MOMENTO
  284.         private function apenasLetrasENumeros( $texto, $converter)
  285.         {
  286.             if( $converter)
  287.             {
  288.                 $texto = $this->converterAcentosEEspeciais( $texto);
  289.             }
  290.  
  291.             $texto_temp = '';
  292.  
  293.             for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  294.             {
  295.                 $ascii = ord( $texto[$contador]);
  296.  
  297.                 if ( ( $ascii >= 97 and $ascii <= 122 )
  298.                   or ( $ascii >= 65 and $ascii <= 90 )
  299.                   or ( $ascii >= 48 and $ascii <= 57 ) )
  300.                 {
  301.                     $texto_temp .= $texto[$contador];
  302.                 }
  303.             }
  304.          
  305.                 return $texto_temp;
  306.         }
  307.  
  308.     // NÃO USADO ATÉ O MOMENTO
  309.         private function apenasLetras( $texto, $converter)
  310.         {
  311.             if( $converter)
  312.             {
  313.                 $texto = $this->converterAcentosEEspeciais( $texto);
  314.             }
  315.  
  316.             $texto_temp = '';
  317.  
  318.             for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  319.             {
  320.                 $ascii = ord( $texto[$contador]);
  321.  
  322.                 if ( ( $ascii >= 97 and $ascii <= 122 )
  323.                   or ( $ascii >= 65 and $ascii <= 90 ) )
  324.                 {
  325.                     $texto_temp .= $texto[$contador];
  326.                 }
  327.             }
  328.  
  329.             return $texto_temp;
  330.         }
  331.  
  332.     // CONVERTE CARACTERES PARA SEM ACENTOS (OPCIONAL) E PEGA APENAS NÚMEROS
  333.         private function apenasNumeros( $texto, $converter)
  334.         {
  335.             if( ! is_string( $texto)) return $texto;
  336.  
  337.             if( $converter)
  338.             {
  339.                 $texto = $this->converterAcentosEEspeciais( $texto);
  340.             }
  341.  
  342.             $texto_temp = '';
  343.  
  344.             for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  345.             {
  346.                 $ascii = ord( $texto[$contador]);
  347.  
  348.                 if ( $ascii >= 48 and $ascii <= 57 )
  349.                 {
  350.                     $texto_temp .= $texto[$contador];
  351.                 }
  352.             }
  353.  
  354.             return $texto_temp;
  355.         }
  356.  
  357.     // CONVERTE CARACTERES PARA SEM ACENTOS (OPCIONAL) E PEGA APENAS NÚMEROS E PONTUAÇÃO
  358.         private function apenasNumerosEPontuacao( $texto, $converter)
  359.         {
  360.             if( ! is_string( $texto)) return $texto;
  361.  
  362.             if( $converter)
  363.             {
  364.                 $texto = $this->converterAcentosEEspeciais( $texto);
  365.             }
  366.  
  367.             $texto_temp = '';
  368.  
  369.             for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  370.             {
  371.                 $ascii = ord( $texto[$contador]);
  372.  
  373.                 if ( $ascii >= 43 and $ascii <= 57 and $ascii != 47 )
  374.                 {
  375.                     $texto_temp .= $texto[$contador];
  376.                 }
  377.             }
  378.  
  379.             return $texto_temp;
  380.         }
  381.  
  382.     // CONVERTE CARACTERES PARA SEM ACENTOS (OPCIONAL) E ESPECIAIS EM ESPAÇOS
  383.         private function apenasASCII( $texto, $converter)
  384.         {
  385.             if( $converter)
  386.             {
  387.                 $texto = $this->converterAcentosEEspeciais( $texto);
  388.             }
  389.  
  390.             $texto_temp = '';
  391.  
  392.             for ( $contador = 0; $contador < strlen( $texto); $contador++ )
  393.             {
  394.                 $ascii = ord( $texto[$contador]);
  395.  
  396.                 if ( $ascii >= 32 and $ascii <= 126)
  397.                 {
  398.                     $texto_temp .= $texto[$contador];
  399.                 }
  400.                 else
  401.                 {
  402.                     $texto_temp .= ' ';
  403.                 }
  404.             }
  405.  
  406.             return $texto_temp;
  407.         }      
  408.     }       // CLASSE JacknpoeConversor
  409. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement