From 5b9ee9da8d3486ecd6ab908d717ecfdb7eb54ed0 Mon Sep 17 00:00:00 2001 From: Dr M H B Ariyaratne Date: Fri, 19 Apr 2024 08:29:34 +0530 Subject: [PATCH 1/2] Signed-off-by: Dr M H B Ariyaratne --- src/main/resources/VERSION.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/VERSION.txt b/src/main/resources/VERSION.txt index 9303bb757b..041265944c 100644 --- a/src/main/resources/VERSION.txt +++ b/src/main/resources/VERSION.txt @@ -1 +1 @@ -3.0.0.20240418.3 +3.0.0.20240417.12 From 7c116cb1386b9e04830fcba5a15a6be87e3bde4c Mon Sep 17 00:00:00 2001 From: Dr M H B Ariyaratne Date: Fri, 19 Apr 2024 21:00:50 +0530 Subject: [PATCH 2/2] Closes #4644 --- .../bean/pharmacy/PharmacySaleController.java | 260 +++++++++++------- .../divudi/bean/pharmacy/StockController.java | 37 ++- .../bean/pharmacy/UserStockController.java | 4 +- 3 files changed, 195 insertions(+), 106 deletions(-) diff --git a/src/main/java/com/divudi/bean/pharmacy/PharmacySaleController.java b/src/main/java/com/divudi/bean/pharmacy/PharmacySaleController.java index 49cfef3c79..8dcd2d945f 100644 --- a/src/main/java/com/divudi/bean/pharmacy/PharmacySaleController.java +++ b/src/main/java/com/divudi/bean/pharmacy/PharmacySaleController.java @@ -813,35 +813,34 @@ public void handleSelect(SelectEvent event) { handleSelectAction(); } - public void calculateRates(BillItem bi) { - ////////System.out.println("calculating rates"); - if (bi.getPharmaceuticalBillItem().getStock() == null) { - ////////System.out.println("stock is null"); - return; - } - getBillItem(); - PharmaceuticalBillItem pharmBillItem = bi.getPharmaceuticalBillItem(); - if (pharmBillItem != null) { - Stock stock = pharmBillItem.getStock(); - if (stock != null) { - ItemBatch itemBatch = stock.getItemBatch(); - if (itemBatch != null) { - // Ensure that each step in the chain is not null before accessing further. - bi.setRate(itemBatch.getRetailsaleRate()); - } else { - } - } else { - } - } else { - } - -// bi.setRate(bi.getPharmaceuticalBillItem().getStock().getItemBatch().getRetailsaleRate()); - bi.setDiscount(calculateBillItemDiscountRate(bi)); - // ////System.err.println("Discount "+bi.getDiscount()); - bi.setNetRate(bi.getRate() - bi.getDiscount()); - // ////System.err.println("Net "+bi.getNetRate()); - } - +// public void calculateRates(BillItem bi) { +// ////////System.out.println("calculating rates"); +// if (bi.getPharmaceuticalBillItem().getStock() == null) { +// ////////System.out.println("stock is null"); +// return; +// } +// getBillItem(); +// PharmaceuticalBillItem pharmBillItem = bi.getPharmaceuticalBillItem(); +// if (pharmBillItem != null) { +// Stock stock = pharmBillItem.getStock(); +// if (stock != null) { +// ItemBatch itemBatch = stock.getItemBatch(); +// if (itemBatch != null) { +// // Ensure that each step in the chain is not null before accessing further. +// bi.setRate(itemBatch.getRetailsaleRate()); +// } else { +// } +// } else { +// } +// } else { +// } +// +//// bi.setRate(bi.getPharmaceuticalBillItem().getStock().getItemBatch().getRetailsaleRate()); +// bi.setDiscount(calculateBillItemDiscountRate(bi)); +// // ////System.err.println("Discount "+bi.getDiscount()); +// bi.setNetRate(bi.getRate() - bi.getDiscount()); +// // ////System.err.println("Net "+bi.getNetRate()); +// } public void fillReplaceableStocksForAmp(Amp ampIn) { String sql; Map m = new HashMap(); @@ -922,52 +921,105 @@ public void calculateBillItem() { } public void addBillItem() { - if(configOptionApplicationController.getBooleanValueByKey("Add quantity from multiple batches in pharmacy retail billing")){ + if (configOptionApplicationController.getBooleanValueByKey("Add quantity from multiple batches in pharmacy retail billing")) { addBillItemMultipleBatches(); - }else{ + } else { addBillItemSingleItem(); } + processBillItems(); + + setActiveIndex(1); } - public void addBillItemSingleItem() { + public void processBillItems() { + calculateAllRates(); + calculateTotals(); + } + + public void calculateAllRates() { + System.out.println("Calculating all rates"); + for (BillItem tbi : getPreBill().getBillItems()) { + calculateRates(tbi); +// calculateBillItemForEditing(tbi); + } + } + + public void calculateRates(BillItem bi) { + System.out.println("Calculating rates"); + PharmaceuticalBillItem pharmBillItem = bi.getPharmaceuticalBillItem(); + if (pharmBillItem != null && pharmBillItem.getStock() != null) { + ItemBatch itemBatch = pharmBillItem.getStock().getItemBatch(); + if (itemBatch != null) { + bi.setRate(itemBatch.getRetailsaleRate()); + } + bi.setGrossValue(bi.getPharmaceuticalBillItem().getStock().getItemBatch().getRetailsaleRate() * bi.getQty()); + bi.setDiscount(calculateBillItemDiscountRate(bi)); + bi.setNetRate(bi.getRate() - bi.getDiscount()); + } + } + + public void calculateTotals() { + getPreBill().setTotal(0); + double netTotal = 0.0, grossTotal = 0.0, discountTotal = 0.0; + int index = 0; + + for (BillItem b : getPreBill().getBillItems()) { + if (!b.isRetired()) { + b.setSearialNo(index++); + netTotal += b.getNetValue(); + grossTotal += b.getGrossValue(); + discountTotal += b.getDiscount(); + getPreBill().setTotal(getPreBill().getTotal() + b.getNetValue()); + } + } + + System.out.println("Total discount: " + discountTotal); + getPreBill().setNetTotal(netTotal); + getPreBill().setTotal(grossTotal); + getPreBill().setGrantTotal(grossTotal); + getPreBill().setDiscount(discountTotal); + setNetTotal(getPreBill().getNetTotal()); + } + + public double addBillItemSingleItem() { editingQty = null; errorMessage = null; - + double addedQty = 0.0; if (billItem == null) { - return; + return addedQty; } if (billItem.getPharmaceuticalBillItem() == null) { - return; + return addedQty; } if (getStock() == null) { - errorMessage = "Item?"; - JsfUtil.addErrorMessage("Item?"); - return; + errorMessage = "Item ??"; + JsfUtil.addErrorMessage("Please select an Item Batch to Dispense ??"); + return addedQty; } if (getStock().getItemBatch().getDateOfExpire().before(commonController.getCurrentDateTime())) { JsfUtil.addErrorMessage("Please not select Expired Items"); - return; + return addedQty; } if (getQty() == null) { errorMessage = "Quantity?"; JsfUtil.addErrorMessage("Quantity?"); - return; + return addedQty; } if (getQty() == 0.0) { errorMessage = "Quantity Zero?"; JsfUtil.addErrorMessage("Quentity Zero?"); - return; + return addedQty; } if (getQty() > getStock().getStock()) { errorMessage = "No sufficient stocks."; JsfUtil.addErrorMessage("No Sufficient Stocks?"); - return; + return addedQty; } if (checkItemBatch()) { errorMessage = "This batch is already there in the bill."; JsfUtil.addErrorMessage("Already added this item batch"); - return; + return addedQty; } // if (CheckDateAfterOneMonthCurrentDateTime(getStock().getItemBatch().getDateOfExpire())) { // errorMessage = "This batch is Expire With in 31 Days."; @@ -977,9 +1029,9 @@ public void addBillItemSingleItem() { //Checking User Stock Entity if (!userStockController.isStockAvailable(getStock(), getQty(), getSessionController().getLoggedUser())) { JsfUtil.addErrorMessage("Sorry Already Other User Try to Billing This Stock You Cant Add"); - return; + return addedQty; } - + addedQty = qty; billItem.getPharmaceuticalBillItem().setQtyInUnit((double) (0 - qty)); billItem.getPharmaceuticalBillItem().setStock(stock); billItem.getPharmaceuticalBillItem().setItemBatch(getStock().getItemBatch()); @@ -999,18 +1051,15 @@ public void addBillItemSingleItem() { UserStock us = saveUserStock(billItem); billItem.setTransUserStock(us); - - calculateAllRatesNew(); - - calTotalNew(); - clearBillItem(); - setActiveIndex(1); + getBillItem(); + return addedQty; } - + public void addBillItemMultipleBatches() { editingQty = null; errorMessage = null; + if (billItem == null) { return; @@ -1019,14 +1068,15 @@ public void addBillItemMultipleBatches() { return; } if (getStock() == null) { - errorMessage = "Item?"; - JsfUtil.addErrorMessage("Item?"); - return; - } - if (getStock().getItemBatch().getDateOfExpire().before(commonController.getCurrentDateTime())) { - JsfUtil.addErrorMessage("You are NOT allowed to select Expired Items"); + errorMessage = "Please select an Item Batch to Dispense?"; + JsfUtil.addErrorMessage("Please select an Item Batch to Dispense?"); return; } + Stock userSelectedStock = stock; +// if (getStock().getItemBatch().getDateOfExpire().before(commonController.getCurrentDateTime())) { +// JsfUtil.addErrorMessage("You are NOT allowed to select Expired Items"); +// return; +// } if (getQty() == null) { errorMessage = "Please enter a Quantity"; JsfUtil.addErrorMessage("Quantity?"); @@ -1037,28 +1087,47 @@ public void addBillItemMultipleBatches() { JsfUtil.addErrorMessage("Quentity Zero?"); return; } - //This was removed as multiple batches will be able to add if one batch stock is not working -// if (getQty() > getStock().getStock()) { -// errorMessage = "No sufficient stocks."; -// JsfUtil.addErrorMessage("No Sufficient Stocks?"); -// return; -// } -// if (checkItemBatch()) { -// errorMessage = "This batch is already there in the bill."; -// JsfUtil.addErrorMessage("Already added this item batch"); -// return; -// } - -// if (!userStockController.isStockAvailable(getStock(), getQty(), getSessionController().getLoggedUser())) { -// JsfUtil.addErrorMessage("Sorry Already Other User Try to Billing This Stock You Cant Add"); -// return; -// } + double requestedQty = getQty(); + double addedQty = 0.0; + double remainingQty = getQty(); + + if (getQty() <= getStock().getStock()) { + double thisTimeAddingQty = addBillItemSingleItem(); + if (thisTimeAddingQty >= requestedQty) { + return; + } else { + addedQty += thisTimeAddingQty; + remainingQty = remainingQty - thisTimeAddingQty; + } + } else { + qty = getStock().getStock(); + double thisTimeAddingQty = addBillItemSingleItem(); + addedQty += thisTimeAddingQty; + remainingQty = remainingQty - thisTimeAddingQty; + } + +// addedQty = addBillItemSingleItem(); +// System.out.println("stock = " + userSelectedStock); +// System.out.println("stock item batch = " + userSelectedStock.getItemBatch()); +// System.out.println("stock item batch item= " + userSelectedStock.getItemBatch().getItem()); + + List availableStocks = stockController.findNextAvailableStocks(userSelectedStock); + for (Stock s : availableStocks) { + stock = s; + if (remainingQty < s.getStock()) { + qty = remainingQty; + } else { + qty = s.getStock(); + } + double thisTimeAddingQty = addBillItemSingleItem(); + addedQty += thisTimeAddingQty; + remainingQty = remainingQty - thisTimeAddingQty; + if (remainingQty <= 0) { + return; + } + } - calculateAllRatesNew(); - calTotalNew(); - clearBillItem(); - setActiveIndex(1); } private void addSingleStock() { @@ -1109,15 +1178,14 @@ private UserStock saveUserStock(BillItem tbi) { return us; } - public void calculateAllRatesNew() { - ////////System.out.println("calculating all rates"); - for (BillItem tbi : getPreBill().getBillItems()) { - calculateRates(tbi); - calculateBillItemForEditing(tbi); - } - calTotal(); - } - +// public void calculateAllRatesNew() { +// ////////System.out.println("calculating all rates"); +// for (BillItem tbi : getPreBill().getBillItems()) { +// calculateRates(tbi); +// calculateBillItemForEditing(tbi); +// } +// calTotal(); +// } public void calTotalNew() { getPreBill().setTotal(0); double netTot = 0.0; @@ -1952,15 +2020,14 @@ public void changeListener() { calculateAllRates(); } - public void calculateAllRates() { - //////System.out.println("calculating all rates"); - for (BillItem tbi : getPreBill().getBillItems()) { - calculateDiscountRates(tbi); - calculateBillItemForEditing(tbi); - } - calTotal(); - } - +// public void calculateAllRates() { +// //////System.out.println("calculating all rates"); +// for (BillItem tbi : getPreBill().getBillItems()) { +// calculateDiscountRates(tbi); +// calculateBillItemForEditing(tbi); +// } +// calTotal(); +// } public void calculateRateListner(AjaxBehaviorEvent event) { } @@ -2157,7 +2224,6 @@ private void clearBill() { private void clearBillItem() { replaceableStocks = null; billItem = null; -// removingBillItem = null; editingBillItem = null; qty = null; stock = null; diff --git a/src/main/java/com/divudi/bean/pharmacy/StockController.java b/src/main/java/com/divudi/bean/pharmacy/StockController.java index ce3cdfc4ce..91ac1a43f5 100644 --- a/src/main/java/com/divudi/bean/pharmacy/StockController.java +++ b/src/main/java/com/divudi/bean/pharmacy/StockController.java @@ -182,23 +182,46 @@ public List completeAvailableStocks(String qry) { public List findNextAvailableStocks(Stock stock) { List stockList; String jpql; - Map params = new HashMap(); + Map params = new HashMap<>(); jpql = "select i " + " from Stock i " - + " where i.stock >:s " - + " and i.department=:d " - + " and i.itemBatch.item=:item" - + " and i!=:stock" + + " where i.stock > :s " + + " and i.department = :d " + + " and i.itemBatch.item = :item" + + " and i != :stock" + + " and i.itemBatch.dateOfExpire > CURRENT_DATE" // Exclude expired stocks + " order by i.itemBatch.dateOfExpire"; params.put("d", getSessionController().getLoggedUser().getDepartment()); - double d = 0.0; - params.put("s", d); + params.put("s", 0.0); params.put("stock", stock); params.put("item", stock.getItemBatch().getItem()); stockList = getStockFacade().findByJpql(jpql, params, 20); return stockList; } + public List findNextAvailableStocks(List excludedStocks) { + List stockList; + String jpql; + Map params = new HashMap<>(); + jpql = "select i " + + " from Stock i " + + " where i.stock > :s " + + " and i.department = :d " + + " and i.itemBatch.dateOfExpire > CURRENT_DATE" + + " and i.itemBatch.item = :item" + + (excludedStocks.isEmpty() ? "" : " and i NOT IN :excludedStocks") // Exclude stocks in the provided list if not empty + + " order by i.itemBatch.dateOfExpire"; + params.put("d", getSessionController().getLoggedUser().getDepartment()); + params.put("s", 0.0); + // Assuming all stocks in the list belong to the same item, which might need checking or adjustment in real scenarios. + params.put("item", excludedStocks.get(0).getItemBatch().getItem()); + if (!excludedStocks.isEmpty()) { + params.put("excludedStocks", excludedStocks); + } + stockList = getStockFacade().findByJpql(jpql, params, 20); + return stockList; + } + public List completeAvailableStocksStartsWith(String qry) { List stockList; String sql; diff --git a/src/main/java/com/divudi/bean/pharmacy/UserStockController.java b/src/main/java/com/divudi/bean/pharmacy/UserStockController.java index 85aedd4fab..59415a5d4a 100644 --- a/src/main/java/com/divudi/bean/pharmacy/UserStockController.java +++ b/src/main/java/com/divudi/bean/pharmacy/UserStockController.java @@ -54,7 +54,7 @@ public boolean isStockAvailable(Stock stock, double qty, WebUser webUser) { + " where us.retired=false " + " and us.userStockContainer.retired=false " + " and us.stock=:stk " -// + " and us.creater!=:wb " + + " and us.creater!=:wb " + " and us.createdAt between :frm and :to "; Calendar cal = Calendar.getInstance(); @@ -64,7 +64,7 @@ public boolean isStockAvailable(Stock stock, double qty, WebUser webUser) { HashMap hm = new HashMap(); hm.put("stk", stock); -// hm.put("wb", webUser); + hm.put("wb", webUser); hm.put("to", toTime); hm.put("frm", fromTime);