Skip to content

Commit

Permalink
Merge pull request #10237 from hmislk/10227-improvements-in-pharmacy-…
Browse files Browse the repository at this point in the history
…retail-sale-cancellation

10227 improvements in pharmacy retail sale cancellation
  • Loading branch information
buddhika75 authored Jan 25, 2025
2 parents 888ad2d + c34099b commit 709bcf4
Show file tree
Hide file tree
Showing 25 changed files with 2,073 additions and 1,222 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/divudi/bean/common/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4449,7 +4449,7 @@ public void createStoreAdjustmentBillItemTable() {

sql += " order by bi.id desc ";

billItems = getBillItemFacade().findByJpql(sql, m, TemporalType.TIMESTAMP, 50);
billItems = getBillItemFacade().findByJpql(sql, m, TemporalType.TIMESTAMP);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ public List<Stock> completeStaffStocks(String qry) {
return items;
}

public List<Stock> completeStaffStocksInStore(String qry) {
List<Stock> items;
String sql;
Map m = new HashMap();
double d = 0.0;
m.put("s", d);
m.put("n", "%" + qry.toUpperCase() + "%");
sql = "select i from Stock i where i.stock !=:s and "
+ "((i.staff.code) like :n or "
+ "(i.staff.person.name) like :n or "
+ "(i.itemBatch.item.name) like :n ) "
+ " and i.itemBatch.item."
+ "order by i.itemBatch.item.name, i.itemBatch.dateOfExpire , i.stock desc";
items = getStockFacade().findByJpql(sql, m, 20);

return items;
}

public List<Stock> completeStaffZeroStocks(String qry) {
List<Stock> items;
String sql;
Expand Down
51 changes: 25 additions & 26 deletions src/main/java/com/divudi/bean/pharmacy/PharmacyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.divudi.light.pharmacy.PharmaceuticalItemLight;
import com.divudi.service.BillService;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -1602,7 +1603,8 @@ public void createStockTransferReport() {

public void generateReportByDepartmentWiseBill(BillType billType) {
Map<String, Object> parameters = new HashMap<>();
String sql = "SELECT b.toDepartment, "
StringBuilder sql = new StringBuilder(
"SELECT b.toDepartment, "
+ "b.department, "
+ "b.deptId, "
+ "b.createdAt, "
Expand All @@ -1614,19 +1616,21 @@ public void generateReportByDepartmentWiseBill(BillType billType) {
+ "WHERE b.retired = false "
+ "AND b.createdAt BETWEEN :fromDate AND :toDate "
+ "AND b.billType = :billType "
+ "GROUP BY b.toDepartment, b";
);

parameters.put("billType", billType);
parameters.put("fromDate", fromDate);
parameters.put("toDate", toDate);

additionalCommonFilltersForBillEntity(new StringBuilder(sql), parameters);
additionalCommonFilltersForBillEntity(sql, parameters);

sql.append(" GROUP BY b.toDepartment, b.department, b.deptId, b.createdAt, b.backwardReferenceBill, b.backwardReferenceBill.deptId, b");

Map<Department, List<DepartmentWiseBill>> departmentWiseBillMap = new HashMap<>();

try {
List<Object[]> results = (List<Object[]>) getBillFacade()
.findLightsByJpql(sql, parameters, TemporalType.TIMESTAMP);
.findLightsByJpql(sql.toString(), parameters, TemporalType.TIMESTAMP);

departmentWiseBillList = new ArrayList<>();
totalPurchase = 0;
Expand All @@ -1649,21 +1653,21 @@ public void generateReportByDepartmentWiseBill(BillType billType) {

departmentWiseBillList.add(departmentWiseBill);
totalPurchase += departmentWiseBill.getBill().getNetTotal();
departmentWiseBillList = departmentWiseBillMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey(Comparator.comparing(Department::getName)))
.flatMap(entry -> entry.getValue().stream())
.collect(Collectors.toList());

}

departmentWiseBillList = departmentWiseBillMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey(Comparator.comparing(Department::getName)))
.flatMap(entry -> entry.getValue().stream())
.collect(Collectors.toList());

} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Error occurred while generating the consumption report.");
}
}

private void addFilter(StringBuilder sql, Map<String, Object> parameters, String sqlField, String paramKey, Object value) {
if (value != null) {
sql.append(" and ").append(sqlField).append(" = :").append(paramKey);
sql.append(" AND ").append(sqlField).append(" = :").append(paramKey).append(" ");
parameters.put(paramKey, value);
}
}
Expand Down Expand Up @@ -1721,45 +1725,40 @@ public void generateReportByBillItems(BillType billType) {
}

public void generateReportAsSummary(BillType billType) {
// Prepare SQL query with dynamic filtering
StringBuilder sql = new StringBuilder();
sql.append("SELECT b.department.name, SUM(b.netTotal) ")
sql.append("SELECT b.department.name, SUM(b.netTotal), ")
.append("SUM(CASE WHEN b.forwardReferenceBill IS NULL AND SIZE(b.forwardReferenceBills) = 0 THEN b.netTotal ELSE 0 END) ")
.append("FROM Bill b ")
.append("WHERE b.retired = false ")
.append("AND b.billType = :billType ")
.append("AND b.createdAt BETWEEN :fromDate AND :toDate ");

// Prepare parameters
Map<String, Object> parameters = new HashMap<>();
parameters.put("billType", billType);
parameters.put("fromDate", getFromDate());
parameters.put("toDate", getToDate());

// Add optional filters
additionalCommonFilltersForBillEntity(sql, parameters);

// Group by department and order by total in descending order
sql.append(" GROUP BY b.department.name ORDER BY SUM(b.netTotal) DESC");

try {
// Execute the query and fetch summarized data
List<Object[]> results = getBillFacade().findObjectsArrayByJpql(sql.toString(), parameters, TemporalType.TIMESTAMP);

// Process results into a summary list
departmentSummaries = new ArrayList<>();
double grandTotal = 0;
double grandTotal = 0.0;
double goodInTransistTotal = 0.0;

for (Object[] result : results) {
String departmentName = (String) result[0]; // Extract department name
Double netTotal = (Double) result[1]; // Extract net total
grandTotal += netTotal; // Accumulate the grand total
String departmentName = (String) result[0];
double netTotal = (double) result[1];
double goodInTransist = (double) result[2];
grandTotal += netTotal;
goodInTransistTotal += goodInTransist;

// Add summary data
departmentSummaries.add(new PharmacySummery(departmentName, netTotal));
departmentSummaries.add(new PharmacySummery(departmentName, netTotal, goodInTransist));
}

// Add grand total
departmentSummaries.add(new PharmacySummery("Total", grandTotal));
departmentSummaries.add(new PharmacySummery("Total", grandTotal, goodInTransistTotal));

} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Something Went Wrong!");
Expand Down
61 changes: 43 additions & 18 deletions src/main/java/com/divudi/bean/pharmacy/SaleReturnController.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ public class SaleReturnController implements Serializable {

PaymentMethodData paymentMethodData;

public String navigateToReturnItemsAndPaymentsForPharmacyRetailSale() {
if (bill == null) {
JsfUtil.addErrorMessage("Please select a bill to return");
return null;
}
if (bill.isCancelled()) {
JsfUtil.addErrorMessage("Cancelled Bills CAN NOT BE returned");
return null;
}

return "/pharmacy/pharmacy_bill_return_retail?faces-redirect=true";
}

public PaymentMethodData getPaymentMethodData() {
if (paymentMethodData == null) {
paymentMethodData = new PaymentMethodData();
Expand All @@ -107,6 +120,7 @@ public void setBill(Bill bill) {
makeNull();
this.bill = bill;
generateBillComponent();
returnPaymentMethod = bill.getPaymentMethod();
}

public Bill getReturnBill() {
Expand Down Expand Up @@ -167,8 +181,9 @@ private void savePreReturnBill() {

getReturnBill().copy(getBill());

getReturnBill().setPaymentMethod(returnPaymentMethod);
getReturnBill().setBillType(BillType.PharmacyPre);
//getReturnBill().setBillTypeAtomic(BillTypeAtomic.PHARMACY_RETAIL_SALE_RETURN_ITEMS_AND_PAYMENTS);
getReturnBill().setBillTypeAtomic(BillTypeAtomic.PHARMACY_RETAIL_SALE_RETURN_ITEMS_AND_PAYMENTS_PREBILL);

getReturnBill().setBilledBill(getBill());

Expand All @@ -181,8 +196,10 @@ private void savePreReturnBill() {
getReturnBill().setInstitution(getSessionController().getInstitution());
getReturnBill().setDepartment(getSessionController().getDepartment());

getReturnBill().setInsId(getBillNumberBean().institutionBillNumberGenerator(getSessionController().getInstitution(), BillType.PharmacyPre, BillClassType.RefundBill, BillNumberSuffix.PHRET));
getReturnBill().setDeptId(getBillNumberBean().institutionBillNumberGenerator(getSessionController().getDepartment(), BillType.PharmacyPre, BillClassType.RefundBill, BillNumberSuffix.PHRET));
String deptId = getBillNumberBean().departmentBillNumberGeneratorYearly(getSessionController().getDepartment(), BillTypeAtomic.PHARMACY_RETAIL_SALE_RETURN_ITEMS_AND_PAYMENTS_PREBILL);

getReturnBill().setInsId(deptId);
getReturnBill().setDeptId(deptId);

if (getReturnBill().getId() == null) {
getBillFacade().create(getReturnBill());
Expand All @@ -193,9 +210,11 @@ private void savePreReturnBill() {
private Bill saveSaleReturnBill() {
RefundBill refundBill = new RefundBill();
refundBill.copy(getReturnBill());
refundBill.setPaymentMethod(returnPaymentMethod);
refundBill.setBillType(BillType.PharmacySale);
refundBill.setReferenceBill(getReturnBill());
refundBill.setBillTypeAtomic(BillTypeAtomic.PHARMACY_RETAIL_SALE_RETURN_ITEMS_AND_PAYMENTS);

refundBill.setReferenceBill(getReturnBill());
refundBill.setTotal(getReturnBill().getTotal());
refundBill.setNetTotal(getReturnBill().getTotal());

Expand All @@ -205,12 +224,12 @@ private Bill saveSaleReturnBill() {
refundBill.setInstitution(getSessionController().getInstitution());
refundBill.setDepartment(getSessionController().getDepartment());
refundBill.setComments(returnBillcomment);
refundBill.setBillTypeAtomic(BillTypeAtomic.PHARMACY_RETAIL_SALE_RETURN_ITEMS_AND_PAYMENTS);

String deptId = billNumberBean.departmentBillNumberGeneratorYearly(sessionController.getDepartment(), BillTypeAtomic.PHARMACY_RETAIL_SALE_RETURN_ITEMS_AND_PAYMENTS);
// refundBill.setInsId(getBillNumberBean().institutionBillNumberGenerator(
// getSessionController().getInstitution(), new RefundBill(), BillType.PharmacySale, BillNumberSuffix.SALRET));
refundBill.setInsId(getReturnBill().getInsId());
refundBill.setDeptId(getReturnBill().getDeptId());
refundBill.setInsId(deptId);
refundBill.setDeptId(deptId);
refundBill.setBillTime(new Date());

if (refundBill.getId() == null) {
Expand Down Expand Up @@ -253,7 +272,7 @@ private void savePreComponent() {
getBillItemFacade().edit(i);

// getPharmaceuticalBillItemFacade().edit(i.getPharmaceuticalBillItem());
getPharmacyBean().addToStock(tmpPh.getStock(), Math.abs(tmpPh.getQtyInUnit()), tmpPh, getSessionController().getDepartment());
getPharmacyBean().addToStock(tmpPh.getStock(), Math.abs(tmpPh.getQty()), tmpPh, getSessionController().getDepartment());

// i.getBillItem().getTmpReferenceBillItem().getPharmaceuticalBillItem().setRemainingQty(i.getRemainingQty() - i.getQty());
// getPharmaceuticalBillItemFacade().edit(i.getBillItem().getTmpReferenceBillItem().getPharmaceuticalBillItem());
Expand Down Expand Up @@ -410,12 +429,21 @@ public void settle() {
JsfUtil.addErrorMessage("Total is Zero cant' return");
return;
}
if (getReturnBillcomment() == null || getReturnBillcomment() .trim().equals("")) {

if (getReturnBillcomment() == null || getReturnBillcomment().trim().equals("")) {
JsfUtil.addErrorMessage("Please enter a comment");
return;
}

if (returnPaymentMethod == null) {
JsfUtil.addErrorMessage("Please select a payment method to return");
return;
}
if (returnPaymentMethod == PaymentMethod.MultiplePaymentMethods) {
JsfUtil.addErrorMessage("Multiple Payment Methods NOT allowed. Please select another payment method to return");
return;
}

savePreReturnBill();
savePreComponent();

Expand All @@ -432,9 +460,6 @@ public void settle() {
getReturnBill().getReturnCashBills().add(b);
getBillFacade().edit(getReturnBill());

WebUser wb = getCashTransactionBean().saveBillCashOutTransaction(getReturnBill(), getSessionController().getLoggedUser());
getSessionController().setLoggedUser(wb);

printPreview = true;
JsfUtil.addSuccessMessage("Successfully Returned");
returnBillcomment = null;
Expand All @@ -451,13 +476,13 @@ public void settle() {
}

}
public void fillReturningQty(){
if(billItems == null || billItems.isEmpty()){

public void fillReturningQty() {
if (billItems == null || billItems.isEmpty()) {
JsfUtil.addErrorMessage("Please add bill items");
return;
}
for(BillItem bi:billItems){
for (BillItem bi : billItems) {
bi.setQty(bi.getPharmaceuticalBillItem().getQty());
onEdit(bi);
}
Expand Down Expand Up @@ -496,7 +521,7 @@ public void generateBillComponent() {
//System.err.println("Refund " + rFund);
// //System.err.println("Cancelled "+rCacnelled);
// //System.err.println("Net "+(rBilled-rCacnelled));
tmp.setQtyInUnit((double) (Math.abs(i.getQty()) - Math.abs(rFund)));
tmp.setQty((double) (Math.abs(i.getQty()) - Math.abs(rFund)));

bi.setPharmaceuticalBillItem(tmp);

Expand Down
Loading

0 comments on commit 709bcf4

Please sign in to comment.