Editar valor venta
This commit is contained in:
@ -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']);
|
||||
});
|
||||
|
@ -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,66 +32,53 @@
|
||||
|
||||
@push('page_scripts')
|
||||
<script type="text/javascript">
|
||||
function getMonthsList() {
|
||||
const formatter = new Intl.DateTimeFormat('es-CL', {month: 'long'})
|
||||
const months = []
|
||||
let m = ''
|
||||
for (let i = 0; i < 12; i ++) {
|
||||
m = formatter.format((new Date()).setMonth(i))
|
||||
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('-')
|
||||
const editVenta = {
|
||||
getMonthsList() {
|
||||
const formatter = new Intl.DateTimeFormat('es-CL', {month: 'long'})
|
||||
const months = []
|
||||
let m = ''
|
||||
for (let i = 0; i < 12; i ++) {
|
||||
m = formatter.format((new Date()).setMonth(i))
|
||||
months.push(m.charAt(0).toUpperCase() + m.slice(1))
|
||||
}
|
||||
return months
|
||||
},
|
||||
redirect() {
|
||||
window.location = '{{$urls->base}}/venta/{{$venta->id}}'
|
||||
},
|
||||
edit() {
|
||||
const uri = '{{$urls->api}}/venta/{{$venta->id}}'
|
||||
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) {
|
||||
return response.json()
|
||||
}
|
||||
return
|
||||
}
|
||||
if (collator.compare(val, original[name]) !== 0) {
|
||||
data[name] = val
|
||||
}
|
||||
})
|
||||
if (Object.keys(data).length === 0) {
|
||||
redirect()
|
||||
return
|
||||
}).then(json => {
|
||||
if (!json.edited) {
|
||||
return
|
||||
}
|
||||
this.redirect()
|
||||
})
|
||||
},
|
||||
setup() {
|
||||
$('#fecha_calendar').calendar({
|
||||
type: 'date',
|
||||
initialDate: '{{$venta->fecha->format('Y-m-d')}}',
|
||||
text: {
|
||||
months: this.getMonthsList()
|
||||
}
|
||||
})
|
||||
$('#edit_form').submit(event => {
|
||||
event.preventDefault()
|
||||
this.edit()
|
||||
return false
|
||||
})
|
||||
}
|
||||
const uri = '{{$urls->api}}/venta/{{$venta->id}}'
|
||||
return fetchAPI(uri,
|
||||
{method: 'put', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}
|
||||
).then(response => {
|
||||
if (response.ok) {
|
||||
redirect()
|
||||
}
|
||||
})
|
||||
}
|
||||
$(document).ready(() => {
|
||||
$('#fecha_calendar').calendar({
|
||||
type: 'date',
|
||||
initialDate: '{{$venta->fecha->format('Y-m-d')}}',
|
||||
text: {
|
||||
months: getMonthsList()
|
||||
}
|
||||
})
|
||||
$('#edit_form').submit(event => {
|
||||
event.preventDefault()
|
||||
editVenta()
|
||||
return false
|
||||
})
|
||||
editVenta.setup()
|
||||
})
|
||||
</script>
|
||||
@endpush
|
||||
|
@ -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 => {
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user