Passed
Push — master ( 4100b8...b04b3d )
by Roberto
02:50 queued 11s
created

Common::simpleGetDate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 3
dl 0
loc 12
ccs 0
cts 12
cp 0
crap 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace NFePHP\DA\Legacy;
4
5
class Common
6
{
7
8
    /**
9
     * Extrai o valor do node DOM
10
     * @param  object $theObj Instancia de DOMDocument ou DOMElement
11
     * @param  string $keyName identificador da TAG do xml
12
     * @param  string $extraTextBefore prefixo do retorno
13
     * @param  string extraTextAfter sufixo do retorno
14
     * @param  number itemNum numero do item a ser retornado
15
     * @return string
16
     */
17
    protected function getTagValue($theObj, $keyName, $extraTextBefore = '', $extraTextAfter = '', $itemNum = 0)
18
    {
19
        if (empty($theObj)) {
20
            return '';
21
        }
22
        $vct = $theObj->getElementsByTagName($keyName)->item($itemNum);
23
        if (isset($vct)) {
24
            $value = trim($vct->nodeValue);
25
            if (strpos($value, '&') !== false) {
26
                //existe um & na string, então deve ser uma entidade
27
                $value = html_entity_decode($value);
28
            }
29
            return $extraTextBefore . $value . $extraTextAfter;
30
        }
31
        return '';
32
    }
33
34
    /**
35
     * Recupera e reformata a data do padrão da NFe para dd/mm/aaaa
36
     * @author Marcos Diez
37
     * @param  DOM    $theObj
38
     * @param  string $keyName   identificador da TAG do xml
39
     * @param  string $extraText prefixo do retorno
40
     * @return string
41
     */
42
    protected function getTagDate($theObj, $keyName, $extraText = '')
43
    {
44
        if (!isset($theObj) || !is_object($theObj)) {
45
            return '';
46
        }
47
        $vct = $theObj->getElementsByTagName($keyName)->item(0);
48
        if (isset($vct)) {
49
            $theDate = explode("-", $vct->nodeValue);
50
            return $extraText . $theDate[2] . "/" . $theDate[1] . "/" . $theDate[0];
51
        }
52
        return '';
53
    }
54
55
    /**
56
     * camcula digito de controle modulo 11
57
     * @param  string $numero
58
     * @return integer modulo11 do numero passado
59
     */
60
    protected function modulo11($numero = '')
61
    {
62
        if ($numero == '') {
63
            return '';
64
        }
65
        $numero = (string) $numero;
66
        $tamanho = strlen($numero);
67
        $soma = 0;
68
        $mult = 2;
69
        for ($i = $tamanho - 1; $i >= 0; $i--) {
70
            $digito = (int) $numero[$i];
71
            $r = $digito * $mult;
72
            $soma += $r;
73
            $mult++;
74
            if ($mult == 10) {
75
                $mult = 2;
76
            }
77
        }
78
        $resto = ($soma * 10) % 11;
79
        return ($resto == 10 || $resto == 0) ? 1 : $resto;
80
    }
81
82
    /**
83
     * Converte datas no formato YMD (ex. 2009-11-02) para o formato brasileiro 02/11/2009)
84
     * @param  string $data Parâmetro extraido da NFe
85
     * @return string Formatada para apresentação da data no padrão brasileiro
86
     */
87
    protected function ymdTodmy($data = '')
88
    {
89
        if ($data == '') {
90
            return '';
91
        }
92
        $needle = "/";
93
        if (strstr($data, "-")) {
94
            $needle = "-";
95
        }
96
        $dt = explode($needle, $data);
97
        return "$dt[2]/$dt[1]/$dt[0]";
98
    }
99
100
    /**
101
     * pConvertTime
102
     * Converte a informação de data e tempo contida na NFe
103
     * @param  string $DH Informação de data e tempo extraida da NFe
104
     * @return timestamp UNIX Para uso com a funçao date do php
105
     */
106
    protected function convertTime($DH = '')
107
    {
108
        if ($DH == '') {
109
            return '';
110
        }
111
        $DH = str_replace('+', '-', $DH);
112
        $aDH = explode('T', $DH);
113
        $adDH = explode('-', $aDH[0]);
114
        if (count($aDH) > 1) {
115
            $inter = explode('-', $aDH[1]);
116
            $atDH = explode(':', $inter[0]);
117
            $timestampDH = mktime($atDH[0], $atDH[1], $atDH[2], $adDH[1], $adDH[2], $adDH[0]);
118
        } else {
119
            $timestampDH = mktime($month = $adDH[1], $day = $adDH[2], $year = $adDH[0]);
120
        }
121
        return $timestampDH;
122
    }
123
124
    /**
125
     * Função de formatação de strings onde o cerquilha # é um coringa
126
     * que será substituido por digitos contidos em campo.
127
     * @param  string $campo   String a ser formatada
128
     * @param  string $mascara Regra de formatção da string (ex. ##.###.###/####-##)
129
     * @return string Retorna o campo formatado
130
     */
131
    protected function formatField($campo = '', $mascara = '')
132
    {
133
        if ($campo == '' || $mascara == '') {
134
            return $campo;
135
        }
136
        //remove qualquer formatação que ainda exista
137
        $sLimpo = preg_replace("(/[' '-./ t]/)", '', $campo);
138
        // pega o tamanho da string e da mascara
139
        $tCampo = strlen($sLimpo);
140
        $tMask = strlen($mascara);
141
        if ($tCampo > $tMask) {
142
            $tMaior = $tCampo;
143
        } else {
144
            $tMaior = $tMask;
145
        }
146
        //contar o numero de cerquilhas da mascara
147
        $aMask = str_split($mascara);
148
        $z = 0;
149
        $flag = false;
150
        foreach ($aMask as $letra) {
151
            if ($letra == '#') {
152
                $z++;
153
            }
154
        }
155
        if ($z > $tCampo) {
156
            //o campo é menor que esperado
157
            $flag = true;
158
        }
159
        //cria uma variável grande o suficiente para conter os dados
160
        $sRetorno = '';
161
        $sRetorno = str_pad($sRetorno, $tCampo + $tMask, " ", STR_PAD_LEFT);
162
        //pega o tamanho da string de retorno
163
        $tRetorno = strlen($sRetorno);
164
        //se houve entrada de dados
165
        if ($sLimpo != '' && $mascara != '') {
166
            //inicia com a posição do ultimo digito da mascara
167
            $x = $tMask;
168
            $y = $tCampo;
169
            $cI = 0;
170
            for ($i = $tMaior - 1; $i >= 0; $i--) {
171
                if ($cI < $z) {
172
                    // e o digito da mascara é # trocar pelo digito do campo
173
                    // se o inicio da string da mascara for atingido antes de terminar
174
                    // o campo considerar #
175
                    if ($x > 0) {
176
                        $digMask = $mascara[--$x];
177
                    } else {
178
                        $digMask = '#';
179
                    }
180
                    //se o fim do campo for atingido antes do fim da mascara
181
                    //verificar se é ( se não for não use
182
                    if ($digMask == '#') {
183
                        $cI++;
184
                        if ($y > 0) {
185
                            $sRetorno[--$tRetorno] = $sLimpo[--$y];
186
                        } else {
187
                            //$sRetorno[--$tRetorno] = '';
188
                        }
189
                    } else {
190
                        if ($y > 0) {
191
                            $sRetorno[--$tRetorno] = $mascara[$x];
192
                        } else {
193
                            if ($mascara[$x] == '(') {
194
                                $sRetorno[--$tRetorno] = $mascara[$x];
195
                            }
196
                        }
197
                        $i++;
198
                    }
199
                }
200
            }
201
            if (!$flag) {
202
                if ($mascara[0] != '#') {
203
                    $sRetorno = '(' . trim($sRetorno);
204
                }
205
            }
206
            return trim($sRetorno);
207
        } else {
208
            return '';
209
        }
210
    }
211
212
    protected function tipoPag($tPag)
213
    {
214
        switch ($tPag) {
215
            case '01':
216
                $tPagNome = 'Dinheiro';
217
                break;
218
            case '02':
219
                $tPagNome = 'Cheque';
220
                break;
221
            case '03':
222
                $tPagNome = 'Cartão de Crédito';
223
                break;
224
            case '04':
225
                $tPagNome = 'Cartão de Débito';
226
                break;
227
            case '05':
228
                $tPagNome = 'Crédito Loja';
229
                break;
230
            case '10':
231
                $tPagNome = 'Vale Alimentação';
232
                break;
233
            case '11':
234
                $tPagNome = 'Vale Refeição';
235
                break;
236
            case '12':
237
                $tPagNome = 'Vale Presente';
238
                break;
239
            case '13':
240
                $tPagNome = 'Vale Combustível';
241
                break;
242
            case '14':
243
                $tPagNome = 'Duplicata Mercantil';
244
                break;
245
            case '15':
246
                $tPagNome = 'Boleto Bancário';
247
                break;
248
            case '90':
249
                $tPagNome = 'Sem Pagamento';
250
                break;
251
            case '99':
252
                $tPagNome = 'Outros';
253
                break;
254
            default:
255
                $tPagNome = '';
256
                // Adicionado default para impressão de notas da 3.10
257
        }
258
        return $tPagNome;
259
    }
260
}
261