function exportOdontogramData() {
const now = new Date()
const instance = $('#odontogram').data('odontogram')
// SIMPLIFIED JSON - ONLY THE EXACT FIELDS REQUESTED
const exportData = {
fecha: now.toISOString().split('T')[0],
nombre: 'PACIENTE - [A obtener de Airtable]',
piezas: []
}
// Process each tooth with treatments
for (const [key, treatments] of Object.entries(currentGeometry)) {
if (treatments && treatments.length > 0) {
// Get tooth number for FDI identification
let toothNum = null
if (instance && instance.teeth) {
for (const [teethKey, teethData] of Object.entries(instance.teeth)) {
if (teethKey === key) {
toothNum = teethData.num
break
}
}
}
if (toothNum) {
// Get tooth information from JSON data
const toothInfo = getToothInfo(toothNum)
// Initialize tooth data structure with ONLY the requested fields
const toothData = {
pieza: toothNum,
condiciones: [],
prestacion_requerida: [],
prestacion_preexistente: [],
notas: toothNotes[toothNum] || ''
}
// Group treatments by surface for proper display
function groupTreatmentsBySurface(treatmentList) {
const grouped = {}
treatmentList.forEach((treatment) => {
const treatmentName = getTreatmentName(treatment.name)
const withSides = [
'CARIES', 'CARIES_UNTREATABLE', 'REF', 'NVT',
'SIL', 'RES', 'AMF', 'COF', 'INC'
]
if (!grouped[treatment.name]) {
grouped[treatment.name] = {
nombre: treatmentName,
superficies: [],
usa_superficies: withSides.includes(treatment.name)
}
}
// Collect surface information for treatments that use surfaces
if (withSides.includes(treatment.name) && treatment.pos && toothInfo) {
// ... surface mapping logic ...
}
})
return grouped
}
// Categorize treatments
const condicionTreatments = treatments.filter((treatment) => {
const treatmentCode = treatment.name
const wholeTooth = ['PRE']
const withSides = ['CARIES_UNTREATABLE']
return wholeTooth.includes(treatmentCode) || withSides.includes(treatmentCode)
})
const prestacionTreatments = treatments.filter((treatment) => {
const treatmentCode = treatment.name
const wholeTooth = [
'CFR', 'FRM_ACR', 'BRIDGE', 'ORT', 'POC',
'FMC', 'IPX', 'RCT', 'MIS', 'UNE', 'PRE'
]
const withSides = [
'CARIES', 'REF', 'NVT', 'SIL', 'RES', 'AMF', 'COF', 'INC'
]
return wholeTooth.includes(treatmentCode) || withSides.includes(treatmentCode)
})
// Process Condiciones
if (condicionTreatments.length > 0) {
const groupedConditions = groupTreatmentsBySurface(condicionTreatments)
Object.values(groupedConditions).forEach((condition) => {
let conditionText = condition.nombre
if (condition.usa_superficies && condition.superficies.length > 0) {
conditionText += ` - Cara/s: ${condition.superficies.join(', ')}`
}
toothData.condiciones.push(conditionText)
})
}
// Process Prestaciones by layer
if (prestacionTreatments.length > 0) {
const preExistentes = prestacionTreatments.filter((t) => t.layer === 'pre')
const requeridas = prestacionTreatments.filter((t) => t.layer === 'req' || !t.layer)
// Prestaciones Preexistentes
if (preExistentes.length > 0) {
const groupedPreExistentes = groupTreatmentsBySurface(preExistentes)
Object.values(groupedPreExistentes).forEach((prestacion) => {
let prestacionText = prestacion.nombre
if (prestacion.usa_superficies && prestacion.superficies.length > 0) {
prestacionText += ` - Cara/s: ${prestacion.superficies.join(', ')}`
}
toothData.prestacion_preexistente.push(prestacionText)
})
}
// Prestaciones Requeridas
if (requeridas.length > 0) {
const groupedRequeridas = groupTreatmentsBySurface(requeridas)
Object.values(groupedRequeridas).forEach((prestacion) => {
let prestacionText = prestacion.nombre
if (prestacion.usa_superficies && prestacion.superficies.length > 0) {
prestacionText += ` - Cara/s: ${prestacion.superficies.join(', ')}`
}
toothData.prestacion_requerida.push(prestacionText)
})
}
}
// Only add tooth data if there are treatments or notes
if (
toothData.condiciones.length > 0 ||
toothData.prestacion_requerida.length > 0 ||
toothData.prestacion_preexistente.length > 0 ||
toothData.notas.trim() !== ''
) {
exportData.piezas.push(toothData)
}
}
}
}
// Sort teeth by FDI number
exportData.piezas.sort((a, b) => parseInt(a.pieza) - parseInt(b.pieza))
// Create and download JSON file
const blob = new Blob([JSON.stringify(exportData, null, 2)], {
type: 'application/json'
})
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `odontograma_${exportData.fecha}.json`
a.click()
URL.revokeObjectURL(url)
console.log('📋 Simple odontogram data exported:', exportData)
return exportData
}