1
|
|
|
<?php |
2
|
|
|
namespace MrPrompt\CaixaEconomicaFederal; |
3
|
|
|
|
4
|
|
|
use MrPrompt\ShipmentCommon\Base\Address; |
5
|
|
|
use MrPrompt\ShipmentCommon\Base\Authorization; |
6
|
|
|
use MrPrompt\ShipmentCommon\Base\Bank; |
7
|
|
|
use MrPrompt\ShipmentCommon\Base\BankAccount; |
8
|
|
|
use MrPrompt\ShipmentCommon\Base\Billet; |
9
|
|
|
use MrPrompt\ShipmentCommon\Base\Charge; |
10
|
|
|
use MrPrompt\ShipmentCommon\Base\ConsumerUnity; |
11
|
|
|
use MrPrompt\ShipmentCommon\Base\CreditCard; |
12
|
|
|
use MrPrompt\ShipmentCommon\Base\Customer; |
13
|
|
|
use MrPrompt\ShipmentCommon\Base\Document; |
14
|
|
|
use MrPrompt\ShipmentCommon\Base\Email; |
15
|
|
|
use MrPrompt\ShipmentCommon\Base\Holder; |
16
|
|
|
use MrPrompt\ShipmentCommon\Base\Occurrence; |
17
|
|
|
use MrPrompt\ShipmentCommon\Base\Parcel; |
18
|
|
|
use MrPrompt\ShipmentCommon\Base\Parcels; |
19
|
|
|
use MrPrompt\ShipmentCommon\Base\Person; |
20
|
|
|
use MrPrompt\ShipmentCommon\Base\Phone; |
21
|
|
|
use MrPrompt\ShipmentCommon\Base\Purchaser; |
22
|
|
|
use MrPrompt\ShipmentCommon\Base\Seller; |
23
|
|
|
use MrPrompt\ShipmentCommon\Base\Sequence; |
24
|
|
|
use MrPrompt\CaixaEconomicaFederal\Shipment\Partial\Detail; |
25
|
|
|
use DateTime; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Caixa Economica Federal Factory |
29
|
|
|
* |
30
|
|
|
* @author Thiago Paes <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
abstract class Factory |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @param array $campos |
36
|
|
|
* @return Document |
37
|
|
|
*/ |
38
|
|
|
public static function createDocumentFromArray(array $campos = []) |
39
|
|
|
{ |
40
|
|
|
$document = new Document(); |
41
|
|
|
$document->setType(strlen($campos['documento']) === 11 ? Document::CPF : Document::CNPJ); |
42
|
|
|
$document->setNumber((int) $campos['documento']); |
43
|
|
|
|
44
|
|
|
return $document; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $campos |
49
|
|
|
* @return Customer |
50
|
|
|
*/ |
51
|
|
|
public static function createCustomerFromArray(array $campos = []) |
52
|
|
|
{ |
53
|
|
|
$customer = new Customer(); |
54
|
|
|
$customer->setCode((int) $campos['cliente']); |
55
|
|
|
$customer->setIdentityNumber((int) $campos['identificador']); |
56
|
|
|
|
57
|
|
|
return $customer; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $campos |
62
|
|
|
* @return Charge |
63
|
|
|
*/ |
64
|
|
|
public static function createChargeFromArray(array $campos = []) |
65
|
|
|
{ |
66
|
|
|
$charge = new Charge(); |
67
|
|
|
$charge->setCharging($campos['cobranca']); |
68
|
|
|
$charge->setOccurrence(self::createOccurrenceFromArray($campos)); |
69
|
|
|
|
70
|
|
|
return $charge; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $campos |
75
|
|
|
* @return ConsumerUnity |
76
|
|
|
*/ |
77
|
|
|
public static function createConsumerUnityFromArray(array $campos = []) |
78
|
|
|
{ |
79
|
|
|
$consumerUnity = new ConsumerUnity(); |
80
|
|
|
|
81
|
|
|
if (array_key_exists('energia', $campos)) { |
82
|
|
|
$leitura = DateTime::createFromFormat('dmY', $campos['energia']['leitura']); |
83
|
|
|
$vencimento = DateTime::createFromFormat('dmY', $campos['energia']['vencimento']); |
84
|
|
|
|
85
|
|
|
$consumerUnity->setRead($leitura); |
86
|
|
|
$consumerUnity->setMaturity($vencimento); |
87
|
|
|
$consumerUnity->setNumber($campos['energia']['numero']); |
88
|
|
|
$consumerUnity->setCode($campos['energia']['concessionaria']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $consumerUnity; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $campos |
96
|
|
|
* @return Occurrence |
97
|
|
|
*/ |
98
|
|
|
public static function createOccurrenceFromArray(array $campos = []) |
99
|
|
|
{ |
100
|
|
|
$occurrence = new Occurrence(); |
101
|
|
|
|
102
|
|
|
if (array_key_exists('ocorrencia', $campos)) { |
103
|
|
|
$occurrence->setType($campos['ocorrencia']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $occurrence; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int $number |
111
|
|
|
* @param int $type |
112
|
|
|
* @return Phone |
113
|
|
|
*/ |
114
|
|
|
public static function createPhone($number, $type = Phone::TELEPHONE) |
115
|
|
|
{ |
116
|
|
|
$phone = new Phone(); |
117
|
|
|
$phone->setNumber($number); |
118
|
|
|
$phone->setType($type); |
119
|
|
|
|
120
|
|
|
return $phone; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $campos |
125
|
|
|
* @return Person |
126
|
|
|
*/ |
127
|
|
|
public static function createPersonFromArray(array $campos = []) |
128
|
|
|
{ |
129
|
|
|
$person = new Person(); |
130
|
|
|
$person->setName($campos['nome']); |
131
|
|
|
$person->setCellPhone(self::createPhone($campos['celular'], Phone::CELLPHONE)); |
132
|
|
|
$person->setHomePhone(self::createPhone($campos['telefone1'], Phone::TELEPHONE)); |
133
|
|
|
$person->setHomePhoneSecondary(self::createPhone($campos['telefone2'], Phone::TELEPHONE)); |
134
|
|
|
$person->setDocument(self::createDocumentFromArray($campos['comprador'])); |
135
|
|
|
$person->setEmail($campos['email']); |
136
|
|
|
$person->setFatherName($campos['pai']); |
137
|
|
|
$person->setMotherName($campos['mae']); |
138
|
|
|
|
139
|
|
|
return $person; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $campos |
144
|
|
|
* @return Holder |
145
|
|
|
*/ |
146
|
|
|
public static function createHolderFromArray(array $campos = []) |
147
|
|
|
{ |
148
|
|
|
$person = new Holder(); |
149
|
|
|
|
150
|
|
|
if (array_key_exists('titular', $campos)) { |
151
|
|
|
$person->setName($campos['titular']['nome']); |
152
|
|
|
$person->setCellPhone($campos['titular']['celular']); |
153
|
|
|
$person->setDocument(self::createDocumentFromArray($campos['titular'])); |
154
|
|
|
$person->setEmail($campos['titular']['email']); |
155
|
|
|
$person->setFatherName($campos['titular']['pai']); |
156
|
|
|
$person->setMotherName($campos['titular']['mae']); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $person; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $campos |
164
|
|
|
* @return Address |
165
|
|
|
*/ |
166
|
|
|
public static function createAddressFromArray(array $campos = []) |
167
|
|
|
{ |
168
|
|
|
$address = new Address(); |
169
|
|
|
$address->setNumber($campos['numero']); |
170
|
|
|
$address->setAddress($campos['logradouro']); |
171
|
|
|
$address->setComplement($campos['complemento']); |
172
|
|
|
$address->setDistrict($campos['bairro']); |
173
|
|
|
$address->setPostalCode($campos['cep']); |
174
|
|
|
$address->setCity($campos['cidade']); |
175
|
|
|
$address->setState($campos['uf']); |
176
|
|
|
|
177
|
|
|
return $address; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array $campos |
182
|
|
|
* @return BankAccount |
183
|
|
|
*/ |
184
|
|
|
public static function createBankAccountFromArray(array $campos = []) |
185
|
|
|
{ |
186
|
|
|
$holder = new Holder(); |
187
|
|
|
$bank = new Bank(); |
188
|
|
|
$account = new BankAccount($bank, $holder); |
189
|
|
|
|
190
|
|
|
if (array_key_exists('banco', $campos)) { |
191
|
|
|
$bank->setAgency($campos['banco']['agencia']); |
192
|
|
|
$bank->setDigit($campos['banco']['digito']); |
193
|
|
|
$bank->setCode($campos['banco']['codigo']); |
194
|
|
|
|
195
|
|
|
$account->setDigit($campos['banco']['conta']['digito']); |
196
|
|
|
$account->setNumber($campos['banco']['conta']['numero']); |
197
|
|
|
$account->setOperation($campos['banco']['conta']['operacao']); |
198
|
|
|
|
199
|
|
|
if (array_key_exists('seguro', $campos['banco']['conta'])) { |
200
|
|
|
$account->setSecurity($campos['banco']['conta']['seguro']); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (array_key_exists('titular', $campos['banco']['conta'])) { |
204
|
|
|
$account->setHolder(self::createHolderFromArray($campos['banco']['conta']['titular'])); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $account; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param array $campos |
213
|
|
|
* @return Purchaser |
214
|
|
|
*/ |
215
|
|
|
public static function createPurchaserFromArray(array $campos = []) |
216
|
|
|
{ |
217
|
|
|
$purchaser = new Purchaser(); |
218
|
|
|
|
219
|
|
|
if (array_key_exists('comprador', $campos)) { |
220
|
|
|
$document = self::createDocumentFromArray($campos['comprador']); |
221
|
|
|
$address = self::createAddressFromArray($campos['comprador']['endereco']); |
222
|
|
|
$birth = DateTime::createFromFormat('dmY', $campos['comprador']['nascimento']); |
223
|
|
|
|
224
|
|
|
$purchaser->setName($campos['comprador']['nome']); |
225
|
|
|
$purchaser->setCellPhone(self::createPhone($campos['comprador']['celular'], Phone::CELLPHONE)); |
226
|
|
|
$purchaser->setHomePhone(self::createPhone($campos['comprador']['telefone1'], Phone::TELEPHONE)); |
227
|
|
|
$purchaser->setHomePhoneSecondary(self::createPhone($campos['comprador']['telefone2'], Phone::TELEPHONE)); |
228
|
|
|
$purchaser->setDocument($document); |
229
|
|
|
$purchaser->setEmail(self::createEmail($campos['comprador']['email'])); |
230
|
|
|
$purchaser->setBirth($birth); |
231
|
|
|
$purchaser->setAddress($address); |
232
|
|
|
$purchaser->setPerson($campos['comprador']['pessoa']); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $purchaser; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param $address |
240
|
|
|
* @return Email |
241
|
|
|
*/ |
242
|
|
|
public static function createEmail($address) |
243
|
|
|
{ |
244
|
|
|
return new Email($address, true); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param array $campos |
249
|
|
|
* @return CreditCard |
250
|
|
|
*/ |
251
|
|
|
public static function createCreditCardFromArray(array $campos = []) |
252
|
|
|
{ |
253
|
|
|
$creditCard = new CreditCard(); |
254
|
|
|
|
255
|
|
|
if (array_key_exists('cartao', $campos)) { |
256
|
|
|
$creditCard->setSecurityNumber($campos['cartao']['seguranca']); |
257
|
|
|
$creditCard->setValidate(DateTime::createFromFormat('mY', $campos['cartao']['validade'])); |
258
|
|
|
$creditCard->setNumber($campos['cartao']['numero']); |
259
|
|
|
$creditCard->setFlag($campos['cartao']['bandeira']); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $creditCard; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param array $campos |
267
|
|
|
* @return \SplFixedArray |
268
|
|
|
*/ |
269
|
|
|
public static function createParcelsFromArray(array $campos = []) |
270
|
|
|
{ |
271
|
|
|
$parcels = new Parcels(count($campos['parcelas'])); |
272
|
|
|
$key = 1; |
273
|
|
|
|
274
|
|
|
foreach ($campos['parcelas'] as $parcela) { |
275
|
|
|
/* @var $maturity \DateTime */ |
276
|
|
|
$maturity = DateTime::createFromFormat('dmY', $parcela['vencimento']); |
277
|
|
|
|
278
|
|
|
$parcelOne = new Parcel(); |
279
|
|
|
$parcelOne->setKey($key); |
280
|
|
|
$parcelOne->setPrice($parcela['valor']); |
281
|
|
|
$parcelOne->setQuantity($parcela['quantidade']); |
282
|
|
|
|
283
|
|
|
if ($maturity) { |
284
|
|
|
$parcelOne->setMaturity($maturity); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
if (array_key_exists('situacao', $parcela)) { |
288
|
|
|
$parcelOne->setStatus($parcela['situacao']); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$parcels->addParcel($parcelOne); |
292
|
|
|
|
293
|
|
|
$key++; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $parcels; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param array $campos |
301
|
|
|
* @return Seller |
302
|
|
|
*/ |
303
|
|
|
public static function createSellerFromArray(array $campos = []) |
304
|
|
|
{ |
305
|
|
|
$seller = new Seller(); |
306
|
|
|
|
307
|
|
|
if (array_key_exists('vendedor', $campos)) { |
308
|
|
|
$seller = new Seller(); |
309
|
|
|
$seller->setCode($campos['vendedor']['codigo']); |
310
|
|
|
$seller->setName($campos['vendedor']['nome']); |
311
|
|
|
$seller->setDocument(static::createDocumentFromArray($campos['vendedor'])); |
312
|
|
|
$seller->setAddress(static::createAddressFromArray($campos['vendedor']['endereco'])); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return $seller; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param array $campos |
320
|
|
|
* @return Authorization |
321
|
|
|
*/ |
322
|
|
|
public static function createAuthorizationFromArray(array $campos = []) |
323
|
|
|
{ |
324
|
|
|
$authorization = new Authorization(); |
325
|
|
|
|
326
|
|
|
if (array_key_exists('autorizacao', $campos)) { |
327
|
|
|
$authorization->setNumber($campos['autorizacao']); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return $authorization; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param array $campos |
335
|
|
|
* @return Billet |
336
|
|
|
*/ |
337
|
|
|
public static function createBilletFromArray(array $campos = []) |
338
|
|
|
{ |
339
|
|
|
$billet = new Billet(); |
340
|
|
|
|
341
|
|
|
if (array_key_exists('boleto', $campos)) { |
342
|
|
|
$billet->setBankAccount(self::createBankAccountFromArray($campos['boleto'])); |
343
|
|
|
$billet->setNumber((int) $campos['boleto']['documento']); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $billet; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Create a cart item object |
351
|
|
|
* |
352
|
|
|
* @param array $campos |
353
|
|
|
* @return Detail |
354
|
|
|
*/ |
355
|
|
|
public static function createDetailFromArray(array $campos = []) |
356
|
|
|
{ |
357
|
|
|
$sequence = new Sequence(); |
358
|
|
|
$customer = self::createCustomerFromArray($campos); |
359
|
|
|
$charge = self::createChargeFromArray($campos); |
360
|
|
|
$purchaser = self::createPurchaserFromArray($campos); |
361
|
|
|
$parcels = self::createParcelsFromArray($campos); |
362
|
|
|
$authorization = self::createAuthorizationFromArray($campos); |
363
|
|
|
$billet = self::createBilletFromArray($campos); |
364
|
|
|
$seller = self::createSellerFromArray($campos); |
365
|
|
|
$detail = new Detail( |
366
|
|
|
$customer, |
367
|
|
|
$charge, |
368
|
|
|
$seller, |
369
|
|
|
$purchaser, |
370
|
|
|
$parcels, |
371
|
|
|
$authorization, |
372
|
|
|
$billet, |
373
|
|
|
$sequence |
374
|
|
|
); |
375
|
|
|
|
376
|
|
|
return $detail; |
377
|
|
|
} |
378
|
|
|
} |