|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class Contingency make a structure to set contingency mode |
|
5
|
|
|
* |
|
6
|
|
|
* @category NFePHP |
|
7
|
|
|
* @package NFePHP\NFe\Common\Contingency |
|
8
|
|
|
* @copyright NFePHP Copyright (c) 2008-2019 |
|
9
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPLv3+ |
|
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
11
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
|
12
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
13
|
|
|
* @link http://github.com/nfephp-org/sped-nfe for the canonical source repository |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace NFePHP\NFe\Factories; |
|
17
|
|
|
|
|
18
|
|
|
use NFePHP\Common\Strings; |
|
19
|
|
|
|
|
20
|
|
|
class Contingency |
|
21
|
|
|
{ |
|
22
|
|
|
const SVCAN = 'SVCAN'; |
|
23
|
|
|
const SVCRS = 'SVCRS'; |
|
24
|
|
|
const OFFLINE = 'OFFLINE'; |
|
25
|
|
|
const EPEC = 'EPEC'; |
|
26
|
|
|
const FSDA = 'FS-DA'; |
|
27
|
|
|
const TPEMIS_FSDA = 5; |
|
28
|
|
|
const TPEMIS_OFFLINE = 9; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \stdClass |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $config; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public $type = ''; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $motive = ''; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
public $timestamp = 0; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var int |
|
48
|
|
|
*/ |
|
49
|
|
|
public $tpEmis = 1; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Constructor |
|
53
|
|
|
* @param string $contingency |
|
54
|
|
|
*/ |
|
55
|
69 |
|
public function __construct($contingency = '') |
|
56
|
|
|
{ |
|
57
|
69 |
|
$this->deactivate(); |
|
58
|
69 |
|
if (!empty($contingency)) { |
|
59
|
1 |
|
$this->load($contingency); |
|
60
|
|
|
} |
|
61
|
69 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Load json string with contingency configurations |
|
65
|
|
|
* @param string $contingency |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
69 |
|
public function load($contingency) |
|
69
|
|
|
{ |
|
70
|
69 |
|
$this->config = json_decode($contingency); |
|
71
|
69 |
|
$this->type = $this->config->type; |
|
72
|
69 |
|
$this->timestamp = $this->config->timestamp; |
|
73
|
69 |
|
$this->motive = $this->config->motive; |
|
74
|
69 |
|
$this->tpEmis = $this->config->tpEmis; |
|
75
|
69 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Create a object with contingency data |
|
79
|
|
|
* @param string $acronym Sigla do estado |
|
80
|
|
|
* @param string $motive |
|
81
|
|
|
* @param string $type Opcional parameter only used if FS-DA, EPEC or OFFLINE |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function activate($acronym, $motive, $type = '') |
|
85
|
|
|
{ |
|
86
|
1 |
|
$dt = new \DateTime('now'); |
|
87
|
|
|
$list = array( |
|
88
|
1 |
|
'AC' => 'SVCAN', |
|
89
|
|
|
'AL' => 'SVCAN', |
|
90
|
|
|
'AM' => 'SVCAN', |
|
91
|
|
|
'AP' => 'SVCRS', |
|
92
|
|
|
'BA' => 'SVCRS', |
|
93
|
|
|
'CE' => 'SVCAN', |
|
94
|
|
|
'DF' => 'SVCAN', |
|
95
|
|
|
'ES' => 'SVCRS', |
|
96
|
|
|
'GO' => 'SVCRS', |
|
97
|
|
|
'MA' => 'SVCRS', |
|
98
|
|
|
'MG' => 'SVCAN', |
|
99
|
|
|
'MS' => 'SVCRS', |
|
100
|
|
|
'MT' => 'SVCRS', |
|
101
|
|
|
'PA' => 'SVCRS', |
|
102
|
|
|
'PB' => 'SVCAN', |
|
103
|
|
|
'PE' => 'SVCRS', |
|
104
|
|
|
'PI' => 'SVCRS', |
|
105
|
|
|
'PR' => 'SVCRS', |
|
106
|
|
|
'RJ' => 'SVCAN', |
|
107
|
|
|
'RN' => 'SVCRS', |
|
108
|
|
|
'RO' => 'SVCAN', |
|
109
|
|
|
'RR' => 'SVCAN', |
|
110
|
|
|
'RS' => 'SVCAN', |
|
111
|
|
|
'SC' => 'SVCAN', |
|
112
|
|
|
'SE' => 'SVCAN', |
|
113
|
|
|
'SP' => 'SVCAN', |
|
114
|
|
|
'TO' => 'SVCAN' |
|
115
|
|
|
); |
|
116
|
1 |
|
$type = strtoupper(str_replace('-', '', $type)); |
|
117
|
|
|
|
|
118
|
1 |
|
if (empty($type)) { |
|
119
|
1 |
|
$type = $list[$acronym]; |
|
120
|
|
|
} |
|
121
|
1 |
|
$this->config = $this->configBuild($dt->getTimestamp(), $motive, $type); |
|
122
|
1 |
|
return $this->__toString(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Deactivate contingency mode |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
69 |
|
public function deactivate() |
|
130
|
|
|
{ |
|
131
|
69 |
|
$this->config = $this->configBuild(0, '', ''); |
|
132
|
69 |
|
$this->timestamp = 0; |
|
133
|
69 |
|
$this->motive = ''; |
|
134
|
69 |
|
$this->type = ''; |
|
135
|
69 |
|
$this->tpEmis = 1; |
|
136
|
69 |
|
return $this->__toString(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns a json string format |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
69 |
|
public function __toString() |
|
144
|
|
|
{ |
|
145
|
69 |
|
return json_encode($this->config); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Build parameter config as stdClass |
|
150
|
|
|
* @param int $timestamp |
|
151
|
|
|
* @param string $motive |
|
152
|
|
|
* @param string $type |
|
153
|
|
|
* @return \stdClass |
|
154
|
|
|
*/ |
|
155
|
69 |
|
private function configBuild($timestamp, $motive, $type) |
|
156
|
|
|
{ |
|
157
|
69 |
|
switch ($type) { |
|
158
|
69 |
|
case 'EPEC': |
|
159
|
|
|
$tpEmis = 4; |
|
160
|
|
|
break; |
|
161
|
69 |
|
case 'FS-DA': |
|
162
|
69 |
|
case 'FSDA': |
|
163
|
|
|
$tpEmis = 5; |
|
164
|
|
|
break; |
|
165
|
69 |
|
case 'SVC-AN': |
|
166
|
69 |
|
case 'SVCAN': |
|
167
|
1 |
|
$tpEmis = 6; |
|
168
|
1 |
|
break; |
|
169
|
69 |
|
case 'SVC-RS': |
|
170
|
69 |
|
case 'SVCRS': |
|
171
|
|
|
$tpEmis = 7; |
|
172
|
|
|
break; |
|
173
|
69 |
|
case 'OFFLINE': //this is only to model 65 NFCe |
|
174
|
|
|
$tpEmis = 9; |
|
175
|
|
|
break; |
|
176
|
|
|
default: |
|
177
|
69 |
|
if ($type == '') { |
|
178
|
69 |
|
$tpEmis = 1; |
|
179
|
69 |
|
$timestamp = 0; |
|
180
|
69 |
|
$motive = ''; |
|
181
|
69 |
|
break; |
|
182
|
|
|
} |
|
183
|
|
|
throw new \InvalidArgumentException( |
|
184
|
|
|
"Tipo de contingência " |
|
185
|
|
|
. "[$type] não está disponível;" |
|
186
|
|
|
); |
|
187
|
|
|
} |
|
188
|
69 |
|
$config = new \stdClass(); |
|
189
|
69 |
|
$config->motive = Strings::replaceUnacceptableCharacters(substr(trim($motive), 0, 256)); |
|
190
|
69 |
|
$config->timestamp = $timestamp; |
|
191
|
69 |
|
$config->type = $type; |
|
192
|
69 |
|
$config->tpEmis = $tpEmis; |
|
193
|
69 |
|
$this->load(json_encode($config)); |
|
194
|
69 |
|
return $config; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|