diff --git a/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt b/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt index f69a6e5..b95d882 100644 --- a/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt +++ b/src/main/java/com/ffii/tsms/modules/project/service/InvoiceService.kt @@ -96,7 +96,7 @@ open class InvoiceService( open fun getMilestonePaymentWithProjectCode(code: List): List> { val sql = StringBuilder("select " - + " p.code, mp.* " + + " p.code, mp.*, m.taskGroupId " + " from milestone_payment mp " + " left join milestone m on mp.milestoneId = m.id " + " left join project p on p.id = m.projectId " @@ -108,6 +108,20 @@ open class InvoiceService( return jdbcDao.queryForList(sql.toString(), args) } + open fun getMilestonePaymentId(code: String, paymentMilestone: String): Long{ + val sql = StringBuilder(" select" + + " mp.id as milestonePaymentId" + + " from milestone_payment mp" + + " left join milestone m on mp.milestoneId = m.id" + + " left join project p on p.id = m.projectId" + + " where p.deleted = false" + + " and p.code = :code" + + " and mp.description = :description " + ) + val args = mapOf("code" to code, "description" to paymentMilestone) + return jdbcDao.queryForInt(sql.toString(), args).toLong() + } + open fun getInvoiceByInvoiceNo(invoiceNo: String): Invoice { return invoiceRepository.findByInvoiceNo(invoiceNo) } @@ -227,17 +241,18 @@ open class InvoiceService( /** * @return true when cellValue Object exist in DB */ - fun checkStringExists(list: List>, cellValue: Map): Boolean { + private fun checkStringExists(list: List>, cellValue: Map): Boolean { // println("LIST-------------: $list") // println("CELL VALUE-------------: $cellValue") -// println(list.contains(cellValue)) -// println(list.any { it["code"] == cellValue["code"] && it["description"] == cellValue["description"] }) -// return list.contains(cellValue) - return list.any { it["code"] == cellValue["code"] && it["description"] == cellValue["description"] } +// println(list.any { it["code"] == cellValue["code"] && it["description"] == cellValue["description"] && it["groupTaskId"] == cellValue["stage"]}) + + return list.any { it["code"] == cellValue["code"] && it["description"] == cellValue["description"] && it["groupTaskId"] == cellValue["stage"]} } - open fun checkMilestonePayment( + + open fun checkMilestonePaymentByStageAndDescription( sheet: Sheet, startingRow: Int, + stageColumnIndex: Int, columnIndex: Int, invoiceColumnIndex: Int, projectCodeColumnIndex: Int, @@ -250,15 +265,18 @@ open class InvoiceService( val milestonePaymentCell = row?.getCell(columnIndex) val invoiceNoCell = row?.getCell(invoiceColumnIndex) val projectCodeCell = row?.getCell(projectCodeColumnIndex) + val stageCell = row?.getCell(stageColumnIndex) if (milestonePaymentCell != null && milestonePaymentCell.cellType == CellType.STRING && invoiceNoCell != null && invoiceNoCell.cellType == CellType.STRING && - projectCodeCell != null && projectCodeCell.cellType == CellType.STRING) + projectCodeCell != null && projectCodeCell.cellType == CellType.STRING && + stageCell != null && stageCell.cellType == CellType.NUMERIC) { val milestonePaymentCellValue = milestonePaymentCell.stringCellValue val invoiceNoCellValue = invoiceNoCell.stringCellValue val projectCodeCellValue = projectCodeCell.stringCellValue + val stageCellValue = stageCell.numericCellValue.toInt() - val cellValue = mapOf("code" to projectCodeCellValue, "description" to milestonePaymentCellValue) + val cellValue = mapOf("code" to projectCodeCellValue, "description" to milestonePaymentCellValue, "groupTaskId" to stageCellValue) if(!checkStringExists(paymentMilestoneWithCode, cellValue)) { if(!nonExistMilestone.contains(mapOf("paymentMilestone" to milestonePaymentCellValue, "invoiceNo" to invoiceNoCellValue))){ @@ -359,7 +377,7 @@ open class InvoiceService( val milestonepaymentWithCode = getMilestonePaymentWithProjectCode(projectsCodes) // println("newProjectCodes == 0") // println(checkMilestonePayment(sheet, 2, 5, 0, 1, milestonepaymentWithCode)) - val paymenMilestones = checkMilestonePayment(sheet, 2, 5, 0, 1, milestonepaymentWithCode) + val paymenMilestones = checkMilestonePaymentByStageAndDescription(sheet, 2, 4,5, 0, 1, milestonepaymentWithCode) if (paymenMilestones.isNotEmpty()){ return InvoiceResponse(false, "Imported Invoice's format is incorrect", newProjectCodes, emptyRowList, invoicesResult, duplicateItemsInInvoice, paymenMilestones) } @@ -376,6 +394,9 @@ open class InvoiceService( } for (i in 2..sheet.lastRowNum){ + val paymentMilestoneId = getMilestonePaymentId(ExcelUtils.getCell(sheet, i, 1).stringCellValue, ExcelUtils.getCell(sheet, i, 5).stringCellValue) + println("paymentMilestoneId--------------: $paymentMilestoneId") + val milestonePayment = milestonePaymentRepository.findById(paymentMilestoneId).orElseThrow() val invoice = Invoice().apply { invoiceNo = ExcelUtils.getCell(sheet, i, 0).stringCellValue projectCode = ExcelUtils.getCell(sheet, i, 1).stringCellValue @@ -390,6 +411,7 @@ open class InvoiceService( invoiceDate = ExcelUtils.getCell(sheet, i, 10).dateCellValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() dueDate = ExcelUtils.getCell(sheet, i, 11).dateCellValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() issueAmount = ExcelUtils.getCell(sheet, i, 12).numericCellValue.toBigDecimal() + this.milestonePayment = milestonePayment } saveAndFlush(invoice) }