Promociones en misma tabla para tipo

This commit is contained in:
Juan Pablo Vial
2025-04-24 18:50:04 -04:00
parent 65088da2f5
commit 555cdb7138
4 changed files with 311 additions and 299 deletions

View File

@ -17,7 +17,7 @@
<th class="right aligned">Comisión</th>
<th class="right aligned">Precio Operador</th>
<th class="right aligned">UF/</th>
<th class="right aligned">Promociones</th>
<th class="center aligned" id="unit_promotions">Promociones</th>
</tr>
</thead>
<tbody></tbody>
@ -48,109 +48,158 @@
'UF/m²',
'promociones',
]
set = {
table: false,
titles: false
}
table
constructor(commission) {
super(commission)
const dto = structuredClone(datatables_defaults)
dto.pageLength = 100
dto.columnDefs = [
{
target: ['tipo_order', 'unidad_order', 'tipologia', 'piso', 'orientacion', 'metros'].map(column => this.columns.indexOf(column)),
visible: false
},
{
target: ['tipo'].map(column => this.columns.indexOf(column)),
orderData: ['tipo_order'].map(column => this.columns.indexOf(column)),
},
{
target: ['unidad'].map(column => this.columns.indexOf(column)),
orderData: ['unidad_order'].map(column => this.columns.indexOf(column)),
},
{
target: ['unidad', 'precio_base', 'commission', 'precio_operador', 'UF/m²', 'porcentaje', 'precio_final']
.map(column => this.columns.indexOf(column)),
className: 'dt-right right aligned'
}
]
dto.order = ['tipo_order', 'unidad_order'].map(column => [this.columns.indexOf(column), 'asc'])
dto.language.searchBuilder = searchBuilder
dto.layout = {
top1Start: {
searchBuilder: {
columns: this.columns.filter(column => !['tipo_order', 'unidad_order'].includes(column))
.map(column => this.columns.indexOf(column)),
}
setup() {
return {
titles: promotions_names => {
if (this.set.titles || promotions_names.size === 0) {
return
}
const nameArray = Array.from(promotions_names)
this.columns.pop()
this.columns.push(`${nameArray[0].toLowerCase().replaceAll(' ', '_')}.amount`)
this.columns.push(`${nameArray[0].toLowerCase().replaceAll(' ', '_')}.final`)
const table = document.getElementById(this.ids.units)
const thead = table.querySelector('thead')
const tr = thead.querySelector('tr')
const th = tr.querySelector('th#unit_promotions')
th.innerHTML = `${nameArray[0]}<br />Porcentaje`
tr.insertAdjacentHTML('beforeend', `<th class="right aligned">${nameArray[0]}<br />Precio Final</th>`)
if (promotions_names.size > 1) {
nameArray.slice(1).forEach(name => {
this.columns.push(`${name.toLowerCase().replaceAll(' ', '_')}.amount`)
this.columns.push(`${name.toLowerCase().replaceAll(' ', '_')}.final`)
tr.insertAdjacentHTML('beforeend', `<th class="right aligned">${name}<br />Porcentaje</th>`)
tr.insertAdjacentHTML('beforeend', `<th class="right aligned">${name}<br />Precio Final</th>`)
})
}
this.set.titles = true
},
top1End: {
buttons: [
table: () => {
if (typeof this.table !== 'undefined' || this.set.table) {
return
}
const dto = structuredClone(datatables_defaults)
dto.pageLength = 100
dto.columnDefs = [
{
extend: 'excelHtml5',
className: 'green',
text: 'Exportar a Excel <i class="file excel icon"></i>',
title: 'Lista de Precios - {{ $contract->broker->name }} - {{ $contract->project->descripcion }} - {{ (new DateTime())->format('Y-m-d') }}',
download: 'open',
exportOptions: {
columns: ['tipo', 'unidad', 'tipologia', 'piso', 'orientacion', 'metros_interior', 'metros_terraza', 'metros', 'metros_total', 'precio_operador', 'promociones']
.map(column => this.columns.indexOf(column)),
rows: (idx, data, node) => {
return data[this.columns.indexOf('estado')] === 'Libre'
},
format: {
body: (data, row, columnIdx, node) => {
if (typeof data === 'string' && data.includes('<span')) {
return data.replace(/<span.*>(.*)<\/span>/, '$1')
}
if (['metros'].map(column => this.columns.indexOf(column)).includes(columnIdx)) {
return data.replaceAll('.', '').replaceAll(',', '.')
}
return data
}
}
},
target: ['tipo_order', 'unidad_order', 'tipologia', 'piso', 'orientacion', 'metros'].map(column => this.columns.indexOf(column)),
visible: false
},
{
target: ['tipo'].map(column => this.columns.indexOf(column)),
orderData: ['tipo_order'].map(column => this.columns.indexOf(column)),
},
{
target: ['unidad'].map(column => this.columns.indexOf(column)),
orderData: ['unidad_order'].map(column => this.columns.indexOf(column)),
},
{
target: ['unidad', 'precio_base', 'commission', 'precio_operador', 'UF/m²', 'porcentaje', 'precio_final']
.map(column => this.columns.indexOf(column)),
className: 'dt-right right aligned'
}
]
dto.order = ['tipo_order', 'unidad_order'].map(column => [this.columns.indexOf(column), 'asc'])
dto.language.searchBuilder = searchBuilder
const exportColumns = ['tipo', 'unidad', 'tipologia', 'piso', 'orientacion', 'metros_interior', 'metros_terraza', 'metros', 'metros_total', 'precio_operador', 'promociones']
if (typeof this.columns['promotions'] === 'undefined') {
exportColumns.pop()
this.columns.slice(this.columns.indexOf('UF/m²')).forEach(name => {
exportColumns.push(name)
})
}
dto.layout = {
top1Start: {
searchBuilder: {
columns: this.columns.filter(column => !['tipo_order', 'unidad_order'].includes(column))
.map(column => this.columns.indexOf(column)),
}
},
top1End: {
buttons: [
{
extend: 'excelHtml5',
className: 'green',
text: 'Exportar a Excel <i class="file excel icon"></i>',
title: 'Lista de Precios - {{ $contract->broker->name }} - {{ $contract->project->descripcion }} - {{ (new DateTime())->format('Y-m-d') }}',
download: 'open',
exportOptions: {
columns: exportColumns
.map(column => this.columns.indexOf(column)),
rows: (idx, data, node) => {
return data[this.columns.indexOf('estado')] === 'Libre'
},
format: {
body: (data, row, columnIdx, node) => {
if (typeof data === 'string' && data.includes('<span')) {
return data.replace(/<span.*>(.*)<\/span>/, '$1')
}
const formatColumns = ['metros', 'metros_interior', 'metros_terraza', 'metros_total']
if (this.set.titles) {
this.columns.filter(column => column.includes('.amount') || column.includes('.final')).forEach(name => {
formatColumns.push(name)
})
}
if (formatColumns.map(column => this.columns.indexOf(column)).includes(columnIdx)) {
return data.replaceAll('.', '').replaceAll(',', '.')
}
return data
}
}
},
}
]
}
}
this.table = $(`#${this.ids.units}`).DataTable(dto)
this.set.table = true
}
}
$(`#${this.ids.units}`).DataTable(dto)
}
draw({units, formatters}) {
const table = $(`#${this.ids.units}`).DataTable()
table.clear()
const tableData = []
const prices = this.prices(units)
const promotions_names = new Set()
const tableData = []
units.forEach(unidad => {
const tipo = unidad.proyecto_tipo_unidad.tipo_unidad.descripcion
const price = prices.find(p => p.id === unidad.id)
let subTable = false
let promotions = []
price.promotions.forEach(p => {
if (Object.hasOwn(promotions, p.name)) {
return
}
promotions[p.name] = {
name: p.name,
type: p.type,
amount: p.type === 1 ? 'UF ' + formatters.ufs.format(p.amount) : formatters.percent.format(p.amount),
final: `UF ${formatters.ufs.format(p.final)}`
}
})
if (price.promotions.length > 0) {
subTable = document.createElement('table')
subTable.className = 'ui table'
const subTHead = document.createElement('thead')
price.promotions.forEach(p => {
subTHead.insertAdjacentHTML('beforeend', [
'<tr>',
'<th class="center aligned">Nombre</th>',
'<th class="center aligned">Forma</th>',
'<th class="right aligned">Precio Final</th>',
'</tr>'
].join("\n"))
})
const subTBody = document.createElement('tbody')
price.promotions.forEach(p => {
subTBody.insertAdjacentHTML('beforeend', [
'<tr>',
`<td class="center aligned">${p.name}</td>`,
`<td class="right aligned">${p.type === 1 ? 'UF ' + formatters.ufs.format(p.amount) : formatters.percent.format(p.amount)}</td>`,
`<td class="right aligned">UF ${formatters.ufs.format(p.final)}</td>`,
'</tr>'
].join("\n"))
})
subTable.appendChild(subTHead).appendChild(subTBody)
}
Object.keys(promotions).forEach(name => {
promotions_names.add(name)
})
const temp = Object.values(promotions)
tableData.push([
const data = [
unidad.sold ? `<span class="ui yellow text">Vendida</span>` : 'Libre',
tipo.charAt(0).toUpperCase() + tipo.slice(1),
unidad.proyecto_tipo_unidad.tipo_unidad.orden,
@ -167,9 +216,33 @@
formatters.percent.format(price.commission ?? 0),
'UF ' + formatters.ufs.format(price.broker ?? 0),
formatters.ufs.format(price.broker / unidad.proyecto_tipo_unidad.vendible),
subTable ? subTable.outerHTML : 'UF ' + formatters.ufs.format(price.broker ?? 0),
])
''
]
if (temp.length > 0) {
data.pop()
temp.forEach((p, i) => {
data.push(p.amount)
data.push(p.final)
})
} else {
if (promotions_names.size > 0) {
Array.from(promotions_names).forEach(name => {
data.push('')
data.push('')
})
}
}
tableData.push(data)
})
this.setup().titles(promotions_names)
this.setup().table()
const table = this.table
table.clear()
table.rows.add(tableData)
table.draw()
}