Skip to content

Commit

Permalink
(feat) O3-3557 : ensure selectable batches have sufficient quantities…
Browse files Browse the repository at this point in the history
… for dispensing (#108)

* Improved Dispensing module- Selectable items must have quantities greater or equal to the dispensing quantity

* camelcase naming fix

* fixed count condition to be greater than 0 only

* enabled autopick batch quantity in cases where it is less than prescribed quantity

* Corrected and simplified date calculation

* Fixed updatequantity when stock is diabled in dispensing module
  • Loading branch information
FelixKiprotich350 authored Jul 8, 2024
1 parent b9a32f6 commit f1b8094
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/forms/dispense-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
// to prevent duplicate submits
const [isSubmitting, setIsSubmitting] = useState(false);

//track quantity to dispense
const [currentQuantity, setCurrentQuantity] = useState(
quantityRemaining ? quantityRemaining : medicationDispense?.quantity?.value,
);

// Submit medication dispense form
const handleSubmit = () => {
if (!isSubmitting) {
Expand Down Expand Up @@ -165,6 +170,10 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
}
};

const setAvailableQuantity = (batch) => {
setCurrentQuantity(Number(batch.quantity));
};

// initialize the internal dispense payload with the dispenses passed in as props
useEffect(() => setMedicationDispensePayload(medicationDispense), [medicationDispense]);

Expand Down Expand Up @@ -209,13 +218,14 @@ const DispenseForm: React.FC<DispenseFormProps> = ({
<MedicationDispenseReview
medicationDispense={medicationDispensePayload}
updateMedicationDispense={setMedicationDispensePayload}
quantityRemaining={quantityRemaining}
quantityRemaining={currentQuantity}
/>
{config.enableStockDispense && (
<StockDispense
inventoryItem={inventoryItem}
medicationDispense={medicationDispense}
updateInventoryItem={setInventoryItem}
updateAvailableQuantityToMedicationDispenseQuantity={setAvailableQuantity}
/>
)}
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/forms/medication-dispense-review.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ const MedicationDispenseReview: React.FC<MedicationDispenseReviewProps> = ({
}
min={0}
max={config.dispenseBehavior.restrictTotalQuantityDispensed ? quantityRemaining : undefined}
value={medicationDispense.quantity.value}
value={
quantityRemaining <= medicationDispense.quantity.value
? quantityRemaining
: medicationDispense.quantity.value
}
onChange={(e) => {
updateMedicationDispense({
...medicationDispense,
Expand Down
66 changes: 63 additions & 3 deletions src/forms/stock-dispense/stock-dispense.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,69 @@ type StockDispenseProps = {
medicationDispense: MedicationDispense;
updateInventoryItem: (inventoryItem: InventoryItem) => void;
inventoryItem: InventoryItem;
updateAvailableQuantityToMedicationDispenseQuantity: (inventoryItem: InventoryItem) => void;
};

const StockDispense: React.FC<StockDispenseProps> = ({ medicationDispense, updateInventoryItem }) => {
const StockDispense: React.FC<StockDispenseProps> = ({
medicationDispense,
updateInventoryItem,
updateAvailableQuantityToMedicationDispenseQuantity,
}) => {
const { t } = useTranslation();
const drugUuid = medicationDispense?.medicationReference?.reference?.split('/')[1];
const { inventoryItems, error, isLoading } = useDispenseStock(drugUuid);
const validInventoryItems = inventoryItems.filter((item) => isValidBatch(medicationDispense, item));

function parseDate(dateString) {
return new Date(dateString);
}

function isValidBatch(medicationToDispense, inventoryItem) {
if (medicationToDispense?.dosageInstruction && medicationToDispense?.dosageInstruction.length > 0) {
return medicationToDispense.dosageInstruction.some((instruction) => {
if (
instruction.timing?.repeat?.duration &&
instruction.timing?.repeat?.durationUnit &&
inventoryItem.quantity > 0
) {
const durationUnit = instruction.timing.repeat.durationUnit;
const durationValue = instruction.timing.repeat.duration;
const lastMedicationDate = new Date();

switch (durationUnit) {
case 's':
lastMedicationDate.setSeconds(lastMedicationDate.getSeconds() + durationValue);
break;
case 'min':
lastMedicationDate.setMinutes(lastMedicationDate.getMinutes() + durationValue);
break;
case 'h':
lastMedicationDate.setHours(lastMedicationDate.getHours() + durationValue);
break;
case 'd':
lastMedicationDate.setDate(lastMedicationDate.getDate() + durationValue);
break;
case 'wk':
lastMedicationDate.setDate(lastMedicationDate.getDate() + durationValue * 7);
break;
case 'mo':
lastMedicationDate.setMonth(lastMedicationDate.getMonth() + durationValue);
break;
case 'y':
lastMedicationDate.setFullYear(lastMedicationDate.getFullYear() + durationValue);
break;
default:
return false;
}

const expiryDate = parseDate(inventoryItem.expiration);
return expiryDate > lastMedicationDate;
}
return false;
});
}
return false;
}

const toStockDispense = (inventoryItems) => {
return t(
Expand Down Expand Up @@ -50,8 +107,11 @@ const StockDispense: React.FC<StockDispenseProps> = ({ medicationDispense, updat
<Layer>
<ComboBox
id="stockDispense"
items={inventoryItems}
onChange={({ selectedItem }) => updateInventoryItem(selectedItem)}
items={validInventoryItems}
onChange={({ selectedItem }) => {
updateInventoryItem(selectedItem);
updateAvailableQuantityToMedicationDispenseQuantity(selectedItem);
}}
itemToString={(item) => (item ? toStockDispense(item) : '')}
titleText={t('stockDispense', 'Stock Dispense')}
placeholder={t('selectStockDispense', 'Select stock to dispense from')}
Expand Down

0 comments on commit f1b8094

Please sign in to comment.