Promotions by unit

This commit is contained in:
Juan Pablo Vial
2025-09-15 20:18:29 -03:00
parent b750f80eb7
commit fc65ee581a

View File

@ -238,6 +238,7 @@
} }
} }
class AddModalUnits { class AddModalUnits {
parent = null
ids = { ids = {
buttons_holder: '', buttons_holder: '',
units: '' units: ''
@ -252,7 +253,8 @@
units: null, units: null,
} }
constructor() { constructor(parent) {
this.parent = parent
this.ids = { this.ids = {
buttons_holder: 'add_unit_buttons', buttons_holder: 'add_unit_buttons',
units: 'add_units' units: 'add_units'
@ -265,6 +267,9 @@
} }
this.setup() this.setup()
} }
get promotions() {
return this.parent.data.promotions[this.parent.data.current_project]
}
draw = { draw = {
button: type => { button: type => {
return [ return [
@ -295,7 +300,24 @@
this.components.units.innerHTML = this.data.units.map(unit => { this.components.units.innerHTML = this.data.units.map(unit => {
return this.draw.unit(unit) return this.draw.unit(unit)
}).join('') }).join('')
this.components.units.querySelectorAll('.dropdown').forEach(dropdown => { this.components.units.querySelectorAll('.dropdown.add_units').forEach(dropdown => {
$(dropdown).dropdown({
onChange: (value, text, $selectedItem) => {
const unitPromotions = this.promotions.filter(promotion => promotion.units.length > 0)
const promotions = unitPromotions.filter(promotion => promotion.units.filter(unit => unit.id === parseInt(value)).length > 0)
console.debug($selectedItem.parent().parent().parent().parent().find('.add_promotions_unit'))
$selectedItem.parent().parent().parent().parent().find('.add_promotions_unit')
.dropdown('change values', promotions.map(promotion => {
return {
value: promotion.id,
name: promotion.description,
text: promotion.description,
}
}))
}
})
})
this.components.units.querySelectorAll('.dropdown.add_promotions_unit').forEach(dropdown => {
$(dropdown).dropdown() $(dropdown).dropdown()
}) })
this.components.units.querySelectorAll('.remove_unit').forEach(button => { this.components.units.querySelectorAll('.remove_unit').forEach(button => {
@ -308,8 +330,9 @@
unit: unit => { unit: unit => {
let promotions = '' let promotions = ''
if (unit.type === 'departamento') { if (unit.type === 'departamento') {
if (this.promotions.filter(promotion => promotion.units.length > 0).length > 0) {
promotions = [ promotions = [
'<div class="three wide field>', '<div class="three wide field">',
'<label>Promociones</label>', '<label>Promociones</label>',
'<div class="ui multiple search selection dropdown add_promotions_unit">', '<div class="ui multiple search selection dropdown add_promotions_unit">',
'<input type="hidden" name="add_units_promotions[]" />', '<input type="hidden" name="add_units_promotions[]" />',
@ -321,11 +344,12 @@
'</div>' '</div>'
].join('') ].join('')
} }
}
return [ return [
'<div class="fields">', '<div class="fields">',
'<div class="four wide field">', '<div class="four wide field">',
`<label>${unit.type.charAt(0).toUpperCase() + unit.type.slice(1)}</label>`, `<label>${unit.type.charAt(0).toUpperCase() + unit.type.slice(1)}</label>`,
`<div class="ui search selection dropdown">`, `<div class="ui search selection dropdown add_units">`,
'<input type="hidden" name="add_units[]" />', '<input type="hidden" name="add_units[]" />',
'<i class="dropdown icon"></i>', '<i class="dropdown icon"></i>',
`<div class="default text">${unit.type.charAt(0).toUpperCase() + unit.type.slice(1)}</div>`, `<div class="default text">${unit.type.charAt(0).toUpperCase() + unit.type.slice(1)}</div>`,
@ -455,11 +479,30 @@
this.get.brokers(project_id) this.get.brokers(project_id)
this.get.promotions(project_id).then(promotions => { this.get.promotions(project_id).then(promotions => {
this.components.promotions.data.promotions = promotions this.components.promotions.data.promotions = promotions.map(promotion => {
return {
text: promotion.name,
name: promotion.name,
value: promotion.id
}
})
this.components.promotions.draw.promotions() this.components.promotions.draw.promotions()
}) })
this.get.units(project_id).then(units => { this.get.units(project_id).then(unitTypes => {
this.components.units.data.types = units Object.entries(unitTypes).map(([type, units]) => {
units = units.map(unit => {
return {
text: unit.descripcion,
name: unit.descripcion,
value: unit.id
}
})
units.sort((a, b) => {
return parseInt(a.text) - parseInt(b.text)
})
unitTypes[type] = units
})
this.components.units.data.types = unitTypes
this.components.units.draw.buttons() this.components.units.draw.buttons()
}) })
} }
@ -583,13 +626,7 @@
if (json.promotions.length === 0) { if (json.promotions.length === 0) {
return this.data.promotions[project_id] = [] return this.data.promotions[project_id] = []
} }
return this.data.promotions[project_id] = json.promotions.map(promotion => { return this.data.promotions[project_id] = json.promotions
return {
text: promotion.name,
name: promotion.name,
value: promotion.id
}
})
}) })
}, },
units: project_id => { units: project_id => {
@ -612,18 +649,9 @@
if (!(type in this.data.units[project_id])) { if (!(type in this.data.units[project_id])) {
this.data.units[project_id][type] = [] this.data.units[project_id][type] = []
} }
this.data.units[project_id][type].push({ this.data.units[project_id][type].push(unit)
text: unit.descripcion,
name: unit.descripcion,
value: unit.id
})
})
Object.entries(this.data.units[project_id]).forEach(([type, units]) => {
units.sort((a, b) => {
return parseInt(a.text) - parseInt(b.text)
})
this.data.units[project_id][type] = units
}) })
return this.data.units[project_id] return this.data.units[project_id]
}) })
}, },
@ -725,7 +753,7 @@
this.components.promotions = new AddModalPromotions() this.components.promotions = new AddModalPromotions()
this.components.projects = document.getElementById(this.ids.projects) this.components.projects = document.getElementById(this.ids.projects)
this.components.project_name = document.getElementById(this.ids.project_name) this.components.project_name = document.getElementById(this.ids.project_name)
this.components.units = new AddModalUnits() this.components.units = new AddModalUnits(this)
this.components.$modal.modal({ this.components.$modal.modal({
onApprove: () => { onApprove: () => {