Files
oficial/app/src/Service/Venta/Credito.php
Juan Pablo Vial 5f348e954e FIX: edit payments
2025-11-25 13:22:52 -03:00

134 lines
4.3 KiB
PHP

<?php
namespace Incoviba\Service\Venta;
use DateMalformedStringException;
use Exception;
use DateTimeImmutable;
use Incoviba\Exception\ServiceAction\Update;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
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 Credito extends Ideal\Service
{
public function __construct(
LoggerInterface $logger,
protected Repository\Venta\Credito $creditoRepository,
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
) {
parent::__construct($logger);
}
/**
* @param int $venta_id
* @return Model\Venta\Credito
* @throws Read
*/
public function getByVenta(int $venta_id): Model\Venta\Credito
{
try {
return $this->creditoRepository->fetchByVenta($venta_id);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
/**
* @param array $data
* @return Model\Venta\Credito
* @throws Create
*/
public function add(array $data): Model\Venta\Credito
{
try {
$fecha = new DateTimeImmutable($data['fecha']);
} catch (DateMalformedStringException) {
$fecha = new DateTimeImmutable();
}
if (array_key_exists('uf', $data)) {
$uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha);
} else {
$uf = $this->moneyService->getUF($fecha);
}
try {
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo');
} catch (EmptyResult $exception) {
throw new Create(__CLASS__, $exception);
}
$valor = $this->valorService->clean($data['valor']);
$pago = $this->pagoService->add([
'fecha' => $fecha->format('Y-m-d'),
'valor' => round($valor * $uf),
'uf' => $uf,
'tipo' => $tipoPago->id
]);
try {
$credito = $this->creditoRepository->create([
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
'pago' => $pago->id
]);
} catch (EmptyResult $exception) {
throw new Create(__CLASS__, $exception);
}
try {
return $this->creditoRepository->save($credito);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
/**
* @param Model\Venta\Credito $credito
* @param array $data
* @return Model\Venta\Credito
* @throws Update
*/
public function edit(Model\Venta\Credito $credito, array $data): Model\Venta\Credito
{
$uf = $this->moneyService->getUF($credito->pago->fecha);
if (array_key_exists('fecha', $data)) {
try {
$fecha = new DateTimeImmutable($data['fecha']);
} catch (DateMalformedStringException $exception) {
throw new Update(__CLASS__, $exception);
}
$data['fecha'] = $fecha->format('Y-m-d');
$uf = $this->moneyService->getUF($fecha);
$data['uf'] = $uf;
}
if (array_key_exists('valor', $data)) {
$data['valor'] = $this->valorService->clean($data['valor']);
$valorPago = round($data['valor'] * $uf);
}
$filteredData = array_intersect_key($data, array_flip([
'fecha',
'uf',
'valor',
'banco'
]));
$filteredDataPago = $filteredData;
if (isset($valorPago)) {
$filteredDataPago['valor'] = $valorPago;
}
$credito->pago = $this->pagoService->edit($credito->pago, $filteredDataPago);
try {
return $this->creditoRepository->edit($credito, $filteredData);
} catch (EmptyResult $exception) {
throw new Update(__CLASS__, $exception);
}
}
}