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
|
|
|
|
{
|
2023-11-29 20:09:08 -03:00
|
|
|
public function __construct(Define\Connection $connection, protected Service\Venta\PropiedadUnidad $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())
|
|
|
|
->register('unidad_principal', (new Implement\Repository\Mapper())
|
|
|
|
->setProperty('unidades')
|
|
|
|
->setFunction(function($data) {
|
2023-09-13 18:51:46 -03:00
|
|
|
if (isset($data['id'])) {
|
|
|
|
return $this->unidadService->getByPropiedad($data['id']);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
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'],
|
2023-09-13 18:51:46 -03:00
|
|
|
[$model->departamentos()[0]->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]
|
2023-07-28 16:22:20 -04:00
|
|
|
);
|
|
|
|
return $model;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|