· 5 years ago · Dec 14, 2020, 06:46 PM
1<?php
2
3namespace App\Commands;
4
5@require_once __DIR__ . '/../../../../directo/informacion_pago/controller/informacionPagoDirecto.php';
6@require_once __DIR__ . '/../../../../invernada/informacion_pago/controller/informacionPagoInvernada.php';
7@require_once __DIR__ . '/../../../../module_states_log/src/LogStates.php';
8@require_once __DIR__ . '/../../../../modulo_notificaciones_sociedades/lib/class.notificacion.php';
9
10use App\Controller\BaseController;
11use App\Controller\DateManager;
12use App\Helper\Globales;
13use App\Controller\Operaciones;
14use App\Controller\Transacciones;
15use App\Model\Cuenta;
16use App\Model\DestinatarioCuenta;
17use App\Model\EstadoCuenta;
18use App\Model\InteresGenerado;
19use App\Model\Operacion;
20use App\Model\OperacionPhysisControl1;
21use App\Model\OperacionPhysisControl1Detalle;
22use App\Model\OperacionPhysisControl2;
23use App\Model\PhysisErrorLog;
24use App\Model\RegistroOperacion;
25use App\Model\RelTransaccionConsumosOperacion;
26use App\Model\Sociedad;
27use App\Model\TipoConceptoTransaccion;
28use App\Model\Transaccion;
29use App\Model\TransaccionDetalle;
30use DateInterval;
31use DateTime;
32use DCAC\Config\Config;
33use EnvioNotificacionesMobile;
34use Exception;
35use GuzzleHttp\Client;
36use GuzzleHttp\Exception\GuzzleException;
37use manejo_invernada;
38use Modules\LogStates;
39use MODULOS\directo\informacion_pago\controller\InformacionPagoDirecto;
40use MODULOS\invernada\informacion_pago\controller\InformacionPagoInvernada;
41use MODULOS\modulo_notificaciones_sociedades\lib\Notificacion\Notificacion;
42use negocios;
43use PDO;
44use trigger_notif;
45
46
47class ProcesarOperacionesLiquidadasTask extends BaseController {
48 /**
49 * ProcesarOperacionesLiquidadasTask command
50 * @return void
51 * @throws GuzzleException
52 */
53 public function command() {
54 try {
55 // Truncar tablas?
56// (new Operacion())->getRepository($this->getCi())->__truncateTable();
57// (new InteresGenerado())->getRepository($this->getCi())->__truncateTable();
58// (new EstadoCuenta())->getRepository($this->getCi())->__truncateTable();
59// (new RegistroOperacion())->getRepository($this->getCi())->__truncateTable();
60// (new Transaccion())->getRepository($this->getCi())->__truncateTable();
61// (new TransaccionDetalle())->getRepository($this->getCi())->__truncateTable();
62// (new EstadoCuenta())->getRepository($this->getCi())->__truncateTable();
63// (new RelTransaccionConsumosOperacion())->getRepository($this->getCi())->__truncateTable();
64// (new OperacionPhysisControl1())->getRepository($this->getCi())->__truncateTable();
65// (new OperacionPhysisControl2())->getRepository($this->getCi())->__truncateTable();
66// (new OperacionPhysisControl1Detalle())->getRepository($this->getCi())->__truncateTable();
67// (new PhysisErrorLog())->getRepository($this->getCi())->__truncateTable();
68
69 // PARTE I: obtencion y storage de los cuit a consultar en physis
70 // buscar operaciones a procesar
71 $operacionesFaena = (new Operacion())->getRepository($this->getCi())->__getOperacionesLiquidadasFaena();
72 $operacionesInvernada = (new Operacion())->getRepository($this->getCi())->__getOperacionesLiquidadasInvernada();
73 $operacionesDcac = array_merge($operacionesFaena, $operacionesInvernada);
74 $cuitsAconsultar = [];
75 $boletosData = $boletosIds = $boletosIdsNotFound = [];
76 $cliente = new Client(['base_uri' => 'http://' . Globales::PHYSIS_HOSTNAME . '/PHYServices/', 'timeout' => 20]);
77
78 // el api de physis no esta comprobando el token en ningun endpoint
79// try {
80// $token = $cliente->request('POST', 'token', [
81// 'headers' => [
82// 'Content-Type' => 'application/x-www-form-urlencoded'
83// ],
84// 'form_params' => [
85// 'grant_type' => 'password',
86// 'username' => 'admin',
87// 'password' => 'super'
88// ]
89// ]);
90//
91// $token = json_decode($token->getBody()->getContents())->access_token;
92// } catch (Exception $exception) {
93// $this->getLogger()->alert('NO HUBO RESPUESTA DEL API DE PHYSIS, NO SE PUDO OBTENER EL TOKEN');
94// die;
95// }
96
97 $token = '';
98
99 $headers = [
100 'Authorization' => 'Bearer ' . $token,
101 'Accept' => 'application/json',
102 ];
103
104 foreach ($operacionesDcac as $operacion) {
105 if (is_numeric($operacion['cuit'])) {
106 if (!in_array($operacion['cuit'], $cuitsAconsultar)) {
107 $cuitsAconsultar[] = $operacion['cuit'];
108 }
109 } else {
110 $this->getLogger()->warning('El lote ID: ' . $operacion['id_lote'] . ' tiene un valor no numerico en el campo CUIT. Detalle: ' . $operacion['cuit'] . ' cargado en el campo CUIT');
111 }
112
113 if (!in_array($operacion['id_lote'], $boletosIds)) {
114 $boletosData[] = [
115 'id' => $operacion['id_lote'],
116 'tipo' => $operacion['tipo']
117 ];
118 $boletosIds[] = $operacion['id_lote'];
119 }
120 }
121
122 if ($cuitsAconsultar) {
123 $this->getLogger()->info('CUITS A CONSULTAR: ', [$cuitsAconsultar]);
124
125 $headers = [
126 'Authorization' => 'Bearer ' . $token,
127 'Accept' => 'application/json',
128 ];
129
130 foreach ($cuitsAconsultar as $key => $cuitAconsultar) {
131 $hoy = new DateTime('NOW');
132 $hoy1 = new DateTime('NOW');
133 $fechaDesde = $hoy1->sub(new DateInterval('P120D'));
134
135 // cliente guzzle
136 $uri = 'api/CRdeCuenta?fechadesde=' . $fechaDesde->format('d/m/Y') . '&fechahasta=' . $hoy->format('d/m/Y') . '&us&cuit=' . $cuitAconsultar . '&agrupaReferenciados=true';
137
138 try {
139 $response = $cliente->request('GET', $uri, [
140 'headers' => $headers
141 ]);
142
143 foreach (json_decode($response->getBody()->getContents(), true) as $detalle) {
144 $this->getLogger()->info('CALL: ' . $uri . 'RESPONSE: ' . $detalle);
145
146 if (isset($detalle['fechaStr'])) {
147 $fecha = DateTime::createFromFormat('d/m/Y', $detalle['fechaStr']);
148 $fecha = $fecha->format('Y-m-d');
149 }
150
151 if (isset($detalle['fechaVencimiento'])) {
152 $fechaVencimiento = DateTime::createFromFormat('d/m/Y', $detalle['fechaStr']);
153 $fechaVencimiento = $fechaVencimiento->format('Y-m-d');
154 }
155
156 $idTipoComprobantesAProcesar = ['ACVA', 'ALCA', 'AFCVA', 'AFCCA', 'AFDCA', 'AFDVA', 'CVAP', 'LCAP', 'CAE', 'DAE', 'REC', 'S3'];
157
158 if (in_array($detalle['idTipoComprobante'], $idTipoComprobantesAProcesar)) {
159 $settings = $this->getCi()->get('settings')['db'];
160 $pdo = new PDO("mysql:host=" . $settings['host'] . ";charset=utf8", $settings['user'], $settings['pass']);
161 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
162 $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
163
164 $dataResponse = [
165 'boletoNro' => isset($detalle['nroBoleto']) ? $detalle['nroBoleto'] : null,
166 'cuit' => $cuitAconsultar,
167 'fecha' => isset($fecha) ? $fecha : null,
168 'numero' => isset($detalle['numero']) ? $detalle['numero'] : null,
169 'comprobanteId' => isset($detalle['idComprobante']) ? $detalle['idComprobante'] : null,
170 'tipoComprobanteId' => isset($detalle['idTipoComprobante']) ? $detalle['idTipoComprobante'] : null,
171 'debe' => isset($detalle['debe']) ? $detalle['debe'] : null,
172 'haber' => isset($detalle['haber']) ? $detalle['haber'] : null,
173 'ejercicioId' => isset($detalle['idEjercicio']) ? $detalle['idEjercicio'] : null,
174 'fechaVencimiento' => isset($fechaVencimiento) ? $fechaVencimiento : null,
175 'netoGravado' => isset($detalle['netoGravado']) ? $detalle['netoGravado'] : null,
176 'retencionIibb' => isset($detalle['retencionIibb']) ? $detalle['retencionIibb'] : null,
177 'percepcionIibb' => isset($detalle['percepcionIibb']) ? $detalle['percepcionIibb'] : null,
178 'iva' => isset($detalle['iva']) ? $detalle['iva'] : null,
179 ];
180
181 $nuevaOperacionPhysisDetalle = (new OperacionPhysisControl1Detalle($dataResponse));
182 $nuevaOperacionPhysisDetalle->getRepository($this->getCi())->__save($nuevaOperacionPhysisDetalle, $pdo);
183 $pdo = null;
184 } else {
185 $this->getLogger()->info('EL DETALLE NO CONTIENE LOS ID TIPO COMPROBANTES BUSCADOS', [$detalle]);
186 null;
187 }
188 }
189 } catch (Exception $exception) {
190 $this->getLogger()->error('ERROR CONSULTANDO / GUARDANDO CUIT', [$exception->getMessage()]);
191 }
192 }
193 } else {
194 $this->getLogger()->error('NO SE ENCONTRARON CUITS A CONSULTAR');
195 die;
196 }
197
198 // PARTE II: procesamiento de las operaciones physis recien ingresadas
199 // buscar las operaciones que los idtipocomprobante sean los pintados en amarillo del excel
200 $idTipoComprobantesAProcesar = ['ACVA', 'ALCA', 'AFCVA', 'AFCCA', 'AFDCA', 'AFDVA', 'CVAP', 'LCAP'];
201 $operacionesPhysisDetalle = (new OperacionPhysisControl1Detalle())->getRepository($this->getCi())->__getSumaDebeHaberByComprobanteId($idTipoComprobantesAProcesar);
202
203 if ($operacionesPhysisDetalle) {
204 foreach ($operacionesPhysisDetalle as $key => $operacionPhysisDetalle) {
205 /* @var OperacionPhysisControl1Detalle $operacionPhysisDetalle */
206
207 if ($operacionPhysisDetalle->getComprobanteId() === 36828)
208 continue;
209
210 $uri = 'api/sach/comprobantes?idEjercicio=' . $operacionPhysisDetalle->getEjercicioId() . '&idComprobante=' . $operacionPhysisDetalle->getComprobanteId();
211
212 try {
213 $response = $cliente->request('GET', $uri, [
214 'headers' => $headers
215 ]);
216
217 $gastos = json_decode($response->getBody()->getContents(), true)['Gastos'];
218 $iva = $netoGravado = $retencionIIBB = $percepcionIIBB = $sellos = 0;
219
220 foreach ($gastos as $gasto) {
221 switch ($gasto['IdGasto']) {
222 case 0: // neto gravado
223 case 55: // bonificación comisión
224 case 60: // bonificación precio
225 $netoGravado += $gasto['Importe'];
226 break;
227 case 1:
228 // COMISION VENTA Y GARANTIA
229 break;
230 case 900:
231 case 902:
232 case 912:
233 // IVA RESPONSABLE INSCRIPTO
234 $iva += $gasto['Importe'];
235 break;
236 case 7:
237 case 8:
238 case 16:
239 case 17:
240 case 18:
241 case 19:
242 case 57:
243 case 62:
244 case 63:
245 case 64:
246 case 65:
247 case 77:
248 case 82:
249 // RETENCION DE INGRESOS BRUTOS
250 $retencionIIBB += $gasto['Importe'];
251 break;
252 case 66:
253 case 68:
254 case 69:
255 case 81:
256 case 74:
257 // PERCEPCION DE INGRESOS BRUTOS
258 $percepcionIIBB += $gasto['Importe'];
259 break;
260 case 58:
261 $sellos += $gasto['Importe'];
262 break;
263 default:
264 break;
265 }
266 }
267
268 if (is_numeric($key = array_search($operacionPhysisDetalle->getBoletoNro(), $boletosIds))) {
269 $settings = $this->getCi()->get('settings')['db'];
270 $pdo = new PDO("mysql:host=" . $settings['host'] . ";charset=utf8", $settings['user'], $settings['pass']);
271 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
272 $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
273
274 $nuevaOperacionPhysis = new OperacionPhysisControl1();
275 $nuevaOperacionPhysis->setBoletoNro($operacionPhysisDetalle->getBoletoNro())
276 ->setCuit($operacionPhysisDetalle->getCuit())
277 ->setFecha($operacionPhysisDetalle->getFecha())
278 ->setFechaVencimiento($operacionPhysisDetalle->getFechaVencimiento())
279 ->setComprobanteId($operacionPhysisDetalle->getComprobanteId())
280 ->setEjercicioId($operacionPhysisDetalle->getEjercicioId())
281 ->setTipoComprobanteId($operacionPhysisDetalle->getTipoComprobanteId())
282 ->setEjercicioId($operacionPhysisDetalle->getEjercicioId())
283 ->setDebe($operacionPhysisDetalle->getDebe())
284 ->setHaber($operacionPhysisDetalle->getHaber())
285 ->setNetoGravado($netoGravado)
286 ->setIva($iva)
287 ->setSellos($sellos)
288 ->setRetencionIibb($retencionIIBB)
289 ->setPercepcionIibb($percepcionIIBB)
290 ->getRepository($this->getCi())->__save($nuevaOperacionPhysis, $pdo);
291
292 $boletosIdsEncontrados[] = $boletosIds[$key];
293
294 // destruyo el objeto pdo para cerrar la conexion
295 $pdo = null;
296 }
297 } catch (Exception $exception) {
298 $this->getLogger()->error('ERROR CONSULTANDO idEjercicio', [$exception->getMessage()]);
299 continue;
300 }
301 }
302
303 // TODO: resolver esto, no hace lo que el nombre de la variable dice
304 // if ($boletosIdsEncontrados)
305 // $this->getLogger()->info("CRONJOB ProcesarOperacionesLiquidadasTask: No se encontro informacion en physis de estos id boleto entre las fechas {$fechaDesde->format('d/m/Y')} y {$hoy->format('d/m/Y')}", array_diff($boletosIds, $boletosIdsEncontrados));
306
307 // CONTROL I
308 $operacionesPhysisControl1 = (new OperacionPhysisControl1())->getRepository($this->getCi())->__findAll();
309
310 if ($operacionesPhysisControl1) {
311 $erroresPhysis = [];
312
313 foreach ($operacionesPhysisControl1 as $operacionPhysis) {
314 $errorPhysis = $this->procesarControl($operacionPhysis, 1);
315
316 if ($errorPhysis != null)
317 $erroresPhysis[] = $errorPhysis;
318 }
319 }
320
321 $operacionesPhysisControl1Agrupadas = (new OperacionPhysisControl1())->getRepository($this->getCi())->__getSumaValoresByIdTipoComprobanteAndCuit($idTipoComprobantesAProcesar);
322
323 if ($operacionesPhysisControl1Agrupadas) {
324 foreach ($operacionesPhysisControl1Agrupadas as $operacionPhysisControl1Agrupada) {
325 $this->procesarControl($operacionPhysisControl1Agrupada, 2, $boletosData);
326 }
327 }
328 } else {
329 $this->getLogger()->error("ERROR: no se encontraron detalles a procesar.");
330 die;
331 }
332
333 // CONTROL II
334 $operacionesPhysisControl2 = (new OperacionPhysisControl2())->getRepository($this->getCi())->__findAll();
335
336 if ($operacionesPhysisControl2) {
337 // agregar las operaciones al modulo financiero
338 foreach ($operacionesPhysisControl2 as $key => $elem) {
339 /* @var OperacionPhysisControl2 $elem */
340 $datosOperacion = [];
341 $detalles = json_decode($elem->getDetalleOperacion(), true);
342
343 switch ($elem->getTipo()) {
344 case 'Invernada':
345 $datosOperacionTmp = [];
346 $valorTotalLote = $elem->getNetoGravado() + $elem->getIva() + $elem->getSellos() + $elem->getIngresosBrutos();
347
348 foreach ($detalles['pagos'] as $keu => $pago) {
349 $fechaInicial = ($detalles['fecha_pond_vendedor'] = '0000-00-00') ? $detalles['fecha_carga_final'] : $detalles['fecha_pond_vendedor'];
350 $fechaPago = explode('/', $pago['fecha_pago']);
351 $fechaDisponibilidadTxt = $fechaPago[2] . '-' . $fechaPago[1] . '-' . $fechaPago[0];
352 $fechaDisponibilidad = new DateTime($fechaDisponibilidadTxt);
353 $fechaVencimientoInicial = $fechaDisponibilidad->format('Y-m-d');
354 $plazo = $detalles['plazo_v' . ($keu + 1)];
355
356 // la fecha de disponibilidad tiene que ser un dia habil
357 $fechaDisponibilidad = $this->fechaDisponibilidadHabilChecker($fechaDisponibilidad);
358
359 $datosOperacionTmp[] = [
360 'loteNro' => $elem->getBoletoNro(),
361 'porcentajeLote' => $detalles['plazo_v' . ($keu + 1) . '_p'],
362 'tipoOperacion' => $elem->getTipo(),
363 'usuarioId' => '11', //TODO: cambiar por usuario correspondiente a los cronjobs
364 'sociedadId' => $detalles['sociedad_vendedora'],
365 'valor' => $valorTotalLote * $detalles['plazo_v' . ($keu + 1) . '_p'] / 100,
366 'precioKg' => str_replace(',', '.', ($detalles['precio_venta2'] < 1000) ? $detalles['precio_final'] : $detalles['precio_venta2']),
367 'kgsPromedio' => $detalles['peso_neto'],
368 'cantCabezas' => $detalles['cantidad_animales'],
369 'plazo' => $plazo,
370 'fechaInicial' => $fechaInicial,
371 'fechaVencimientoInicial' => $fechaVencimientoInicial,
372 'fechaDisponibilidad' => $fechaDisponibilidad->format('Y-m-d')
373 ];
374 }
375
376 $datosOperacion = $datosOperacionTmp;
377 break;
378 case 'Faena':
379 $fechaInicial = DateManager::formatearFechaUS($detalles['lo_fecha_faena_real']);
380 $fechaFaena = DateTime::createFromFormat('d/m/Y', $detalles['lo_fecha_faena_real']);
381 $interval = new DateInterval('P' . $detalles['plazo1'] . 'D');
382 $fechaDisponibilidad = $fechaFaena->add($interval);
383 $fechaVencimientoInicial = $fechaDisponibilidad->format('Y-m-d');
384 $plazo = $detalles['plazo1'];
385
386 // la fecha de disponibilidad tiene que ser un dia habil
387 $fechaDisponibilidad = $this->fechaDisponibilidadHabilChecker($fechaDisponibilidad);
388
389 $datosOperacion[] = [
390 'loteNro' => $elem->getBoletoNro(),
391 'porcentajeLote' => 100,
392 'tipoOperacion' => $elem->getTipo(),
393 'usuarioId' => '11',
394 'sociedadId' => $detalles['sociedad_vendedora'],
395 'valor' => $elem->getNetoGravado() + $elem->getIva() + $elem->getSellos() + $elem->getIngresosBrutos(),
396 'precioKg' => $detalles['lo_precio_prom'],
397 'kgsPromedio' => $detalles['lo_total_kilos'],
398 'cantCabezas' => $detalles['lo_total_cabezas'],
399 'plazo' => $plazo,
400 'fechaInicial' => $fechaInicial,
401 'fechaVencimientoInicial' => $fechaVencimientoInicial,
402 'fechaDisponibilidad' => $fechaDisponibilidad->format('Y-m-d')
403 ];
404 break;
405 default:
406 //TODO: error aca
407 break;
408 }
409
410 //TODO: si el lote ya existe, comparamos los valores:
411 //Si el saldo de la operación corresponde con el importe original en Invernada o con el importe original - IVA en Faena, entonces borra la operación ya ingresada y corre nuevamente para ingresar.
412 //Si no sucede lo anterior entonces avisar a un responsable para que junto al cliente deshaga las transacciones, corran el proceso nuevamente para ingresar y rehagan las transacciones. Sugiero esto porque el caso parece bastante improbable.
413
414 foreach ($datosOperacion as $item) {
415 $operacionesIdLote = (new Operacion())->getRepository($this->getCi())->__findByLote($item['loteNro']);
416
417 if ($operacionesIdLote) {
418 foreach ($operacionesIdLote as $operacionId) {
419 /* @var Operacion $operacion */
420 $operacion = (new Operacion())->getRepository($this->getCi())->__findByID($operacionId);
421
422 if ($operacion) {
423 $to = 'fbruzzoni@decampoacampo.com';
424 $fromName = 'API Modulo Financiero';
425 $subject = 'DCAC Modulo Financiero: error en re-ingreso de lote';
426 $body = $this->getTwig()->fetch('physis_error.twig', ['errores' => $erroresPhysis]);
427
428 if ($item['tipoOperacion'] === 'Faena') {
429 if (abs($operacion->getValorActualizado() - ($operacion->getValorOriginal() - $elem->getIva())) <= 0.0001) {
430 //borra la operacion y restaura el estado de cuenta
431 (new Operaciones($this->getCi()))->eliminarOperaciones($operacionesIdLote);
432 //borra la transaccion iva
433 $transaccionId = (new RegistroOperacion())->getRepository($this->getCi())->__findIVATransaccionId($operacionId);
434 $transaccion = (new Transaccion())->getRepository($this->getCi())->__findByID($transaccionId);
435 $transaccion->getRepository($this->getCi())->__delete($transaccion);
436 } else {
437 if ($this->getEmailer()->sendEmail($to, $subject, $body, $fromName, true)) {
438 $this->getLogger()->error("CRONJOB 'ProcesarOperacionesLiquidadasTask' -- Se envio email con errores de re-ingreso.");
439 } else {
440 $this->getLogger()->error("CRONJOB 'ProcesarOperacionesLiquidadasTask' -- Fallo el envio del email con errores de re-ingreso.");
441 }
442 }
443 }
444
445 if ($item['tipoOperacion'] === 'Invernada') {
446 if (abs($operacion->getValorActualizado() - $operacion->getValorOriginal()) <= 0.0001) {
447 //borra la operacion y restaura el estado de cuenta
448 (new Operaciones($this->getCi()))->eliminarOperaciones($operacionesIdLote);
449 } else {
450 if ($this->getEmailer()->sendEmail($to, $subject, $body, $fromName, true)) {
451 $this->getLogger()->error("CRONJOB 'ProcesarOperacionesLiquidadasTask' -- Se envio email con errores de re-ingreso.");
452 } else {
453 $this->getLogger()->error("CRONJOB 'ProcesarOperacionesLiquidadasTask' -- Fallo el envio del email con errores de re-ingreso.");
454 }
455 }
456 }
457 }
458 }
459 }
460 }
461
462 // agregamos la operacion al modulo
463 if ($nuevaOperacion = (new Operaciones($this->getCi()))->agregarOperaciones($datosOperacion)) {
464 // se pasa la operacion de estado en la db dcac
465 // faena
466 if ($elem->getTipo() == 'Faena') {
467 $nuevaTransaccion = (new Transacciones($this->getCi()))->agregarTransaccionFaenaIva($nuevaOperacion, $elem->getIva());
468 $operacionDcac = new negocios();
469 if (@$operacionDcac->pasarCerradas($elem->getBoletoNro())) {
470 $this->getLogger()->info("Se movio de estado a 'CERRADAS' la operacion con id lote {$elem->getBoletoNro()}");
471 } else {
472 $this->getLogger()->error("ERROR moviendo a estado 'CERRADAS' la operacion con id lote {$elem->getBoletoNro()}");
473 }
474 }
475 // invernada
476 if ($elem->getTipo() == 'Invernada') {
477 $datalote = new manejo_invernada();
478 $datalote->cargarRevisacion($elem->getBoletoNro()) OR die($datalote->error);
479 // Log Change States
480// $ls = new LogStates();
481// $ls->setType('invernada');
482// $ls->setUser('11');
483// $ls->setId($elem->getBoletoNro());
484// @require_once("/var/www/lib/i2.0/class.action_envios.php");
485// @include_once("/var/www/MODULOS/invernada/modulo_envioMails_i/lib/class.envio_notificaciones_mobile.php");
486// @include_once '/var/www/lib/notificaciones/class.notif_trigger.php';
487// $ls->log();
488//
489// //~Notificacion------------------------------------------------------------
490// $s = new trigger_notif();
491// $s->setOnlyRep(true);
492// $s->DatosNotificacionInvernada($elem->getBoletoNro(), 121);
493// $s->enviar();
494//
495// //~Notificacion Mobile Vendedor-------------------------------------------
496// $nm = new EnvioNotificacionesMobile($elem->getBoletoNro());
497// $nm->setOnlyRep(true);
498// $nm->PasarCerradasVendedor();
499//
500// unset($s, $nm);
501// // si el sitio esta en modo sociedad, incluye el modulo de notificaciones de las sociedades
502// // recorre los usuarios destinatarios de la notificacion y los almacena en array $data_notif_sociedad['data'] (id y mail)
503// require_once '/var/www/MODULOS/modulo_notificaciones/modulo_envio/class.notif_trigger_sociedad.php';
504// require_once '/var/www/MODULOS/modulo_notificaciones/helper/NotificationSociedadesType.php';
505// $notificacionType = \NotificationSociedadesType::$LOTE_VENDIDO_LIQUIDADO_INVERNADA;
506// $usuarios = json_decode(Notificacion::getDestinatarios($notificacionType, $elem->getBoletoNro()));
507// $data_notif_sociedad['lote'] = $elem->getBoletoNro();
508// $data_notif_sociedad['tipo'] = $notificacionType;
509// $data_notif_sociedad['data'] = array();
510// foreach ($usuarios as $usuario) {
511// $data_notif_sociedad['data'][] = array(
512// 'id' => $usuario->usuario_id,
513// 'email' => $usuario->mail
514// );
515// }
516//
517// (new \Envio($data_notif_sociedad))->index();
518// (new \Envio($data_notif_sociedad))->indexApp();
519//
520// // si el sitio esta en modo sociedad, incluye el modulo de notificaciones de las sociedades
521// // recorre los usuarios destinatarios de la notificacion y los almacena en array $data_notif_sociedad['data'] (id y mail)
522// require_once '/var/www/MODULOS/modulo_notificaciones/modulo_envio/class.notif_trigger_sociedad.php';
523// require_once '/var/www/MODULOS/modulo_notificaciones/helper/NotificationSociedadesType.php';
524// $notificacionType = \NotificationSociedadesType::$LOTE_COMPRADO_LIQUIDADO_INVERNADA;
525// $usuarios = json_decode(Notificacion::getDestinatarios($notificacionType, $elem->getBoletoNro()));
526// $data_notif_sociedad['lote'] = $elem->getBoletoNro();
527// $data_notif_sociedad['tipo'] = $notificacionType;
528// $data_notif_sociedad['data'] = array();
529// foreach ($usuarios as $usuario) {
530// $data_notif_sociedad['data'][] = array(
531// 'id' => $usuario->usuario_id,
532// 'email' => $usuario->mail
533// );
534// }
535//
536// (new \Envio($data_notif_sociedad))->index();
537// (new \Envio($data_notif_sociedad))->indexApp();
538
539 if ($datalote->moverA('tropas_cerradas')) {
540 $this->getLogger()->info("Se movio de estado a 'CERRADAS' la operacion con id lote {$elem->getBoletoNro()}");
541 } else {
542 $this->getLogger()->error("ERROR moviendo a estado 'CERRADAS' la operacion con id lote {$elem->getBoletoNro()}");
543 }
544 }
545
546 $elem->getRepository($this->getCi())->__delete($elem);
547 } else {
548 $this->getLogger()->error("ERROR ingresando al modulo financiero la operacion con id lote {$elem->getBoletoNro()}");
549 }
550 }
551 }
552
553
554 // AVISO A ADMINISTRACION POR ERRORES
555 if (isset($erroresPhysis) && count($erroresPhysis) > 0) {
556 $to = 'alerta_facturacion@decampoacampo.com';
557 $fromName = 'API Modulo Financiero';
558 $subject = 'Control financiero Physis: errores en operaciones';
559 $body = $this->getTwig()->fetch('physis_error.twig', ['errores' => $erroresPhysis]);
560
561 if ($this->getEmailer()->sendEmail($to, $subject, $body, $fromName, true)) {
562 $this->getLogger()->error("CRONJOB 'ProcesarOperacionesLiquidadasTask' -- Se envio email con errores physis.");
563 } else {
564 $this->getLogger()->error("CRONJOB 'ProcesarOperacionesLiquidadasTask' -- Fallo el envio del email con errores physis.");
565 }
566 }
567
568 //SOFT DELETE DE LOS DATOS OBTENIDOS DE PHYSIS
569 $this->softDeletePhysis();
570
571 } catch (Exception $e) {
572 $this->getLogger()->error($e->getMessage());
573 }
574 }
575
576 /**
577 * @param OperacionPhysisControl1 $operacion
578 * @param $controlNro
579 * @param null $boletosData
580 * @return bool
581 */
582 public function procesarControl($operacion, $controlNro, $boletosData = null) {
583 if ($controlNro == 1)
584 $debeHaber = ($operacion->getDebe() > 0) ? $operacion->getDebe() : $operacion->getHaber();
585 else {
586 if ($boletosData) {
587 $tipo = $boletosData[array_search($operacion->getBoletoNro(), array_column($boletosData, 'id'))]['tipo'];
588 $classInfoCompleta = ($tipo == 'Faena') ? (new InformacionPagoDirecto()) : (new InformacionPagoInvernada());
589 $infoCompletaNroBoleto = $classInfoCompleta->getInformacionCompleta($operacion->getBoletoNro());
590 } else {
591 return false;
592 }
593 }
594
595 if ($controlNro == 1 && $operacion->getTipoComprobanteId() == 'AFDVA') {
596 $operacion->setNetoGravado($operacion->getNetoGravado() * -1);
597 $operacion->setRetencionIibb($operacion->getRetencionIibb() * -1);
598 $operacion->setIva($operacion->getIva() * -1);
599 $operacion->setSellos($operacion->getSellos() * -1);
600 }
601
602 if ($controlNro == 1) {
603 $control = $operacion->getNetoGravado() + $operacion->getRetencionIibb() + $operacion->getIva() + $operacion->getSellos();
604 } else {
605 $parte1 = ($tipo === 'Faena') ? $infoCompletaNroBoleto['lo_importe_compra'] : $infoCompletaNroBoleto['importe_venta'];
606 $parte2 = $infoCompletaNroBoleto['porcentaje_tp2'];
607 $parte1bis = ($tipo === 'Faena') ? preg_replace(['/\./', '/\,/'], ['', '.'], $infoCompletaNroBoleto['importe_comision']) : $infoCompletaNroBoleto['comision_vendedor'];
608
609 if ($parte2 > 0 && $parte1bis === '0')
610 $parte1bis = $parte1 * 0.01;
611
612 $control = ($parte1 - $parte1bis) * (1 - ($parte2 / 100));
613
614 if ($parte1 == null)
615 $this->getLogger()->info('El lote ID ' . $operacion->getBoletoNro() . ' no tiene importe de venta');
616
617 if ($parte2 == null)
618 $this->getLogger()->info('El lote ID ' . $operacion->getBoletoNro() . ' no tiene TP2');
619
620 if ($parte1bis == null)
621 $this->getLogger()->info('El lote ID ' . $operacion->getBoletoNro() . ' no tiene comision vendedora');
622 }
623
624 $valor = ($controlNro == 1) ? $debeHaber : $operacion->getNetoGravado();
625 $porcentajeMayor = $valor + (($valor * 1) / 100);
626 $porcentajeMenor = $valor - (($valor * 1) / 100);
627
628 $operacionPhysisDetalle = (new OperacionPhysisControl1Detalle())->getRepository($this->getCi())->__findByComprobanteId($operacion->getComprobanteId());
629
630 if ($controlNro === 1) {
631 if ($control > $porcentajeMayor || $control < $porcentajeMenor) {
632 (new OperacionPhysisControl1())->getRepository($this->getCi())->__delete($operacion);
633
634 $newPhysisErrorLog = new PhysisErrorLog();
635 $newPhysisErrorLog->setTablaOperacionPhysis(json_encode($operacion))
636 ->setControlNro($controlNro)
637 ->setTablaOperacionPhysisDetalle(json_encode($operacionPhysisDetalle))
638 ->getRepository($this->getCi())->__save($newPhysisErrorLog);
639
640 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 1 -- BOLETO NRO: {$operacion->getBoletoNro()}");
641 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 1 -- VALOR CONTROL: {$control}");
642 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 1 -- % MENOR: {$porcentajeMenor}");
643 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 1 -- % MAYOR: {$porcentajeMayor}");
644
645 return $operacion->getBoletoNro();
646 }
647 }
648
649 // si es el control 2 y pasa el control grabo el nuevo objeto
650 if ($controlNro == 2) {
651 if ($control < $porcentajeMayor && $control > $porcentajeMenor) {
652 $newOperaciopnPhysisControl2 = new OperacionPhysisControl2();
653 $newOperaciopnPhysisControl2->setTipo($tipo)
654 ->setBoletoNro($operacion->getBoletoNro())
655 ->setCuit($operacion->getCuit())
656 ->setNetoGravado($operacion->getNetoGravado())
657 ->setIngresosBrutos($operacion->getRetencionIibb())
658 ->setIva($operacion->getIva())
659 ->setSellos($operacion->getSellos())
660 ->setImporteVendedor($parte1)
661 ->setComision($parte1bis)
662 ->setDetalleOperacion(json_encode($infoCompletaNroBoleto))
663 ->getRepository($this->getCi())->__save($newOperaciopnPhysisControl2);
664
665 $this->getLogger()->error("DEBUG PHYSIS CONTROL 2 -- SE AGREGO OPERACION A CONTROL 2");
666 $this->getLogger()->error("DEBUG PHYSIS CONTROL 2 -- BOLETO NRO: {$operacion->getBoletoNro()}");
667 $this->getLogger()->error("DEBUG PHYSIS CONTROL 2 -- VALOR CONTROL: {$control}");
668 $this->getLogger()->error("DEBUG PHYSIS CONTROL 2 -- % MENOR: {$porcentajeMenor}");
669 $this->getLogger()->error("DEBUG PHYSIS CONTROL 2 -- % MAYOR: {$porcentajeMayor}");
670
671 } else {
672 $newPhysisErrorLog = new PhysisErrorLog();
673 $newPhysisErrorLog->setTablaOperacionPhysis(json_encode($operacion))
674 ->setControlNro($controlNro)
675 ->setTablaOperacionPhysisDetalle(json_encode($operacionPhysisDetalle))
676 ->getRepository($this->getCi())->__save($newPhysisErrorLog);
677
678 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- FALLO EN CONTROL 2");
679 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- BOLETO NRO: {$operacion->getBoletoNro()}");
680 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- OPERACION: ", [$operacion]);
681 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- parte1: {$parte1}");
682 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- parte1bis: {$parte1bis}");
683 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- parte2: {$parte2}");
684 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- infoCompletaNroBoleto: ", [$infoCompletaNroBoleto]);
685 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- VALOR CONTROL: {$control}");
686 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- % MENOR: {$porcentajeMenor}");
687 $this->getLogger()->error("DEBUG ERRORES PHYSIS CONTROL 2 -- % MAYOR: {$porcentajeMayor}");
688 }
689 }
690
691 }
692
693 /**
694 * @param DateTime $fechaDisponibilidadDatetime
695 * @return DateTime
696 * @throws Exception
697 */
698 public function fechaDisponibilidadHabilChecker(DateTime $fechaDisponibilidadDatetime) {
699 if (!$this->getHelper()->esDiaHabil($fechaDisponibilidadDatetime)) {
700 $interval = new DateInterval('P1D');
701
702 while (!$this->getHelper()->esDiaHabil($fechaDisponibilidadDatetime)) {
703 $fechaDisponibilidadDatetime->add($interval);
704 }
705 }
706 return $fechaDisponibilidadDatetime;
707 }
708
709 public function softDeletePhysis() {
710 $tablaControl1 = (new OperacionPhysisControl1())->getRepository($this->getCi())->__findAll();
711 $tablaControl2 = (new OperacionPhysisControl2())->getRepository($this->getCi())->__findAll();
712 $tablaControl1Detalles = (new OperacionPhysisControl1Detalle())->getRepository($this->getCi())->__findAll();
713 $tablaErrores = (new PhysisErrorLog())->getRepository($this->getCi())->__findAll();
714
715 foreach ($tablaControl1 as $item) {
716 /* @var $item OperacionPhysisControl1 */
717 $item->getRepository($this->getCi())->__delete($item);
718 }
719
720 foreach ($tablaControl2 as $item) {
721 /* @var $item OperacionPhysisControl2 */
722 $item->getRepository($this->getCi())->__delete($item);
723 }
724
725 foreach ($tablaControl1Detalles as $item) {
726 /* @var $item OperacionPhysisControl1Detalle */
727 $item->getRepository($this->getCi())->__delete($item);
728 }
729 foreach ($tablaErrores as $item) {
730 /* @var $item PhysisErrorLog */
731 $item->getRepository($this->getCi())->__delete($item);
732 }
733 }
734}
735