feat: permettre la correction des soins antiparasitaires
This commit is contained in:
parent
a932db0ace
commit
f20d7cc965
14 changed files with 233 additions and 33 deletions
|
|
@ -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" : '') .
|
||||
|
|
|
|||
Loading…
Reference in a new issue