Facturacion, se agrega PropiedadUnidad
This commit is contained in:
35
app/src/Service/UF.php
Normal file
35
app/src/Service/UF.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyRedis;
|
||||
|
||||
class UF
|
||||
{
|
||||
protected string $redisKey = 'uf';
|
||||
|
||||
public function __construct(protected Redis $redisService, protected Money $moneyService) {}
|
||||
|
||||
public function get(DateTimeInterface $date): float
|
||||
{
|
||||
$ufs = [];
|
||||
try {
|
||||
$ufs = json_decode($this->redisService->get($this->redisKey), JSON_OBJECT_AS_ARRAY);
|
||||
if (!isset($ufs[$date->format('Y-m-d')])) {
|
||||
throw new EmptyRedis($this->redisKey);
|
||||
}
|
||||
$uf = $ufs[$date->format('Y-m-d')];
|
||||
} catch (EmptyRedis) {
|
||||
$uf = $this->moneyService->getUF($date);
|
||||
$ufs[$date->format('Y-m-d')] = $uf;
|
||||
$this->redisService->set($this->redisKey, json_encode($ufs), 60 * 60 * 24 * 30);
|
||||
}
|
||||
return $uf;
|
||||
}
|
||||
public function transform(DateTimeInterface $date, float $input, string $from = 'uf'): float
|
||||
{
|
||||
|
||||
$uf = $this->get($date);
|
||||
return $input * (($from === 'uf') ? $uf : 1/$uf);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user