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
51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?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) {
|
|
return (float) $value;
|
|
}
|
|
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;
|
|
}
|
|
}
|