diff --git a/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java b/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java index c31a6c70d..874fa4c96 100644 --- a/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java +++ b/src/main/java/org/isf/medicalinventory/manager/MedicalInventoryManager.java @@ -23,12 +23,14 @@ import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import org.isf.generaldata.GeneralData; import org.isf.generaldata.MessageBundle; +import org.isf.medicalinventory.model.InventoryStatus; import org.isf.medicalinventory.model.MedicalInventory; import org.isf.medicalinventory.model.MedicalInventoryRow; import org.isf.medicalinventory.service.MedicalInventoryIoOperation; @@ -37,11 +39,16 @@ import org.isf.medicalstock.manager.MovStockInsertingManager; import org.isf.medicalstock.model.Lot; import org.isf.medicalstock.model.Movement; +import org.isf.medstockmovtype.manager.MedicalDsrStockMovementTypeBrowserManager; +import org.isf.medstockmovtype.model.MovementType; +import org.isf.supplier.manager.SupplierBrowserManager; +import org.isf.supplier.model.Supplier; import org.isf.utils.exception.OHDataValidationException; import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; import org.isf.utils.time.TimeTools; +import org.isf.ward.manager.WardBrowserManager; import org.isf.ward.model.Ward; import org.springframework.data.domain.Page; import org.springframework.stereotype.Component; @@ -58,12 +65,20 @@ public class MedicalInventoryManager { private MovBrowserManager movBrowserManager; - public MedicalInventoryManager(MedicalInventoryIoOperation medicalInventoryIoOperation, MedicalInventoryRowManager medicalInventoryRowManager, - MovStockInsertingManager movStockInsertingManager, - MovBrowserManager movBrowserManager) { + private MedicalDsrStockMovementTypeBrowserManager medicalDsrStockMovementTypeBrowserManager; + + private SupplierBrowserManager supplierManager; + + private WardBrowserManager wardManager; + + public MedicalInventoryManager(MedicalInventoryIoOperation medicalInventoryIoOperation, MedicalInventoryRowManager medicalInventoryRowManager, MedicalDsrStockMovementTypeBrowserManager medicalDsrStockMovementTypeBrowserManager, + SupplierBrowserManager supplierManager, MovStockInsertingManager movStockInsertingManager, WardBrowserManager wardManager, MovBrowserManager movBrowserManager) { this.ioOperations = medicalInventoryIoOperation; this.medicalInventoryRowManager = medicalInventoryRowManager; + this.medicalDsrStockMovementTypeBrowserManager = medicalDsrStockMovementTypeBrowserManager; + this.supplierManager = supplierManager; this.movStockInsertingManager = movStockInsertingManager; + this.wardManager = wardManager; this.movBrowserManager = movBrowserManager; } @@ -91,21 +106,6 @@ public MedicalInventory updateMedicalInventory(MedicalInventory medicalInventory return ioOperations.updateMedicalInventory(medicalInventory); } - /** - * Delete the specified {@link MedicalInventory}. - * - * @param medicalInventory - the {@link MedicalInventory} to delete. - * @throws OHServiceException - */ - @Transactional(rollbackFor = OHServiceException.class) - public void deleteMedicalInventory(MedicalInventory medicalInventory) throws OHServiceException { - List inventoryRowList = medicalInventoryRowManager.getMedicalInventoryRowByInventoryId(medicalInventory.getId()); - for (MedicalInventoryRow invRow : inventoryRowList) { - medicalInventoryRowManager.deleteMedicalInventoryRow(invRow); - } - ioOperations.deleteMedicalInventory(medicalInventory); - } - /** * Check if the reference number is already used. * @@ -350,6 +350,9 @@ public void validateMedicalInventoryRow(MedicalInventory inventory, List confirmMedicalInventoryRow(MedicalInventory inventory, List inventoryRowSearchList) throws OHServiceException { + // validate the inventory + this.validateMedicalInventoryRow(inventory, inventoryRowSearchList); + // get general info + String referenceNumber = inventory.getInventoryReference(); + // TODO: make possibility to allow charges and discharges with same referenceNumber + String chargeReferenceNumber = referenceNumber + "-charge"; + String dischargeReferenceNumber = referenceNumber + "-discharge"; + MovementType chargeType = medicalDsrStockMovementTypeBrowserManager.getMovementType(inventory.getChargeType()); + MovementType dischargeType = medicalDsrStockMovementTypeBrowserManager.getMovementType(inventory.getDischargeType()); + Supplier supplier = supplierManager.getByID(inventory.getSupplier()); + Ward ward = wardManager.findWard(inventory.getDestination()); + LocalDateTime now = TimeTools.getNow(); + // prepare movements + List chargeMovements = new ArrayList<>(); + List dischargeMovements = new ArrayList<>(); + for (Iterator iterator = inventoryRowSearchList.iterator(); iterator.hasNext();) { + MedicalInventoryRow medicalInventoryRow = (MedicalInventoryRow) iterator.next(); + + double theoQty = medicalInventoryRow.getTheoreticQty(); + double realQty = medicalInventoryRow.getRealQty(); + Double ajustQty = realQty - theoQty; + Medical medical = medicalInventoryRow.getMedical(); + Lot currentLot = medicalInventoryRow.getLot(); + if (ajustQty > 0) { // charge movement when realQty > theoQty + Movement movement = new Movement(medical, chargeType, null, currentLot, now, ajustQty.intValue(), supplier, chargeReferenceNumber); + chargeMovements.add(movement); + } else if (ajustQty < 0) { // discharge movement when realQty < theoQty + Movement movement = new Movement(medical, dischargeType, ward, currentLot, now, -(ajustQty.intValue()), null, dischargeReferenceNumber); + dischargeMovements.add(movement); + } // else ajustQty = 0, continue + } + // create movements + List insertedMovements = new ArrayList<>(); + if (!chargeMovements.isEmpty()) { + insertedMovements.addAll(movStockInsertingManager.newMultipleChargingMovements(chargeMovements, chargeReferenceNumber)); + } + if (!dischargeMovements.isEmpty()) { + insertedMovements.addAll(movStockInsertingManager.newMultipleDischargingMovements(dischargeMovements, dischargeReferenceNumber)); + } + String status = InventoryStatus.done.toString(); + inventory.setStatus(status); + this.updateMedicalInventory(inventory); + return insertedMovements; + } } diff --git a/src/main/java/org/isf/medicalstock/service/MedicalStockIoOperations.java b/src/main/java/org/isf/medicalstock/service/MedicalStockIoOperations.java index 7fd528864..0b8e5fa0a 100644 --- a/src/main/java/org/isf/medicalstock/service/MedicalStockIoOperations.java +++ b/src/main/java/org/isf/medicalstock/service/MedicalStockIoOperations.java @@ -91,7 +91,7 @@ public enum MovementOrder { * * @return {@code true} if automatic lot mode, {@code false} otherwise. */ - private boolean isAutomaticLotMode() { + private boolean isAutomaticLotInMode() { return GeneralData.AUTOMATICLOT_IN; } @@ -180,26 +180,23 @@ public List newAutomaticDischargingMovement(Movement movement) throws * Stores the specified {@link Movement}. * * @param movement - the movement to store. + * @return the stored {@link Movement}. * @throws OHServiceException if an error occurs during the store operation. */ public Movement newMovement(Movement movement) throws OHServiceException { String lotCode = null; + Lot lot = movement.getLot(); - if (movement.getLot() != null) { - lotCode = movement.getLot().getCode(); + if (lot != null) { + lotCode = lot.getCode(); } - // we have to manage the Lot - if (movement.getType().getType().contains("+")) { - // if is in automatic lot mode then we have to generate a new lot code - if (isAutomaticLotMode() || "".equals(lotCode)) { - lotCode = generateLotCode(); - } - - boolean lotExists = lotExists(lotCode); - if (!lotExists) { - storeLot(lotCode, movement.getLot(), movement.getMedical()); - } + // if charging we have to manage the Lot, if discharging the lot should be given + boolean chargeMovement = movement.getType().getType().contains("+"); + boolean lotExists = lotExists(lotCode); + if (chargeMovement && !lotExists) { + lot = storeLot(lotCode, movement.getLot(), movement.getMedical()); + lotCode = lot.getCode(); } Movement movementStored = storeMovement(movement, lotCode); @@ -312,7 +309,7 @@ public Lot updateLot(Lot lot) throws OHServiceException { /** * Stores the specified {@link Lot}. * - * @param lotCode the {@link Lot} code. + * @param lotCode the {@link Lot} code. If {@code null} or {@code empty} it will be generated. * @param lot the lot to store. * @param medical * @return the stored {@link Lot} object. @@ -322,6 +319,9 @@ public Lot updateLot(Lot lot) throws OHServiceException { public Lot storeLot(String lotCode, Lot lot, Medical medical) throws OHServiceException { if (lotCode == null || lotCode.equals("")) { lotCode = this.generateLotCode(); + if (!isAutomaticLotInMode()) { + LOGGER.warn("AUTOMATICLOT_IN mode set to 'false' but lot code not provided. Generating... {}.", lotCode); + } } lot.setCode(lotCode); lot.setMedical(medical); @@ -590,7 +590,8 @@ public List getMovementForPrint( } /** - * Retrieves lot referred to the specified {@link Medical}, expiring first on top Lots with zero quantities will be stripped out if removeEmpty is set to true. + * Retrieves lot referred to the specified {@link Medical}, expiring first on top Lots with zero quantities will be stripped out if removeEmpty is set to + * true. * * @param medical the medical. * @param removeEmpty diff --git a/src/main/java/org/isf/supplier/model/Supplier.java b/src/main/java/org/isf/supplier/model/Supplier.java index 1d521de22..20cc63f80 100644 --- a/src/main/java/org/isf/supplier/model/Supplier.java +++ b/src/main/java/org/isf/supplier/model/Supplier.java @@ -101,6 +101,7 @@ public Supplier() { * @param supEmail * @param supNote */ + // TODO: to verify if it is really needed to have supID in the constructor public Supplier(Integer supID, String supName, String supAddress, String supTaxcode, String supPhone, String supFax, String supEmail, String supNote) { this.supId = supID; this.supName = supName; @@ -123,6 +124,7 @@ public Supplier(Integer supID, String supName, String supAddress, String supTaxc * @param supNote * @param supDeleted */ + // TODO: to remove, used only in tests public Supplier(Integer supID, String supName, String supAddress, String supTaxcode, String supPhone, String supFax, String supEmail, String supNote, Character supDeleted) { this.supId = supID; diff --git a/src/test/java/org/isf/medicalsinventory/TestMedicalInventory.java b/src/test/java/org/isf/medicalsinventory/TestMedicalInventory.java index 6911881c1..e3b0aaa81 100644 --- a/src/test/java/org/isf/medicalsinventory/TestMedicalInventory.java +++ b/src/test/java/org/isf/medicalsinventory/TestMedicalInventory.java @@ -27,20 +27,23 @@ import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; +import org.isf.medicalinventory.model.InventoryStatus; +import org.isf.medicalinventory.model.InventoryType; import org.isf.medicalinventory.model.MedicalInventory; import org.isf.utils.exception.OHException; +import org.isf.utils.time.TimeTools; import org.isf.ward.model.Ward; public class TestMedicalInventory { private int id = 1; - private String status = "STATUS"; - private LocalDateTime inventoryDate = LocalDateTime.now(); - private String user = "USER"; + private String status = InventoryStatus.draft.toString(); + private LocalDateTime inventoryDate = TimeTools.getNow(); + private String user = "admin"; private String inventoryReference = "REFERENCE"; - private String inventoryType = "TYPE"; + private String inventoryType = InventoryType.main.toString(); private String ward = "Z"; - private int supplier = 3; + private int supplier = 1; private String destination = "INV"; private String charge = "inventory+"; private String discharge = "inventory-"; diff --git a/src/test/java/org/isf/medicalsinventory/Tests.java b/src/test/java/org/isf/medicalsinventory/Tests.java index c767753fc..714ac20c3 100644 --- a/src/test/java/org/isf/medicalsinventory/Tests.java +++ b/src/test/java/org/isf/medicalsinventory/Tests.java @@ -25,8 +25,10 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.stream.Stream; import org.isf.OHCoreTestCase; +import org.isf.generaldata.GeneralData; import org.isf.medicalinventory.manager.MedicalInventoryManager; import org.isf.medicalinventory.manager.MedicalInventoryRowManager; import org.isf.medicalinventory.model.InventoryStatus; @@ -41,11 +43,21 @@ import org.isf.medicals.model.Medical; import org.isf.medicals.service.MedicalsIoOperationRepository; import org.isf.medicalstock.TestLot; +import org.isf.medicalstock.TestMedicalStock; +import org.isf.medicalstock.TestMovement; import org.isf.medicalstock.model.Lot; +import org.isf.medicalstock.model.MedicalStock; +import org.isf.medicalstock.model.Movement; import org.isf.medicalstock.service.LotIoOperationRepository; +import org.isf.medicalstock.service.MedicalStockIoOperationRepository; +import org.isf.medicalstock.service.MedicalStockIoOperations; +import org.isf.medstockmovtype.model.MovementType; +import org.isf.medstockmovtype.service.MedicalDsrStockMovementTypeIoOperationRepository; import org.isf.medtype.TestMedicalType; import org.isf.medtype.model.MedicalType; import org.isf.medtype.service.MedicalTypeIoOperationRepository; +import org.isf.supplier.model.Supplier; +import org.isf.supplier.service.SupplierIoOperationRepository; import org.isf.utils.exception.OHException; import org.isf.utils.exception.OHServiceException; import org.isf.utils.time.TimeTools; @@ -55,17 +67,22 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; class Tests extends OHCoreTestCase { - + private static TestMedicalInventory testMedicalInventory; private static TestMedicalInventoryRow testMedicalInventoryRow; private static TestWard testWard; private static TestMedical testMedical; private static TestLot testLot; private static TestMedicalType testMedicalType; + private static TestMovement testMovement; + private static TestMedicalStock testMedicalStock; @Autowired MedicalInventoryManager medicalInventoryManager; @@ -75,28 +92,56 @@ class Tests extends OHCoreTestCase { @Autowired MedicalInventoryIoOperationRepository medIvnIoOperationRepository; - + @Autowired WardIoOperationRepository wardIoOperationRepository; - + @Autowired MedicalInventoryIoOperation medicalInventoryIoOperation; - + @Autowired MedicalInventoryRowIoOperationRepository medIvnRowIoOperationRepository; - + @Autowired MedicalInventoryRowIoOperation medIvnRowIoOperation; - + @Autowired MedicalsIoOperationRepository medicalsIoOperationRepository; - + @Autowired MedicalTypeIoOperationRepository medicalTypeIoOperationRepository; - + @Autowired LotIoOperationRepository lotIoOperationRepository; + @Autowired + MedicalInventoryRowIoOperationRepository medicalInventoryRowIoOperationRepository; + + @Autowired + MedicalDsrStockMovementTypeIoOperationRepository medicalDsrStockMovementTypeIoOperationRepository; + + @Autowired + SupplierIoOperationRepository supplierIoOperationRepository; + + @Autowired + MedicalStockIoOperationRepository medicalStockIoOperationRepository; + + @Autowired + MedicalStockIoOperations medicalStockIoOperation; + + static Stream automaticlot() { + return Stream.of(Arguments.of(true, true, false), + Arguments.of(true, true, true), + Arguments.of(false, true, false), + Arguments.of(false, true, true)); + } + + private static void setGeneralData(boolean in, boolean out, boolean toward) { + GeneralData.AUTOMATICLOT_IN = in; + GeneralData.AUTOMATICLOT_OUT = out; + GeneralData.AUTOMATICLOTWARD_TOWARD = toward; + } + @BeforeAll static void setUpClass() { testMedicalInventory = new TestMedicalInventory(); @@ -105,13 +150,15 @@ static void setUpClass() { testMedical = new TestMedical(); testLot = new TestLot(); testMedicalType = new TestMedicalType(); + testMovement = new TestMovement(); + testMedicalStock = new TestMedicalStock(); } - + @BeforeEach void setUp() { cleanH2InMemoryDb(); } - + @Test void testMedicalInventoryGets() throws Exception { int code = setupTestMedicalInventory(false); @@ -162,7 +209,7 @@ void testMedicalInventoryRowGetsSets() throws Exception { double realQty = -37.3; medicalInventoryRow.setRealqty(realQty); assertThat(medicalInventoryRow.getRealQty()).isEqualTo(realQty); - medicalInventoryRow.setRealQty(realQty); // Note the uppercase 'Q' + medicalInventoryRow.setRealQty(realQty); // Note the uppercase 'Q' assertThat(medicalInventoryRow.getRealQty()).isEqualTo(realQty); int lock = -99; @@ -206,7 +253,7 @@ void testIoNewMedicalInventory() throws Exception { checkMedicalInventoryIntoDb(newMedicalInventory.getId()); assertThat(medicalInventoryIoOperation.referenceExists(newMedicalInventory.getInventoryReference())).isTrue(); } - + @Test void testMgrUpdateMedicalInventory() throws Exception { Integer id = setupTestMedicalInventory(false); @@ -230,25 +277,42 @@ void testIoUpdateMedicalInventory() throws Exception { } @Test - void testMgrDeleteMedicalInventory() throws Exception { + void testDeleteMedicalInventoryWithInventoryRowsWithoutNewLot() throws Exception { Integer id = setupTestMedicalInventory(false); MedicalInventory foundMedicalInventory = medIvnIoOperationRepository.findById(id).orElse(null); assertThat(foundMedicalInventory).isNotNull(); - String reference = foundMedicalInventory.getInventoryReference(); - medicalInventoryManager.deleteMedicalInventory(foundMedicalInventory); - assertThat(medicalInventoryManager.referenceExists(reference)).isFalse(); + MedicalType medicalType = testMedicalType.setup(false); + Medical medical = testMedical.setup(medicalType, false); + Lot lot = testLot.setup(medical, false); + medicalType = medicalTypeIoOperationRepository.save(medicalType); + medical = medicalsIoOperationRepository.save(medical); + lot = lotIoOperationRepository.save(lot); + MedicalInventoryRow medicalInventoryRowOne = testMedicalInventoryRow.setup(foundMedicalInventory, medical, lot, false); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowOne); + medicalInventoryManager.deleteInventory(foundMedicalInventory); + foundMedicalInventory = medIvnIoOperationRepository.findById(id).orElse(null); + assertThat(foundMedicalInventory).isNotNull(); + assertThat(foundMedicalInventory.getStatus()).isEqualTo(InventoryStatus.canceled.toString()); } - + @Test - void testIoDeleteMedicalInventory() throws Exception { + void testDeleteMedicalInventoryWithInventoryRowsWithNewLot() throws Exception { Integer id = setupTestMedicalInventory(false); MedicalInventory foundMedicalInventory = medIvnIoOperationRepository.findById(id).orElse(null); assertThat(foundMedicalInventory).isNotNull(); - String reference = foundMedicalInventory.getInventoryReference(); - Integer code = foundMedicalInventory.getId(); - medicalInventoryIoOperation.deleteMedicalInventory(foundMedicalInventory); - assertThat(medicalInventoryIoOperation.referenceExists(reference)).isFalse(); - assertThat(medicalInventoryIoOperation.isCodePresent(code)).isFalse(); + MedicalType medicalType = testMedicalType.setup(false); + Medical medical = testMedical.setup(medicalType, false); + Lot lot = testLot.setup(medical, false); + medicalType = medicalTypeIoOperationRepository.save(medicalType); + medical = medicalsIoOperationRepository.save(medical); + lot = lotIoOperationRepository.save(lot); + MedicalInventoryRow medicalInventoryRowOne = testMedicalInventoryRow.setup(foundMedicalInventory, medical, lot, false); + medicalInventoryRowOne.setNewLot(true); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowOne); + medicalInventoryManager.deleteInventory(foundMedicalInventory); + foundMedicalInventory = medIvnIoOperationRepository.findById(id).orElse(null); + assertThat(foundMedicalInventory).isNotNull(); + assertThat(foundMedicalInventory.getStatus()).isEqualTo(InventoryStatus.canceled.toString()); } @Test @@ -264,7 +328,8 @@ void testMgrGetMedicalInventoryWithStatus() throws Exception { inventory.setInventoryType(inventoryType); MedicalInventory secondMedicalInventory = medIvnIoOperationRepository.saveAndFlush(inventory); assertThat(secondMedicalInventory).isNotNull(); - List medicalInventories = medicalInventoryManager.getMedicalInventoryByStatusAndInventoryType(firstMedicalInventory.getStatus(), firstMedicalInventory.getInventoryType()); + List medicalInventories = medicalInventoryManager.getMedicalInventoryByStatusAndInventoryType(firstMedicalInventory.getStatus(), + firstMedicalInventory.getInventoryType()); assertThat(medicalInventories).hasSize(1); assertThat(medicalInventories.get(0).getStatus()).isEqualTo(firstMedicalInventory.getStatus()); } @@ -282,7 +347,8 @@ void testIoGetMedicalInventoryWithStatus() throws Exception { inventory.setInventoryType(inventoryType); MedicalInventory secondMedicalInventory = medIvnIoOperationRepository.saveAndFlush(inventory); assertThat(secondMedicalInventory).isNotNull(); - List medicalInventories = medicalInventoryIoOperation.getMedicalInventoryByStatusAndInventoryType(firstMedicalInventory.getStatus(), firstMedicalInventory.getInventoryType()); + List medicalInventories = medicalInventoryIoOperation.getMedicalInventoryByStatusAndInventoryType(firstMedicalInventory.getStatus(), + firstMedicalInventory.getInventoryType()); assertThat(medicalInventories).hasSize(1); assertThat(medicalInventories.get(0).getStatus()).isEqualTo(firstMedicalInventory.getStatus()); } @@ -325,7 +391,7 @@ void testIoGetMedicalInventoryWithStatusAndWard() throws Exception { MedicalInventory secondMedicalInventory = medIvnIoOperationRepository.saveAndFlush(inventory); assertThat(secondMedicalInventory).isNotNull(); List medicalinventories = medicalInventoryIoOperation - .getMedicalInventoryByStatusAndWard(firstMedicalInventory.getStatus(), firstMedicalInventory.getWard()); + .getMedicalInventoryByStatusAndWard(firstMedicalInventory.getStatus(), firstMedicalInventory.getWard()); assertThat(medicalinventories).hasSize(1); assertThat(medicalinventories.get(0).getStatus()).isEqualTo(firstMedicalInventory.getStatus()); assertThat(medicalinventories.get(0).getWard()).isEqualTo(firstMedicalInventory.getWard()); @@ -501,7 +567,7 @@ void testMgrUpdateMedicalInventoryRow() throws Exception { MedicalInventoryRow updatedMedicalInventoryRow = medicalInventoryRowManager.updateMedicalInventoryRow(foundMedicalInventoryRow); assertThat(updatedMedicalInventoryRow.getRealQty()).isEqualTo(realQty); } - + @Test void testMgrGetMedicalInventoryRowByInventoryId() throws Exception { Ward ward = testWard.setup(false); @@ -521,7 +587,6 @@ void testMgrGetMedicalInventoryRowByInventoryId() throws Exception { List medicalInventoryRows = medicalInventoryRowManager.getMedicalInventoryRowByInventoryId(inventoryId); assertThat(medicalInventoryRows).isNotEmpty(); assertThat(medicalInventoryRows).hasSize(1); - } private int setupTestMedicalInventory(boolean usingSet) throws OHException, OHServiceException { @@ -531,7 +596,7 @@ private int setupTestMedicalInventory(boolean usingSet) throws OHException, OHSe MedicalInventory savedMedicalInventory = medicalInventoryIoOperation.newMedicalInventory(medicalInventory); return savedMedicalInventory.getId(); } - + private void checkMedicalInventoryIntoDb(int id) throws OHException { MedicalInventory foundMedicalInventory = medIvnIoOperationRepository.findById(id).orElse(null); assertThat(foundMedicalInventory).isNotNull(); @@ -572,4 +637,180 @@ void testDeleteInventory() throws Exception { assertThat(deletedInventory).isNotNull(); assertThat(deletedInventory.getStatus()).isEqualTo(InventoryStatus.canceled.toString()); } + + @Test + void testValidateMedicalInventoryRow() throws Exception { + Ward ward = testWard.setup(false); + wardIoOperationRepository.saveAndFlush(ward); + MedicalInventory inventory = testMedicalInventory.setup(ward, false); + MedicalInventory savedInventory = medicalInventoryIoOperation.newMedicalInventory(inventory); + MedicalType medicalType = testMedicalType.setup(false); + medicalTypeIoOperationRepository.saveAndFlush(medicalType); + Medical medical = testMedical.setup(medicalType, false); + medicalsIoOperationRepository.saveAndFlush(medical); + Lot lot = testLot.setup(medical, false); + lotIoOperationRepository.saveAndFlush(lot); + MedicalInventoryRow medicalInventoryRow = testMedicalInventoryRow.setup(savedInventory, medical, lot, false); + int inventoryRowId = medicalInventoryRow.getId(); + MedicalInventoryRow newMedicalInventoryRow = medicalInventoryRowManager.newMedicalInventoryRow(medicalInventoryRow); + assertThat(newMedicalInventoryRow).isNotNull(); + List medicalInventoryRows = medicalInventoryRowManager.getMedicalInventoryRowByInventoryId(inventoryRowId); + assertThat(medicalInventoryRows).isNotEmpty(); + assertThat(medicalInventoryRows).hasSize(1); + medicalInventoryManager.validateMedicalInventoryRow(savedInventory, medicalInventoryRows); + int inventoryId = inventory.getId(); + String status = InventoryStatus.validated.toString(); + inventory = medicalInventoryIoOperation.getInventoryById(inventoryId); + assertThat(inventory).isNotNull(); + assertThat(inventory.getStatus()).isEqualTo(status); + } + + @ParameterizedTest(name = "Test with AUTOMATICLOT_IN={0}, AUTOMATICLOT_OUT={1}, AUTOMATICLOTWARD_TOWARD={2}") + @MethodSource("automaticlot") + void testConfirmMedicalInventory(boolean in, boolean out, boolean toward) throws Exception { + setGeneralData(in, out, toward); + Ward ward = testWard.setup(false); + wardIoOperationRepository.saveAndFlush(ward); + MovementType chargeType = new MovementType("inventory+", "Inventory+", "+", "non-operational"); + MovementType dischargeType = new MovementType("inventory-", "Inventory-", "-", "non-operational"); + Supplier supplier = new Supplier(1, "INVENTORY", null, null, null, null, null, null); + Ward destination = new Ward("INV", "ward inventory", null, null, null, 8, 1, 1, false, false); + dischargeType = medicalDsrStockMovementTypeIoOperationRepository.save(dischargeType); + chargeType = medicalDsrStockMovementTypeIoOperationRepository.save(chargeType); + supplier = supplierIoOperationRepository.save(supplier); + destination = wardIoOperationRepository.save(destination); + MedicalInventory inventory = testMedicalInventory.setup(ward, false); + inventory.setChargeType(chargeType.getCode()); + inventory.setDestination(destination.getCode()); + inventory.setSupplier(supplier.getSupId()); + inventory.setDischargeType(dischargeType.getCode()); + inventory = medicalInventoryIoOperation.newMedicalInventory(inventory); + MedicalType medicalType = testMedicalType.setup(false); + Medical medical = testMedical.setup(medicalType, false); + Lot lotOne = testLot.setup(medical, false); + Movement firstMovement = testMovement.setup(medical, chargeType, ward, lotOne, supplier, false); + firstMovement.setQuantity(100); + MedicalStock firstmedicalStock = testMedicalStock.setup(firstMovement); + Lot lotTwo = testLot.setup(medical, false); + lotTwo.setCode("LOT-001"); + Movement secondMovement = testMovement.setup(medical, chargeType, ward, lotTwo, supplier, false); + secondMovement.setQuantity(100); + MedicalStock secondmedicalStock = testMedicalStock.setup(firstMovement); + Lot lotThree = testLot.setup(medical, false); + lotTwo.setCode("LOT-003"); + medicalType = medicalTypeIoOperationRepository.save(medicalType); + medical = medicalsIoOperationRepository.save(medical); + lotOne = lotIoOperationRepository.save(lotOne); + lotTwo = lotIoOperationRepository.save(lotTwo); + lotThree = lotIoOperationRepository.save(lotThree); + firstMovement = medicalStockIoOperation.newMovement(firstMovement); + secondMovement = medicalStockIoOperation.newMovement(secondMovement); + medicalStockIoOperationRepository.saveAndFlush(firstmedicalStock); + medicalStockIoOperationRepository.saveAndFlush(secondmedicalStock); + MedicalInventoryRow medicalInventoryRowOne = testMedicalInventoryRow.setup(inventory, medical, lotOne, false); + medicalInventoryRowOne.setRealqty(60); + MedicalInventoryRow medicalInventoryRowTwo = testMedicalInventoryRow.setup(inventory, medical, lotTwo, false); + medicalInventoryRowTwo.setId(2); + medicalInventoryRowTwo.setRealqty(30); + MedicalInventoryRow medicalInventoryRowThree = testMedicalInventoryRow.setup(inventory, medical, lotThree, false); + medicalInventoryRowThree.setId(3); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowOne); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowTwo); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowThree); + int inventoryId = inventory.getId(); + List medicalInventoryRows = medicalInventoryRowManager.getMedicalInventoryRowByInventoryId(inventoryId); + assertThat(medicalInventoryRows).isNotEmpty(); + assertThat(medicalInventoryRows.size()).isEqualTo(3); + List insertMovements = medicalInventoryManager.confirmMedicalInventoryRow(inventory, medicalInventoryRows); + assertThat(insertMovements).isNotEmpty(); + String status = InventoryStatus.done.toString(); + inventory = medicalInventoryIoOperation.getInventoryById(inventoryId); + assertThat(inventory).isNotNull(); + assertThat(inventory.getStatus()).isEqualTo(status); + } + + @Test + void testReferenceOfInventoryExist() throws Exception { + int id = setupTestMedicalInventory(false); + MedicalInventory inventory = medIvnIoOperationRepository.findById(id).orElse(null); + assertThat(inventory).isNotNull(); + String reference = inventory.getInventoryReference(); + boolean exist = medicalInventoryManager.referenceExists(reference); + assertThat(exist).isEqualTo(true); + } + + @Test + void testGetInventoryByID() throws Exception { + int id = setupTestMedicalInventory(false); + MedicalInventory inventory = medicalInventoryManager.getInventoryById(id); + assertThat(inventory).isNotNull(); + } + + @Test + void testGetInventoryByReference() throws Exception { + int id = setupTestMedicalInventory(false); + MedicalInventory inventory = medIvnIoOperationRepository.findById(id).orElse(null); + assertThat(inventory).isNotNull(); + String reference = inventory.getInventoryReference(); + MedicalInventory found = medicalInventoryManager.getInventoryByReference(reference); + assertThat(found).isNotNull(); + assertThat(found.getInventoryReference()).isEqualTo(inventory.getInventoryReference()); + } + + @Test + void testValidateMedicalInventory() throws Exception { + Ward ward = testWard.setup(false); + wardIoOperationRepository.saveAndFlush(ward); + MovementType chargeType = new MovementType("inventory+", "Inventory+", "+", "non-operational"); + MovementType dischargeType = new MovementType("inventory-", "Inventory-", "-", "non-operational"); + Supplier supplier = new Supplier(1, "INVENTORY", null, null, null, null, null, null); + Ward destination = new Ward("INV", "ward inventory", null, null, null, 8, 1, 1, false, false); + dischargeType = medicalDsrStockMovementTypeIoOperationRepository.save(dischargeType); + chargeType = medicalDsrStockMovementTypeIoOperationRepository.save(chargeType); + supplier = supplierIoOperationRepository.save(supplier); + destination = wardIoOperationRepository.save(destination); + MedicalInventory inventory = testMedicalInventory.setup(ward, false); + inventory.setChargeType(chargeType.getCode()); + inventory.setDestination(destination.getCode()); + inventory.setSupplier(supplier.getSupId()); + inventory.setDischargeType(dischargeType.getCode()); + inventory = medicalInventoryIoOperation.newMedicalInventory(inventory); + MedicalType medicalType = testMedicalType.setup(false); + Medical medical = testMedical.setup(medicalType, false); + Lot lotOne = testLot.setup(medical, false); + Movement firstMovement = testMovement.setup(medical, chargeType, ward, lotOne, supplier, false); + firstMovement.setQuantity(100); + MedicalStock firstmedicalStock = testMedicalStock.setup(firstMovement); + Lot lotTwo = testLot.setup(medical, false); + lotTwo.setCode("LOT-001"); + Movement secondMovement = testMovement.setup(medical, chargeType, ward, lotTwo, supplier, false); + secondMovement.setQuantity(100); + MedicalStock secondmedicalStock = testMedicalStock.setup(firstMovement); + Lot lotThree = testLot.setup(medical, false); + lotTwo.setCode("LOT-003"); + medicalType = medicalTypeIoOperationRepository.save(medicalType); + medical = medicalsIoOperationRepository.save(medical); + lotOne = lotIoOperationRepository.save(lotOne); + lotTwo = lotIoOperationRepository.save(lotTwo); + lotThree = lotIoOperationRepository.save(lotThree); + MedicalInventoryRow medicalInventoryRowOne = testMedicalInventoryRow.setup(inventory, medical, lotOne, false); + medicalInventoryRowOne.setRealqty(60); + MedicalInventoryRow medicalInventoryRowTwo = testMedicalInventoryRow.setup(inventory, medical, lotTwo, false); + medicalInventoryRowTwo.setId(2); + medicalInventoryRowTwo.setRealqty(30); + MedicalInventoryRow medicalInventoryRowThree = testMedicalInventoryRow.setup(inventory, medical, lotThree, false); + medicalInventoryRowThree.setId(3); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowOne); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowTwo); + medicalInventoryRowIoOperationRepository.saveAndFlush(medicalInventoryRowThree); + firstMovement = medicalStockIoOperation.newMovement(firstMovement); + secondMovement = medicalStockIoOperation.newMovement(secondMovement); + medicalStockIoOperationRepository.saveAndFlush(firstmedicalStock); + medicalStockIoOperationRepository.saveAndFlush(secondmedicalStock); + int inventoryId = inventory.getId(); + List medicalInventoryRows = medicalInventoryRowManager.getMedicalInventoryRowByInventoryId(inventoryId); + assertThat(medicalInventoryRows).isNotEmpty(); + assertThat(medicalInventoryRows.size()).isEqualTo(3); + medicalInventoryManager.validateMedicalInventoryRow(inventory, medicalInventoryRows); + } }