This repository was archived by the owner on Mar 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
354 lines (303 loc) · 13.1 KB
/
server.R
File metadata and controls
354 lines (303 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# === SERVEUR PRINCIPAL COMPLET ===
# Protection : charger la gestion des données si besoin
if (!exists("load_supervisors")) source("modules/data_management.R")
# Protection : charger la logique serveur modulaire si besoin
if (!exists("register_server_logic")) source("modules/server_modules.R")
# Charger la génération de fiches si nécessaire
if (!exists("generate_affectation_fiche")) source("modules/fiche_generation.R")
# Serveur principal avec toute la logique
server <- function(input, output, session) {
# === DONNÉES RÉACTIVES ===
supervisors <- reactiveVal(load_supervisors())
user_role <- reactiveVal(NULL)
current_user <- reactiveVal(NULL)
registered_tablets <- reactiveVal(load_registered_tablets())
assignments <- reactiveVal(load_assignments())
tablet_returns <- reactiveVal(load_tablet_returns())
tablet_incidents <- reactiveVal(load_tablet_incidents())
generated_fiches <- reactiveVal(load_generated_fiches())
selected_fiche_index <- reactiveVal(NULL)
# Synchroniser le rôle utilisateur global pour l'UI
observe({
if (exists("global_user_role", envir = .GlobalEnv)) {
global_user_role <<- user_role
}
})
# === GESTION DE LA CONNEXION ===
observeEvent(TRUE, {
showModal(login_ui())
}, once = TRUE)
observeEvent(input$login_btn, {
req(input$login_user, input$login_pass)
if (input$login_user == "admin" && input$login_pass == "admin") {
user_role("admin")
current_user("admin")
} else {
sup <- supervisors()
idx <- which(sup$user_login == input$login_user & sup$user_password == input$login_pass)
if (length(idx) == 1) {
user_role("supervisor")
current_user(input$login_user)
} else {
showNotification("Identifiants invalides", type = "error")
return()
}
}
removeModal()
if (user_role() == "supervisor") {
hideTab("navbar", "Administration")
} else {
showTab("navbar", "Administration")
}
# Ajouter le bouton de déconnexion
removeUI(selector = "#logout_button", immediate = TRUE)
insertUI(
selector = ".navbar-nav",
where = "beforeEnd",
ui = tags$li(
id = "logout_button",
class = "nav-item",
tags$a(
class = "nav-link",
href = "#",
icon("sign-out-alt"),
"Déconnexion",
onclick = "Shiny.setInputValue('logout_click', Math.random())"
)
)
)
updateNavbarPage(session, "navbar", selected = "Accueil")
})
# Gestion de la déconnexion
observeEvent(input$logout_click, {
# Sauvegarder toutes les données avant déconnexion
save_registered_tablets(registered_tablets())
save_assignments(assignments())
save_tablet_returns(tablet_returns())
save_tablet_incidents(tablet_incidents())
save_generated_fiches(generated_fiches())
save_supervisors(supervisors())
# Réinitialiser l'utilisateur et l'UI
user_role(NULL)
current_user(NULL)
removeUI(selector = "#logout_button", immediate = TRUE)
showModal(login_ui())
})
# === MESSAGE DE BIENVENUE ===
output$welcome_message <- renderUI({
req(current_user(), user_role())
if (user_role() == "admin") {
HTML(paste0("<h2 style='color: white;'>Bienvenue Administrateur</h2>",
"<p style='color: white; font-size: 1.2em;'>Vous êtes connecté en tant qu'administrateur. Vous voyez toutes les actions de tous les utilisateurs.</p>"))
} else if (user_role() == "supervisor") {
sup <- supervisors()
user_idx <- which(sup$user_login == current_user())
user_display_name <- if (length(user_idx) > 0) sup$user_name[user_idx] else current_user()
HTML(paste0("<h2 style='color: white;'>Bienvenue ", user_display_name, "</h2>",
"<p style='color: white; font-size: 1.2em;'>Vous êtes connecté en tant que superviseur. Vous ne voyez que vos propres actions.</p>"))
}
})
# === OBSERVATEURS POUR LES BOUTONS DE SCAN QR ===
observeEvent(input$scan_tablet_btn, {
showModal(modalDialog(
title = "Scanner QR Tablette",
tags$div(id = "qr-reader"),
easyClose = TRUE,
footer = modalButton("Fermer")
))
session$sendCustomMessage('start-scan', list(target = 'scanned_tablet_code'))
})
observeEvent(input$scan_charger_btn, {
showModal(modalDialog(
title = "Scanner QR Chargeur",
tags$div(id = "qr-reader"),
easyClose = TRUE,
footer = modalButton("Fermer")
))
session$sendCustomMessage('start-scan', list(target = 'scanned_charger_code'))
})
observeEvent(input$scanned_tablet_code, {
updateTextInput(session, 'reg_tab_num_qr', value = input$scanned_tablet_code)
})
observeEvent(input$scanned_charger_code, {
updateTextInput(session, 'reg_charger_num_qr', value = input$scanned_charger_code)
})
# === OBSERVATEURS POUR MISE À JOUR DES CHOIX ===
# Observateur pour mettre à jour les choix de tablettes affectées
observe({
current_assignments <- get_user_data(assignments(), user_role(), current_user())
if (nrow(current_assignments) > 0) {
choices <- paste(current_assignments$tablette, "-", current_assignments$agent_name)
updateSelectInput(session, "return_tablet_select", choices = choices)
updateSelectInput(session, "incident_tablet_select", choices = choices)
} else {
updateSelectInput(session, "return_tablet_select", choices = "Aucune tablette affectée")
updateSelectInput(session, "incident_tablet_select", choices = "Aucune tablette affectée")
}
})
# Observateur pour mettre à jour le powerbank selon la tablette sélectionnée
observeEvent(input$return_tablet_select, {
req(input$return_tablet_select)
current_assignments <- get_user_data(assignments(), user_role(), current_user())
if (nrow(current_assignments) > 0) {
tablet_num <- strsplit(input$return_tablet_select, " - ")[[1]][1]
tablet_idx <- which(current_assignments$tablette == tablet_num)
if (length(tablet_idx) > 0) {
assignment <- current_assignments[tablet_idx[1], ]
updateMaterialSwitch(session, "return_has_powerbank", value = assignment$powerbank)
if (assignment$powerbank) {
shinyjs::enable("return_has_powerbank")
} else {
shinyjs::disable("return_has_powerbank")
}
updateTextInput(session, "return_charger_num", value = assignment$chargeur)
}
}
})
observeEvent(input$incident_tablet_select, {
req(input$incident_tablet_select)
current_assignments <- get_user_data(assignments(), user_role(), current_user())
if (nrow(current_assignments) > 0) {
tablet_num <- strsplit(input$incident_tablet_select, " - ")[[1]][1]
tablet_idx <- which(current_assignments$tablette == tablet_num)
if (length(tablet_idx) > 0) {
assignment <- current_assignments[tablet_idx[1], ]
updateMaterialSwitch(session, "incident_powerbank_ok", value = assignment$powerbank)
if (assignment$powerbank) {
shinyjs::enable("incident_powerbank_ok")
} else {
shinyjs::disable("incident_powerbank_ok")
}
}
}
})
# Output pour afficher l'information sur le powerbank
output$powerbank_info <- renderText({
req(input$return_tablet_select)
current_assignments <- get_user_data(assignments(), user_role(), current_user())
if (nrow(current_assignments) > 0) {
tablet_num <- strsplit(input$return_tablet_select, " - ")[[1]][1]
tablet_idx <- which(current_assignments$tablette == tablet_num)
if (length(tablet_idx) > 0) {
assignment <- current_assignments[tablet_idx[1], ]
if (assignment$powerbank) {
"ℹ️ Cette tablette a été affectée avec un powerbank (vous pouvez le décocher si perdu)"
} else {
"ℹ️ Cette tablette a été affectée sans powerbank (case désactivée)"
}
} else {
""
}
} else {
""
}
})
# === OUTPUT POUR SAVOIR SI L'UTILISATEUR EST ADMIN ===
output$isAdmin <- reactive({
user_role() == "admin"
})
outputOptions(output, "isAdmin", suspendWhenHidden = FALSE)
# Masquer/afficher l'onglet Administration selon le rôle
observe({
if (!is.null(user_role()) && user_role() == "admin") {
showTab("navbar", "Administration")
} else {
hideTab("navbar", "Administration")
}
})
# === APPEL DES MODULES SERVEUR ===
# Module Enregistrement
register_server_logic(input, output, session, registered_tablets, user_role, current_user)
# Module Affectation
assignment_server_logic(input, output, session, assignments, registered_tablets, user_role, current_user)
# Module Génération de fiches
fiche_server_logic(input, output, session, assignments, generated_fiches, user_role, current_user, selected_fiche_index)
# Module Retour
return_server_logic(input, output, session, tablet_returns, assignments, registered_tablets, user_role, current_user)
# Module Incident
incident_server_logic(input, output, session, tablet_incidents, assignments, registered_tablets, user_role, current_user)
# Module Administration
administration_server_logic(input, output, session, supervisors, registered_tablets, assignments, tablet_returns, tablet_incidents, generated_fiches, user_role, current_user)
# === OBSERVATEURS POUR LA CONFIRMATION DU RETOUR AVEC CHARGEUR MANQUANT ===
# Fonction pour traiter le retour de tablette
process_tablet_return <- function(assignment, input_data) {
new_return <- data.frame(
tablette = assignment$tablette,
agent_id = assignment$agent_id,
agent_name = assignment$agent_name,
charger_retourne = input_data$return_charger_num,
powerbank_retourne = input_data$return_has_powerbank,
return_reason = input_data$return_reason,
return_condition = input_data$return_condition,
return_date = as.character(input_data$return_date),
return_notes = input_data$return_notes,
user_login = current_user(),
stringsAsFactors = FALSE
)
current_returns <- tablet_returns()
updated_returns <- rbind(current_returns, new_return)
tablet_returns(updated_returns)
save_tablet_returns(updated_returns)
current_tablets <- registered_tablets()
tablet_idx <- which(current_tablets$tablette == assignment$tablette)
if (length(tablet_idx) > 0) {
new_state <- switch(input_data$return_condition,
"Bon état" = "En stock",
"Légèrement endommagée" = "En réparation",
"Endommagée" = "En réparation",
"Hors service" = "Hors service",
"En stock"
)
current_tablets$etat[tablet_idx] <- new_state
registered_tablets(current_tablets)
save_registered_tablets(current_tablets)
}
all_assignments <- assignments()
all_assignment_idx <- which(all_assignments$tablette == assignment$tablette)
if (length(all_assignment_idx) > 0) {
updated_assignments <- all_assignments[-all_assignment_idx, ]
assignments(updated_assignments)
save_assignments(updated_assignments)
}
# Réinitialiser les champs
updateTextInput(session, "return_agent_id", value = "")
updateSelectInput(session, "return_tablet_select", selected = "")
updateTextInput(session, "return_charger_num", value = "")
updateMaterialSwitch(session, "return_has_powerbank", value = FALSE)
updateTextInput(session, "return_reason", value = "")
updateSelectInput(session, "return_condition", selected = "Bon état")
updateDateInput(session, "return_date", value = Sys.Date())
updateTextAreaInput(session, "return_notes", value = "")
showNotification("Retour de tablette enregistré avec succès!", type = "default")
}
# Observateur pour la confirmation du retour avec chargeur manquant
observeEvent(input$confirm_return, {
req(input$return_agent_id)
current_assignments <- get_user_data(assignments(), user_role(), current_user())
agent_idx <- which(current_assignments$agent_id == input$return_agent_id)
if (length(agent_idx) == 0) {
showNotification("Erreur: affectation non trouvée", type = "error")
removeModal()
return()
}
assignment <- current_assignments[agent_idx[1], ]
# Ajouter les informations sur le chargeur manquant
notes <- input$return_notes
if (assignment$chargeur != input$return_charger_num) {
charger_status <- ifelse(input$charger_lost == "Oui", "perdu/endommagé", "non retourné")
notes <- paste(notes, paste0("Chargeur ", charger_status, "."), sep = " ")
}
# Créer un objet input modifié
modified_input <- list(
return_charger_num = input$return_charger_num,
return_has_powerbank = input$return_has_powerbank,
return_reason = input$return_reason,
return_condition = input$return_condition,
return_date = input$return_date,
return_notes = notes
)
# Traiter le retour
process_tablet_return(assignment, modified_input)
removeModal()
})
}