From 7d4c4054ea67e3668f0fb2e41e0815b22ef3b51a Mon Sep 17 00:00:00 2001 From: "MSI\\2Fi" Date: Thu, 19 Sep 2024 11:07:57 +0800 Subject: [PATCH] Add project Expense to Project Cash Flow Report --- .../modules/report/service/ReportService.kt | 102 +++++++++++++++++- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt b/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt index d26f681..650c151 100644 --- a/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt +++ b/src/main/java/com/ffii/tsms/modules/report/service/ReportService.kt @@ -856,7 +856,7 @@ open class ReportService( val dateFormatter = if (dateType == "Date") DateTimeFormatter.ofPattern("yyyy/MM/dd") else DateTimeFormatter.ofPattern("MMM YYYY") val combinedResults = - (invoices.map { it.receiptDate } + timesheets.map { it.recordDate }).filterNotNull().sortedBy { it } + (invoices.map { it.receiptDate } + timesheets.map { it.recordDate } + projectExpenses.map{ it.issueDate }).filterNotNull().sortedBy { it } .map { it.format(dateFormatter) }.distinct() val groupedTimesheets = timesheets.sortedBy { it.recordDate } .groupBy { timesheetEntry -> timesheetEntry.recordDate?.format(dateFormatter).toString() } @@ -902,8 +902,8 @@ open class ReportService( ) } } -// println("grouped Project Expense") -// println(groupedProjectExpense) + println("grouped Project Expense") + println(groupedProjectExpense) // groupedTimesheets.entries.forEach { (key, value) -> // logger.info("key: $key") // logger.info("value: " + value.sumOf { it }) @@ -1060,6 +1060,102 @@ open class ReportService( } } + if (groupedProjectExpense.containsKey(result)) { + if (dateType == "Date") { + groupedProjectExpense[result]!!.forEachIndexed { _, expense -> + sheet.createRow(rowIndex++).apply { + createCell(0).apply { + setCellValue(result) + } + + createCell(1).apply { + setCellValue(0.0) + cellStyle.dataFormat = accountingStyle + } + + createCell(2).apply { + setCellValue(expense["projectExpense"] as Double? ?: 0.0) + cellStyle.dataFormat = accountingStyle + } + + createCell(3).apply { + setCellValue(0.0) + cellStyle.dataFormat = accountingStyle + } + + createCell(4).apply { + val lastRow = rowIndex - 1 + if (lastRow == 16) { + cellFormula = + "C{currentRow}-B{currentRow}".replace("{currentRow}", rowIndex.toString()) + } else { + cellFormula = + "IF(B{currentRow}>0,E{lastRow}-B{currentRow}-C{currentRow},E{lastRow}+D{currentRow}-C{currentRow})".replace( + "{currentRow}", + rowIndex.toString() + ).replace("{lastRow}", lastRow.toString()) + } + cellStyle.dataFormat = accountingStyle + } + + createCell(5)?.apply { + setCellValue( + "Expense No.: " + (expense["expenseNo"] as String? ?: "N/A").toString() + ) + } + } + } + } else { +// groupedInvoices[result]!!.forEachIndexed { _, invoice -> + sheet.createRow(rowIndex++).apply { + createCell(0).apply { + setCellValue(result) + } + + createCell(1).apply { + setCellValue(0.0) + cellStyle.dataFormat = accountingStyle + } + + createCell(2).apply { + setCellValue(groupedProjectExpense[result]?.sumOf { it["projectExpense"] as Double } ?: 0.0) + cellStyle.dataFormat = accountingStyle + } + + createCell(3).apply { + setCellValue(0.0) + cellStyle.dataFormat = accountingStyle + } + + createCell(4).apply { + val lastRow = rowIndex - 1 + if (lastRow == 16) { + cellFormula = + "C{currentRow}-B{currentRow}".replace("{currentRow}", rowIndex.toString()) + } else { + cellFormula = + "IF(B{currentRow}>0,E{lastRow}-B{currentRow}-C{currentRow},E{lastRow}+D{currentRow}-C{currentRow})".replace( + "{currentRow}", + rowIndex.toString() + ).replace("{lastRow}", lastRow.toString()) + } + cellStyle.dataFormat = accountingStyle + } + + createCell(5)?.apply { +// setCellValue(invoice["description"].toString()) + val expenseNos = groupedProjectExpense[result]?.map { it["expenseNo"] } + if (expenseNos?.size != null && expenseNos.size > 1) { +// setCellValue("Invoice Receipt: " + (groupedInvoices[result]?.map { it["invoiceNo"] }?.joinToString() ?: "N/A").toString()) + setCellValue("Multiple Expenses") + } else { + setCellValue("Expense No.: " + (expenseNos?.joinToString() ?: "N/A").toString()) + } + } +// } + } + } + } }