package com.restapi.controllers import com.restapi.domain.* import org.apache.poi.hssf.usermodel.DVConstraint import org.apache.poi.hssf.usermodel.HSSFDataValidation import org.apache.poi.hssf.usermodel.HSSFSheet import org.apache.poi.hssf.usermodel.HSSFWorkbook import org.apache.poi.ss.usermodel.* import org.apache.poi.ss.util.CellRangeAddressList import java.io.File import java.io.FileOutputStream import java.text.SimpleDateFormat import java.time.LocalDate import java.time.ZoneId import java.util.* fun createHeaderRow(cols: List, sh: HSSFSheet, wb: Workbook) { val boldFont = wb.createFont() boldFont.bold = true val style = wb.createCellStyle() style.setFont(boldFont) style.locked = true sh.createRow(0).apply { cols.forEachIndexed { index, value -> val cell = createCell(index) cell.setCellValue(value) cell.setCellStyle(style) } } } fun String.parseDate(format: String): Date? { val locale = Locale.getDefault() return try { SimpleDateFormat(format, locale).parse(this) } catch (e: Exception) { null } } fun dateFromCellHelper(cell: Cell): LocalDate? { val date = when (cell.cellType) { CellType.STRING -> cell.stringCellValue.parseDate("yyyy-MM-dd") CellType.NUMERIC -> { if (DateUtil.isCellDateFormatted(cell)) { cell.getDateCellValue() } else { null } } else -> null } return date?.toInstant()?.atZone(ZoneId.systemDefault())?.toLocalDate() } fun stringFromCellHelper(cell: Cell): String { val string = when (cell.cellType) { CellType.NUMERIC -> cell.numericCellValue.toString() CellType.STRING -> cell.stringCellValue else -> "" } return string } fun doubleFromCellHelper(cell: Cell): Double { val double = when (cell.cellType) { CellType.NUMERIC -> cell.numericCellValue CellType.STRING -> cell.stringCellValue.toDoubleOrNull() else -> 0.0 } return double ?: 0.0 } fun longIntFromCellHelper(cell: Cell): Long { val long = when (cell.cellType) { CellType.NUMERIC -> cell.numericCellValue.toLong() CellType.STRING -> cell.stringCellValue.toLong() else -> 0 } return long } enum class FileType { QUOTES, POS, VENDORS, PRODS, DOCS } enum class EnumFor { UOM, DocType } fun saveExcelFileLocally(fileName: String, wb: Workbook) { val path = "./excel" val file = File(path, fileName) if (!file.parentFile.exists()) { file.parentFile.mkdirs() } val out = FileOutputStream(file) wb.use { it.write(out) } out.close() } fun TemplateExcelFile(fileType: FileType) { when (fileType) { FileType.QUOTES -> { val headers: List = listOf( "Quotation Number", "Date", "Open Till", "Product Id", "Product Name", "Product Unit Price", "Quantity", "Vendor Name", "Vendor Address", "RFQ Number", "Total Amount", "Terms and Conditions" ) val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) saveExcelFileLocally("Quotes_Template.xls", wb) } FileType.POS -> { val headers: List = listOf( "Number", "Date", "Open Till", "Reference Quotation Number", "Vendor Name", "Vendor Address", "Product Id", "Product Name", "Unit Price", "Quantity", "Total Amount", "Terms and Conditions" ) val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) saveExcelFileLocally("Purchase_Order_Template.xls", wb) } FileType.VENDORS -> { val headers: List = listOf( "Name", "MSME", "GST Number", "Address", "Rating", "Contact Name", "Contact Email", "Contact Mobile" ) val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) saveExcelFileLocally("Vendors_Template.xls", wb) } FileType.PRODS -> { val headers: List = listOf("Id", "Name", "Description", "HSN Code", "UOM") val wb = HSSFWorkbook() val sh = wb.createSheet() createHeaderRow(headers, sh, wb) val r0 = CellRangeAddressList(0, 1000, 4, 4) val dv0 = HSSFDataValidation( r0, DVConstraint.createExplicitListConstraint(arrayOf("LTR", "MTR", "NOS", "ALL")) ).apply { suppressDropDownArrow = true } sh.addValidationData(dv0) saveExcelFileLocally("Products_Template.xls", wb) } FileType.DOCS -> { } } } fun exportQuotations(quotes: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf( "Quotation Number", "Date", "Open Till", "Product Id", "Product Name", "Product Unit Price", "Quantity", "Vendor Name", "Vendor Address", "RFQ Number", "Total AMount", "Terms and Conditions" ) createHeaderRow(headers, sh, wb) var rowCnt = 1 for (quote in quotes) { val prodCnt = quote.products.size for (j in 0..) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf( "No.", "Name", "MSME", "GST Number", "Address", "Rating", "Contact Name", "Contact Email", "Contact Mobile" ) createHeaderRow(headers, sh, wb) val totalCols = headers.size var rowCnt = 1 for (vendor in vendors) { val contactCnt = vendor.contacts.size for (j in 0..) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf("Id", "Name", "Description", "HSN Code", "UOM") createHeaderRow(headers, sh, wb) var rowCnt = 1 for (prod in prods) { val row = sh.createRow(rowCnt++) var i = 0 row.createCell(i++).setCellValue(prod.code.toString()) row.createCell(i++).setCellValue(prod.name) row.createCell(i++).setCellValue(prod.description) row.createCell(i++).setCellValue(prod.hsnCode) row.createCell(i++).setCellValue(prod.uom?.name) } saveExcelFileLocally("Products.xls", wb) } fun exportPos(pos: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf( "Number", "Date", "Open Till", "Reference Quotation Number", "Vendor Name", "Vendor Address", "Product Id", "Product Name", "Unit Price", "Quantity", "Total Amount", "Terms and Conditions" ) createHeaderRow(headers, sh, wb) var rowCnt = 1 for (po in pos) { val prodCnt = po.products.size for (j in 0..) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf( "MRN", "Date", "Vendor Name", "Vendor Bill Number", "Vendor Bill Amount", "Product Id", "Product Name", "Unit Price", "Quantity", ) createHeaderRow(headers, sh, wb) var rowCnt = 1 for (ticket in tickets) { val prodCnt = ticket.products?.size for (j in 0..) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf( "MDN", "Date", "Out Mode", "Purpose", "Product Id", "Product Name", "Unit Price", "Quantity", ) createHeaderRow(headers, sh, wb) var rowCnt = 1 for (ticket in tickets) { val prodCnt = ticket.products?.size for (j in 0..) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf("Reference Number", "Vendor", "Amount Paid", "Amount Deducted", "Excess Amount") createHeaderRow(headers, sh, wb) var rowCnt = 1 for (pmt in payments) { val row = sh.createRow(rowCnt++) var i = 0 row.createCell(i++).setCellValue(pmt.refNumber) row.createCell(i++).setCellValue(pmt.vendor?.name) row.createCell(i++).setCellValue(pmt.amount) pmt.amountDeducted?.let { row.createCell(i++).setCellValue(it) } pmt.excessAmount?.let { row.createCell(i++).setCellValue(it) } } saveExcelFileLocally("Payments.xls", wb) } fun exportInvoices(invoices: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf( "Number", "Date", "Reference PO", "Status", "Vendor Name", "Vendor Address", "Product Id", "Product Name", "Unit Price", "Quantity", "Total Amount" ) createHeaderRow(headers, sh, wb) var rowCnt = 1 for (invoice in invoices) { val prodCnt = invoice.products?.size for (j in 0..) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf("Type", "Reg. Number", "Reg. Date", "Model", "Make", "Driver", "Mileage") createHeaderRow(headers, sh, wb) var rowCnt = 1 for (flt in fleets) { val row = sh.createRow(rowCnt++) var i = 0 row.createCell(i++).setCellValue(flt.type) row.createCell(i++).setCellValue(flt.regNumber) row.createCell(i++).setCellValue(flt.regDate) row.createCell(i++).setCellValue(flt.model) row.createCell(i++).setCellValue(flt.make) row.createCell(i++).setCellValue(flt.driver) row.createCell(i++).setCellValue(flt.mileage) } saveExcelFileLocally("Fleets.xls", wb) } fun exportReminderLogs(logs: List) { val wb = HSSFWorkbook() val sh = wb.createSheet() val headers: List = listOf("Type", "Amount", "Frequency", "Reminder Date", "Acted Upon", "Vehicle") createHeaderRow(headers, sh, wb) var rowCnt = 1 for (log in logs) { val row = sh.createRow(rowCnt++) var i = 0 row.createCell(i++).setCellValue(log.reminderType) row.createCell(i++).setCellValue(log.amount) row.createCell(i++).setCellValue(log.reminder?.frequency.toString()) row.createCell(i++).setCellValue(log.reminderDate) row.createCell(i++).setCellValue(log.actedUpon) row.createCell(i++).setCellValue(log.reminder?.fleet?.regNumber) } saveExcelFileLocally("ReminderLogs.xls", wb) } fun main() { //ImportFromExcel(FileType.QUOTES, "C:\\Users\\arsalan\\Downloads\\Book.xlsx") TemplateExcelFile(FileType.PRODS) } fun ImportFromExcel(fileType: FileType, filePath: String) { val wb = WorkbookFactory.create(File(filePath)) val sh = wb.getSheetAt(0) when (fileType) { FileType.QUOTES -> { //Quote Number, ProductName, Product Quantity, Total Amount, RFQ Number, Quote Date, Valid Till, TNC[], Documents[] val quotesMap: MutableMap = mutableMapOf() val quotesList: List = mutableListOf() sh.rowIterator().forEach { row -> if (row == null) { //reached eof return@forEach } val quoteNumber = stringFromCellHelper(row.getCell(0)) val quoteDt = dateFromCellHelper(row.getCell(1)) val rfqNum = stringFromCellHelper(row.getCell(2)) val quoteValidTill = dateFromCellHelper(row.getCell(3)) val vendorName = stringFromCellHelper(row.getCell(4)) val vendorGstNum = stringFromCellHelper(row.getCell(5)) val vendorAddress = stringFromCellHelper(row.getCell(6)) val prodName = stringFromCellHelper(row.getCell(7)) val prodQuantity = doubleFromCellHelper(row.getCell(8)) val prodUnitPrice = doubleFromCellHelper(row.getCell(9)) val totalQuoteAmount = doubleFromCellHelper(row.getCell(10)) val prod = POProducts(0, prodName, prodUnitPrice, prodQuantity) if (quotesMap.containsKey(quoteNumber)) { //duplicated row quotesMap.get(quoteNumber)?.products?.add(prod) } else { val v = Vendor() v.apply { name = vendorName address = vendorAddress gstNumber = vendorGstNum } val quote = Quotation() quote.apply { quoteNum = quoteNumber quoteDate = quoteDt reqForQuoteNum = rfqNum validTill = quoteValidTill products = mutableListOf(prod) vendor = v totalAmount = totalQuoteAmount } quotesMap.put(quoteNumber, quote) } } //docs, tncs // println("$quotesMap") // quotesMap.forEach { (k, v) -> // println("$v") // } } FileType.POS -> { //poNum, poDate, validTill, refQuoteNum, prodName, prodQuantity, totalAmount, products, vendorName, vendorGst, vendorAddress, tnc[]. docs[] val poMap: MutableMap = mutableMapOf() sh.rowIterator().forEach { row -> if (row == null) return@forEach val poNum = stringFromCellHelper(row.getCell(0)) val poDate = dateFromCellHelper(row.getCell(1)) val refQuoteNum = stringFromCellHelper(row.getCell(2)) val poValidTill = dateFromCellHelper(row.getCell(3)) val prodName = stringFromCellHelper(row.getCell(4)) val prodQuantity = doubleFromCellHelper(row.getCell(5)) val vendorName = stringFromCellHelper(row.getCell(6)) val vendorGstNum = stringFromCellHelper(row.getCell(7)) val vendorAddress = stringFromCellHelper(row.getCell(8)) val totalPoAmount = doubleFromCellHelper(row.getCell(9)) //tncs, docs val prod = POProducts( productId = 0, productName = prodName, unitPrice = 0.0, quantity = prodQuantity, billQty = prodQuantity, gstPct = 0.0, taxableValue = 0.0, totalValue = 0.0, description = "", uom = "" ) if (poMap.containsKey(poNum)) { //repeated row poMap.get(poNum)?.products?.add(prod) } else { val vendor = Vendor() vendor.name = vendorName vendor.address = vendorAddress vendor.gstNumber = vendorGstNum val po = PurchaseOrder() po.poNum = poNum po.poDate = poDate po.referenceQuotation = refQuoteNum po.validTill = poValidTill poMap[poNum] = po } } } FileType.VENDORS -> { sh.rowIterator().forEach { row -> //name, msme, gstNum, addresss, rating, contacts if (row == null) return@forEach val name = stringFromCellHelper(row.getCell(0)) val msme = stringFromCellHelper(row.getCell(1)) val gstNum = stringFromCellHelper(row.getCell(2)) val address = stringFromCellHelper(row.getCell(3)) val rating = doubleFromCellHelper(row.getCell(4)) //vendor object val vendor = Vendor() vendor.name = name vendor.address = address vendor.msme = msme vendor.gstNumber = gstNum vendor.rating = rating } } FileType.PRODS -> { sh.rowIterator().forEach { row -> if (row == null) return@forEach //id, name, description, hsnCode, uom val prodId = stringFromCellHelper(row.getCell(0)) val prodName = stringFromCellHelper(row.getCell(1)) val prodDesc = stringFromCellHelper(row.getCell(2)) val prodHsnCode = stringFromCellHelper(row.getCell(3)) val prodUom = stringFromCellHelper(row.getCell(4)) //new prod object val prod = Product() prod.code = prodId prod.name = prodName prod.description = prodDesc prod.hsnCode = prodHsnCode prod.uom = when (prodUom) { "nos" -> UOM.NOS "ltr" -> UOM.LTR "mtr" -> UOM.MTR else -> UOM.ALL } } } FileType.DOCS -> { sh.rowIterator().forEach { row -> //Document Name, Document Type, RefID, url if (row == null) return@forEach val docName = stringFromCellHelper(row.getCell(0)) val docType = stringFromCellHelper(row.getCell(1)) val refId = stringFromCellHelper(row.getCell(2)) val url = stringFromCellHelper(row.getCell(3)) //new doc object val doc = Document() doc.name = docName doc.typeOfDoc = when (docType) { "quote" -> DocType.QUOTE "po" -> DocType.PO "invoice" -> DocType.INVOICE else -> DocType.ALL } doc.refIdOfDoc = refId.toLong() doc.url = url } } } }