Update frontend print logic
This commit is contained in:
@@ -702,7 +702,44 @@ async function handlePrescribeDrug(e) {
|
||||
const formattedExpiry = `${expiryParts[2]}/${expiryParts[1]}/${expiryParts[0]}`;
|
||||
|
||||
try {
|
||||
// First, dispense the drug (decrement inventory)
|
||||
// First, print the label
|
||||
const labelData = {
|
||||
variables: {
|
||||
practice_name: "Many Tears Animal Rescue",
|
||||
animal_name: animalName,
|
||||
drug_name: `${drugName} ${variantStrength}`,
|
||||
dosage: dosage,
|
||||
quantity: `${quantity} ${unit}`,
|
||||
expiry_date: formattedExpiry
|
||||
}
|
||||
};
|
||||
|
||||
const labelResponse = await apiCall('/labels/print', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(labelData)
|
||||
});
|
||||
|
||||
if (!labelResponse.ok) {
|
||||
const error = await labelResponse.json();
|
||||
throw new Error(error.detail || 'Label printing request failed');
|
||||
}
|
||||
|
||||
const labelResult = await labelResponse.json();
|
||||
console.log('Label print result:', labelResult);
|
||||
|
||||
if (!labelResult.success) {
|
||||
// Label printing failed - don't dispense the drug
|
||||
const isError = labelResult.message && (
|
||||
labelResult.message.includes('not found') ||
|
||||
labelResult.message.includes('error') ||
|
||||
labelResult.message.includes('failed')
|
||||
);
|
||||
const toastType = isError ? 'error' : 'warning';
|
||||
showToast('Cannot dispense: ' + labelResult.message, toastType, 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
// Label printed successfully, now dispense the drug
|
||||
const dispensingData = {
|
||||
drug_variant_id: variantId,
|
||||
quantity: quantity,
|
||||
@@ -721,42 +758,8 @@ async function handlePrescribeDrug(e) {
|
||||
throw new Error(error.detail || 'Failed to dispense drug');
|
||||
}
|
||||
|
||||
// Second, print the label
|
||||
const labelData = {
|
||||
variables: {
|
||||
practice_name: "Many Tears Animal Rescue",
|
||||
animal_name: animalName,
|
||||
drug_name: `${drugName} ${variantStrength}`,
|
||||
dosage: dosage,
|
||||
quantity: `${quantity} ${unit}`,
|
||||
expiry_date: formattedExpiry
|
||||
}
|
||||
};
|
||||
|
||||
const labelResponse = await apiCall('/labels/print', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(labelData)
|
||||
});
|
||||
|
||||
if (!labelResponse.ok) {
|
||||
console.error('Label printing failed, but drug was dispensed');
|
||||
showToast('Drug prescribed successfully, but label printing failed', 'warning', 5000);
|
||||
} else {
|
||||
const labelResult = await labelResponse.json();
|
||||
console.log('Label print result:', labelResult);
|
||||
if (labelResult.success) {
|
||||
showToast('Drug prescribed and label printed successfully!', 'success');
|
||||
} else {
|
||||
// Show as error toast if it contains specific error keywords
|
||||
const isError = labelResult.message && (
|
||||
labelResult.message.includes('not found') ||
|
||||
labelResult.message.includes('error') ||
|
||||
labelResult.message.includes('failed')
|
||||
);
|
||||
const toastType = isError ? 'error' : 'warning';
|
||||
showToast('Drug prescribed but ' + labelResult.message, toastType, 5000);
|
||||
}
|
||||
}
|
||||
// Both operations succeeded
|
||||
showToast('Drug prescribed and label printed successfully!', 'success');
|
||||
|
||||
document.getElementById('prescribeForm').reset();
|
||||
closeModal(document.getElementById('prescribeModal'));
|
||||
|
||||
Reference in New Issue
Block a user