Skip to content

Commit

Permalink
Merge pull request #4670 from hmislk/Issue#4644
Browse files Browse the repository at this point in the history
Issue#4644  Closes #4644
  • Loading branch information
Irani96 authored Apr 20, 2024
2 parents da2dcc0 + 3a1e4a8 commit f8d4918
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 107 deletions.
260 changes: 163 additions & 97 deletions src/main/java/com/divudi/bean/pharmacy/PharmacySaleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.";
Expand All @@ -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());
Expand All @@ -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;
Expand All @@ -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?");
Expand All @@ -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<Stock> 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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

}
Expand Down Expand Up @@ -2157,7 +2224,6 @@ private void clearBill() {
private void clearBillItem() {
replaceableStocks = null;
billItem = null;
// removingBillItem = null;
editingBillItem = null;
qty = null;
stock = null;
Expand Down
Loading

0 comments on commit f8d4918

Please sign in to comment.