Files
oficial/app/src/Service/Venta/Subsidio.php
2025-05-15 15:23:34 -04:00

67 lines
2.4 KiB
PHP

<?php
namespace Incoviba\Service\Venta;
use DateTimeImmutable;
use DateMalformedStringException;
use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Model;
use Incoviba\Service;
class Subsidio
{
public function __construct(
protected Repository\Venta\Subsidio $subsidioRepository,
protected Pago $pagoService,
protected Repository\Venta\TipoPago $tipoPagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Service\Money $moneyService,
protected Service\Valor $valorService
) {}
/**
* @param int $venta_id
* @return Model\Venta\Subsidio
* @throws Read
*/
public function getByVenta(int $venta_id): Model\Venta\Subsidio
{
try {
return $this->subsidioRepository->fetchByVenta($venta_id);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
/**
* @param array $data
* @return Model\Venta\Subsidio
* @throws Create
*/
public function add(array $data): Model\Venta\Subsidio
{
$fecha = new DateTimeImmutable();
try {
$fecha = new DateTimeImmutable($data['fecha']);
} catch (DateMalformedStringException) {}
$uf = $data['uf'] ?? $this->moneyService->getUF($fecha);
try {
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('vale vista');
} catch (EmptyResult $exception) {
throw new Create(__CLASS__, $exception);
}
$ahorro = $this->pagoService->add(['fecha' => $fecha->format('Y-m-d'), 'valor' => $this->valorService->clean($data['ahorro']) * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$subsidioPago = $this->pagoService->add(['fecha' => $fecha->format('Y-m-d'), 'valor' => $this->valorService->clean($data['subsidio']) * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$subsidio = $this->subsidioRepository->create(['pago' => $ahorro->id, 'subsidio' => $subsidioPago->id]);
try {
return $this->subsidioRepository->save($subsidio);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
}