From 6d2e1e9bfc0062f63e8e72bade5265a020be9e32 Mon Sep 17 00:00:00 2001 From: JantBogard Date: Thu, 28 Nov 2024 17:25:27 +0100 Subject: [PATCH] Add actualize inventory row function --- .../manager/MedicalInventoryManager.java | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java b/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java index 2cfbfcbbf..0fd29d959 100644 --- a/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java +++ b/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java @@ -369,7 +369,7 @@ public void validateMedicalInventoryRow(MedicalInventory inventory, List inventoryRowSearchList) throws OHServiceException { - LocalDateTime movFrom = inventory.getLastModifiedDate(); + LocalDateTime movFrom = inventory.getInventoryDate(); LocalDateTime movTo = TimeTools.getNow(); StringBuilder medDescriptionForLotUpdated = new StringBuilder("\n"); // initial new line StringBuilder medDescriptionForNewLot = new StringBuilder("\n"); // initial new line @@ -619,4 +619,60 @@ public MedicalInventory actualizeMedicalInventoryRow(MedicalInventory inventory) } return this.updateMedicalInventory(inventory, true); } + + /** + * Actualize the {@link MedicalInventory}'s ward. + * + * @param inventory - The {@link MedicalInventory} + * @return {@link MedicalInventory}. It could be {@code null}. + * @throws OHServiceException + */ + public MedicalInventory actualizeMedicalWardInventoryRow(MedicalInventory inventory) throws OHServiceException { + LocalDateTime movFrom = inventory.getInventoryDate(); + LocalDateTime movTo = TimeTools.getNow(); + + List movementWards = new ArrayList<>(movWardBrowserManager.getMovementWard(inventory.getWard(), movFrom, movTo)); + List movementToWards = new ArrayList<>(movBrowserManager.getMovements(inventory.getWard(), movFrom, movTo)); + List inventoryRowList = medicalInventoryRowManager.getMedicalInventoryRowByInventoryId(inventory.getId()); + + // Get all the lot of the ward movements + List lotOfMovements = new ArrayList<>(movementWards.stream().map(MovementWard::getLot).toList()); + // Get all the lot of the movements + lotOfMovements.addAll(movementToWards.stream().map(Movement::getLot).toList()); + // Remove duplicates by converting the list to a set + Set uniqueLots = new HashSet<>(lotOfMovements); + // Convert the set back to a list + List uniqueLotList = new ArrayList<>(uniqueLots); + // Cycle fetched movements to see if they impact inventoryRowSearchList + for (Lot lot : uniqueLotList) { + String lotCodeOfMovement = lot.getCode(); + Medical medical = lot.getMedical(); + Integer medicalCode = medical.getCode(); + // Fetch also empty lots because some movements may have discharged them completely + Optional optMedicalWard = movWardBrowserManager.getMedicalsWard(inventory.getWard(), medical.getCode(), false).stream() + .filter(m -> m.getLot().getCode().equals(lotCodeOfMovement)).findFirst(); + double wardStoreQty = optMedicalWard.isPresent() ? optMedicalWard.get().getQty() : 0.0; + + // Search for the specific Lot and Medical in inventoryRowSearchList (Lot should be enough) + Optional matchingRow = inventoryRowList.stream() + .filter(row -> row.getLot().getCode().equals(lotCodeOfMovement) && row.getMedical().getCode().equals(medicalCode)) + .findFirst(); + + if (matchingRow.isPresent()) { + MedicalInventoryRow medicalInventoryRow = matchingRow.get(); + double theoQty = medicalInventoryRow.getTheoreticQty(); + if (wardStoreQty != theoQty) { + // Update Lot + medicalInventoryRow.setTheoreticQty(wardStoreQty); + medicalInventoryRowManager.updateMedicalInventoryRow(medicalInventoryRow); + } + } else { + // TODO: to decide if to give control to the user about this + double realQty = wardStoreQty; + MedicalInventoryRow newMedicalInventoryRow = new MedicalInventoryRow(null, wardStoreQty, realQty, inventory, medical, lot); + medicalInventoryRowManager.newMedicalInventoryRow(newMedicalInventoryRow); + } + } + return this.updateMedicalInventory(inventory, true); + } }