get Uf asincronico

This commit is contained in:
Juan Pablo Vial
2025-06-24 11:20:53 -04:00
parent 200510d60a
commit 39198bbe7c
2 changed files with 115 additions and 6 deletions

View File

@ -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);
}
}