From 4df26488e3fd91e2577919604a31468836545019 Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 13 Dec 2024 04:37:45 +0100 Subject: [PATCH] Extracted mergeUnitData(); removed redundant name initializer from Column for most calls --- .../codinux/invoicing/parser/CodeGenerator.kt | 47 ++++++++++++++----- .../excel/ZugferdExcelCodeListsParser.kt | 4 +- .../codinux/invoicing/parser/model/Column.kt | 2 +- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/CodeGenerator.kt b/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/CodeGenerator.kt index 4fed84d..d3336a8 100644 --- a/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/CodeGenerator.kt +++ b/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/CodeGenerator.kt @@ -25,6 +25,7 @@ class CodeGenerator { matchedCodeLists.forEach { (type, codeLists) -> val (columns, rows) = (if (type == CodeListType.IsoCountryCodes) mergeCountryData(codeLists.first, codeLists.second!!) else if (type == CodeListType.IsoCurrencyCodes) mergeCurrencyData(codeLists.first, codeLists.second!!) + else if (type == CodeListType.Units) mergeUnitData(codeLists.first, codeLists.second!!) else { addFrequentlyUsedColumn(reorder(map(filter( // Factur-X (= codeLists.second) has the better column names and often also a Description column @@ -73,7 +74,7 @@ class CodeGenerator { val index = columns.indexOf(useForColumn) val modifiedColumns = columns.toMutableList().apply { removeAt(index) - add(Column(columns.last().index + 1, "UseFor", "InvoiceTypeUseFor", "UseFor")) + add(Column(columns.last().index + 1, "UseFor", "InvoiceTypeUseFor")) } val modifiedRows = rows.onEach { val useFor = it.removeValueAtIndex(index)?.toString() @@ -112,11 +113,11 @@ class CodeGenerator { private fun mergeCountryData(cefCodeList: CodeList, zugferdCodeList: net.codinux.invoicing.parser.excel.CodeList): Pair, List> { val columns = listOf( - Column(0, "alpha2Code", "String", "alpha2Code"), - Column(1, "alpha3Code", "String", "alpha3Code"), - Column(2, "numericCode", "Int", "numericCode"), - Column(3, "numericCodeAsString", "String", "numericCodeAsString"), - Column(4, "englishName", "String", "englishName"), + Column(0, "alpha2Code", "String"), + Column(1, "alpha3Code", "String"), + Column(2, "numericCode", "Int"), + Column(3, "numericCodeAsString", "String"), + Column(4, "englishName", "String"), ) val cefByIsoCode = cefCodeList.rows.associateBy { it.values[0] } @@ -133,12 +134,12 @@ class CodeGenerator { private fun mergeCurrencyData(cefCodeList: CodeList, zugferdCodeList: net.codinux.invoicing.parser.excel.CodeList): Pair, List> { val columns = listOf( - Column(0, "alpha3Code", "String", "alpha3Code"), - Column(1, "numericCode", "Int", "numericCode"), - Column(2, "currencySymbol", "String", "currencySymbol"), - Column(3, "englishName", "String", "englishName"), - Column(4, "countries", "Set", "countries"), - Column(Int.MAX_VALUE, "isFrequentlyUsedValue", "Boolean", "isFrequentlyUsedValue") + Column(0, "alpha3Code", "String"), + Column(1, "numericCode", "Int"), + Column(2, "currencySymbol", "String"), + Column(3, "englishName", "String"), + Column(4, "countries", "Set"), + Column(Int.MAX_VALUE, "isFrequentlyUsedValue", "Boolean") ) val cefByIsoCode = cefCodeList.rows.associateBy { it.values[0] } @@ -156,11 +157,31 @@ class CodeGenerator { return columns to rows.sortedBy { it.enumName!! } // sort by English name } + private fun mergeUnitData(cefCodeList: CodeList, zugferdCodeList: net.codinux.invoicing.parser.excel.CodeList): Pair, List> { + val columns = listOf( + Column(0, "code", "String"), + Column(1, "englishName", "String"), + Column(3, "isFrequentlyUsedValue", "Boolean"), + ) + + val cefByIsoCode = cefCodeList.rows.associateBy { it.values[0] } + val zugferdByIsoCode = zugferdCodeList.rows.groupBy { it.values[0] } + + val rows = cefByIsoCode.map { (code, cefRow) -> + val row = zugferdByIsoCode[code]!!.first() + val values = row.values + + Row(listOf(code, values[0], row.isFrequentlyUsedValue), row.isFrequentlyUsedValue) + } + + return columns to rows + } + private fun addFrequentlyUsedColumn(columnsToRows: Pair, List>): Pair, List> { val hasFrequentlyUsedValue = columnsToRows.second.any { it.isFrequentlyUsedValue } return if (hasFrequentlyUsedValue) { - (columnsToRows.first + Column(Int.MAX_VALUE, "isFrequentlyUsedValue", "Boolean", "isFrequentlyUsedValue")) to + (columnsToRows.first + Column(Int.MAX_VALUE, "isFrequentlyUsedValue", "Boolean")) to columnsToRows.second.onEach { it.addIsFrequentlyUsedValueCell() } } else { columnsToRows diff --git a/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/excel/ZugferdExcelCodeListsParser.kt b/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/excel/ZugferdExcelCodeListsParser.kt index 751a12b..dccdc69 100644 --- a/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/excel/ZugferdExcelCodeListsParser.kt +++ b/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/excel/ZugferdExcelCodeListsParser.kt @@ -88,7 +88,7 @@ class ZugferdExcelCodeListsParser { }.filterNot { it.values.all { it == null } } // filter out empty rows if (isTypeWithDescription) { - columns.add(Column(indexOfNextEmptyCell!! - 1, "Description", "String", "Description")) + columns.add(Column(indexOfNextEmptyCell!! - 1, "Description", "String")) } return CodeList(type, name, url, usedInInvoiceFields, additionalUsedInInvoiceFields, columns, rows) @@ -122,7 +122,7 @@ class ZugferdExcelCodeListsParser { private fun mapColumn(cell: Cell?): Column? = cell?.let { val name = cell.stringCellValue ?: "" - return Column(cell.columnIndex, name, "String", name) + return Column(cell.columnIndex, name, "String") } private fun getCellValue(cell: Cell?, type: CodeListType? = null) = when (cell?.cellType) { diff --git a/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/model/Column.kt b/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/model/Column.kt index 854e612..53db65a 100644 --- a/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/model/Column.kt +++ b/e-invoice-spec-parser/src/main/kotlin/net/codinux/invoicing/parser/model/Column.kt @@ -4,7 +4,7 @@ data class Column( val index: Int, val id: String, val dataType: String, - val name: String, + val name: String = id, ) { override fun toString() = "$dataType $name ($id)" } \ No newline at end of file