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

119 lines
4.0 KiB
PHP
Raw Normal View History

2023-11-29 20:09:08 -03:00
<?php
namespace Incoviba\Service\Venta;
2025-04-22 13:07:31 -04:00
use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
2023-11-29 20:09:08 -03:00
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
2023-11-29 20:09:08 -03:00
class PropiedadUnidad
{
2023-12-01 15:00:25 -03:00
public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository,
protected Repository\Venta\Unidad $unidadRepository,
protected Precio $precioService, protected Service\Valor $valorService) {}
2023-11-29 20:09:08 -03:00
2025-04-22 13:07:31 -04:00
/**
* @param int $unidad_id
* @return Model\Venta\PropiedadUnidad
* @throws Read
*/
2023-11-29 20:09:08 -03:00
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
{
2025-04-22 13:07:31 -04:00
try {
return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id));
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
2023-11-29 20:09:08 -03:00
}
public function getByVenta(int $venta_id): array
{
2025-04-22 13:07:31 -04:00
try {
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id));
} catch (EmptyResult) {
return [];
}
2023-11-29 20:09:08 -03:00
}
public function getByPropiedad(int $propiedad_id): array
{
2025-04-22 13:07:31 -04:00
try {
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
} catch (EmptyResult) {
return [];
2025-04-24 19:23:39 -04:00
}
}
public function getArrayByPropiedad(int $propiedad_id): array
{
try {
return $this->propiedadUnidadRepository->fetchArrayByPropiedad($propiedad_id);
} catch (EmptyResult) {
return [];
2025-04-22 13:07:31 -04:00
}
2023-11-29 20:09:08 -03:00
}
2025-04-22 13:07:31 -04:00
/**
* @param array $data
* @return Model\Venta\PropiedadUnidad
* @throws Create
* @throws Read
*/
2023-12-01 15:00:25 -03:00
public function add(array $data): Model\Venta\PropiedadUnidad
{
2025-04-22 13:07:31 -04:00
try {
$unidad = $this->unidadRepository->fetchById($data['unidad']);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
2023-12-01 15:00:25 -03:00
$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);
2025-04-22 13:07:31 -04:00
try {
return $this->process($this->propiedadUnidadRepository->save($pu));
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
2023-12-01 15:00:25 -03:00
}
2025-04-22 13:07:31 -04:00
/**
* @param Model\Venta\PropiedadUnidad $propiedadUnidad
* @param array $data
* @return Model\Venta\PropiedadUnidad
* @throws EmptyResult
* @throws Update
*/
2023-11-29 20:09:08 -03:00
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
{
2025-04-22 13:07:31 -04:00
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));
2025-04-22 13:07:31 -04:00
} catch (PDOException $exception) {
throw new Update(__CLASS__, $exception);
}
2023-11-29 20:09:08 -03:00
}
2024-10-01 13:11:46 -03:00
protected function process(Model\Venta\PropiedadUnidad $unidad): Model\Venta\PropiedadUnidad
2023-11-29 20:09:08 -03:00
{
try {
$unidad->precios = $this->precioService->getByUnidad($unidad->id);
$unidad->currentPrecio = $this->precioService->getVigenteByUnidad($unidad->id);
if ($unidad->valor === null) {
$unidad->valor = $unidad->currentPrecio->valor;
}
2025-03-03 14:57:22 -03:00
} catch (Read) {}
2023-11-29 20:09:08 -03:00
return $unidad;
}
}