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

90 lines
3.1 KiB
PHP
Raw Normal View History

2023-09-13 18:51:46 -03:00
<?php
namespace Incoviba\Service\Venta;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
2024-11-18 23:25:20 -03:00
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Read, Create, Update};
2023-09-13 18:51:46 -03:00
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
2023-09-13 18:51:46 -03:00
class BonoPie extends Ideal\Service
2023-09-13 18:51:46 -03:00
{
public function __construct(LoggerInterface $logger,
protected Repository\Venta\BonoPie $bonoPieRepository,
protected Service\Valor $valorService,
protected Service\UF $ufService,
protected Pago $pagoService)
{
parent::__construct($logger);
}
2023-09-13 18:51:46 -03:00
/**
* @param array $data
* @return Model\Venta\BonoPie
* @throws Create
*/
2023-09-13 18:51:46 -03:00
public function add(array $data): Model\Venta\BonoPie
{
if (array_key_exists('valor', $data)) {
$data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true);
}
if (!array_key_exists('pago', $data)) {
$pago = $this->pagoService->add($data);
$data['pago'] = $pago->id;
2024-11-18 23:25:20 -03:00
}
$filteredData = $this->bonoPieRepository->filterData($data);
2024-11-18 23:25:20 -03:00
try {
return $this->bonoPieRepository->fetchByPago($filteredData['pago']);
2024-11-18 23:25:20 -03:00
} catch (EmptyResult) {
$filteredData['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true);
2024-11-18 23:25:20 -03:00
$bono = $this->bonoPieRepository->create($filteredData);
try {
return $this->bonoPieRepository->save($bono);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
}
/**
* @param Model\Venta\BonoPie $bonoPie
* @param array $data
* @return Model\Venta\BonoPie
* @throws Update
*/
public function edit(Model\Venta\BonoPie $bonoPie, array $data): Model\Venta\BonoPie
{
if (array_key_exists('valor', $data)) {
$data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true);
}
if (!array_key_exists('pago', $data)) {
$pago = $this->pagoService->edit($bonoPie->pago, $data);
$data['pago'] = $pago->id;
}
$data['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true);
$filteredData = $this->bonoPieRepository->filterData($data);
try {
return $this->bonoPieRepository->edit($bonoPie, $filteredData);
} catch (PDOException | EmptyResult $exception) {
throw new Update(__CLASS__, $exception);
2024-11-18 23:25:20 -03:00
}
2023-09-13 18:51:46 -03:00
}
2025-03-03 14:57:22 -03:00
/**
* @param int $venta_id
* @return Model\Venta\BonoPie
* @throws Read
*/
2024-03-13 14:38:44 -03:00
public function getByVenta(int $venta_id): Model\Venta\BonoPie
{
2025-03-03 14:57:22 -03:00
try {
return $this->bonoPieRepository->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
}