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