Skip to content

Commit

Permalink
Merge pull request #9837 from hmislk/Issue#9783_Consumption_report
Browse files Browse the repository at this point in the history
Issue#9783 consumption report  Closes #9783
  • Loading branch information
Irani96 authored Dec 24, 2024
2 parents 56a64cb + 3c5cb43 commit ba17786
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 63 deletions.
194 changes: 163 additions & 31 deletions src/main/java/com/divudi/bean/pharmacy/PharmacyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.divudi.bean.common.util.JsfUtil;
import com.divudi.data.BillClassType;
import com.divudi.data.BillTypeAtomic;
import com.divudi.data.DepartmentCategoryWiseItems;
import com.divudi.data.PaymentMethod;
import com.divudi.data.dataStructure.CategoryWithItem;
import com.divudi.data.dataStructure.PharmacySummery;
Expand Down Expand Up @@ -737,6 +738,7 @@ public void deleteMultipleAmpps() {
private List<BillItem> billItems;
private List<PharmacySummery> departmentSummaries;
private List<CategoryWithItem> issueDepartmentCategoryWiseItems;
private List<DepartmentCategoryWiseItems> resultsList;

private String transferType;
private Institution FromSite;
Expand Down Expand Up @@ -910,42 +912,33 @@ public void generateGRNReportTable() {
}

public void createConsumptionReportTable() {
resetFields();

if ("byBillItem".equals(reportType)) {
switch (reportType) {
case "byBillItem":
generateConsumptionReportTableByBillItems(BillType.PharmacyIssue);
break;

bills = null;
departmentSummaries = null;
issueDepartmentCategoryWiseItems = null;
generateConsumptionReportTableByBillItems(BillType.PharmacyIssue);

} else if ("byBill".equals(reportType)) {

billItems = null;
departmentSummaries = null;
issueDepartmentCategoryWiseItems = null;
generateConsumptionReportTableByBill(BillType.PharmacyIssue);
case "byBill":
generateConsumptionReportTableByBill(BillType.PharmacyIssue);
break;

} else if ("summeryReport".equals(reportType)) {

bills = null;
billItems = null;
issueDepartmentCategoryWiseItems = null;
generateConsumptionReportTableAsSummary(BillType.PharmacyIssue);

} else if ("categoryWise".equals(reportType)) {

bills = null;
billItems = null;
generateConsumptionReportTableByDepartmentAndCategoryWise();

} else {
bills = null;
billItems = null;
departmentSummaries = null;
generateConsumptionReportTableByDepartmentAndCategoryWise();
case "summeryReport":
case "categoryWise":
generateConsumptionReportTableByDepartmentAndCategoryWise(BillType.PharmacyIssue);
break;

default:
throw new IllegalArgumentException("Invalid report type: " + reportType);
}
}

private void resetFields() {
bills = null;
billItems = null;
departmentSummaries = null;
issueDepartmentCategoryWiseItems = null;
resultsList = null;
}

public void generateConsumptionReportTableByBill(BillType billType) {
Expand Down Expand Up @@ -993,7 +986,7 @@ public void generateConsumptionReportTableByBill(BillType billType) {
}
totalPurchase = 0.0;
for (Bill i : bills) {
totalPurchase += i.getNetTotal();
totalPurchase += i.getPaidAmount();
}

}
Expand Down Expand Up @@ -1057,6 +1050,7 @@ public void generateConsumptionReportTableByBillItems(BillType billType) {
}
}

@Deprecated
public void generateConsumptionReportTableAsSummary(BillType billType) {
// Initialize bill types
List<BillType> bt = new ArrayList<>();
Expand Down Expand Up @@ -1119,6 +1113,7 @@ public void generateConsumptionReportTableAsSummary(BillType billType) {
}
}

@Deprecated
public void generateConsumptionReportTableByDepartmentAndCategoryWise() {
totalPurchase = 0.0;
grantIssueQty = 0.0;
Expand Down Expand Up @@ -1227,6 +1222,135 @@ public void generateConsumptionReportTableByDepartmentAndCategoryWise() {
}
}

public List<DepartmentCategoryWiseItems> generateConsumptionReportTableByDepartmentAndCategoryWise(BillType billType) {
Map<String, Object> parameters = new HashMap<>();
String jpql = "SELECT new com.divudi.data.DepartmentCategoryWiseItems("
+ "bi.bill.department, "
+ "bi.bill.toDepartment, "
+ "bi.item, "
+ "bi.item.category, "
+ "SUM(bi.qty * bi.pharmaceuticalBillItem.purchaseRate), "
+ "bi.pharmaceuticalBillItem.purchaseRate, "
+ "SUM(bi.qty)) "
+ "FROM BillItem bi "
+ "WHERE bi.retired = false AND bi.bill.retired = false "
+ "AND bi.bill.createdAt BETWEEN :fromDate AND :toDate "
+ "AND bi.bill.billType = :billType ";

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

// Dynamic filters
if (institution != null) {
jpql += "AND bi.bill.institution = :institution ";
parameters.put("institution", institution);
}

if (site != null) {
jpql += "AND bi.bill.department.site = :site ";
parameters.put("site", site);
}

if (dept != null) {
jpql += "AND bi.bill.department = :department ";
parameters.put("department", dept);
}

if (category != null) {
jpql += "AND bi.item.category = :category ";
parameters.put("category", category);
}

if (item != null) {
jpql += "AND bi.item = :item ";
parameters.put("item", item);
}

if (toDepartment != null) {
jpql += "AND bi.bill.toDepartment = :toDepartment ";
parameters.put("toDepartment", toDepartment);
}

// Group by clause
jpql += "GROUP BY bi.bill.department, bi.bill.toDepartment, bi.item, bi.item.category, bi.pharmaceuticalBillItem.purchaseRate "
+ "ORDER BY bi.bill.toDepartment, bi.item.category";

try {
resultsList = (List<DepartmentCategoryWiseItems>) getBillItemFacade().findLightsByJpql(jpql, parameters, TemporalType.TIMESTAMP);

if (resultsList.isEmpty()) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_WARN, "No Data", "No records found for the selected criteria."));
return Collections.emptyList();
}

