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->group('/venta/{venta_id}', function($app) {
$app->get('/unidades', [Ventas::class, 'unidades']); $app->get('/unidades', [Ventas::class, 'unidades']);
$app->get('/comentarios', [Ventas::class, 'comentarios']); $app->get('/comentarios', [Ventas::class, 'comentarios']);
$app->post('[/]', [Ventas::class, 'edit']);
$app->get('[/]', [Ventas::class, 'get']); $app->get('[/]', [Ventas::class, 'get']);
}); });

View File

@ -2,7 +2,10 @@
@section('page_content') @section('page_content')
<div class="ui container"> <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"> <form class="ui form" id="edit_form">
<div class="inline field"> <div class="inline field">
<label for="valor">Valor</label> <label for="valor">Valor</label>
@ -29,66 +32,53 @@
@push('page_scripts') @push('page_scripts')
<script type="text/javascript"> <script type="text/javascript">
function getMonthsList() { const editVenta = {
const formatter = new Intl.DateTimeFormat('es-CL', {month: 'long'}) getMonthsList() {
const months = [] const formatter = new Intl.DateTimeFormat('es-CL', {month: 'long'})
let m = '' const months = []
for (let i = 0; i < 12; i ++) { let m = ''
m = formatter.format((new Date()).setMonth(i)) for (let i = 0; i < 12; i ++) {
months.push(m.charAt(0).toUpperCase() + m.slice(1)) m = formatter.format((new Date()).setMonth(i))
} months.push(m.charAt(0).toUpperCase() + m.slice(1))
return months }
} return months
function redirect() { },
const uri = '{{$urls->base}}/venta/{{$venta->id}}' redirect() {
window.location = uri window.location = '{{$urls->base}}/venta/{{$venta->id}}'
} },
function editVenta() { edit() {
const original = { const uri = '{{$urls->api}}/venta/{{$venta->id}}'
valor: {{$venta->valor}}, const data = new FormData()
fecha: new Date('{{$venta->fecha->format('Y-m-d')}}T00:00:00') data.set('valor', $('#valor').val())
} data.set('fecha', $('#fecha_calendar').calendar('get date').toISOString())
const collator = new Intl.Collator('es-CL') return fetchAPI(uri, {method: 'post', body: data}).then(response => {
const data = {} if (response.ok) {
Object.keys(original).forEach(name => { return response.json()
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 }).then(json => {
} if (!json.edited) {
if (collator.compare(val, original[name]) !== 0) { return
data[name] = val }
} this.redirect()
}) })
if (Object.keys(data).length === 0) { },
redirect() setup() {
return $('#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(() => { $(document).ready(() => {
$('#fecha_calendar').calendar({ editVenta.setup()
type: 'date',
initialDate: '{{$venta->fecha->format('Y-m-d')}}',
text: {
months: getMonthsList()
}
})
$('#edit_form').submit(event => {
event.preventDefault()
editVenta()
return false
})
}) })
</script> </script>
@endpush @endpush

View File

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

View File

@ -189,6 +189,23 @@ class Ventas
} }
return $this->withJson($response, $output); 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, public function unidades(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface
{ {

View File

@ -75,10 +75,10 @@ class Venta extends Ideal\Model
if (!isset($this->valor_util)) { if (!isset($this->valor_util)) {
$sum = $this->valor; $sum = $this->valor;
$sum -= array_reduce($this->propiedad()->estacionamientos(), function(float $sum, Venta\Unidad $unidad) { $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); }, 0);
$sum -= array_reduce($this->propiedad()->bodegas(), function(float $sum, Venta\Unidad $unidad) { $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); }, 0);
$this->valor_util = $sum; $this->valor_util = $sum;
} }