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

95 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace Incoviba\Service\Venta;
2025-03-26 16:41:54 -03:00
use PDOException;
use DateTimeImmutable;
use DateMalformedStringException;
2023-12-20 08:44:49 -03:00
use Incoviba\Common\Implement\Exception\EmptyResult;
2025-03-26 16:41:54 -03:00
use Incoviba\Exception\ServiceAction\{Create, Read};
use Incoviba\Model;
2025-03-26 16:41:54 -03:00
use Incoviba\Repository;
use Incoviba\Service\UF;
class Pie
{
public function __construct(
protected Repository\Venta\Pie $pieRepository,
2023-12-20 08:44:49 -03:00
protected Cuota $cuotaService,
2025-03-26 16:41:54 -03:00
protected Pago $pagoService,
protected UF $ufService
) {}
public function getById(int $pie_id): Model\Venta\Pie
{
2023-12-20 08:44:49 -03:00
return $this->process($this->pieRepository->fetchById($pie_id));
}
2024-03-13 14:38:44 -03:00
public function getByVenta(int $venta_id): Model\Venta\Pie
{
2025-03-03 14:57:22 -03:00
try {
return $this->process($this->pieRepository->fetchByVenta($venta_id));
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
2024-03-13 14:38:44 -03:00
}
2023-09-13 18:51:46 -03:00
2025-03-26 16:41:54 -03:00
/**
* @param array $data
* @return Model\Venta\Pie
* @throws Create
*/
2023-09-13 18:51:46 -03:00
public function add(array $data): Model\Venta\Pie
{
2025-03-26 16:41:54 -03:00
try {
$filteredData = $this->pieRepository->filterData($data);
if (!isset($filteredData['uf'])) {
try {
$date = new DateTimeImmutable($filteredData['fecha']);
$filteredData['uf'] = $this->ufService->get($date);
} catch (DateMalformedStringException) {}
}
$pie = $this->pieRepository->create($filteredData);
return $this->pieRepository->save($pie);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
2023-09-13 18:51:46 -03:00
}
2025-05-10 12:39:31 -04:00
/**
* @param array $data
* @return Model\Venta\Cuota
* @throws Create
*/
2023-09-13 18:51:46 -03:00
public function addCuota(array $data): Model\Venta\Cuota
{
return $this->cuotaService->add($data);
}
2023-11-29 22:21:46 -03:00
public function edit(Model\Venta\Pie $pie, array $data): Model\Venta\Pie
{
$filteredData = $this->pieRepository->filterData($data);
return $this->pieRepository->edit($pie, $filteredData);
2023-11-29 22:21:46 -03:00
}
2023-12-20 08:44:49 -03:00
public function reajustar(Model\Venta\Pie $pie, array $data): Model\Venta\Pie
{
$pago = $this->pagoService->add($data);
return $this->pieRepository->edit($pie, ['reajuste' => $pago->id]);
}
protected function process(Model\Venta\Pie $pie): Model\Venta\Pie
{
2023-12-20 15:37:47 -03:00
$pie->cuotasArray = $this->cuotaService->getByPie($pie->id);
2023-12-20 08:44:49 -03:00
try {
$pie->asociados = $this->pieRepository->fetchAsociados($pie->id);
} catch (EmptyResult) {}
if (isset($pie->asociado)) {
$pie->asociado = $this->getById($pie->asociado->id);
}
if (($pie->uf === null or $pie->uf === 0.0) and $pie->fecha <= new DateTimeImmutable()) {
$pie->uf = $this->ufService->get($pie->fecha);
try {
$this->pieRepository->edit($pie, ['uf' => $pie->uf]);
} catch (EmptyResult) {}
}
2023-12-20 08:44:49 -03:00
return $pie;
}
}