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 {
parent = null
ids = {
buttons_holder: '',
units: ''
@ -252,7 +253,8 @@
units: null,
}
constructor() {
constructor(parent) {
this.parent = parent
this.ids = {
buttons_holder: 'add_unit_buttons',
units: 'add_units'
@ -265,6 +267,9 @@
}
this.setup()
}
get promotions() {
return this.parent.data.promotions[this.parent.data.current_project]
}
draw = {
button: type => {
return [
@ -295,7 +300,24 @@
this.components.units.innerHTML = this.data.units.map(unit => {
return this.draw.unit(unit)
}).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()
})
this.components.units.querySelectorAll('.remove_unit').forEach(button => {
@ -308,8 +330,9 @@
unit: unit => {
let promotions = ''
if (unit.type === 'departamento') {
if (this.promotions.filter(promotion => promotion.units.length > 0).length > 0) {
promotions = [
'<div class="three wide field>',
'<div class="three wide field">',
'<label>Promociones</label>',
'<div class="ui multiple search selection dropdown add_promotions_unit">',
'<input type="hidden" name="add_units_promotions[]" />',
@ -321,11 +344,12 @@
'</div>'
].join('')
}
}
return [
'<div class="fields">',
'<div class="four wide field">',
`<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[]" />',
'<i class="dropdown icon"></i>',
`<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.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.get.units(project_id).then(units => {
this.components.units.data.types = units
this.get.units(project_id).then(unitTypes => {
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()
})
}
@ -583,13 +626,7 @@
if (json.promotions.length === 0) {
return this.data.promotions[project_id] = []
}
return this.data.promotions[project_id] = json.promotions.map(promotion => {
return {
text: promotion.name,
name: promotion.name,
value: promotion.id
}
})
return this.data.promotions[project_id] = json.promotions
})
},
units: project_id => {
@ -612,18 +649,9 @@
if (!(type in this.data.units[project_id])) {
this.data.units[project_id][type] = []
}
this.data.units[project_id][type].push({
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
this.data.units[project_id][type].push(unit)
})
return this.data.units[project_id]
})
},
@ -725,7 +753,7 @@
this.components.promotions = new AddModalPromotions()
this.components.projects = document.getElementById(this.ids.projects)
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({
onApprove: () => {