feat: permettre la correction des soins antiparasitaires

This commit is contained in:
Alexandre NOEL 2026-09-15 22:15:59 +02:00
commit f20d7cc965
14 changed files with 233 additions and 33 deletions

View file

@ -562,15 +562,30 @@ trait AnimalMedicalActions
$db = DB::pdo();
$id = (int) ($_POST['animal_id'] ?? 0);
$entryId = (int) ($_POST['entry_id'] ?? 0);
$vacId = (int) ($_POST['vaccine_id'] ?? 0);
if ($id <= 0 || $vacId <= 0) {
http_response_code(400);
echo h(t('error.bad_request'));
return;
}
if ($entryId > 0) {
$stmt = $db->prepare('SELECT 1 FROM vaccinations WHERE id=:entry AND animal_id=:animal');
$stmt->execute([':entry' => $entryId, ':animal' => $id]);
if (!$stmt->fetchColumn()) {
http_response_code(404);
echo h(t('error.not_found'));
return;
}
}
$done = trim((string) ($_POST['done_date'] ?? date('Y-m-d')));
$due = trim((string) ($_POST['due_date'] ?? ''));
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $done) || ($due !== '' && !preg_match('/^\d{4}-\d{2}-\d{2}$/', $due))) {
http_response_code(400);
echo h(t('error.bad_request'));
return;
}
// Si due_date est fourni, on respecte.
// Sinon auto selon protocole.
if ($due === '') {
@ -602,9 +617,9 @@ trait AnimalMedicalActions
SELECT COUNT(*)
FROM vaccinations v
JOIN ref_vaccines rv ON rv.id = v.vaccine_id
WHERE v.animal_id = :aid AND upper(coalesce(rv.family,'')) = 'L'
WHERE v.animal_id = :aid AND v.id <> :entry AND upper(coalesce(rv.family,'')) = 'L'
");
$stmt->execute([':aid' => $id]);
$stmt->execute([':aid' => $id, ':entry' => $entryId]);
$countL = (int) $stmt->fetchColumn();
$isPrimoL = $countL === 0;
}
@ -658,11 +673,7 @@ trait AnimalMedicalActions
}
$vet_name = $vetContact['name'] ?? $vet_name;
$stmt = $db->prepare('
INSERT INTO vaccinations(animal_id, vaccine_id, done_date, due_date, lot,manufacturer,batch_expires_on, notes, clinic_id, vet_name, clinic_contact_id, vet_contact_id, created_by)
VALUES(:aid,:vid,:done,:due,:lot,:manufacturer,:batch_expiry,:notes,:clinic_id,:vet_name,:clinic_contact_id,:vet_contact_id,:created_by)
');
$stmt->execute([
$values = [
':aid' => $id,
':vid' => $vacId,
':done' => $done,
@ -676,21 +687,38 @@ trait AnimalMedicalActions
':clinic_contact_id' => $clinicContact['id'] ?? null,
':vet_contact_id' => $vetContact['id'] ?? null,
':created_by' => Auth::id(),
]);
InventoryService::consumeBatch(
(int) ($_POST['inventory_batch_id'] ?? 0),
(float) str_replace(',', '.', (string) ($_POST['inventory_quantity'] ?? 0)),
$id,
'Vaccination du ' . $done,
);
if ((int) ($_POST['tariff_id'] ?? 0) > 0) {
PricingService::record($db, $id, (int) $_POST['tariff_id'], 1, $done, 'Ajoutée avec la vaccination');
];
if ($entryId > 0) {
unset($values[':aid'], $values[':created_by']);
$values[':entry'] = $entryId;
$stmt = $db->prepare('
UPDATE vaccinations SET vaccine_id=:vid,done_date=:done,due_date=:due,lot=:lot,
manufacturer=:manufacturer,batch_expires_on=:batch_expiry,notes=:notes,clinic_id=:clinic_id,
vet_name=:vet_name,clinic_contact_id=:clinic_contact_id,vet_contact_id=:vet_contact_id
WHERE id=:entry
');
$stmt->execute($values);
} else {
$stmt = $db->prepare('
INSERT INTO vaccinations(animal_id, vaccine_id, done_date, due_date, lot,manufacturer,batch_expires_on, notes, clinic_id, vet_name, clinic_contact_id, vet_contact_id, created_by)
VALUES(:aid,:vid,:done,:due,:lot,:manufacturer,:batch_expiry,:notes,:clinic_id,:vet_name,:clinic_contact_id,:vet_contact_id,:created_by)
');
$stmt->execute($values);
InventoryService::consumeBatch(
(int) ($_POST['inventory_batch_id'] ?? 0),
(float) str_replace(',', '.', (string) ($_POST['inventory_quantity'] ?? 0)),
$id,
'Vaccination du ' . $done,
);
if ((int) ($_POST['tariff_id'] ?? 0) > 0) {
PricingService::record($db, $id, (int) $_POST['tariff_id'], 1, $done, 'Ajoutée avec la vaccination');
}
}
self::logHistory(
$id,
'vaccine',
'Vaccin ajouté',
$entryId > 0 ? 'Vaccin corrigé' : 'Vaccin ajouté',
"Vaccin #$vacId\nFait le : $done" .
($due !== '' ? "\nRappel : $due" : '') .
($manufacturer !== '' ? "\nFabricant : $manufacturer" : '') .

View file

@ -182,6 +182,7 @@ trait AnimalRecordActions
'adoption_unavailability_reason' => '',
'website_published' => 0,
'website_description' => '',
'breed' => 'Chat Européen',
'color' => '',
'hair_type' => '',
'birth_date' => '',

View file

@ -17,6 +17,7 @@ final class DewormingController
}
$db = DB::pdo();
$animalId = (int) ($_POST['animal_id'] ?? 0);
$entryId = (int) ($_POST['entry_id'] ?? 0);
$date = trim((string) ($_POST['administered_on'] ?? ''));
$dewormer = (int) ($_POST['dewormer_id'] ?? 0);
if (!$animalId || !self::date($date)) {
@ -31,6 +32,15 @@ final class DewormingController
echo h(t('error.animal_not_found'));
return;
}
if ($entryId > 0) {
$s = $db->prepare('SELECT 1 FROM dewormings WHERE id=? AND animal_id=?');
$s->execute([$entryId, $animalId]);
if (!$s->fetchColumn()) {
http_response_code(404);
echo h(t('error.not_found'));
return;
}
}
if (isset($_POST['create_dewormer'])) {
$name = trim((string) ($_POST['new_dewormer_name'] ?? ''));
if ($name === '') {
@ -59,7 +69,7 @@ final class DewormingController
}
$ids = [$animalId];
$litterId = null;
if (isset($_POST['apply_litter'])) {
if ($entryId === 0 && isset($_POST['apply_litter'])) {
$s = $db->prepare(
'SELECT l.id FROM litters l LEFT JOIN litter_kittens lk ON lk.litter_id=l.id WHERE l.mother_id=? OR lk.animal_id=? ORDER BY l.id DESC LIMIT 1',
);
@ -85,8 +95,36 @@ final class DewormingController
'INSERT INTO dewormings(animal_id,dewormer_id,administered_on,dose_text,weight_kg,next_due_date,notes,source_litter_id,created_by) VALUES(?,?,?,?,?,?,?,?,?)',
);
$history = $db->prepare(
"INSERT INTO animal_history(animal_id,action,type,label,details,user_id) VALUES(?,'deworming','deworming','Vermifuge administré',?,?)",
"INSERT INTO animal_history(animal_id,action,type,label,details,user_id) VALUES(?,'deworming','deworming','Vermifuge / anti-puce administré',?,?)",
);
if ($entryId > 0) {
$s = $db->prepare('SELECT birth_date FROM animals WHERE id=?');
$s->execute([$animalId]);
$birthDate = $s->fetchColumn() ?: null;
$next = $manual ?: self::nextDue(is_string($birthDate) ? $birthDate : null, $date);
$db->prepare(
"UPDATE dewormings SET dewormer_id=?,administered_on=?,dose_text=?,weight_kg=?,next_due_date=?,notes=?,updated_at=datetime('now') WHERE id=? AND animal_id=?",
)->execute([
$dewormer ?: null,
$date,
trim((string) ($_POST['dose_text'] ?? '')) ?: null,
($_POST['weight_kg'] ?? '') !== '' ? (float) $_POST['weight_kg'] : null,
$next,
trim((string) ($_POST['notes'] ?? '')) ?: null,
$entryId,
$animalId,
]);
$db->prepare(
"INSERT INTO animal_history(animal_id,action,type,label,details,user_id) VALUES(?,'deworming_updated','deworming','Vermifuge / anti-puce corrigé',?,?)",
)->execute([
$animalId,
'Administration : ' . self::fr($date) . ($next ? ' · Prochaine échéance : ' . self::fr($next) : ''),
Auth::id(),
]);
$db->commit();
header('Location: /animal?id=' . $animalId . '#tab-med');
exit();
}
foreach ($ids as $id) {
$s = $db->prepare('SELECT name,birth_date FROM animals WHERE id=? AND deleted_at IS NULL');
$s->execute([$id]);
@ -117,7 +155,7 @@ final class DewormingController
(int) ($_POST['inventory_batch_id'] ?? 0),
$perAnimal * count($ids),
$animalId,
'Vermifuge du ' . $date . ($litterId ? ' · portée #' . $litterId : ''),
'Vermifuge / anti-puce du ' . $date . ($litterId ? ' · portée #' . $litterId : ''),
);
$db->commit();
header('Location: /animal?id=' . $animalId . '#tab-med');