diff --git a/app/src/Service/Venta/Pago.php b/app/src/Service/Venta/Pago.php index 0434451..8d54938 100644 --- a/app/src/Service/Venta/Pago.php +++ b/app/src/Service/Venta/Pago.php @@ -20,7 +20,8 @@ class Pago protected Repository\Venta\EstadoPago $estadoPagoRepository, protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository, protected Service\UF $ufService, - protected Service\Valor $valorService + protected Service\Valor $valorService, + protected Service\Queue $queueService ) {} public function depositar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool @@ -89,13 +90,23 @@ class Pago return false; } } + + /** + * @param int|null $pago_id + * @return Model\Venta\Pago|null + * @throws Read + */ public function getById(?int $pago_id): ?Model\Venta\Pago { if ($pago_id === null) { return null; } - $pago = $this->pagoRepository->fetchById($pago_id); - return $this->process($pago); + try { + $pago = $this->pagoRepository->fetchById($pago_id); + return $this->process($pago); + } catch (EmptyResult) { + throw new Read(__CLASS__); + } } public function getByVenta(int $venta_id): array @@ -144,9 +155,6 @@ class Pago $fecha = new DateTimeImmutable(); } $data['fecha'] = $fecha->format('Y-m-d'); - if (!array_key_exists('uf', $data)) { - $data['uf'] = $this->ufService->get($fecha); - } } $data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha']); @@ -158,6 +166,9 @@ class Pago } catch (PDOException $exception) { throw new Create(__CLASS__, $exception); } + if (!array_key_exists('uf', $data)) { + $this->getUFAsync($pago); + } $tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado'); try { @@ -226,6 +237,21 @@ class Pago return $this->process($this->pagoRepository->fetchById($pago->id)); } + public function updateUF(int $pago_id): bool + { + try { + $pago = $this->getById($pago_id); + } catch (Read) { + return false; + } + $uf = $this->ufService->get($pago->currentEstado->fecha); + try { + $this->pagoRepository->edit($pago, ['uf' => $uf]); + return true; + } catch (EmptyResult | PDOException) { + return false; + } + } protected function process($pago): Model\Venta\Pago { @@ -258,4 +284,14 @@ class Pago } return $pago->uf; } + protected function getUFAsync(Model\Venta\Pago $pago): void + { + $queueData = [ + 'type' => 'service', + 'service' => __CLASS__, + 'method' => 'updateUF', + 'params' => ['pago_id' => $pago->id] + ]; + $this->queueService->push($queueData); + } } diff --git a/app/tests/unit/src/Service/Venta/PagoTest.php b/app/tests/unit/src/Service/Venta/PagoTest.php new file mode 100644 index 0000000..830a172 --- /dev/null +++ b/app/tests/unit/src/Service/Venta/PagoTest.php @@ -0,0 +1,73 @@ +uf = 37568.84; + + $tipoEstadoPago = $this->getMockBuilder(Model\Venta\TipoEstadoPago::class)->disableOriginalConstructor()->getMock(); + $tipoEstadoPago->id = 1; + $tipoEstadoPago->descripcion = 'depositado'; + $estadoPago = $this->getMockBuilder(Model\Venta\EstadoPago::class)->disableOriginalConstructor()->getMock(); + $estadoPago->id = 1; + $estadoPago->tipoEstadoPago = $tipoEstadoPago; + $estadoPago->fecha = $fecha; + + $this->pago = $this->getMockBuilder(Model\Venta\Pago::class)->disableOriginalConstructor()->getMock(); + $this->pago->id = 1; + $this->pago->fecha = $fecha; + $this->pago->uf = $this->uf; + $this->pago->currentEstado = $estadoPago; + $this->pagoRepository = $this->getMockBuilder(Repository\Venta\Pago::class)->disableOriginalConstructor()->getMock(); + $this->pagoRepository->method('fetchById')->willReturn($this->pago); + $this->pagoRepository->method('edit')->willReturnCallback(function() { + $this->pago->uf = $this->uf; + return $this->pago; + }); + $this->estadoPagoRepository = $this->getMockBuilder(Repository\Venta\EstadoPago::class)->disableOriginalConstructor()->getMock(); + $this->estadoPagoRepository->method('fetchCurrentByPago')->with($this->pago->id)->willReturn($estadoPago); + $this->tipoEstadoPagoRepository = $this->getMockBuilder(Repository\Venta\TipoEstadoPago::class)->disableOriginalConstructor()->getMock(); + $this->ufService = $this->getMockBuilder(Service\UF::class)->disableOriginalConstructor()->getMock(); + $this->ufService->method('get')->with($fecha)->willReturn($this->uf); + $this->valorService = $this->getMockBuilder(Service\Valor::class)->disableOriginalConstructor()->getMock(); + $this->queueService = $this->getMockBuilder(Service\Queue::class)->disableOriginalConstructor()->getMock(); + } + + public function testUpdateUF(): void + { + $this->pago->uf = null; + + $pagoService = new Service\Venta\Pago( + $this->pagoRepository, + $this->estadoPagoRepository, + $this->tipoEstadoPagoRepository, + $this->ufService, + $this->valorService, + $this->queueService + ); + + $status = $pagoService->updateUF($this->pago->id); + $pago = $pagoService->getById($this->pago->id); + + $this->assertTrue($status); + $this->assertEquals($pago->uf, $this->uf); + } +} \ No newline at end of file