100 lines
3.0 KiB
Go
100 lines
3.0 KiB
Go
package xlsx
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/core/utils/gender"
|
|
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type BeneficiaryGeoInfo struct {
|
|
Commune string
|
|
EPCI string
|
|
Departement string
|
|
Region string
|
|
}
|
|
|
|
func (r *XLSXRenderer) Beneficiaries(w http.ResponseWriter, accounts []mobilityaccountsstorage.Account, geoInfoMap map[string]BeneficiaryGeoInfo) {
|
|
spreadsheet := r.NewSpreadsheet("Bénéficiaires")
|
|
|
|
// Build headers dynamically based on configuration
|
|
beneficiaryOptionalFields := r.Config.Get("modules.beneficiaries.profile_optional_fields")
|
|
beneficiaryFields := []string{"last_name", "first_name", "email", "phone_number", "birthdate", "gender", "file_number"}
|
|
headers := []string{"ID", "Nom", "Prénom", "Email", "Téléphone", "Date de naissance", "Genre", "Numéro de dossier"}
|
|
|
|
if beneficiaryOptionalFieldsList, ok := beneficiaryOptionalFields.([]interface{}); ok {
|
|
for _, field := range beneficiaryOptionalFieldsList {
|
|
if fieldMap, ok := field.(map[string]interface{}); ok {
|
|
if name, ok := fieldMap["name"].(string); ok {
|
|
beneficiaryFields = append(beneficiaryFields, name)
|
|
label := name
|
|
if labelVal, ok := fieldMap["label"].(string); ok {
|
|
label = labelVal
|
|
}
|
|
headers = append(headers, label)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
headers = append(headers, "Adresse", "Commune", "EPCI", "Département", "Région", "Archivé")
|
|
|
|
spreadsheet.SetHeaders(headers)
|
|
|
|
for _, account := range accounts {
|
|
row := []interface{}{}
|
|
|
|
row = append(row, account.ID)
|
|
|
|
for _, field := range beneficiaryFields {
|
|
value := getAccountFieldValue(account.Data, field)
|
|
if field == "gender" && value != "" {
|
|
value = gender.ISO5218ToString(value)
|
|
}
|
|
row = append(row, value)
|
|
}
|
|
|
|
// Address
|
|
address := ""
|
|
if addr, ok := account.Data["address"]; ok {
|
|
if addrMap, ok := addr.(map[string]interface{}); ok {
|
|
if props, ok := addrMap["properties"]; ok {
|
|
if propsMap, ok := props.(map[string]interface{}); ok {
|
|
if label, ok := propsMap["label"].(string); ok {
|
|
address = label
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
row = append(row, address)
|
|
|
|
// Geographic info (Commune, EPCI, Département, Région)
|
|
geoInfo := geoInfoMap[account.ID]
|
|
row = append(row, geoInfo.Commune)
|
|
row = append(row, geoInfo.EPCI)
|
|
row = append(row, geoInfo.Departement)
|
|
row = append(row, geoInfo.Region)
|
|
|
|
// Archived status
|
|
archived := "Non"
|
|
if archivedVal, ok := account.Data["archived"].(bool); ok && archivedVal {
|
|
archived = "Oui"
|
|
}
|
|
row = append(row, archived)
|
|
|
|
spreadsheet.AddRow(row)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"export-beneficiaires.xlsx\""))
|
|
|
|
if err := spreadsheet.GetFile().Write(w); err != nil {
|
|
log.Error().Err(err).Msg("Error generating Excel file")
|
|
http.Error(w, "Error generating Excel file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|