Editar valor venta

This commit is contained in:
2023-12-04 19:00:21 -03:00
parent 57579a52f1
commit 377cf51b44
5 changed files with 69 additions and 61 deletions

View File

@ -24,5 +24,6 @@ $app->group('/ventas', function($app) {
$app->group('/venta/{venta_id}', function($app) {
$app->get('/unidades', [Ventas::class, 'unidades']);
$app->get('/comentarios', [Ventas::class, 'comentarios']);
$app->post('[/]', [Ventas::class, 'edit']);
$app->get('[/]', [Ventas::class, 'get']);
});

View File

@ -2,7 +2,10 @@
@section('page_content')
<div class="ui container">
<h2 class="ui header">Editar Venta</h2>
<h2 class="ui header">Editar Venta -
{{$venta->proyecto()->descripcion}} -
<a href="{{$urls->base}}/venta/{{$venta->id}}">{{$venta->propiedad()->summary()}}</a>
</h2>
<form class="ui form" id="edit_form">
<div class="inline field">
<label for="valor">Valor</label>
@ -29,7 +32,8 @@
@push('page_scripts')
<script type="text/javascript">
function getMonthsList() {
const editVenta = {
getMonthsList() {
const formatter = new Intl.DateTimeFormat('es-CL', {month: 'long'})
const months = []
let m = ''
@ -38,57 +42,43 @@
months.push(m.charAt(0).toUpperCase() + m.slice(1))
}
return months
}
function redirect() {
const uri = '{{$urls->base}}/venta/{{$venta->id}}'
window.location = uri
}
function editVenta() {
const original = {
valor: {{$venta->valor}},
fecha: new Date('{{$venta->fecha->format('Y-m-d')}}T00:00:00')
}
const collator = new Intl.Collator('es-CL')
const data = {}
Object.keys(original).forEach(name => {
let val = $("[name='" + name + "']").val()
if (name === 'fecha') {
val = $('#fecha_calendar').calendar('get date')
if (val.getTime() !== original[name].getTime()) {
data[name] = [val.getFullYear(), (''+(val.getMonth()+1)).padStart(2, '0'), (''+val.getDate()).padStart(2, '0')].join('-')
}
return
}
if (collator.compare(val, original[name]) !== 0) {
data[name] = val
}
})
if (Object.keys(data).length === 0) {
redirect()
return
}
},
redirect() {
window.location = '{{$urls->base}}/venta/{{$venta->id}}'
},
edit() {
const uri = '{{$urls->api}}/venta/{{$venta->id}}'
return fetchAPI(uri,
{method: 'put', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}
).then(response => {
const data = new FormData()
data.set('valor', $('#valor').val())
data.set('fecha', $('#fecha_calendar').calendar('get date').toISOString())
return fetchAPI(uri, {method: 'post', body: data}).then(response => {
if (response.ok) {
redirect()
return response.json()
}
}).then(json => {
if (!json.edited) {
return
}
this.redirect()
})
}
$(document).ready(() => {
},
setup() {
$('#fecha_calendar').calendar({
type: 'date',
initialDate: '{{$venta->fecha->format('Y-m-d')}}',
text: {
months: getMonthsList()
months: this.getMonthsList()
}
})
$('#edit_form').submit(event => {
event.preventDefault()
editVenta()
this.edit()
return false
})
}
}
$(document).ready(() => {
editVenta.setup()
})
</script>
@endpush

View File

@ -158,10 +158,10 @@
return
}
const old_value = this.unidades[idx].valor
if (old_value === parseFloat(value)) {
if (old_value === parseFloat(valor)) {
return
}
const url = '{{$urls->api}}/ventas/propiedades/unidad/' + id + '/edit'
const url = '{{$urls->api}}/ventas/propiedades/unidad/' + pid + '/edit'
const data = new FormData()
data.set('valor', valor)
return fetchAPI(url, {method: 'post', body: data}).then(response => {

View File

@ -189,6 +189,23 @@ class Ventas
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta $ventaRepository, int $venta_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $body,
'edited' => false
];
try {
$venta = $ventaRepository->fetchById($venta_id);
$body['valor_uf'] = $body['valor'];
$body['fecha'] = (new DateTimeImmutable($body['fecha']))->format('Y-m-d');
$ventaRepository->edit($venta, $body);
$output['edited'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function unidades(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface
{

View File

@ -75,10 +75,10 @@ class Venta extends Ideal\Model
if (!isset($this->valor_util)) {
$sum = $this->valor;
$sum -= array_reduce($this->propiedad()->estacionamientos(), function(float $sum, Venta\Unidad $unidad) {
return $unidad->precio($this->fecha)->valor;
return $unidad->valor ?? $unidad->precio($this->fecha)->valor;
}, 0);
$sum -= array_reduce($this->propiedad()->bodegas(), function(float $sum, Venta\Unidad $unidad) {
return $unidad->precio($this->fecha)->valor;
return $unidad->valor ?? $unidad->precio($this->fecha)->valor;
}, 0);
$this->valor_util = $sum;
}