Files
oficial/app/src/Repository/Venta/Propiedad.php
2025-04-22 15:18:59 -04:00

72 lines
2.4 KiB
PHP

<?php
namespace Incoviba\Repository\Venta;
use PDO;
use PDOException;
use Incoviba\Common\Ideal;
use Incoviba\Common\Define;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Service;
class Propiedad extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Service\Venta\PropiedadUnidad $propiedadUnidadService, protected Service\Venta\Unidad $unidadService)
{
parent::__construct($connection);
$this->setTable('propiedad');
}
public function create(?array $data = null): Model\Venta\Propiedad
{
$map = (new Implement\Repository\MapperParser())
->register('estado', new Implement\Repository\Mapper\Boolean('estado'));
return $this->parseData(new Model\Venta\Propiedad(), $data, $map);
}
public function load(array $data_row): Define\Model
{
$propiedad = parent::load($data_row);
if (isset($propiedad->id)) {
$propiedad->unidades = $this->propiedadUnidadService->getByPropiedad($propiedad->id);
}
return $propiedad;
}
public function save(Define\Model $model): Model\Venta\Propiedad
{
$model->id = $this->saveNew(
['unidad_principal', 'estacionamientos', 'bodegas', 'estado'],
[$model->principal()?->id,
implode(',', array_map(function(Model\Venta\Unidad $unidad) {return $unidad->id;}, $model->estacionamientos())),
implode(',', array_map(function(Model\Venta\Unidad $unidad) {return $unidad->id;}, $model->bodegas())),
1]
);
return $model;
}
/**
* @param Define\Model $model
* @param array $new_data
* @return Model\Venta\Propiedad
* @throws Implement\Exception\EmptyResult
*/
public function edit(Define\Model $model, array $new_data): Model\Venta\Propiedad
{
return $this->update($model, ['unidad_principal', 'estacionamientos', 'bodegas', 'estado'], $new_data);
}
/**
* @param int $unidad_id
* @return Model\Venta\Propiedad
* @throws Implement\Exception\EmptyResult
*/
public function fetchVigenteByUnidad(int $unidad_id): Model\Venta\Propiedad
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('`unidad_principal` = ? AND `estado` = 1');
return $this->fetchOne($query, [$unidad_id]);
}
}