Future<void> saveRapport(
int interventionID, String? interventionRef, Rapport rapport) async {
try {
print("=== Starting saveRapport in repository ===");
print("Images count in rapport: ${rapport.images.length}");
print("Intervention ref: $interventionRef");
// Convert postes data to match API format
final List<Map<String, dynamic>> formattedPostes =
rapport.postes.map((poste) {
return {
"label": "Poste ROTECH Rascal ${poste.numero?.toString()}" ?? "TEST",
"fk_consumption": int.tryParse(poste.consumption ?? "1") ?? 1,
"fk_type": poste.baitType,
"fk_trap_status": poste.status,
"fk_fixation": poste.fixation,
"fk_product": int.parse(poste.product ?? "0"),
"fk_intervention": interventionID,
"ref": "${interventionRef}_Num_${poste.numero}",
"comment": postement ?? "",
"image": poste.image != null
? {
"id_intervention": interventionID.toString(),
"ref_post": "${interventionRef}_Num_${poste.numero}",
"filename": "poste${poste.numero}.png",
"file_content": poste.image
}
: null
};
}).toList();
// Format markers for imageplan
final List<Map<String, dynamic>> formattedMarkers = rapport.postes
.where((poste) => poste.marker != null)
.map((poste) => {
"x": poste.marker!.position.dx,
"y": poste.marker!.position.dy,
"color": _colorToHex(poste.marker!.color)
})
.toList();
// Create a list for other images with unique timestamps
final List<Map<String, dynamic>> formattedImages =
rapport.images.map((image) {
return {
"ref_intervention": interventionRef,
"filename":
"${interventionRef}-autre${rapport.images.indexOf(image)}.png",
"file_content": image
};
}).toList();
final List<Map<String, dynamic>> formattedLevelPlans =
rapport.levelPlans.map((levelPlan) {
print(
'Formatting level plan: Level ${levelPlan.level}, Name: ${levelPlan.levelName}');
return {
"ref_intervention": interventionRef,
"filename":
"$interventionRef-plan_${levelPlan.level}_${levelPlan.levelName}.png",
"file_content": levelPlan.image,
"level": levelPlan.level, // Use the level directly from the levelPlan
"markers": levelPlan.markers
.map((marker) => {
"x": marker.position.dx,
"y": marker.position.dy,
"color": _colorToHex(marker.color)
})
.toList()
};
}).toList();
final List<Map<String, dynamic>> formattedOriginalPlans =
rapport.levelPlans
.map((levelPlan) => {
"ref_intervention": interventionRef,
"filename":
"planoriginal_${levelPlan.level}_${levelPlan.levelName}.png",
"file_content": levelPlan.originalImage,
"level": levelPlan.level,
})
.toList();
print("Formatting ${rapport.images.length} images for request");
// Create the request body
final Map<String, dynamic> requestBody = {
"clientRapportDesc": rapport.clientRapportDesc,
"internRapportDesc": rapport.internRapportDesc,
"time": rapport.time.toString(),
"postes": formattedPostes,
'implantation': rapport.implantationDate,
'controle': rapport.controleDate,
'presence_nuisible': rapport.nuisancesPresence,
'nombre_egouts': rapport.nbEgoutsTraites,
'nombre_egouts_consomm': rapport.nbEgoutsAvecConsommation,
'pourcentage_presence': rapport.pourcentagePresence,
"imageplan": formattedLevelPlans,
"imageplan_original": formattedOriginalPlans,
"imagesignature": rapport.clientSignature.isNotEmpty
? {
"ref_intervention": interventionRef,
"filename": "signature.png",
"file_content": rapport.clientSignature
}
: null,
"autresimages": formattedImages
};
print("Request body prepared with ${formattedImages.length} images");
var request = http.Request(
'POST',
_getUri('${EnvironmentConfig.path}/gestion/data'),
);
request.headers['Content-Type'] = 'application/json';
request.body = json.encode(requestBody);
final response = await _sendRequest(request);
print('Response status: ${response.statusCode}');
if (response.statusCode != 200) {
throw Exception('Failed to save rapport: ${response.statusCode}');
}
print('Rapport registered successfully');
} catch (e) {
print('Error saving rapport: $e');
throw Exception('Error saving rapport: $e');
}
}
When I send images to the backend from my real device, I have to refresh the ERP page multiple times for all of them to display. This issue doesn't occur when I send the images from my emulator. The saverapport()
method is the function I use to send the images.
I think it might be a network issues but I am not sure about it.