feature/editar-bono-pie (#24)

Rutas y controladores para editar bono

Formulario para editar bono pie

Filtro de columnas

Uso de nuevas estructuras y editar Bono Pie

Uso de nuevas estructuras.

Capacidad de pasar a pesos o a UF en Servicio Valor

FIX: botón incorrecto

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #24
This commit is contained in:
2025-04-11 17:34:40 +00:00
parent 86d030bb71
commit ba57cad514
11 changed files with 230 additions and 32 deletions

View File

@ -1,8 +1,14 @@
<?php
namespace Incoviba\Service;
use DateTimeInterface;
use DateTimeImmutable;
use DateMalformedStringException;
class Valor
{
public function __construct(protected UF $ufService) {}
public function clean(string|float|int $value): float
{
if ((float) $value == $value) {
@ -10,4 +16,35 @@ class Valor
}
return (float) str_replace(['.', ','], ['', '.'], $value);
}
public function toPesos(string $value, null|string|DateTimeInterface $date = null, bool $force = false): int
{
$date = $this->getDateTime($date);
if (abs((float) $value - (int) $value) > 0 or $force) {
return round($value * $this->ufService->get($date));
}
return (int) $value;
}
public function toUF(string $value, null|string|DateTimeInterface $date = null, bool $force = false): float
{
$date = $this->getDateTime($date);
if (abs((float) $value - (int) $value) > 0 and !$force) {
return (float) $value;
}
return $value / $this->ufService->get($date);
}
protected function getDateTime(null|string|DateTimeInterface $date): DateTimeInterface
{
if ($date === null) {
return new DateTimeImmutable();
}
if (is_string($date)) {
try {
return new DateTimeImmutable($date);
} catch (DateMalformedStringException) {
return new DateTimeImmutable();
}
}
return $date;
}
}

View File

@ -1,32 +1,76 @@
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Model;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Read, Create, Update};
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class BonoPie
class BonoPie extends Ideal\Service
{
public function __construct(protected Repository\Venta\BonoPie $bonoPieRepository,
protected LoggerInterface $logger,
protected Pago $pagoService) {}
public function __construct(LoggerInterface $logger,
protected Repository\Venta\BonoPie $bonoPieRepository,
protected Service\Valor $valorService,
protected Service\UF $ufService,
protected Pago $pagoService)
{
parent::__construct($logger);
}
/**
* @param array $data
* @return Model\Venta\BonoPie
* @throws Create
*/
public function add(array $data): Model\Venta\BonoPie
{
if (array_key_exists('valor', $data)) {
$data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true);
}
if (!array_key_exists('pago', $data)) {
$pago = $this->pagoService->add($data);
$data['pago'] = $pago->id;
}
$filteredData = $this->bonoPieRepository->filterData($data);
if (!key_exists('pago', $filteredData)) {
$pago = $this->pagoService->add($filteredData);
$filteredData['pago'] = $pago->id;
}
try {
$bono = $this->bonoPieRepository->fetchByPago($filteredData['pago']);
return $this->bonoPieRepository->fetchByPago($filteredData['pago']);
} catch (EmptyResult) {
$filteredData['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true);
$bono = $this->bonoPieRepository->create($filteredData);
$bono = $this->bonoPieRepository->save($bono);
try {
return $this->bonoPieRepository->save($bono);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
}
/**
* @param Model\Venta\BonoPie $bonoPie
* @param array $data
* @return Model\Venta\BonoPie
* @throws Update
*/
public function edit(Model\Venta\BonoPie $bonoPie, array $data): Model\Venta\BonoPie
{
if (array_key_exists('valor', $data)) {
$data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true);
}
if (!array_key_exists('pago', $data)) {
$pago = $this->pagoService->edit($bonoPie->pago, $data);
$data['pago'] = $pago->id;
}
$data['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true);
$filteredData = $this->bonoPieRepository->filterData($data);
try {
return $this->bonoPieRepository->edit($bonoPie, $filteredData);
} catch (PDOException | EmptyResult $exception) {
throw new Update(__CLASS__, $exception);
}
return $bono;
}
/**

View File

@ -6,6 +6,7 @@ use DateTimeInterface;
use DateTimeImmutable;
use DateMalformedStringException;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Exception\ServiceAction\Update;
use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
@ -131,15 +132,21 @@ class Pago
public function add(array $data): Model\Venta\Pago
{
if (!isset($data['uf'])) {
if (array_key_exists('fecha', $data)) {
try {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
$fecha = new DateTimeImmutable($data['fecha']);
} catch (DateMalformedStringException) {
$data['uf'] = 0;
$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']);
$filtered_data = $this->pagoRepository->filterData($data);
$filtered_data['valor'] = round($this->valorService->clean($filtered_data['valor']));
$pago = $this->pagoRepository->create($filtered_data);
$pago = $this->pagoRepository->save($pago);
@ -154,27 +161,34 @@ class Pago
return $pago;
}
/**
* @param Model\Venta\Pago $pago
* @param array $data
* @return Model\Venta\Pago
* @throws Update
*/
public function edit(Model\Venta\Pago $pago, array $data): Model\Venta\Pago
{
if (array_key_exists('fecha', $data)) {
try {
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
$fecha = new DateTimeImmutable($data['fecha']);
} catch (DateMalformedStringException) {
$data['fecha'] = (new DateTimeImmutable())->format('Y-m-d');
$fecha = new DateTimeImmutable();
}
}
if (array_key_exists('uf', $data)) {
try {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
} catch (DateMalformedStringException) {
$data['uf'] = 0;
$data['fecha'] = $fecha->format('Y-m-d');
if (!array_key_exists('uf', $data)) {
$data['uf'] = $this->ufService->get($fecha);
}
}
$filteredData = $this->pagoRepository->filterData($data);
if (array_key_exists('valor', $filteredData)) {
$filteredData['valor'] = round($this->valorService->clean($filteredData['valor']));
$filteredData['valor'] = $this->valorService->toPesos($this->valorService->clean($filteredData['valor']), $filteredData['fecha']);
}
try {
$pago = $this->pagoRepository->edit($pago, $filteredData);
} catch (PDOException | EmptyResult $exception) {
throw new Update(__CLASS__, $exception);
}
$pago = $this->pagoRepository->edit($pago, $filteredData);
$pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago->id);
return $pago;
}