Capacidad de pasar a pesos o a UF en Servicio Valor
This commit is contained in:
@ -1,8 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Incoviba\Service;
|
namespace Incoviba\Service;
|
||||||
|
|
||||||
|
use DateTimeInterface;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use DateMalformedStringException;
|
||||||
|
|
||||||
class Valor
|
class Valor
|
||||||
{
|
{
|
||||||
|
public function __construct(protected UF $ufService) {}
|
||||||
|
|
||||||
public function clean(string|float|int $value): float
|
public function clean(string|float|int $value): float
|
||||||
{
|
{
|
||||||
if ((float) $value == $value) {
|
if ((float) $value == $value) {
|
||||||
@ -10,4 +16,35 @@ class Valor
|
|||||||
}
|
}
|
||||||
return (float) str_replace(['.', ','], ['', '.'], $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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user