Editar propiedad en venta
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
<?php
|
||||
use Incoviba\Controller\API\Ventas\PropiedadesUnidades;
|
||||
|
||||
$app->group('/unidades', function($app) {
|
||||
$app->post('/add[/]', [PropiedadesUnidades::class, 'add']);
|
||||
});
|
||||
$app->group('/unidad/{pu_id}', function($app) {
|
||||
$app->post('/edit[/]', [PropiedadesUnidades::class, 'edit']);
|
||||
$app->delete('[/]', [PropiedadesUnidades::class, 'remove']);
|
||||
});
|
||||
|
@ -45,7 +45,7 @@
|
||||
<div class="ui modal" id="add_modal">
|
||||
<div class="content">
|
||||
<h3 class="header">Agregar</h3>
|
||||
<div class="ui form" id="add_form">
|
||||
<form class="ui form" id="add_form">
|
||||
<div class="field">
|
||||
<label for="tipo">Tipo</label>
|
||||
<select id="tipo" name="tipo" class="ui search selection dropdown">
|
||||
@ -58,8 +58,14 @@
|
||||
<label for="unidad">Unidad</label>
|
||||
<select id="unidad" name="unidad" class="ui search selection dropdown" size="4"></select>
|
||||
</div>
|
||||
<button class="ui button">Agregar</button>
|
||||
<div class="field">
|
||||
<label for="valor">Valor Venta</label>
|
||||
<input id="valor" type="text" name="valor" />
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button class="ui approve button">Agregar</button>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@ -72,6 +78,7 @@
|
||||
{
|
||||
id: {{$unidad->id}},
|
||||
tipo: '{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}',
|
||||
tipo_id: {{$unidad->proyectoTipoUnidad->tipoUnidad->id}},
|
||||
descripcion: '{{$unidad->descripcion}}',
|
||||
pid: {{$unidad->pu_id}},
|
||||
valor: {{($unidad->valor > 0) ? $unidad->valor : $unidad->precio($venta->fecha)->valor}}
|
||||
@ -99,8 +106,51 @@
|
||||
addUnidad: function() {
|
||||
$('#add_modal').modal('show')
|
||||
},
|
||||
doAddUnidad: function() {
|
||||
const url = '{{$urls->api}}/ventas/propiedades/unidades/add'
|
||||
const data = new FormData(document.getElementById('add_form'))
|
||||
data.set('propiedad', {{$propiedad->id}})
|
||||
return fetchAPI(url, {method: 'post', body: data}).then(response => {
|
||||
if (response.ok) {
|
||||
return response.json()
|
||||
}
|
||||
}).then(json => {
|
||||
if (!json.added) {
|
||||
return
|
||||
}
|
||||
const tipo = json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.descripcion
|
||||
this.unidades.push({
|
||||
id: json.propiedad_unidad.id,
|
||||
tipo: tipo.charAt(0).toUpperCase() + tipo.slice(1),
|
||||
tipo_id: json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.id,
|
||||
descripcion: json.propiedad_unidad.descripcion,
|
||||
pid: json.propiedad_unidad.pu_id,
|
||||
valor: parseFloat(json.propiedad_unidad.valor)
|
||||
})
|
||||
this.draw()
|
||||
const idx = this.tipos[json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.id].findIndex(unidad => unidad.value === json.propiedad_unidad.id)
|
||||
this.tipos[json.propiedad_unidad.proyecto_tipo_unidad.tipo_unidad.id].splice(idx,1)
|
||||
})
|
||||
},
|
||||
removeUnidad: function(unidad_id) {
|
||||
console.debug(unidad_id)
|
||||
const url = '{{$urls->api}}/ventas/propiedades/unidad/' + unidad_id
|
||||
return fetchAPI(url, {method: 'delete'}).then(response => {
|
||||
if (response.ok) {
|
||||
return response.json()
|
||||
}
|
||||
}).then(json => {
|
||||
if (!json.removed) {
|
||||
return
|
||||
}
|
||||
const idx = this.unidades.findIndex(unidad => unidad.pid === json.propiedad_unidad_id)
|
||||
const unidad = this.unidades.splice(idx,1)[0]
|
||||
this.draw()
|
||||
this.tipos[unidad.tipo_id].push({
|
||||
value: unidad.id,
|
||||
text: unidad.descripcion,
|
||||
name: unidad.descripcion
|
||||
})
|
||||
})
|
||||
},
|
||||
updatePrecio: function(pid, valor) {
|
||||
const idx = this.unidades.findIndex(unidad => unidad.pid === pid)
|
||||
@ -113,7 +163,7 @@
|
||||
}
|
||||
const url = '{{$urls->api}}/ventas/propiedades/unidad/' + id + '/edit'
|
||||
const data = new FormData()
|
||||
data.set('valor', value)
|
||||
data.set('valor', valor)
|
||||
return fetchAPI(url, {method: 'post', body: data}).then(response => {
|
||||
if (response.ok) {
|
||||
return response.json()
|
||||
@ -170,7 +220,11 @@
|
||||
const unidad_id = $(event.currentTarget).data('pid')
|
||||
this.removeUnidad(unidad_id)
|
||||
})
|
||||
$('#add_modal').modal()
|
||||
$('#add_modal').modal({
|
||||
onApprove: ($element) => {
|
||||
this.doAddUnidad()
|
||||
}
|
||||
})
|
||||
const tipo = $('#tipo')
|
||||
tipo.dropdown()
|
||||
tipo.change(event => {
|
||||
@ -180,7 +234,8 @@
|
||||
this.changeTipoUnidad(tipo.val())
|
||||
$('#add_form').submit(event => {
|
||||
event.preventDefault()
|
||||
tipo.model('hide')
|
||||
this.doAddUnidad()
|
||||
tipo.modal('hide')
|
||||
return false
|
||||
})
|
||||
$('.precio').change(event => {
|
||||
|
@ -7,12 +7,30 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Incoviba\Controller\API\{withJson, emptyBody};
|
||||
use Incoviba\Controller\withRedis;
|
||||
use Incoviba\Common\Implement\Exception\{EmptyResult,EmptyRedis};
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class PropiedadesUnidades
|
||||
{
|
||||
use emptyBody, withJson, withRedis;
|
||||
|
||||
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'propiedad_unidad' => null,
|
||||
'added' => false
|
||||
];
|
||||
try {
|
||||
$pu = $propiedadUnidadService->add($body);
|
||||
$output['propiedad_unidad'] = $pu;
|
||||
$output['added'] = true;
|
||||
} catch (EmptyResult $exception) {
|
||||
error_log($exception);
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Venta\PropiedadUnidad $propiedadUnidadService, int $pu_id): ResponseInterface
|
||||
{
|
||||
@ -29,4 +47,18 @@ class PropiedadesUnidades
|
||||
} catch (PDOException | EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function remove(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, int $pu_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'propiedad_unidad_id' => $pu_id,
|
||||
'removed' => false
|
||||
];
|
||||
try {
|
||||
$pu = $propiedadUnidadRepository->fetchById($pu_id);
|
||||
$propiedadUnidadRepository->remove($pu);
|
||||
$output['removed'] = true;
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ class PropiedadUnidad extends Unidad
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'pu_id' => $this->pu_id,
|
||||
'propiedad_id' => $this->propiedad_id,
|
||||
'valor' => $this->valor
|
||||
]);
|
||||
}
|
||||
|
@ -86,6 +86,13 @@ class PropiedadUnidad extends Ideal\Repository
|
||||
->group('`unidad`.`id`');
|
||||
return $this->fetchMany($query, [$propiedad_id]);
|
||||
}
|
||||
public function remove(Define\Model $model): void
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->delete()->from($this->getTable())
|
||||
->where("id = ?");
|
||||
$this->connection->execute($query, [$model->pu_id]);
|
||||
}
|
||||
|
||||
protected function update(Define\Model $model, array $columns, array $data): Define\Model
|
||||
{
|
||||
|
@ -8,7 +8,9 @@ use Incoviba\Model;
|
||||
|
||||
class PropiedadUnidad
|
||||
{
|
||||
public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository, protected Precio $precioService) {}
|
||||
public function __construct(protected Repository\Venta\PropiedadUnidad $propiedadUnidadRepository,
|
||||
protected Repository\Venta\Unidad $unidadRepository,
|
||||
protected Precio $precioService) {}
|
||||
|
||||
public function getById(int $unidad_id): Model\Venta\PropiedadUnidad
|
||||
{
|
||||
@ -22,6 +24,23 @@ class PropiedadUnidad
|
||||
{
|
||||
return array_map([$this, 'process'], $this->propiedadUnidadRepository->fetchByPropiedad($propiedad_id));
|
||||
}
|
||||
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));
|
||||
}
|
||||
public function edit(Model\Venta\PropiedadUnidad $propiedadUnidad, array $data): Model\Venta\PropiedadUnidad
|
||||
{
|
||||
return $this->process($this->propiedadUnidadRepository->edit($propiedadUnidad, $data));
|
||||
|
Reference in New Issue
Block a user