Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
buddhika75 committed Jul 13, 2024
1 parent c0cbc7a commit bc2ce57
Show file tree
Hide file tree
Showing 5 changed files with 413 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public String navigateToOpticianPatientManagement() {
public String navigateToOpticianAppointmentManagement() {
return "/optician/appointment_management?faces-redirect=true";
}


// Navigation to the optician Appointment Management page
public String navigateToStockReport() {
return "/optician/stock_by_batch?faces-redirect=true";
}

// Navigation to the optician Stock Management page
public String navigateToOpticianStockManagement() {
Expand Down
37 changes: 34 additions & 3 deletions src/main/java/com/divudi/bean/pharmacy/StockController.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void addItemStockToStocks(Set<Stock> inputStocks) {
if (inputStocks == null) {
return;
}
if(inputStocks.size()>20){
if (inputStocks.size() > 20) {
return;
}
// Map to store the total stock quantity for each item
Expand Down Expand Up @@ -563,9 +563,9 @@ public void saveSelected() {
recreateModel();
getItems();
}

public void save(Stock s) {
if (s==null) {
if (s == null) {
return;
}
if (s.getId() != null && s.getId() > 0) {
Expand All @@ -576,6 +576,17 @@ public void save(Stock s) {
JsfUtil.addSuccessMessage("Saved Successfully");
}
}

public void saveSilantly(Stock s) {
if (s == null) {
return;
}
if (s.getId() != null && s.getId() > 0) {
getFacade().edit(s);
} else {
getFacade().create(s);
}
}

public void setSelectText(String selectText) {
this.selectText = selectText;
Expand Down Expand Up @@ -650,6 +661,26 @@ public Stock getStock() {
return stock;
}

public Stock findStockById(String strId) {
if (strId == null || strId.isEmpty()) {
throw new IllegalArgumentException("The provided ID string is null or empty.");
}

try {
Long id = Long.parseLong(strId);
return findStockById(id);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The provided ID string is not a valid number.", e);
}
}

public Stock findStockById(Long id) {
if (id == null) {
throw new IllegalArgumentException("The provided ID is null.");
}
return getFacade().find(id);
}

public void setStock(Stock stock) {
this.stock = stock;
}
Expand Down
103 changes: 102 additions & 1 deletion src/main/java/com/divudi/ws/lims/Lims.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.divudi.bean.common.CommonController;
import com.divudi.bean.common.ItemController;
import com.divudi.bean.common.SecurityController;
import com.divudi.bean.pharmacy.StockController;
import com.divudi.data.InvestigationItemType;
import com.divudi.entity.Bill;
import com.divudi.entity.BillItem;
Expand Down Expand Up @@ -88,6 +89,8 @@ public class Lims {
ItemController itemController;
@Inject
BillBeanController billBeanController;
@Inject
StockController stockController;

/**
* Creates a new instance of LIMS
Expand Down Expand Up @@ -350,6 +353,52 @@ public String processBarcodeFromBill(String barcode, WebUser requestSendingUser)
return jSONObjectOut.toString();
}

public String processBarcodeFromStock(String barcode, WebUser requestSendingUser) {
JSONArray array = new JSONArray();
if (barcode == null) {
return "";
}

Stock stock = stockController.findStockById(barcode);
if (stock == null) {
return "";
}

Item i = stock.getItemBatch().getItem();
if (i == null || i.getItemBarcodeGenerationStrategy() == null) {
return "";
}

switch (i.getItemBarcodeGenerationStrategy()) {
case BY_INDIVIDUAL_UNIT:
JSONArray jArray = constructUnitBarcodesJson(stock);
if (jArray != null) {
for (int index = 0; index < jArray.length(); index++) {
array.put(jArray.get(index));
}
}
break;
case BY_BATCH:
JSONObject bcBatch = constructBatchBarcodeJson(stock);
if (bcBatch != null) {
array.put(bcBatch);
}
break;
case BY_ITEM:
JSONObject bcItem = constructItemBarcodeJson(i);
if (bcItem != null) {
array.put(bcItem);
}
break;
default:
break;
}

JSONObject jSONObjectOut = new JSONObject();
jSONObjectOut.put("Barcodes", array);
return jSONObjectOut.toString();
}

public String processSamplesFromBill(String billId, String username, String password) {

String validationError = validateInput(billId, username, password);
Expand Down Expand Up @@ -377,7 +426,7 @@ public String processItemBarcodesFromBillID(String billId, String username, Stri
return constructErrorJson(1, "Username / password mismatch.", billId);
}

return processBarcodeFromBill(billId, requestSendingUser);
return processBarcodeFromStock(billId, requestSendingUser);
}

public String generateSamplesForInternalUse(String billId, WebUser user) {
Expand Down Expand Up @@ -604,6 +653,58 @@ private JSONArray constructUnitBarcodesJson(BillItem bi, Long startBarcode, Long
return jsonArray;
}

public Long createBarcode(String prefix, Long number) {
String formattedNumber = String.format("%06d", number);
String combinedString = prefix + formattedNumber;
Long barcode = Long.parseLong(combinedString);
return barcode;
}

private JSONArray constructUnitBarcodesJson(Stock stock) {

String initialPartOfBarcode = stock.getItemBatch().getItem().getBarcode();
Long startLongSecondPart = stock.getItemBatch().getItem().getLastBarcode() + 1;
Double qty = stock.getStock();
Long endLongSecondPart = stock.getItemBatch().getItem().getLastBarcode() + Math.round(qty);
Long startFullBarcode = createBarcode(initialPartOfBarcode, startLongSecondPart);
Long endFullBarcode = createBarcode(initialPartOfBarcode, endLongSecondPart);
stock.setStartBarcode(startFullBarcode);
stock.setEndBarcode(endFullBarcode);
stock.getItemBatch().getItem().setLastBarcode(endLongSecondPart);
itemController.saveSelected(stock.getItemBatch().getItem());
stockController.saveSilantly(stock);

JSONArray jsonArray = new JSONArray();

DecimalFormat rateFormatter = new DecimalFormat("#,##0.00");
String formattedRate = "";
try {
Double rate = stock.getItemBatch().getRetailsaleRate();
if (rate != null) {
formattedRate = rateFormatter.format(rate);
}
} catch (Exception e) {
System.out.println("Error in formatting rate: " + e);
}

Long barcode = stock.getStartBarcode();
Long endBarcode = stock.getEndBarcode();
try {
while (barcode <= endBarcode) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("itemName", stock.getItemBatch().getItem().getName() != null ? stock.getItemBatch().getItem().getName() : "");
jsonObject.put("rate", formattedRate);
jsonObject.put("barcode", barcode);
jsonArray.put(jsonObject);
barcode++;
}
} catch (Exception e) {
System.out.println("Error while constructing JSON array: " + e);
}

return jsonArray;
}

private JSONObject constructBatchBarcodeJson(Stock stock) {
if (stock == null || stock.getStartBarcode() == null) {
return null; // Return an empty array if input is invalid
Expand Down
Loading

0 comments on commit bc2ce57

Please sign in to comment.