52 lines
2.0 KiB
PHP
52 lines
2.0 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Venta;
|
|
|
|
use DateTimeImmutable;
|
|
use DateMalformedStringException;
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('vale vista');
|
|
$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]);
|
|
return $this->subsidioRepository->save($subsidio);
|
|
}
|
|
}
|