totalPurchase = 0.0;
grantIssueQty = 0.0;
for (DepartmentCategoryWiseItems i : resultsList) {
totalPurchase += i.getNetTotal();
grantIssueQty += i.getQty();
}
if ("summeryReport".equals(reportType)) {
generateConsumptionReportTableAsDepartmentSummary(resultsList);
}
return resultsList;
} catch (Exception e) {
e.printStackTrace();
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Failed to generate report. Please try again."));
return Collections.emptyList();
}
}

public void generateConsumptionReportTableAsDepartmentSummary(List<DepartmentCategoryWiseItems> list) {
// Initialize department summaries and reset total sales value
departmentSummaries = new ArrayList<>();
totalSaleValue = 0.0;

// Create a map to store net totals grouped by main and consumption departments
Map<String, Map<String, Double>> departmentTotals = new HashMap<>();

// Populate the map with department-wise data
for (DepartmentCategoryWiseItems item : list) {
String mainDepartmentName = item.getMainDepartment().getName();
String consumptionDepartmentName = item.getConsumptionDepartment().getName();
double paidAmount = item.getNetTotal();

// Initialize nested maps if necessary
departmentTotals.putIfAbsent(mainDepartmentName, new HashMap<>());
Map<String, Double> consumptionMap = departmentTotals.get(mainDepartmentName);

// Accumulate paid amounts for consumption departments
consumptionMap.put(consumptionDepartmentName,
consumptionMap.getOrDefault(consumptionDepartmentName, 0.0) + paidAmount);

// Accumulate the total sale value for the main department
totalSaleValue += paidAmount;
}

// Build the summaries
for (Map.Entry<String, Map<String, Double>> mainEntry : departmentTotals.entrySet()) {
String mainDepartmentName = mainEntry.getKey();
Map<String, Double> consumptionMap = mainEntry.getValue();

// Add a header entry for the main department
departmentSummaries.add(new PharmacySummery(mainDepartmentName, null, 0.0)); // Main department header

// Add each consumption department's contribution
for (Map.Entry<String, Double> consumptionEntry : consumptionMap.entrySet()) {
String consumptionDepartmentName = consumptionEntry.getKey();
double netTotal = consumptionEntry.getValue();
departmentSummaries.add(new PharmacySummery(null, consumptionDepartmentName, netTotal));
}

// Add a total entry for the main department
double mainTotal = consumptionMap.values().stream().mapToDouble(Double::doubleValue).sum();
departmentSummaries.add(new PharmacySummery("Total", null, mainTotal));
}
}

public List<String1Value1> calculateTotals(List<Bill> billList) {
data = new ArrayList<>();

Expand Down Expand Up @@ -3748,4 +3872,12 @@ public void setToSite(Institution toSite) {
this.toSite = toSite;
}

public List<DepartmentCategoryWiseItems> getResultsList() {
return resultsList;
}

public void setResultsList(List<DepartmentCategoryWiseItems> resultsList) {
this.resultsList = resultsList;
}

}
112 changes: 112 additions & 0 deletions src/main/java/com/divudi/data/DepartmentCategoryWiseItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.divudi.data;

import com.divudi.entity.Category;
import com.divudi.entity.Department;
import com.divudi.entity.Item;

/**
*
* @author Pubudu Piyankara
*/
public class DepartmentCategoryWiseItems {


private Department mainDepartment;
private Department consumptionDepartment;
private Item item;
private Category category;
private Double netTotal;
private Double purchaseRate;
private double qty;
private double paidAmount;

public DepartmentCategoryWiseItems(){ }

public DepartmentCategoryWiseItems(
Department mainDepartment,
Department consumptionDepartment,
Item item,
Category category,
Double netTotal,
Double purchaseRate,
double qty
) {
this.mainDepartment = mainDepartment;
this.consumptionDepartment = consumptionDepartment;
this.item = item;
this.category = category;
this.netTotal = netTotal;
this.purchaseRate = purchaseRate;
this.qty = qty;
}

public Department getMainDepartment() {
return mainDepartment;
}

public void setMainDepartment(Department mainDepartment) {
this.mainDepartment = mainDepartment;
}

public Department getConsumptionDepartment() {
return consumptionDepartment;
}

public void setConsumptionDepartment(Department consumptionDepartment) {
this.consumptionDepartment = consumptionDepartment;
}

public Item getItem() {
return item;
}

public void setItem(Item item) {
this.item = item;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public Double getNetTotal() {
return netTotal;
}

public void setNetTotal(Double netTotal) {
this.netTotal = netTotal;
}

public Double getPurchaseRate() {
return purchaseRate;
}

public void setPurchaseRate(Double purchaseRate) {
this.purchaseRate = purchaseRate;
}

public double getQty() {
return qty;
}

public void setQty(double qty) {
this.qty = qty;
}

public double getPaidAmount() {
return paidAmount;
}

public void setPaidAmount(double paidAmount) {
this.paidAmount = paidAmount;
}


}
Loading

0 comments on commit ba17786

Please sign in to comment.