Files
oficial/app/src/Service/Venta/Credito.php

97 lines
3.1 KiB
PHP
Raw Normal View History

2023-09-13 18:51:46 -03:00
<?php
namespace Incoviba\Service\Venta;
use Exception;
2023-09-13 18:51:46 -03:00
use DateTimeImmutable;
2025-03-03 14:57:22 -03:00
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Read;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
2023-09-13 18:51:46 -03:00
use Incoviba\Repository;
use Incoviba\Model;
use Incoviba\Service;
2023-09-13 18:51:46 -03:00
class Credito extends Ideal\Service
2023-09-13 18:51:46 -03:00
{
public function __construct(
2024-02-15 18:57:56 -03:00
LoggerInterface $logger,
2023-09-13 18:51:46 -03:00
protected Repository\Venta\Credito $creditoRepository,
protected Pago $pagoService,
2023-09-13 18:51:46 -03:00
protected Repository\Venta\TipoPago $tipoPagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Service\Money $moneyService,
protected Service\Valor $valorService
2024-02-15 18:57:56 -03:00
) {
parent::__construct($logger);
}
2023-09-13 18:51:46 -03:00
2025-03-03 14:57:22 -03:00
/**
* @param int $venta_id
* @return Model\Venta\Credito
* @throws Read
*/
2024-03-13 14:38:44 -03:00
public function getByVenta(int $venta_id): Model\Venta\Credito
{
2025-03-03 14:57:22 -03:00
try {
return $this->creditoRepository->fetchByVenta($venta_id);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
2024-03-13 14:38:44 -03:00
}
/**
* @throws Exception
*/
2023-09-13 18:51:46 -03:00
public function add(array $data): Model\Venta\Credito
{
$fecha = new DateTimeImmutable($data['fecha']);
$uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha);
2023-09-13 18:51:46 -03:00
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo');
$valor = $this->valorService->clean($data['valor']);
$pago = $this->pagoService->add([
'fecha' => $fecha->format('Y-m-d'),
'valor' => $valor * $uf,
'uf' => $uf,
'tipo' => $tipoPago->id
]);
2023-09-13 18:51:46 -03:00
$credito = $this->creditoRepository->create([
'valor' => $valor,
2023-09-13 18:51:46 -03:00
'fecha' => $fecha->format('Y-m-d'),
'pago' => $pago->id
]);
return $this->creditoRepository->save($credito);
}
/**
* @throws Exception
*/
2024-02-15 18:57:56 -03:00
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)) {
$fecha = new DateTimeImmutable($data['fecha']);
$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);
2024-02-15 18:57:56 -03:00
}
2024-11-29 18:09:55 -03:00
$filteredData = array_intersect_key($data, array_flip([
2024-02-15 18:57:56 -03:00
'fecha',
'uf',
'valor',
'banco'
2024-11-29 18:09:55 -03:00
]));
$filteredDataPago = $filteredData;
if (isset($valorPago)) {
$filteredDataPago['valor'] = $valorPago;
}
$credito->pago = $this->pagoService->edit($credito->pago, $filteredDataPago);
2024-02-15 18:57:56 -03:00
return $this->creditoRepository->edit($credito, $filteredData);
}
2023-09-13 18:51:46 -03:00
}