Files
oficial/app/src/Service/Venta/PropiedadUnidad.php
2025-04-29 13:01:57 -04:00

119 lines
4.0 KiB
PHP

<?php
namespace Incoviba\Service\Venta;
use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class PropiedadUnidad
{
public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository,
protected Repository\Venta\Unidad $unidadRepository,
protected Precio $precioService, protected Service\Valor $valorService) {}
/**
* @param int $unidad_id
* @return Model\Venta\PropiedadUnidad
* @throws Read
*/
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
{
try {
return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id));
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
public function getByVenta(int $venta_id): array
{
try {
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id));
} catch (EmptyResult) {
return [];
}
}
public function getByPropiedad(int $propiedad_id): array
{
try {
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
} catch (EmptyResult) {
return [];
}
}
public function getArrayByPropiedad(int $propiedad_id): array
{
try {
return $this->propiedadUnidadRepository->fetchArrayByPropiedad($propiedad_id);
} catch (EmptyResult) {
return [];
}
}
/**
* @param array $data
* @return Model\Venta\PropiedadUnidad
* @throws Create
* @throws Read
*/
public function add(array $data): Model\Venta\PropiedadUnidad
{
try {
$unidad = $this->unidadRepository->fetchById($data['unidad']);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
$temp = json_decode(json_encode($unidad), JSON_OBJECT_AS_ARRAY);
$columnMap = [
'proyecto_tipo_unidad' => 'pt'
];
foreach ($temp as $key => $value) {
if (isset($columnMap[$key])) {
$temp[$columnMap[$key]] = $value['id'];
}
}
$temp['propiedad'] = $data['propiedad'];
$temp['valor'] = $data['valor'];
$pu = $this->propiedadUnidadRepository->create($temp);
try {
return $this->process($this->propiedadUnidadRepository->save($pu));
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
/**
* @param Model\Venta\PropiedadUnidad $propiedadUnidad
* @param array $data
* @return Model\Venta\PropiedadUnidad
* @throws EmptyResult
* @throws Update
*/
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
{
try {
$filteredData = $this->propiedadUnidadRepository->filterData($data);
if (array_key_exists('valor', $filteredData)) {
$filteredData['valor'] = $this->valorService->clean($filteredData['valor']);
}
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $filteredData));
} catch (PDOException $exception) {
throw new Update(__CLASS__, $exception);
}
}
protected function process(Model\Venta\PropiedadUnidad $unidad): Model\Venta\PropiedadUnidad
{
try {
$unidad->precios = $this->precioService->getByUnidad($unidad->id);
$unidad->currentPrecio = $this->precioService->getVigenteByUnidad($unidad->id);
if ($unidad->valor === null) {
$unidad->valor = $unidad->currentPrecio->valor;
}
} catch (Read) {}
return $unidad;
}
}