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

62 lines
2.4 KiB
PHP
Raw Normal View History

2023-11-29 20:09:08 -03:00
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Common\Implement\Exception\EmptyResult;
2025-03-03 14:57:22 -03:00
use Incoviba\Exception\ServiceAction\Read;
2023-11-29 20:09:08 -03:00
use Incoviba\Repository;
use Incoviba\Service;
use Incoviba\Model;
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) {}
2023-11-29 20:09:08 -03:00
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
{
return $this->process($this->propiedadUnidadRepository->fetchById($unidad_id));
}
public function getByVenta(int $venta_id): array
{
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByVenta($venta_id));
}
public function getByPropiedad(int $propiedad_id): array
{
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
}
2023-12-01 15:00:25 -03:00
public function add(array $data): Model\Venta\PropiedadUnidad
{
$unidad = $this->unidadRepository->fetchById($data['unidad']);
$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);
return $this->process($this->propiedadUnidadRepository->save($pu));
}
2023-11-29 20:09:08 -03:00
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
{
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));
}
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);
2025-03-03 14:57:22 -03:00
if ($unidad->valor === null or $unidad->valor === 0) {
2023-11-29 21:13:03 -03:00
$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;
}
}