|
|
@@ -0,0 +1,97 @@ |
|
|
|
package com.ffii.tsms.modules.common.service |
|
|
|
|
|
|
|
import com.ffii.tsms.modules.project.entity.Project |
|
|
|
import org.apache.poi.ss.usermodel.Cell |
|
|
|
import org.apache.poi.ss.usermodel.CellStyle |
|
|
|
import org.apache.poi.ss.usermodel.HorizontalAlignment |
|
|
|
import org.apache.poi.ss.usermodel.Row |
|
|
|
import org.apache.poi.ss.usermodel.Sheet |
|
|
|
import org.apache.poi.ss.usermodel.Workbook |
|
|
|
import org.apache.poi.ss.util.CellRangeAddress |
|
|
|
import org.apache.poi.ss.util.CellUtil |
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook |
|
|
|
import org.springframework.core.io.ClassPathResource |
|
|
|
import org.springframework.stereotype.Service |
|
|
|
import java.io.ByteArrayOutputStream |
|
|
|
import java.io.IOException |
|
|
|
import java.time.LocalDate |
|
|
|
import java.time.LocalDateTime |
|
|
|
import java.time.format.DateTimeFormatter |
|
|
|
|
|
|
|
@Service |
|
|
|
open class ExcelReportService { |
|
|
|
private val DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd") |
|
|
|
private val FORMATTED_TODAY = LocalDate.now().format(DATE_FORMATTER) |
|
|
|
|
|
|
|
private val EX02_PROJECT_CASH_FLOW_REPORT = "templates/report/EX02_Project Cash Flow Report.xlsx" |
|
|
|
|
|
|
|
// ==============================|| GENERATE REPORT ||============================== // |
|
|
|
@Throws(IOException::class) |
|
|
|
fun generateEX02ProjectCashFlowReport(project: Project): ByteArray { |
|
|
|
// Generate the Excel report with query results |
|
|
|
val workbook: Workbook = createEX02ProjectCashFlowReport(project, EX02_PROJECT_CASH_FLOW_REPORT) |
|
|
|
|
|
|
|
// Write the workbook to a ByteArrayOutputStream |
|
|
|
val outputStream: ByteArrayOutputStream = ByteArrayOutputStream() |
|
|
|
workbook.write(outputStream) |
|
|
|
workbook.close() |
|
|
|
|
|
|
|
return outputStream.toByteArray() |
|
|
|
} |
|
|
|
|
|
|
|
// ==============================|| CREATE REPORT ||============================== // |
|
|
|
@Throws(IOException::class) |
|
|
|
private fun createEX02ProjectCashFlowReport( |
|
|
|
project: Project, |
|
|
|
templatePath: String, |
|
|
|
): Workbook { |
|
|
|
// please create a new function for each report template |
|
|
|
val resource = ClassPathResource(templatePath) |
|
|
|
val templateInputStream = resource.inputStream |
|
|
|
val workbook: Workbook = XSSFWorkbook(templateInputStream) |
|
|
|
|
|
|
|
val sheet: Sheet = workbook.getSheetAt(0) |
|
|
|
|
|
|
|
// val alignLeftStyle: CellStyle = workbook.createCellStyle().apply { |
|
|
|
// alignment = HorizontalAlignment.LEFT // Set the alignment to left |
|
|
|
// } |
|
|
|
// |
|
|
|
// val alignRightStyle: CellStyle = workbook.createCellStyle().apply { |
|
|
|
// alignment = HorizontalAlignment.RIGHT // Set the alignment to right |
|
|
|
// } |
|
|
|
|
|
|
|
var rowIndex = 1 // Assuming the location is in (1,2), which is the report date field |
|
|
|
var columnIndex = 2 |
|
|
|
sheet.getRow(rowIndex).getCell(columnIndex).apply { |
|
|
|
setCellValue(FORMATTED_TODAY) |
|
|
|
} |
|
|
|
|
|
|
|
rowIndex = 2 |
|
|
|
sheet.getRow(rowIndex).getCell(columnIndex).apply { |
|
|
|
setCellValue(project.code) |
|
|
|
} |
|
|
|
|
|
|
|
rowIndex = 3 |
|
|
|
sheet.getRow(rowIndex).getCell(columnIndex).apply { |
|
|
|
setCellValue(project.name) |
|
|
|
} |
|
|
|
|
|
|
|
rowIndex = 4 |
|
|
|
sheet.getRow(rowIndex).getCell(columnIndex).apply { |
|
|
|
setCellValue(if (project.customer?.name == null) "N/A" else project.customer?.name) |
|
|
|
} |
|
|
|
|
|
|
|
rowIndex = 5 |
|
|
|
sheet.getRow(rowIndex).getCell(columnIndex).apply { |
|
|
|
setCellValue(if (project.teamLead?.team?.name == null) "N/A" else project.teamLead?.team?.name) |
|
|
|
} |
|
|
|
|
|
|
|
rowIndex = 9 |
|
|
|
sheet.getRow(rowIndex).apply { |
|
|
|
getCell(1).setCellValue(project.expectedTotalFee!! * 0.8) |
|
|
|
getCell(2).setCellValue(project.expectedTotalFee!!) |
|
|
|
} |
|
|
|
|
|
|
|
return workbook |
|
|
|
} |
|
|
|
} |