Facturacion

This commit is contained in:
Juan Pablo Vial
2024-04-19 23:19:35 -04:00
parent 9388dc17fc
commit d2511901ec
7 changed files with 126 additions and 43 deletions

View File

@ -24,7 +24,7 @@
@endforeach
</div>
</div>
<table class="ui table" id="data"></table>
<div id="data"></div>
@endsection
@include('layout.head.styles.datatables')
@ -226,49 +226,104 @@
ids: {},
selected: 0,
data: JSON.parse('{!! json_encode($proyectos) !!}'),
sent: false,
ufs: {},
ipcs: {},
table: null,
get: function() {
return {
ventas: () => {
if (this.sent) {
return
}
this.sent = true
const url = '{{$urls->api}}/ventas/facturacion/proyecto/' + this.selected
this.draw().loading()
return fetchAPI(url).then(response => {
if (response.ok) {
return response.json()
if (!response) {
return
}
return response.json()
}).then(json => {
const idx = this.data.findIndex(proyecto => proyecto.id === json.proyecto_id)
const fecha_terreno = new Date(this.data[idx].terreno.date)
const fecha_terreno = (typeof this.data[idx].terreno.date === 'undefined') ? new Date() : new Date(this.data[idx].terreno.date)
this.data[idx]['ventas'] = []
const ventas = []
json.ventas.forEach(venta => {
const data = {
id: venta.id,
precio: venta.valor,
fecha: new Date(venta.fecha),
escritura: null
}
if (['escriturando'].includes(venta.current_estado.tipo_estado_venta.descripcion)) {
data.escritura = new Date(venta.current_estado.fecha)
}
const v = new Venta(data)
const promises = []
if (v.escritura !== null) {
promises.push(money.get().uf(v.escritura).then(uf => {
v.uf = uf
}))
promises.push(money.get().ipc(v.escritura, fecha_terreno).then(ipc => {
v.ipc = ipc
}))
}
promises.push(v.get().unidades())
const promise = Promise.all(promises).then(() => {
this.data[idx].ventas.push(v)
const unidadesQueue = []
const ufQueue = {}
const ipcQueue = {}
const chunkSize = 100
const url = '{{$urls->api}}/ventas/get'
for (let i = 0; i < json.ventas.length; i += chunkSize) {
const chunk = json.ventas.slice(i, i + chunkSize).map(venta => venta.id)
const body = new FormData()
body.set('ventas', chunk)
const promise = fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return response
}
return response.json().then(json => {
json.ventas.forEach(venta => {
const data = {
id: venta.id,
precio: venta.valor,
fecha: new Date(venta.fecha),
escritura: new Date(venta.fecha)
}
if (['escriturando'].includes(venta.current_estado.tipo_estado_venta.descripcion)) {
data.escritura = new Date(venta.current_estado.fecha)
}
const v = new Venta(data)
if (v.escritura !== null) {
const dateString = v.escritura.toString()
if (!Object.hasOwn(ufQueue, dateString)) {
ufQueue[dateString] = []
}
ufQueue[dateString].push(v.id)
if (!Object.hasOwn(ipcQueue, dateString)) {
ipcQueue[dateString] = []
}
ipcQueue[dateString].push(v.id)
}
unidadesQueue.push(v.id)
this.data[idx].ventas.push(v)
})
});
})
ventas.push(promise)
})
}
Promise.all(ventas).then(() => {
this.data[idx].ventas.sort((a, b) => parseInt(a.principal.descripcion) - parseInt(b.principal.descripcion))
this.draw().ventas(idx)
const promises = []
Object.entries(ufQueue).forEach(([dateString, ventas]) => {
const date = new Date(dateString)
promises.push(money.get().uf(date).then(uf => {
ventas.forEach(id => {
const vidx = this.data[idx].ventas.findIndex(venta => venta.id === id)
this.data[idx].ventas[vidx].uf = uf
})
}))
})
Object.entries(ipcQueue).forEach(([dateString, ventas]) => {
const date = new Date(dateString)
promises.push(money.get().ipc(date, fecha_terreno).then(ipc => {
ventas.forEach(id => {
const vidx = this.data[idx].ventas.findIndex(venta => venta.id === id)
this.data[idx].ventas[vidx].ipc = ipc
})
}))
})
for (let i = 0; i < unidadesQueue.length; i += chunkSize) {
const chunk = unidadesQueue.slice(i, i + chunkSize)
chunk.forEach(id => {
const vidx = this.data[idx].ventas.findIndex(venta => venta.id === id)
promises.push(this.data[idx].ventas[vidx].get().unidades())
})
}
Promise.all(promises).then(() => {
this.draw().ventas(idx)
this.sent = false
})
})
})
}
@ -277,23 +332,37 @@
draw: function() {
return {
proyectos: () => {
$(this.ids.title).html('Proyectos')
if (this.table !== null) {
this.table.clear()
this.table.destroy()
$(this.ids.data).html('')
}
$(this.ids.data).hide()
$(this.ids.proyectos).find('.item').css('cursor', 'pointer')
$(this.ids.proyectos).show()
},
ventas: proyecto_idx => {
const table = $(this.ids.data)
$(this.ids.title).html(this.data[proyecto_idx].descripcion)
const parent = $(this.ids.data)
if (this.table !== null) {
this.table.clear().destroy()
this.table.clear()
this.table.destroy()
this.table = null
parent.html('')
}
const table = $('<table></table>').addClass('ui table')
table.append(this.draw().thead())
table.append(this.draw().tbody(proyecto_idx))
table.show()
parent.show()
parent.html(table)
$(this.ids.proyectos).hide()
if (this.table === null) {
this.table = table.DataTable()
this.table = table.DataTable({
orders: [
[0, 'asc']
]
})
}
},
thead: () => {
@ -331,6 +400,9 @@
venta.draw({tbody, valor_terreno: this.data[proyecto_idx].terreno.valor})
})
return tbody
},
loading: () => {
$(this.ids.proyectos).find('.item').css('cursor', 'wait')
}
}
},
@ -369,6 +441,7 @@
}
$(document).ready(() => {
proyectos.setup({ids: {
title: '#list_title',
proyectos: '#proyectos',
data: '#data',
buttons: {