2023-07-28 16:22:20 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository\Venta;
|
|
|
|
|
|
|
|
use Incoviba\Common\Ideal;
|
|
|
|
use Incoviba\Common\Define;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Common\Implement;
|
2023-07-28 16:22:20 -04:00
|
|
|
use Incoviba\Model;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Service;
|
2023-07-28 16:22:20 -04:00
|
|
|
|
|
|
|
class Propiedad extends Ideal\Repository
|
|
|
|
{
|
2024-06-10 15:53:05 -04:00
|
|
|
public function __construct(Define\Connection $connection, protected Service\Venta\PropiedadUnidad $propiedadUnidadService, protected Service\Venta\Unidad $unidadService)
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
|
|
|
parent::__construct($connection);
|
|
|
|
$this->setTable('propiedad');
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:51:46 -03:00
|
|
|
public function create(?array $data = null): Model\Venta\Propiedad
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
2023-08-08 23:53:49 -04:00
|
|
|
$map = (new Implement\Repository\MapperParser())
|
2024-06-07 17:16:58 -04:00
|
|
|
->register('unidad_principal', (new Implement\Repository\Mapper())
|
|
|
|
->setProperty('unidades')
|
|
|
|
->setDefault([])
|
|
|
|
->setFunction(function($data) {
|
|
|
|
return [$this->unidadService->getById($data['unidad_principal'])];
|
|
|
|
})
|
|
|
|
)
|
2023-08-08 23:53:49 -04:00
|
|
|
->register('estado', new Implement\Repository\Mapper\Boolean('estado'));
|
2023-07-28 16:22:20 -04:00
|
|
|
return $this->parseData(new Model\Venta\Propiedad(), $data, $map);
|
|
|
|
}
|
2024-03-13 22:43:37 -03:00
|
|
|
public function load(array $data_row): Define\Model
|
|
|
|
{
|
|
|
|
$propiedad = parent::load($data_row);
|
|
|
|
if (isset($propiedad->id)) {
|
2024-06-10 15:53:05 -04:00
|
|
|
$propiedad->unidades = $this->propiedadUnidadService->getByPropiedad($propiedad->id);
|
2024-03-13 22:43:37 -03:00
|
|
|
}
|
|
|
|
return $propiedad;
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:51:46 -03:00
|
|
|
public function save(Define\Model $model): Model\Venta\Propiedad
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
|
|
|
$model->id = $this->saveNew(
|
|
|
|
['unidad_principal', 'estacionamientos', 'bodegas', 'estado'],
|
2024-06-07 17:16:58 -04:00
|
|
|
[$model->principal()?->id,
|
2023-09-13 18:51:46 -03:00
|
|
|
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]
|
2023-07-28 16:22:20 -04:00
|
|
|
);
|
|
|
|
return $model;
|
|
|
|
}
|
2025-03-03 14:57:22 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Define\Model $model
|
|
|
|
* @param array $new_data
|
|
|
|
* @return Model\Venta\Propiedad
|
|
|
|
* @throws Implement\Exception\EmptyResult
|
|
|
|
*/
|
2023-09-13 18:51:46 -03:00
|
|
|
public function edit(Define\Model $model, array $new_data): Model\Venta\Propiedad
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
|
|
|
return $this->update($model, ['unidad_principal', 'estacionamientos', 'bodegas', 'estado'], $new_data);
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
|
2025-03-03 14:57:22 -03:00
|
|
|
/**
|
|
|
|
* @param int $unidad_id
|
|
|
|
* @return Model\Venta\Propiedad
|
|
|
|
* @throws Implement\Exception\EmptyResult
|
|
|
|
*/
|
2023-09-13 18:51:46 -03:00
|
|
|
public function fetchVigenteByUnidad(int $unidad_id): Model\Venta\Propiedad
|
|
|
|
{
|
|
|
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `unidad_principal` = ?";
|
|
|
|
return $this->fetchOne($query, [$unidad_id]);
|
|
|
|
}
|
2023-07-28 16:22:20 -04:00
|
|
|
}
|