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