Skip to content

Commit

Permalink
Merge pull request #9860 from hmislk/#9851_Fix_Report_Format_Excel_Do…
Browse files Browse the repository at this point in the history
…wnload

#9851 fix report format excel download Bug  Closes #9851
  • Loading branch information
Irani96 authored Dec 26, 2024
2 parents 6e4d03a + ecbc69e commit 2c832b4
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 32 deletions.
123 changes: 121 additions & 2 deletions src/main/java/com/divudi/bean/common/ReportsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public class ReportsController implements Serializable {
Map<Integer, Map<String, Map<Integer, Double>>> weeklyDailyBillItemMap7to1;
Map<Integer, Map<String, Map<Integer, Double>>> weeklyDailyBillItemMap1to7;


private boolean showChart;

public String getDischargedStatus() {
Expand Down Expand Up @@ -3506,7 +3507,7 @@ private void groupBills() {
bundle.setGroupedBillItemsByInstitution(billMap);
}

public Double calculateNetTotalByBills(List<Bill> bills) {
public Double calculateNetAmountSubTotalByBills(List<Bill> bills) {
Double netTotal = 0.0;

for (Bill bill : bills) {
Expand All @@ -3516,7 +3517,7 @@ public Double calculateNetTotalByBills(List<Bill> bills) {
return netTotal;
}

public Double calculateDiscountByBills(List<Bill> bills) {
public Double calculateDiscountSubTotalByBills(List<Bill> bills) {
Double discount = 0.0;

for (Bill bill : bills) {
Expand Down Expand Up @@ -3632,6 +3633,111 @@ public Double calculateDueAmountNetTotal() {
return dueAmountNetTotal;
}

public Double calculateNetAmountNetTotal() {
double netAmountNetTotal = 0.0;
Map<Institution, List<Bill>> billMap = bundle.getGroupedBillItemsByInstitution();

for (Map.Entry<Institution, List<Bill>> entry : billMap.entrySet()) {
List<Bill> bills = entry.getValue();

netAmountNetTotal += calculateNetAmountSubTotalByBills(bills);
}

return netAmountNetTotal;
}

public Double calculateGrossAmountSubTotalByBills(List<Bill> bills) {
Double billTotal = 0.0;

for (Bill bill : bills) {
billTotal += bill.getBillTotal();
}

return billTotal;
}

public Double calculatePatientShareSubTotalByBills(List<Bill> bills) {
Double settledAmountByPatient = 0.0;

for (Bill bill : bills) {
settledAmountByPatient += bill.getSettledAmountByPatient();
}

return settledAmountByPatient;
}

public Double calculateSponsorShareSubTotalByBills(List<Bill> bills) {
Double settledAmountBySponsor = 0.0;

for (Bill bill : bills) {
settledAmountBySponsor += bill.getSettledAmountBySponsor();
}

return settledAmountBySponsor;
}

public Double calculateDueAmountSubTotalByBills(List<Bill> bills) {
Double balance = 0.0;

for (Bill bill : bills) {
balance += bill.getBalance();
}

return balance;
}

public Double calculateGrossAmountNetTotal() {
double grossAmountNetTotal = 0.0;
Map<Institution, List<Bill>> billMap = bundle.getGroupedBillItemsByInstitution();

for (Map.Entry<Institution, List<Bill>> entry : billMap.entrySet()) {
List<Bill> bills = entry.getValue();

grossAmountNetTotal += calculateGrossAmountSubTotalByBills(bills);
}

return grossAmountNetTotal;
}

public Double calculateDiscountNetTotal() {
double discountNetTotal = 0.0;
Map<Institution, List<Bill>> billMap = bundle.getGroupedBillItemsByInstitution();

for (Map.Entry<Institution, List<Bill>> entry : billMap.entrySet()) {
List<Bill> bills = entry.getValue();

discountNetTotal += calculateDiscountSubTotalByBills(bills);
}

return discountNetTotal;
}

public Double calculatePatientShareNetTotal() {
double patientShareNetTotal = 0.0;
Map<Institution, List<Bill>> billMap = bundle.getGroupedBillItemsByInstitution();

for (Map.Entry<Institution, List<Bill>> entry : billMap.entrySet()) {
List<Bill> bills = entry.getValue();

patientShareNetTotal += calculatePatientShareSubTotalByBills(bills);
}

return patientShareNetTotal;
}

public Double calculateDueAmountNetTotal() {
double dueAmountNetTotal = 0.0;
Map<Institution, List<Bill>> billMap = bundle.getGroupedBillItemsByInstitution();

for (Map.Entry<Institution, List<Bill>> entry : billMap.entrySet()) {
List<Bill> bills = entry.getValue();

dueAmountNetTotal += calculateDueAmountSubTotalByBills(bills);
}

return dueAmountNetTotal;
}

public Double calculateSponsorShareNetTotal() {
double sponsorShareNetTotal = 0.0;
Map<Institution, List<Bill>> billMap = bundle.getGroupedBillItemsByInstitution();
Expand All @@ -3644,6 +3750,19 @@ public Double calculateSponsorShareNetTotal() {

return sponsorShareNetTotal;
}

public Double calculateNetAmountNetTotal() {
double netAmountNetTotal = 0.0;
Map<Institution, List<Bill>> billMap = bundle.getGroupedBillItemsByInstitution();

for (Map.Entry<Institution, List<Bill>> entry : billMap.entrySet()) {
List<Bill> bills = entry.getValue();

netAmountNetTotal += calculateNetAmountSubTotalByBills(bills);
}

return sponsorShareNetTotal;
}

public Double calculateNetAmountSubTotalByBills(List<Bill> bills) {
Double netTotal = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@
import com.divudi.data.CssVerticalAlign;
import com.divudi.facade.ReportItemFacade;
import com.divudi.java.CommonFunctions;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import static org.apache.poi.ss.usermodel.CellType.FORMULA;
import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.CellType.STRING;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
*
Expand Down Expand Up @@ -78,6 +82,7 @@ public class LabReportExportImportController implements Serializable {
private List<InvestigationItem> uploadedSuccessItems;
private List<InvestigationItem> uploadedRejectedItems;
private UploadedFile file;
private StreamedContent downloadingExcel;

// </editor-fold>

Expand Down Expand Up @@ -133,20 +138,26 @@ public List<InvestigationItem> listInvestigationItemsFilteredByItemTypes(Investi
return tis;
}

public void export() throws IOException {
public StreamedContent export(){
if (getCurrent() == null) {
JsfUtil.addErrorMessage("Pleace select Investigations ");
return;
return null;
}
List<InvestigationItem> items = investigationItemController.getUserChangableItems();

if (items == null) {
JsfUtil.addErrorMessage("This Investigation has no InvestigationItem");
return;
return null;
}

exportInvestigationItemsToExcel(items);
JsfUtil.addSuccessMessage("Successfuly Uploaed !");

try {
downloadingExcel = exportInvestigationItemsToExcel(items);
} catch (IOException e) {
// Handle IOException
}
JsfUtil.addSuccessMessage("Successfuly Download !");
return downloadingExcel;

}

public void importFormat() {
Expand Down Expand Up @@ -193,19 +204,21 @@ private String defaultIfNullOrEmpty(String value, String defaultValue) {
return (value == null || value.trim().isEmpty()) ? defaultValue : value;
}

public void exportInvestigationItemsToExcel(List<InvestigationItem> investigationItems) throws IOException {
public StreamedContent exportInvestigationItemsToExcel(List<InvestigationItem> investigationItems) throws IOException {
// Define a predefined directory (e.g., user's desktop)
System.out.println("Investigation Items = " + investigationItems.size());
String userHome = System.getProperty("user.home");
String defaultDirectory = userHome + File.separator + "Desktop"; // Change to any other directory if needed
String fileName = getCurrent().getName() + ".xlsx";
String fullFilePath = defaultDirectory + File.separator + fileName;
// String userHome = System.getProperty("user.home");
// String defaultDirectory = userHome + File.separator + "Desktop"; // Change to any other directory if needed
// String fullFilePath = defaultDirectory + File.separator + fileName;
//
// // Ensure the directory exists
// File file = new File(fullFilePath);
// file.getParentFile().mkdirs(); // Create parent directories if they don't exist

// Ensure the directory exists
File file = new File(fullFilePath);
file.getParentFile().mkdirs(); // Create parent directories if they don't exist
StreamedContent excelSc;

// Create a new workbook and a sheet

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("InvestigationItems");

Expand Down Expand Up @@ -262,17 +275,21 @@ public void exportInvestigationItemsToExcel(List<InvestigationItem> investigatio
sheet.autoSizeColumn(i);
}

// Write the workbook to the default file path
System.out.println("Attempting to write to: " + fullFilePath);
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
System.out.println("Excel file created successfully at " + fullFilePath);
} catch (IOException e) {
System.out.println("Error writing Excel file: " + e.getMessage());
e.printStackTrace();
} finally {
workbook.close();
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
workbook.close();

InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

String fileName = getCurrent().getName() + ".xlsx";
// Set the downloading file
excelSc = DefaultStreamedContent.builder()
.name(fileName)
.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
.stream(() -> inputStream)
.build();

return excelSc;
}

// </editor-fold>
Expand Down Expand Up @@ -762,4 +779,12 @@ public void setDataUploadController(DataUploadController dataUploadController) {
}
// </editor-fold>

public StreamedContent getDownloadingExcel() {
return downloadingExcel;
}

public void setDownloadingExcel(StreamedContent downloadingExcel) {
this.downloadingExcel = downloadingExcel;
}

}
10 changes: 7 additions & 3 deletions src/main/webapp/admin/lims/export.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<ui:composition template="/resources/template/template.xhtml">
<ui:define name="content">
<h:form id="form" >
<p:growl />

<p:panel header="Format Expoet" styleClass="w-100 p-0 m-1" >
<f:facet name="header">
<div class="d-flex justify-content-between">
Expand All @@ -30,15 +32,17 @@
<div class="d-flex col-5 gap-4" style="font-size: 18px; font-weight: 700">
<h:outputText value="Current Investigation" class="mt-2"/>
<h:outputText value="-" class="mt-2"/>
<h:outputText value="#{investigationItemController.currentInvestigation.name}" class="mt-2 text-dark"/>
<h:outputText value="#{labReportExportImportController.current.name}" class="mt-2 text-dark"/>
</div>
<p:commandButton
ajax="false"
style="float: right; width: 200px;"
icon="fas fa-file-export export-icon"
class="ui-button-warning"
action="#{labReportExportImportController.export()}"
value="Export" >
value="Export"
onclick="PrimeFaces.monitorDownload(start, stop);" >
<p:fileDownload value="#{labReportExportImportController.export()}" />

</p:commandButton>
</div>

Expand Down
18 changes: 16 additions & 2 deletions src/main/webapp/admin/lims/import_report_format.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@

<p:growl />

<p:panel header="Upload Lab Report Format" >
<p:panel >
<f:facet name="header">
<h:form >
<h:outputText value="Upload Lab Report Format" class="mt-2"/>

<p:commandButton
class="ui-button-info"
ajax="false"
style="float: right;"
action="#{labReportExportImportController.makeNull()}"
icon="fa fa-plus"
value="New Format Upload" >
</p:commandButton>
</h:form>
</f:facet>
<h:form enctype="multipart/form-data" >
<div class="row ">
<div class="col-1">
Expand Down Expand Up @@ -70,7 +84,7 @@
<f:facet name="header">
<h:outputText value="Successfully Upload Items" class="mt-2"/>
</f:facet>

<div class="row mb-1" style="font-weight: 700; font-size: 16px;">
<div class="col-3"><p:outputLabel value="Item Name" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="Item Type" ></p:outputLabel></div>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/admin/lims/investigation.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@
icon="fas fa-file-export export-icon"
action="#{investigationController.navigateExportReoirtFormat()}"
value="Export Format" >
<f:setPropertyActionListener value="#{investigationController.current}" target="#{labReportExportImportController.current}"/>
</p:commandButton>

<p:commandButton
Expand Down

0 comments on commit 2c832b4

Please sign in to comment.