1
|
|
|
<?php |
2
|
|
|
namespace MrPrompt\BoletoCaixaEconomicaFederal\Shipment; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use OpenBoleto\Agente; |
6
|
|
|
use OpenBoleto\Banco\Caixa; |
7
|
|
|
use MrPrompt\ShipmentCommon\Base\Cart; |
8
|
|
|
use MrPrompt\ShipmentCommon\Util\Number; |
9
|
|
|
use MrPrompt\ShipmentCommon\Base\Customer; |
10
|
|
|
use MrPrompt\ShipmentCommon\Base\Sequence; |
11
|
|
|
use MrPrompt\BoletoCaixaEconomicaFederal\Converter\Pdf; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Billet file class |
15
|
|
|
* |
16
|
|
|
* @author Thiago Paes <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class Billet |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var DateTime |
22
|
|
|
*/ |
23
|
|
|
protected $now; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Cart |
27
|
|
|
*/ |
28
|
|
|
protected $cart; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Sequence |
32
|
|
|
*/ |
33
|
|
|
protected $sequence; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Customer |
37
|
|
|
*/ |
38
|
|
|
protected $customer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $storage; |
44
|
|
|
private $fileNameCreator; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Customer $customer |
48
|
|
|
* @param Sequence $sequence |
49
|
|
|
* @param DateTime $today |
50
|
|
|
* @param string $storageDir |
51
|
|
|
*/ |
52
|
4 |
|
public function __construct(Customer $customer, Sequence $sequence, DateTime $today, $storageDir = null) |
53
|
|
|
{ |
54
|
4 |
|
$this->fileNameCreator = new FileNameCreator(); |
55
|
4 |
|
$this->customer = $customer; |
56
|
4 |
|
$this->sequence = $sequence; |
57
|
4 |
|
$this->now = $today; |
58
|
4 |
|
$this->storage = $storageDir; |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return the cart |
63
|
|
|
* |
64
|
|
|
* @return Cart |
65
|
|
|
*/ |
66
|
1 |
|
public function getCart() |
67
|
|
|
{ |
68
|
1 |
|
return $this->cart; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the for with the payments |
73
|
|
|
* |
74
|
|
|
* @param Cart $cart |
75
|
|
|
*/ |
76
|
1 |
|
public function setCart(Cart $cart) |
77
|
|
|
{ |
78
|
1 |
|
$this->cart = $cart; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Save the output result |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
1 |
|
public function save() |
88
|
|
|
{ |
89
|
|
|
/* @var $item \BoletoCaixaEconomicaFederal\Gateway\Shipment\Partial\Detail */ |
90
|
1 |
|
$item = $this->cart->offsetGet(0); |
91
|
|
|
|
92
|
|
|
/* @var filename string */ |
93
|
1 |
|
$filename = $this->fileNameCreator->create($this->customer, $this->sequence, new DateTime()); |
94
|
|
|
|
95
|
|
|
/* @var $outputFile string */ |
96
|
1 |
|
$outputFile = $this->storage . DIRECTORY_SEPARATOR . $filename; |
97
|
|
|
|
98
|
|
|
/* @var $parcel \BoletoCaixaEconomicaFederal\Common\Base\Parcel */ |
99
|
1 |
|
$parcel = $item->getParcels()->offsetGet(0); |
100
|
|
|
|
101
|
|
|
/* @var $sacado \OpenBoleto\Agente */ |
102
|
1 |
|
$sacado = new Agente( |
103
|
1 |
|
$item->getPurchaser()->getName(), |
104
|
1 |
|
preg_replace('/[^[:digit:]]/', '', $item->getPurchaser()->getDocument()->getNumber()), |
105
|
1 |
|
sprintf( |
106
|
1 |
|
'%s %s', |
107
|
1 |
|
$item->getPurchaser()->getAddress()->getAddress(), |
108
|
1 |
|
$item->getPurchaser()->getAddress()->getComplement() |
109
|
|
|
), |
110
|
1 |
|
$item->getPurchaser()->getAddress()->getPostalCode(), |
111
|
1 |
|
$item->getPurchaser()->getAddress()->getCity(), |
112
|
1 |
|
$item->getPurchaser()->getAddress()->getState() |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
/* @var $cedente \OpenBoleto\Agente */ |
116
|
1 |
|
$cedente = new Agente( |
117
|
1 |
|
$item->getSeller()->getName(), |
118
|
1 |
|
preg_replace('/[^[:digit:]]/', '', $item->getSeller()->getDocument()->getNumber()), |
119
|
1 |
|
sprintf( |
120
|
1 |
|
'%s %s', |
121
|
1 |
|
$item->getSeller()->getAddress()->getAddress(), |
122
|
1 |
|
$item->getSeller()->getAddress()->getComplement() |
123
|
|
|
), |
124
|
1 |
|
$item->getSeller()->getAddress()->getPostalCode(), |
125
|
1 |
|
$item->getSeller()->getAddress()->getCity(), |
126
|
1 |
|
$item->getSeller()->getAddress()->getState() |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
/* @var $boleto \OpenBoleto\Banco\Caixa */ |
130
|
1 |
|
$boleto = new Caixa([ |
131
|
1 |
|
'dataVencimento' => $parcel->getMaturity(), |
132
|
1 |
|
'valor' => $parcel->getPrice(), |
133
|
1 |
|
'sequencial' => $item->getAuthorization()->getNumber(), |
134
|
1 |
|
'sacado' => $sacado, |
135
|
1 |
|
'cedente' => $cedente, |
136
|
1 |
|
'agencia' => $item->getBillet()->getBankAccount()->getBank()->getAgency(), |
137
|
1 |
|
'agenciaDv' => $item->getBillet()->getBankAccount()->getBank()->getDigit(), |
138
|
1 |
|
'carteira' => 'RG', |
139
|
1 |
|
'conta' => $item->getSeller()->getCode(), |
140
|
1 |
|
'contaDv' => 2, |
141
|
1 |
|
'moeda' => Caixa::MOEDA_REAL, |
142
|
1 |
|
'dataDocumento' => new DateTime(), |
143
|
1 |
|
'dataProcessamento' => new DateTime(), |
144
|
1 |
|
'aceite' => 'N', |
145
|
1 |
|
'especieDoc' => 'DM', |
146
|
1 |
|
'numeroDocumento' => sprintf('%s/%s', $item->getBillet()->getNumber(), $this->sequence->getValue()), |
147
|
|
|
'contraApresentacao' => false, |
148
|
|
|
'descricaoDemonstrativo' => [''], |
149
|
|
|
'instrucoes' => [''], |
150
|
|
|
]); |
151
|
|
|
|
152
|
1 |
|
$boleto->setLayout('caixa.phtml'); |
153
|
|
|
|
154
|
|
|
/* @var $content string */ |
155
|
1 |
|
$content = str_replace( |
156
|
|
|
[ |
157
|
1 |
|
$item->getBillet()->getBankAccount()->getBank()->getAgency() . '-' . $item->getBillet()->getBankAccount()->getBank()->getDigit(), |
158
|
1 |
|
'REAL' |
159
|
|
|
], |
160
|
|
|
[ |
161
|
1 |
|
$item->getBillet()->getBankAccount()->getBank()->getAgency(), |
162
|
1 |
|
'R$' |
163
|
|
|
], |
164
|
1 |
|
$boleto->getOutput() |
165
|
|
|
); |
166
|
|
|
|
167
|
1 |
|
file_put_contents($outputFile, $content); |
168
|
|
|
|
169
|
1 |
|
Pdf::convert($content, $outputFile); |
170
|
|
|
|
171
|
1 |
|
return $outputFile; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Read a return file |
176
|
|
|
* |
177
|
|
|
* @return array [Header, Detail, Footer] |
178
|
|
|
* @throws \Exception |
179
|
|
|
*/ |
180
|
1 |
|
public function read() |
181
|
|
|
{ |
182
|
1 |
|
$filename = $this->fileNameCreator->create($this->customer, $this->sequence, new DateTime()); |
183
|
1 |
|
$inputFile = $this->storage . DIRECTORY_SEPARATOR . $filename; |
184
|
|
|
|
185
|
1 |
|
return file_get_contents($inputFile); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|