Files
oficial/app/resources/views/ventas/cierres/list.blade.php
2025-05-15 16:04:35 -04:00

294 lines
12 KiB
PHP

@extends('layout.base')
@section('page_title')
Cierres - Listado
@endsection
@section('page_content')
<div class="ui container">
<h2 class="ui header">Listado de Cierres</h2>
<h4 class="ui dividing header">Proyectos</h4>
<div class="ui compact fluid styled accordion" id="cierres_accordion"></div>
</div>
@endsection
@include('layout.head.styles.datatables')
@include('layout.body.scripts.datatables')
@push('page_styles')
<style>
.proyecto, .estado-cierres {
cursor: pointer;
}
</style>
@endpush
@push('page_scripts')
<script>
class Cierre {
id
proyecto_id
estado = {
id: 0,
descripcion: '',
titulo: ''
}
departamento
tipologia
vendible
fecha
valor
unitario() {
return this.valor / this.vendible
}
draw(formatter) {
const date = new Date(this.fecha)
const dateFormatter = new Intl.DateTimeFormat('es-CL')
const estado = this.estado.titulo.charAt(0).toUpperCase() + this.estado.titulo.slice(1)
return $('<tr></tr>').addClass('cierre ' + this.estado.descripcion)
.attr('data-proyecto', this.proyecto_id).attr('data-estado', this.estado.id).append(
$('<td></td>').attr('data-order', this.departamento).append(
$('<a></a>').attr('href', '{{$urls->base}}/ventas/cierre/' + this.id).html(this.departamento + ' (' + this.tipologia + ')')
)
).append(
$('<td></td>').attr('data-order', this.fecha).html(dateFormatter.format(date))
).append(
$('<td></td>').html(formatter.format(this.valor) + ' UF (' + formatter.format(this.unitario()) + ' UF/m²)')
).append(
$('<td></td>').html(estado)
)
}
}
class Estado {
id
proyecto_id
title
descripcion
cierres = []
total = 0
proporcion() {
return this.cierres.length / this.total * 100
}
add(data) {
let cierre = this.cierres.find(cierre => cierre.id === data.id)
if (typeof cierre !== 'undefined') {
return
}
cierre = new Cierre()
cierre.id = data.id
cierre.proyecto_id = this.proyecto_id
cierre.departamento = ''
cierre.tipologia = ''
cierre.vendible = 0
const departamento = data.unidades.filter(unidad => unidad.proyecto_tipo_unidad.tipo_unidad.descripcion === 'departamento')[0]
if (typeof departamento !== 'undefined') {
cierre.departamento = departamento.descripcion
cierre.tipologia = departamento.proyecto_tipo_unidad.abreviacion
cierre.vendible = departamento.proyecto_tipo_unidad.vendible
}
cierre.fecha = data.date_time
cierre.valor = data.precio
cierre.estado.id = this.id
cierre.estado.descripcion = this.descripcion
cierre.estado.titulo = data.estado_cierre.tipo_estado_cierre.descripcion
this.cierres.push(cierre)
}
draw(parent, formatter) {
const tbody = $('<tbody></tbody>')
this.cierres.forEach(cierre => {
tbody.append(cierre.draw(formatter))
})
const table = $('<table></table>').addClass('ui striped table').append(
$('<thead></thead>').append(
$('<tr></tr>').append(
$('<th></th>').html('Departamento')
).append(
$('<th></th>').html('Fecha')
).append(
$('<th></th>').html('Valor')
).append(
$('<th></th>').html('Estado')
)
)
).append(tbody)
parent.append(
$('<div></div>').addClass('title ' + this.descripcion).append(
$('<span></span>').addClass('ui text ' + this.descripcion.replace('positive', 'success')).html(this.title).append(
$('<i></i>').addClass('right caret icon')
).append(' [' + this.cierres.length + '] ' + formatter.format(this.proporcion()) + '%')
)
).append(
$('<div></div>').addClass('content').append(table)
)
new DataTable(table, {
order: [[1, 'desc'], [0, 'asc']]
})
}
}
class Proyecto {
id
descripcion
total = 0
estados
constructor({id, descripcion}) {
this.id = id
this.descripcion = descripcion
this.estados = {
aprobado: new Estado(),
promesado: new Estado(),
rechazado: new Estado()
}
this.estados.aprobado.id = 1
this.estados.aprobado.proyecto_id = this.id
this.estados.aprobado.title = 'Aprobado'
this.estados.aprobado.descripcion = 'warning'
this.estados.promesado.id = 2
this.estados.promesado.proyecto_id = this.id
this.estados.promesado.title = 'Promesado'
this.estados.promesado.descripcion = 'positive'
this.estados.rechazado.id = 3
this.estados.rechazado.proyecto_id = this.id
this.estados.rechazado.title = 'Abandonado/Rechazado'
this.estados.rechazado.descripcion = 'error'
}
add(data) {
if (data.proyecto.id !== this.id) {
return
}
this.total ++
if (['revisado', 'aprobado'].includes(data.estado_cierre.tipo_estado_cierre.descripcion)) {
return this.estados.aprobado.add(data)
}
if (['vendido', 'promesado'].includes(data.estado_cierre.tipo_estado_cierre.descripcion)) {
return this.estados.promesado.add(data)
}
this.estados.rechazado.add(data)
}
draw(parent, formatter) {
const accordion = $('<div></div>').addClass('compact accordion')
Object.keys(this.estados).forEach(key => {
this.estados[key].total = this.total
this.estados[key].draw(accordion, formatter)
})
parent.append(
$('<div></div>').addClass('title').html(this.descripcion).append(
$('<i></i>').addClass('dropdown icon')
).append(' [' + this.total + ']')
).append(
$('<div></div>').addClass('content').append(accordion)
)
}
}
const cierres = {
ids: {
accordion: '',
},
data: {
proyectos: []
},
get: function() {
return {
proyectos: () => {
this.data.proyectos = []
this.draw().loading()
return fetchAPI('{{$urls->api}}/proyectos').then(response => {
if (response.ok) {
return response.json()
}
}).then(data => {
const promises = []
if (data.total > 0) {
data.proyectos.forEach(proyecto => {
promises.push(this.get().cierres(proyecto.id))
})
return promises
}
}).then(promises => {
Promise.all(promises).then(() => {
this.data.proyectos.sort((a, b) => {
return a.descripcion.localeCompare(b.descripcion)
})
this.draw().proyectos()
})
})
},
cierres: proyecto_id => {
return fetchAPI('{{$urls->api}}/ventas/cierres',
{method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})}).then(response => {
if (response.ok) {
return response.json()
}
}).then(data => {
if (data.total > 0) {
data.cierres.forEach(cierre => {
this.add().cierre(cierre)
})
}
})
},
}
},
add: function() {
return {
cierre: data => {
let proyecto = this.data.proyectos.find(proyecto => proyecto.id === data.proyecto.id)
if (this.data.proyectos.length === 0 || typeof proyecto === 'undefined') {
proyecto = new Proyecto({
id: data.proyecto.id,
descripcion: data.proyecto.descripcion
})
this.data.proyectos.push(proyecto)
}
proyecto.add(data)
}
}
},
draw: function() {
return {
proyectos: () => {
const accordion = $(this.ids.accordion)
accordion.html('')
const formatter = new Intl.NumberFormat('es-CL', {minimumFractionDigits: 2, maximumFractionDigits: 2})
this.data.proyectos.forEach(proyecto => {
proyecto.draw(accordion, formatter)
})
},
loading: () => {
const accordion = $(this.ids.accordion)
accordion.append(
$('<div></div>').addClass('ui basic segment').append(
$('<div></div>').addClass('ui active dimmer').append(
$('<div></div>').addClass('ui loader')
)
).append(
$('<div></div>').addClass('ui fluid placeholder').append(
$('<div></div>').addClass('image')
)
)
)
}
}
},
setup: function() {
$(this.ids.accordion).accordion()
this.get().proyectos()
}
}
$(document).ready(() => {
cierres.ids.accordion = '#cierres_accordion'
cierres.setup()
})
</script>
@endpush