fix: corriger le suivi médical et les styles publics

This commit is contained in:
Alexandre NOEL 2026-09-22 18:05:38 +02:00
commit a55d27935f
15 changed files with 206 additions and 24 deletions

View file

@ -400,6 +400,7 @@ trait AnimalMedicalActions
}
$id = (int) ($_POST['animal_id'] ?? 0);
$entryId = (int) ($_POST['entry_id'] ?? 0);
$medId = (int) ($_POST['medication_id'] ?? 0);
$createMedication = !empty($_POST['create_medication']);
if ($id <= 0 || (!$createMedication && $medId <= 0)) {
@ -429,6 +430,15 @@ trait AnimalMedicalActions
}
$db = DB::pdo();
if ($entryId > 0) {
$existing = $db->prepare('SELECT 1 FROM treatments WHERE id=:entry AND animal_id=:animal');
$existing->execute([':entry' => $entryId, ':animal' => $id]);
if (!$existing->fetchColumn()) {
http_response_code(404);
echo h(t('error.not_found'));
return;
}
}
if ($createMedication) {
$medicationName = trim((string) ($_POST['new_medication_name'] ?? ''));
$medicationMolecule = trim((string) ($_POST['new_medication_molecule'] ?? ''));
@ -475,12 +485,7 @@ trait AnimalMedicalActions
return;
}
$vet_name = $vetContact['name'] ?? $vet_name;
$stmt = $db->prepare('
INSERT INTO treatments(animal_id, medication_id, route, dose_text, start_date, end_date, ongoing, notes, clinic_id, vet_name, clinic_contact_id, vet_contact_id, give_morning, give_evening, give_as_needed, created_by)
VALUES(:aid,:mid,:route,:dose,:start,:end,1,:notes,:clinic_id,:vet_name,:clinic_contact_id,:vet_contact_id,:give_morning,:give_evening,:give_as_needed,:created_by)
');
$stmt->execute([
':aid' => $id,
$values = [
':mid' => $medId,
':route' => $route !== '' ? $route : null,
':dose' => $dose,
@ -494,16 +499,32 @@ trait AnimalMedicalActions
':give_morning' => $giveMorning,
':give_evening' => $giveEvening,
':give_as_needed' => $giveAsNeeded,
':created_by' => Auth::id(),
]);
if ((int) ($_POST['tariff_id'] ?? 0) > 0) {
PricingService::record($db, $id, (int) $_POST['tariff_id'], 1, $start, 'Ajoutée avec le traitement');
];
if ($entryId > 0) {
$values[':entry'] = $entryId;
$db->prepare('UPDATE treatments SET medication_id=:mid,route=:route,dose_text=:dose,start_date=:start,
end_date=:end,notes=:notes,clinic_id=:clinic_id,vet_name=:vet_name,
clinic_contact_id=:clinic_contact_id,vet_contact_id=:vet_contact_id,
give_morning=:give_morning,give_evening=:give_evening,give_as_needed=:give_as_needed
WHERE id=:entry')->execute($values);
} else {
$values[':aid'] = $id;
$values[':created_by'] = Auth::id();
$db->prepare('INSERT INTO treatments(animal_id, medication_id, route, dose_text, start_date, end_date,
ongoing, notes, clinic_id, vet_name, clinic_contact_id, vet_contact_id,
give_morning, give_evening, give_as_needed, created_by)
VALUES(:aid,:mid,:route,:dose,:start,:end,1,:notes,:clinic_id,:vet_name,
:clinic_contact_id,:vet_contact_id,:give_morning,:give_evening,:give_as_needed,:created_by)')
->execute($values);
if ((int) ($_POST['tariff_id'] ?? 0) > 0) {
PricingService::record($db, $id, (int) $_POST['tariff_id'], 1, $start, 'Ajoutée avec le traitement');
}
}
self::logHistory(
$id,
'treatment',
'Traitement ajouté',
$entryId > 0 ? 'Traitement corrigé' : 'Traitement ajouté',
"Médicament #$medId\nDose : $dose\nDébut : $start" .
($end !== '' ? "\nFin : $end" : '') .
($notes !== '' ? "\nNotes : $notes" : ''),

View file

@ -4,6 +4,53 @@ declare(strict_types=1);
final class DewormingController
{
public static function renameProduct(): void
{
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
http_response_code(405);
return;
}
if (!PermissionService::can('medical', 'edit')) {
http_response_code(403);
return;
}
$tables = ['dewormer' => 'ref_dewormers', 'medication' => 'ref_medications', 'vaccine' => 'ref_vaccines'];
$category = (string) ($_POST['category'] ?? '');
$table = $tables[$category] ?? null;
$id = (int) ($_POST['product_id'] ?? 0);
$animalId = (int) ($_POST['animal_id'] ?? 0);
$name = trim((string) ($_POST['name'] ?? ''));
if ($table === null || $id <= 0 || $animalId <= 0 || $name === '' || mb_strlen($name) > 150) {
http_response_code(400);
echo h(t('error.bad_request'));
return;
}
$db = DB::pdo();
$stmt = $db->prepare("SELECT name FROM $table WHERE id=:id");
$stmt->execute([':id' => $id]);
$previous = $stmt->fetchColumn();
if ($previous === false) {
http_response_code(404);
echo h(t('error.not_found'));
return;
}
$duplicate = $db->prepare("SELECT 1 FROM $table WHERE name=:name COLLATE NOCASE AND id<>:id");
$duplicate->execute([':name' => $name, ':id' => $id]);
if ($duplicate->fetchColumn()) {
http_response_code(409);
echo h(t('catalog.name_exists'));
return;
}
$db->prepare("UPDATE $table SET name=:name WHERE id=:id")->execute([':name' => $name, ':id' => $id]);
AuditService::log('catalog_renamed', '/animal/product/rename', 'Intitulé de produit corrigé', 'animal', $animalId, [
'category' => $category,
'product_id' => $id,
'previous_name' => $previous,
'new_name' => $name,
]);
header('Location: /animal?id=' . $animalId . '#tab-med');
}
public static function save(): void
{
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {