diff --git a/src/main/java/com/ffii/fpsms/modules/common/mail/service/MailTemplateService.kt b/src/main/java/com/ffii/fpsms/modules/common/mail/service/MailTemplateService.kt index f4348da..3261d7a 100644 --- a/src/main/java/com/ffii/fpsms/modules/common/mail/service/MailTemplateService.kt +++ b/src/main/java/com/ffii/fpsms/modules/common/mail/service/MailTemplateService.kt @@ -24,7 +24,15 @@ open class MailTemplateService( private val inventoryLotService: InventoryLotService, private val qcResultService: QcResultService ) { + data class MailTemplateHtml ( + val to: String = "", + val subject: String = "", + val content: String = "", + val filename: String = "", + ) + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") + fun allMailTemplates(): List { return mailTemplateRepository.findAllByDeletedIsFalse(); } @@ -80,8 +88,57 @@ open class MailTemplateService( mailTemplateRepository.saveAll(mailTemplates) } - fun getMailTemplateForStockInLine(stockInLineId: Long): DownloadMailTemplateResponse { - val emailTemplate = findByCode("RM-01") ?: throw NoSuchElementException("No RP-01 Email Template") + // ------------------------------------------- Stock In Line - Mail Template ------------------------------------------- // + data class MailTemplateForStockInLineArgs( + val supplierEmail: String = "", + val supplierName: String = "", + val dnNo: String = "", + val dnDate: String = "", + val poNo: String = "", + val supplierId: String = "", + val itemNo: String = "", + val itemName: String = "", + val itemQty: String = "", + val uom: String = "", + val planDnDate: String = "", + val unitPrice: String = "", + val receivedCompany: String = "", + val lotNo: String = "", + val qcResult: String = "", + val receivedQty: String = "", + val nonDelieveredQty: String = "", + val rejectedQty: String = "", + val qcDate: String = "" + ) { + fun applyToTemplate(template: String?): String { + val content = template ?: "N/A" + val args = mapOf( + "\${supplierName}" to supplierName, + "\${dnNo}" to dnNo, + "\${dnDate}" to dnDate, + "\${poNo}" to poNo, + "\${supplierId}" to supplierId, + "\${itemNo}" to itemNo, + "\${itemName}" to itemName, + "\${itemQty}" to itemQty, + "\${uom}" to uom, + "\${planDnDate}" to planDnDate, + "\${unitPrice}" to unitPrice, + "\${receivedCompany}" to receivedCompany, + "\${lotNo}" to lotNo, + "\${qcResult}" to qcResult, + "\${receivedQty}" to receivedQty, + "\${nonDelieveredQty}" to nonDelieveredQty, + "\${rejectedQty}" to rejectedQty, + "\${qcDate}" to qcDate + ) + return args.entries.fold(content) { acc, (key, value) -> + acc.replace(key, value) + } + } + } + + fun getMailTemplateArgsForStockInLine(stockInLineId: Long): MailTemplateForStockInLineArgs { val stockInLine = stockInLineRepository.findById(stockInLineId).getOrNull() ?: throw NoSuchElementException("Cant find this stock in line.") // Value @@ -90,7 +147,7 @@ open class MailTemplateService( val pol = stockInLine.purchaseOrderLine val item = stockInLine.item val supplierName = po?.supplier?.name ?: "N/A" - val supplierEmail = if((po?.supplier?.contactEmail).isNullOrEmpty()) "N/A" else po?.supplier?.contactEmail + val supplierEmail = (if((po?.supplier?.contactEmail).isNullOrEmpty()) "N/A" else po?.supplier?.contactEmail) ?: "N/A" val dnNo = stockInLine.dnNo ?: "N/A" val dnDate = formatter.format(stockInLine.dnDate) ?: "N/A" val poNo = po?.code ?: "N/A" @@ -120,50 +177,92 @@ open class MailTemplateService( val element = tempDoc.appendElement("ul") for (result in filteredResult) { element.appendElement("li") - .text("${result.name} - ${result.description}") + .text("${result.code} - ${result.name}") } tempDoc.outerHtml() } else { "N/A" } } ?: "N/A" - val rejectedQty = (stockInLine.acceptedQty ?: zero).minus(stockInLine.demandQty ?: zero).toString() // reject? - val acceptedQty = (stockInLine.acceptedQty ?: zero).toString() + val rejectedQty = (stockInLine.acceptedQty ?: zero).minus(stockInLine.demandQty ?: zero).toString() // = reject qty + val receivedQty = (stockInLine.acceptedQty ?: zero).toString() // = received qty val nonDelieveredQty = (zero).toString() + // Final + val args = MailTemplateForStockInLineArgs( + supplierEmail = supplierEmail, + supplierName = supplierName, + dnNo = dnNo, + dnDate = dnDate, + poNo = poNo, + supplierId = supplierId, + itemNo = itemNo, + itemName = itemName, + itemQty = itemQty, + uom = uom, + planDnDate = planDnDate, + unitPrice = unitPrice, + receivedCompany = receivedCompany, + lotNo = lotNo, + qcResult = qcResult, + receivedQty = receivedQty, + nonDelieveredQty = nonDelieveredQty, + rejectedQty = rejectedQty, + qcDate = qcDate + ); + + return args; + } + + fun getMailTemplateHtmlForStockInLine(stockInLineId: Long): MailTemplateHtml { + val emailTemplate = findByCode("RM-01") ?: throw NoSuchElementException("No RM-01 Email Template"); + val args = getMailTemplateArgsForStockInLine(stockInLineId); + // HTML - val to = supplierEmail + val to = args.supplierEmail val toHtmlStr = Jsoup.parse("").appendElement("p").text("To: $to").outerHtml() - val subject = (emailTemplate.subjectCht ?: "N/A") -// .replace("{supplierName}", supplierName) - .replace("{poNo}", poNo) - .replace("{itemNo}", itemNo) + val subject = args.applyToTemplate(emailTemplate.subjectCht ?: "N/A") +// .replace("\${supplierName}", supplierName) +// .replace("\${poNo}", poNo) +// .replace("\${itemNo}", itemNo) val subjectHtmlStr = Jsoup.parse("").appendElement("p").text("Subject: $subject").outerHtml() - val content = (emailTemplate.contentCht ?: "N/A") - .replace("{supplierName}", supplierName) - .replace("{dnNo}", dnNo) - .replace("{dnDate}", dnDate) - .replace("{poNo}", poNo) - .replace("{supplierId}", supplierId) - .replace("{itemNo}", itemNo) - .replace("{itemName}", itemName) - .replace("{itemQty}", itemQty) - .replace("{uom}", uom) - .replace("{planDnDate}", planDnDate) - .replace("{unitPrice}", unitPrice) - .replace("{receivedCompany}", receivedCompany) - .replace("{lotNo}", lotNo) - .replace("{qcResult}", qcResult) - .replace("{acceptedQty}", acceptedQty) - .replace("{nonDelieveredQty}", nonDelieveredQty) - .replace("{rejectedQty}", rejectedQty) - .replace("{qcDate}", qcDate) - val contentHtmlStr = Jsoup.parse("").appendElement("p").append("Content: $content").outerHtml() + val content = args.applyToTemplate(emailTemplate.contentCht ?: "N/A") +// .replace("\${supplierName}", supplierName) +// .replace("\${dnNo}", dnNo) +// .replace("\${dnDate}", dnDate) +// .replace("\${poNo}", poNo) +// .replace("\${supplierId}", supplierId) +// .replace("\${itemNo}", itemNo) +// .replace("\${itemName}", itemName) +// .replace("\${itemQty}", itemQty) +// .replace("\${uom}", uom) +// .replace("\${planDnDate}", planDnDate) +// .replace("\${unitPrice}", unitPrice) +// .replace("\${receivedCompany}", receivedCompany) +// .replace("\${lotNo}", lotNo) +// .replace("\${qcResult}", qcResult) +// .replace("\${acceptedQty}", receivedQty) +// .replace("\${nonDelieveredQty}", nonDelieveredQty) +// .replace("\${rejectedQty}", rejectedQty) +// .replace("\${qcDate}", qcDate) + val contentHtmlStr = Jsoup.parse("").appendElement("p").append("Content: $content").outerHtml(); // Result - val resultHtmlStr = toHtmlStr + subjectHtmlStr + contentHtmlStr + val resultHtmlStr = toHtmlStr + subjectHtmlStr + contentHtmlStr; + + return MailTemplateHtml( + to = toHtmlStr, + subject = subjectHtmlStr, + content = contentHtmlStr, + filename = subject, + ); + } + + fun getMailTemplatePdfForStockInLine(stockInLineId: Long): DownloadMailTemplateResponse { + val htmlMailTemplate = getMailTemplateHtmlForStockInLine(stockInLineId) + val resultHtmlStr = htmlMailTemplate.to + htmlMailTemplate.subject + htmlMailTemplate.content // println(resultHtmlStr) val resultPdf = ByteArrayOutputStream() @@ -178,7 +277,7 @@ open class MailTemplateService( return DownloadMailTemplateResponse( file = resultPdf.toByteArray(), - fileName = subject + fileName = htmlMailTemplate.filename ); } } \ No newline at end of file diff --git a/src/main/java/com/ffii/fpsms/modules/common/mail/web/MailTemplateController.kt b/src/main/java/com/ffii/fpsms/modules/common/mail/web/MailTemplateController.kt index 63b80f2..ab034c2 100644 --- a/src/main/java/com/ffii/fpsms/modules/common/mail/web/MailTemplateController.kt +++ b/src/main/java/com/ffii/fpsms/modules/common/mail/web/MailTemplateController.kt @@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController -import java.net.URLEncoder @RequestMapping("/mailTemplates") @RestController @@ -28,9 +27,9 @@ class MailTemplateController( return mailTemplateService.saveMailTemplate(request); } - @GetMapping("/getMailTemplateForStockInLine/{stockInLineId}") - fun getMailTemplateForStockInLine(@PathVariable stockInLineId: Long): ResponseEntity { - val response = mailTemplateService.getMailTemplateForStockInLine(stockInLineId) + @GetMapping("/getMailTemplatePdfForStockInLine/{stockInLineId}") + fun getMailTemplatePdfForStockInLine(@PathVariable stockInLineId: Long): ResponseEntity { + val response = mailTemplateService.getMailTemplatePdfForStockInLine(stockInLineId) val headers = HttpHeaders().apply { contentType = MediaType.APPLICATION_PDF // add("Content-Disposition", "attachment; filename=${response.fileName}")