1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2018 Joe Nilson <joenilson at gmail.com> |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
require_once 'plugins/residentes/extras/residentes_pdf.php'; |
19
|
|
|
require_once 'plugins/residentes/extras/fpdf183/ResidentesFpdf.php'; |
20
|
|
|
require_once 'extras/phpmailer/class.phpmailer.php'; |
21
|
|
|
require_once 'extras/phpmailer/class.smtp.php'; |
22
|
|
|
require_once 'plugins/residentes/extras/residentes_controller.php'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Controller to manage all the documents to be printed, showed or emailed |
26
|
|
|
* in the Residentes plugin for FS_2017 |
27
|
|
|
* @author Joe Nilson <joenilson at gmail.com> |
28
|
|
|
*/ |
29
|
|
|
class documentos_residentes extends residentes_controller |
30
|
|
|
{ |
31
|
|
|
public $archivo; |
32
|
|
|
public $cliente_residente; |
33
|
|
|
public $documento; |
34
|
|
|
public $pagado; |
35
|
|
|
public $pendiente; |
36
|
|
|
public $tipo_accion; |
37
|
|
|
public $idprogramacion; |
38
|
|
|
public $idfactura; |
39
|
|
|
public $envio_masivo; |
40
|
|
|
public $factura; |
41
|
|
|
public $facturas; |
42
|
|
|
public $contador_facturas; |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
parent::__construct(__CLASS__, 'Documentos Residentes', 'admin', false, false, false); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function private_core() |
49
|
|
|
{ |
50
|
|
|
parent::private_core(); |
51
|
|
|
$this->init(); |
52
|
|
|
$cod = $this->filter_request('codcliente'); |
53
|
|
|
if ($cod) { |
54
|
|
|
$this->obtenerInformacionResidente($cod); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
$info_accion = $this->filter_request('info_accion'); |
57
|
|
|
$tipo_documento = $this->filter_request('tipo_documento'); |
58
|
|
|
$this->idprogramacion = $this->filter_request('idprogramacion'); |
59
|
|
|
$this->envio_masivo = !(($this->filter_request('envio_masivo') === 'false')); |
60
|
|
|
$this->tipo_accion = $tipo_documento; |
61
|
|
|
$this->verificarCantidadFacturas(); |
62
|
|
|
$this->archivo = $tipo_documento.'_'.\date('dmYhis') . '.pdf'; |
63
|
|
|
if ($this->cliente_residente && $info_accion) { |
64
|
|
|
switch ($info_accion) { |
65
|
|
|
case 'imprimir': |
66
|
|
|
$this->template = false; |
|
|
|
|
67
|
|
|
$this->imprimir_documento($tipo_documento); |
68
|
|
|
break; |
69
|
|
|
case 'enviar': |
70
|
|
|
$this->enviar_documento($tipo_documento); |
71
|
|
|
break; |
72
|
|
|
default: |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $cod |
80
|
|
|
*/ |
81
|
|
|
public function obtenerInformacionResidente($cod) |
82
|
|
|
{ |
83
|
|
|
$cliente = new cliente(); |
84
|
|
|
$this->cliente_residente = $cliente->get($cod); |
85
|
|
|
$residenteInformacion = new residentes_informacion(); |
86
|
|
|
$informacion = $residenteInformacion->get($cod); |
|
|
|
|
87
|
|
|
$residenteEdificacion = new residentes_edificaciones(); |
88
|
|
|
$residente = $residenteEdificacion->get_by_field('codcliente', $cod); |
|
|
|
|
89
|
|
|
$this->cliente_residente->inmueble = $residente[0]; |
90
|
|
|
$this->cliente_residente->informacion = $informacion; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function crear_documento($tipo_documento) |
94
|
|
|
{ |
95
|
|
|
switch ($tipo_documento) { |
96
|
|
|
case 'informacion_cobros': |
97
|
|
|
$this->crearEstadoCuenta(); |
98
|
|
|
break; |
99
|
|
|
case 'factura_residente_detallada': |
100
|
|
|
//$this->verificarCantidadFacturas(); |
101
|
|
|
$this->crearFacturaDetallada(); |
102
|
|
|
break; |
103
|
|
|
default: |
104
|
|
|
$this->documento = false; |
105
|
|
|
break; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function verificarCantidadFacturas() |
110
|
|
|
{ |
111
|
|
|
|
112
|
|
|
$facturas = $this->filter_request('idfactura'); |
113
|
|
|
if ($facturas === '') { |
114
|
|
|
$this->new_message('No hay facturas para enviar.'); |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
$this->getFacturaProgramadaPendiente($facturas); |
118
|
|
|
//$this->crearFactura(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array|string $facturas |
123
|
|
|
*/ |
124
|
|
|
private function getFacturaProgramadaPendiente($facturas) |
125
|
|
|
{ |
126
|
|
|
$lista_facturas = explode(',', $facturas); |
|
|
|
|
127
|
|
|
$this->idfactura = (is_array($lista_facturas)) ? $lista_facturas[0] : $facturas; |
|
|
|
|
128
|
|
|
if (is_array($lista_facturas)) { |
|
|
|
|
129
|
|
|
unset($lista_facturas[0]); |
130
|
|
|
$this->contador_facturas = count($lista_facturas); |
131
|
|
|
$this->facturas = implode(',', $lista_facturas); |
132
|
|
|
} else { |
133
|
|
|
$this->contador_facturas = 0; |
134
|
|
|
$this->facturas = ''; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
$facturasCliente = new factura_cliente(); |
139
|
|
|
$f = $facturasCliente->get($this->idfactura); |
140
|
|
|
$this->factura = $f; |
141
|
|
|
$this->obtenerInformacionResidente($f->codcliente); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function datosFactura() |
145
|
|
|
{ |
146
|
|
|
$datosFacturaCabecera = []; |
147
|
|
|
$datosFacturaDetalle = []; |
148
|
|
|
if ($this->idfactura !== '') { |
149
|
|
|
$facturas = new factura_cliente(); |
150
|
|
|
$factura = $facturas->get($this->idfactura); |
151
|
|
|
$datosFacturaCabecera = (array) $factura; |
152
|
|
|
if ($this->RD_plugin) { |
153
|
|
|
$ncf = new ncf_ventas(); |
154
|
|
|
$ncfTipo = $ncf->get($this->empresa->id, $factura->numero2); |
155
|
|
|
$datosFacturaCabecera['tiponcf'] = $ncfTipo[0]->tipo_descripcion; |
156
|
|
|
$datosFacturaCabecera['vencimientoncf'] = $ncfTipo[0]->fecha_vencimiento; |
157
|
|
|
} |
158
|
|
|
$lineas = $factura->get_lineas(); |
159
|
|
|
$totalAntesDescuento = 0; |
160
|
|
|
$totalDescuento = 0; |
161
|
|
|
foreach ($lineas as $linea) { |
162
|
|
|
$totalAntesDescuento += $linea->pvpsindto; |
163
|
|
|
$totalDescuento += ($linea->pvpsindto - $linea->pvptotal); |
164
|
|
|
$datosFacturaDetalle[] = (array) $linea; |
165
|
|
|
} |
166
|
|
|
$datosFacturaCabecera['total_antes_descuento'] = $totalAntesDescuento; |
167
|
|
|
$datosFacturaCabecera['total_descuento'] = $totalDescuento; |
168
|
|
|
} |
169
|
|
|
return [$datosFacturaCabecera, $datosFacturaDetalle]; |
170
|
|
|
} |
171
|
|
|
public function crearEstadoCuenta() |
172
|
|
|
{ |
173
|
|
|
$this->documento = new residentes_pdf('letter', 'portrait'); |
174
|
|
|
$this->documento->cliente_residente = $this->cliente_residente; |
175
|
|
|
$this->documento->pdf->addInfo('Title', 'Pagos Residente ' . |
176
|
|
|
$this->cliente_residente->codcliente); |
177
|
|
|
$this->documento->pdf->addInfo('Subject', 'Pagos del Residente ' . |
178
|
|
|
$this->cliente_residente->codcliente); |
179
|
|
|
$this->documento->pdf->addInfo('Author', $this->empresa->nombre); |
180
|
|
|
$this->documento->pdf->ezSetMargins(10, 10, 10, 10); |
181
|
|
|
$this->crear_documento_cobros(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function crearFacturaDetallada() |
185
|
|
|
{ |
186
|
|
|
$customerInfo = (array) $this->cliente_residente; |
187
|
|
|
$customerInfo['direccion'] = trim($this->cliente_residente->inmueble->codigo_externo()) . ' numero ' |
188
|
|
|
. $this->cliente_residente->inmueble->numero; |
189
|
|
|
$datosFactura = $this->datosFactura(); |
190
|
|
|
$datosEmpresa = (array) $this->empresa; |
191
|
|
|
$this->documento = new ResidentesFpdf('L', 'mm', 'A5'); |
|
|
|
|
192
|
|
|
// $this->documento = new ResidentesFpdf('P', 'mm', 'letter'); |
193
|
|
|
$this->documento->createDocument($datosEmpresa, $datosFactura[0], $datosFactura[1], $customerInfo); |
194
|
|
|
$this->pendiente = $this->pagosFactura(false); |
195
|
|
|
$this->documento->addEstadoCuentaPendiente($this->pendiente); |
196
|
|
|
|
197
|
|
|
if ($this->filter_request('info_accion') === 'enviar') { |
198
|
|
|
$this->documento->Output( |
199
|
|
|
'F', |
200
|
|
|
'tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo, |
201
|
|
|
true |
202
|
|
|
); |
203
|
|
|
} else { |
204
|
|
|
$this->documento->Output( |
205
|
|
|
'I', |
206
|
|
|
'factura_' .$datosFactura[0]['numero2']. '_' . \date('dmYhis') . '.pdf' |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function crear_documento_cobros() |
212
|
|
|
{ |
213
|
|
|
$this->pendiente = $this->pagosFactura(false); |
214
|
|
|
$this->pagado = $this->pagosFactura(true); |
215
|
|
|
|
216
|
|
|
$linea_actual = 0; |
217
|
|
|
$pagina = 1; |
218
|
|
|
$lppag = 32; /// líneas por página |
219
|
|
|
while ($linea_actual < count($this->pendiente)) { |
220
|
|
|
/// salto de página |
221
|
|
|
if ($linea_actual > 0) { |
222
|
|
|
$this->documento->pdf->ezNewPage(); |
223
|
|
|
} |
224
|
|
|
$this->documento->generar_pdf_cabecera($this->empresa, $lppag); |
225
|
|
|
$this->documento->generar_datos_residente($this->documento, 'informe_cobros', $lppag); |
226
|
|
|
$this->documento->generar_pdf_lineas( |
227
|
|
|
$this->documento, |
228
|
|
|
$this->pendiente, |
229
|
|
|
$linea_actual, |
230
|
|
|
$lppag, |
231
|
|
|
'pendiente' |
232
|
|
|
); |
233
|
|
|
$this->documento->set_y($this->documento->pdf->y - 16); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$linea_actual2 = 0; |
237
|
|
|
while ($linea_actual2 < count($this->pagado)) { |
238
|
|
|
if ($linea_actual2 > 0) { |
239
|
|
|
$this->documento->pdf->ezNewPage(); |
240
|
|
|
} elseif ($linea_actual === 0) { |
241
|
|
|
$this->documento->generar_pdf_cabecera($this->empresa, $lppag); |
242
|
|
|
$this->documento->generar_datos_residente($this->documento, 'informe_cobros', $lppag); |
243
|
|
|
} |
244
|
|
|
$this->documento->generar_pdf_lineas( |
245
|
|
|
$this->documento, |
246
|
|
|
$this->pagado, |
247
|
|
|
$linea_actual2, |
248
|
|
|
$lppag, |
249
|
|
|
'pagado' |
250
|
|
|
); |
251
|
|
|
$pagina++; |
252
|
|
|
} |
253
|
|
|
$this->documento->set_y(80); |
254
|
|
|
if ($this->empresa->pie_factura) { |
255
|
|
|
$this->documento->pdf->addText(20, 40, 8, fs_fix_html('<b>Generado por:</b> ' . |
256
|
|
|
$this->user->get_agente_fullname()), 0); |
257
|
|
|
$this->documento->pdf->addText( |
258
|
|
|
10, |
259
|
|
|
30, |
260
|
|
|
8, |
261
|
|
|
$this->documento->center_text(fs_fix_html($this->empresa->pie_factura), 180) |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if ($this->filter_request('info_accion') == 'enviar') { |
266
|
|
|
$this->documento->save('tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo); |
267
|
|
|
} else { |
268
|
|
|
$this->documento->show('documento_cobros_' . \date('dmYhis') . '.pdf'); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @throws phpmailerException |
274
|
|
|
*/ |
275
|
|
|
public function enviar_documento($tipo_documento) |
276
|
|
|
{ |
277
|
|
|
$this->crear_documento($tipo_documento); |
278
|
|
|
$tipo_doc = $this->generar_tipo_doc($tipo_documento); |
|
|
|
|
279
|
|
|
if (file_exists('tmp/'.FS_TMP_NAME.'enviar/'.$this->archivo)) { |
280
|
|
|
$mail = $this->empresa->new_mail(); |
281
|
|
|
$mail->FromName = $this->user->get_agente_fullname(); |
282
|
|
|
$email = (trim($this->filter_request('email')) !== '') |
|
|
|
|
283
|
|
|
? $this->filter_request('email') |
284
|
|
|
: $this->cliente_residente->informacion->email; |
285
|
|
|
$this->new_message('Enviando factura a: '.$email); |
286
|
|
|
$mail->addAddress($email, $this->cliente_residente->informacion->nombre); |
287
|
|
|
|
288
|
|
|
$elSubject = ($tipo_documento === 'informacion_cobros') |
289
|
|
|
? ': Su Estado de cuenta al '. \date('d-m-Y') |
290
|
|
|
: ': Su factura ' . $this->factura->codigo . ' ' . $this->factura->numero2; |
291
|
|
|
|
292
|
|
|
$mail->Subject = fs_fix_html($this->empresa->nombre) . $elSubject; |
293
|
|
|
$mail->AltBody = ($tipo_documento === 'informacion_cobros') |
294
|
|
|
? strip_tags($_POST['mensaje']) |
295
|
|
|
: plantilla_email( |
|
|
|
|
296
|
|
|
'factura', |
297
|
|
|
$this->factura->codigo . ' ' . $this->factura->numero2, |
298
|
|
|
$this->empresa->email_config['mail_firma'] |
299
|
|
|
); |
300
|
|
|
if (trim($this->filter_request('email_copia')) !== '') { |
301
|
|
|
if ($this->filter_request('cco') !== null) { |
|
|
|
|
302
|
|
|
$mail->addBCC( |
303
|
|
|
trim($this->filter_request('email_copia')), |
304
|
|
|
$this->cliente_residente->informacion->nombre |
305
|
|
|
); |
306
|
|
|
} else { |
307
|
|
|
$mail->addCC( |
308
|
|
|
trim($this->filter_request('email_copia')), |
309
|
|
|
$this->cliente_residente->informacion->nombre |
310
|
|
|
); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
$mail->msgHTML(nl2br($mail->AltBody)); |
314
|
|
|
$mail->isHTML(true); |
315
|
|
|
$mail->addAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo); |
316
|
|
|
|
317
|
|
|
if (is_uploaded_file($_FILES['adjunto']['tmp_name'])) { |
318
|
|
|
$mail->aºddAttachment($_FILES['adjunto']['tmp_name'], $_FILES['adjunto']['name']); |
319
|
|
|
} |
320
|
|
|
if ($this->empresa->mail_connect($mail) && $mail->send()) { |
321
|
|
|
$this->factura->femail = $this->today(); |
322
|
|
|
$this->factura->save(); |
323
|
|
|
|
324
|
|
|
$this->empresa->save_mail($mail); |
325
|
|
|
$done = true; |
|
|
|
|
326
|
|
|
} else { |
327
|
|
|
$this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo); |
331
|
|
|
|
332
|
|
|
// $mail = $this->empresa->new_mail(); |
333
|
|
|
// $mail->FromName = $this->user->get_agente_fullname(); |
334
|
|
|
// $mailDe = $this->filter_request('de'); |
335
|
|
|
// if ($_POST['de'] !== $mail->From) { |
336
|
|
|
// $mail->addReplyTo($_POST['de'], $mail->FromName); |
337
|
|
|
// } |
338
|
|
|
// $email = ($this->filter_request('email') !== '') |
339
|
|
|
// ? $this->filter_request('email') |
340
|
|
|
// : $this->cliente_residente->email; |
341
|
|
|
// $mail->addAddress($email, $this->cliente_residente->nombre); |
342
|
|
|
// if ($_POST['email_copia']) { |
343
|
|
|
// if (isset($_POST['cco'])) { |
344
|
|
|
// $mail->addBCC($_POST['email_copia'], $this->cliente_residente->nombre); |
345
|
|
|
// } else { |
346
|
|
|
// $mail->addCC($_POST['email_copia'], $this->cliente_residente->nombre); |
347
|
|
|
// } |
348
|
|
|
// } |
349
|
|
|
// |
350
|
|
|
// $mail->Subject = $this->empresa->nombre . ': ' . $tipo_doc; |
351
|
|
|
// |
352
|
|
|
// if ($this->is_html($_POST['mensaje'])) { |
353
|
|
|
// $mail->AltBody = strip_tags($_POST['mensaje']); |
354
|
|
|
// $mail->msgHTML($_POST['mensaje']); |
355
|
|
|
// $mail->isHTML(true); |
356
|
|
|
// } else { |
357
|
|
|
// $mail->Body = $_POST['mensaje']; |
358
|
|
|
// } |
359
|
|
|
// |
360
|
|
|
// $mail->addAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo); |
361
|
|
|
// if (is_uploaded_file($_FILES['adjunto']['tmp_name'])) { |
362
|
|
|
// $mail->addAttachment($_FILES['adjunto']['tmp_name'], $_FILES['adjunto']['name']); |
363
|
|
|
// } |
364
|
|
|
// |
365
|
|
|
// if ($this->empresa->mail_connect($mail) && $mail->send()) { |
366
|
|
|
// $this->new_message('Mensaje enviado correctamente.'); |
367
|
|
|
// $this->empresa->save_mail($mail); |
368
|
|
|
// } else { |
369
|
|
|
// $this->new_error_msg("Error al enviar el email: " . $mail->ErrorInfo); |
370
|
|
|
// } |
371
|
|
|
// |
372
|
|
|
// unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $this->archivo); |
373
|
|
|
} else { |
374
|
|
|
$this->new_error_msg('Imposible generar el PDF.'); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function imprimir_documento($tipo_documento) |
379
|
|
|
{ |
380
|
|
|
$this->template = false; |
|
|
|
|
381
|
|
|
$this->crear_documento($tipo_documento); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function init() |
385
|
|
|
{ |
386
|
|
|
$this->envio_masivo = false; |
387
|
|
|
$this->existe_tesoreria(); |
388
|
|
|
$this->cliente_residente = false; |
389
|
|
|
if (!file_exists('tmp/' . FS_TMP_NAME . 'enviar') && |
390
|
|
|
!mkdir($concurrentDirectory = 'tmp/' . FS_TMP_NAME . 'enviar') && |
391
|
|
|
!is_dir($concurrentDirectory)) { |
392
|
|
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory)); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
public function is_html($txt) |
397
|
|
|
{ |
398
|
|
|
return $txt !== strip_tags($txt); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
// public function url() |
402
|
|
|
// { |
403
|
|
|
// return parent::url(); |
404
|
|
|
// } |
405
|
|
|
/** |
406
|
|
|
* @param $factura |
407
|
|
|
*/ |
408
|
|
|
private function crearFactura(): void |
|
|
|
|
409
|
|
|
{ |
410
|
|
|
$this->crearFacturaDetallada(); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|