213 lines
4.8 KiB
TypeScript
213 lines
4.8 KiB
TypeScript
// #DIM dimensionsnr namn
|
|
// #DIM 6 "Projekt"
|
|
const rDim = /#DIM\s+(-?\d+)\s+"([^"]*)"$/
|
|
|
|
export function parseDim(line: string) {
|
|
const result = line.match(rDim)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, number, name] = result
|
|
|
|
return {
|
|
number: parseInt(number),
|
|
name,
|
|
}
|
|
}
|
|
|
|
// #IB årsnr konto saldo kvantitet
|
|
// #IB 0 1790 11946.07 0
|
|
// #IB 0 2083 -57106301 0
|
|
const rIB = /^#IB\s+(-?\d+)\s+(\d{4,4})\s+(-?\d+(?:\.\d{1,2})?)\s+(-?\d+)$/
|
|
|
|
export function parseIB(line: string) {
|
|
const result = line.match(rIB)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, yearNumber, accountNumber, balance, quantity] = result
|
|
|
|
return {
|
|
accountNumber: parseInt(accountNumber),
|
|
yearNumber: parseInt(yearNumber),
|
|
balance: parseFloat(balance),
|
|
quantity: quantity ? parseInt(quantity) : null,
|
|
}
|
|
}
|
|
|
|
// #KONTO 1249 "Ack avskr bilar/transportmedel"
|
|
const rKonto = /^#KONTO\s+(\d{4,4})\s+"([^"]*)"$/
|
|
|
|
export function parseKonto(line: string) {
|
|
const result = line.match(rKonto)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, number, description] = result
|
|
|
|
return {
|
|
number: parseInt(number),
|
|
description,
|
|
}
|
|
}
|
|
|
|
// #OBJEKT dimensionsnr objektnr objektnamn
|
|
// #OBJEKT 6 "4" "Entreer"
|
|
const rObjekt = /^#OBJEKT\s+(\d+)\s+"?(\d+)"?\s"([^"]*)"$/
|
|
|
|
export function parseObjekt(line: string) {
|
|
const result = line.match(rObjekt)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, dimensionNumber, number, name] = result
|
|
|
|
return {
|
|
dimensionNumber: parseInt(dimensionNumber),
|
|
number: parseInt(number),
|
|
name,
|
|
}
|
|
}
|
|
|
|
// #RAR årsnr start slut
|
|
// #RAR 0 20160101 20161231
|
|
// #RAR -1 20150101 20151231
|
|
const rRAR = /^#RAR\s+(-?\d+)\s+(\d{8,8})\s+(\d{8,8})$/
|
|
|
|
export function parseRAR(line: string) {
|
|
const result = line.match(rRAR)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, yearNumber, startDate, endDate] = result
|
|
|
|
return {
|
|
yearNumber: parseInt(yearNumber),
|
|
startDate,
|
|
endDate,
|
|
}
|
|
}
|
|
|
|
// #SRU konto SRU-kod
|
|
// #SRU 1240 7215
|
|
const rSRU = /^#SRU\s+(\d{4,4})\s+(\d{4,4})$/
|
|
|
|
export function parseSRU(line: string) {
|
|
const result = line.match(rSRU)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, number, sru] = result
|
|
|
|
return {
|
|
number: parseInt(number),
|
|
sru: parseInt(sru),
|
|
}
|
|
}
|
|
|
|
// #TRANS kontonr {objektlista} belopp transdat transtext kvantitet sign
|
|
// #TRANS 1790 {1 "1"} 7509 "" "Faktura 9631584500436 172-57 - Perspektiv Bredband AB" 0
|
|
// #TRANS 6310 {} 9076.00 20160131 "jan - mars"
|
|
const rTrans =
|
|
/^#TRANS\s+(\d{4,4})\s+\{([^}]+)?\}\s+(-?\d+(?:\.\d+)?)\s+"?([^"]*)"?\s+"?([^"]*)"?(?:\s+(\d+))?(?:\s+(.*))?$/
|
|
const rObjectList = /"?(\d+)"?\s+"?(\d+)"/g
|
|
|
|
export function parseTrans(line: string) {
|
|
const result = line.match(rTrans)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw new Error('parsing error')
|
|
}
|
|
|
|
const [, accountNumber, objectListString, amount, transactionDate, description, quantity, signature] = result
|
|
|
|
let objectList: [number, number][] | null = null
|
|
|
|
if (objectListString) {
|
|
objectList = []
|
|
const result = objectListString.matchAll(rObjectList)
|
|
|
|
for (const match of result) {
|
|
const [, dimension, object] = match as string[]
|
|
|
|
objectList.push([dimension, object].map((val) => parseInt(val)) as [number, number])
|
|
}
|
|
}
|
|
|
|
return {
|
|
accountNumber: parseInt(accountNumber),
|
|
objectList,
|
|
amount: parseFloat(amount),
|
|
transactionDate: transactionDate || null,
|
|
description: description || null,
|
|
quantity: quantity ? parseInt(quantity) : null,
|
|
signature: signature || null,
|
|
}
|
|
}
|
|
|
|
// #UB årsnr konto saldo kvantitet
|
|
// #UB 0 1110 77246210 0
|
|
// #UB 0 1229 -37390.26 0
|
|
const rUB = /^#UB\s+(-?\d+)\s+(\d{4,4})\s+(-?\d+(?:\.\d{1,2})?)\s+(-?\d+)$/
|
|
|
|
export function parseUB(line: string) {
|
|
const result = line.match(rUB)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, yearNumber, accountNumber, balance, quantity] = result
|
|
|
|
return {
|
|
accountNumber: parseInt(accountNumber),
|
|
yearNumber: parseInt(yearNumber),
|
|
balance: parseFloat(balance),
|
|
quantity: parseInt(quantity),
|
|
}
|
|
}
|
|
|
|
// #VER serie vernr verdatum vertext regdatum sign
|
|
// #VER A 1 20151231 "BR/RR 2015" 20231213
|
|
const rVer = /#VER\s+(\w+)\s+(\d+)\s+(\d{8,8})\s+"([^"]*)"\s+(\d{8,8})(?:\s+(.*))?/
|
|
|
|
export function parseVer(line: string) {
|
|
const result = line.match(rVer)
|
|
|
|
if (!result) {
|
|
console.error(line)
|
|
throw Error('parsing error')
|
|
}
|
|
|
|
const [, journal, number, transactionDate, description, entryDate, signature] = result
|
|
|
|
return {
|
|
journal,
|
|
number: parseInt(number),
|
|
transactionDate,
|
|
description,
|
|
entryDate,
|
|
signature: signature || null,
|
|
}
|
|
}
|