Created
May 9, 2021 13:57
-
-
Save MichaelSp/bf0ef5c1b1f63d5f881e519192eed1a8 to your computer and use it in GitHub Desktop.
Import Pecunia into MoneyMoney via CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Importer{ | |
version=1.00, | |
format="Import from Pecunia", | |
fileExtension="csv", | |
description="Read Transactions from Pecunia exported csv-file" | |
} | |
local categoryMapping = { | |
["1. A"] = "A", | |
["1.1 B"] = "A - B", | |
} | |
local function strToDate (str) | |
-- Helper function for converting localized date strings to timestamps. | |
local d, m, y = string.match(str, "(%d%d).(%d%d).(%d%d%d%d)") | |
return os.time{year=y, month=m, day=d} | |
end | |
local function splitByTabs(str, previous) | |
previous = previous or 1 | |
if previous <= #str then | |
local comma = str:find("\t", previous) or #str+1 | |
return str:sub(previous, comma-1), splitByTabs(str, comma+1) | |
end | |
end | |
function ReadTransactions (account) | |
-- Read transactions from a file with the format "date<TAB>amount<TAB>purpose". | |
local transactions = {} | |
local linecount = 0 | |
for line in assert(io.lines()) do | |
local values = {splitByTabs(line)} | |
if #values >= 4 then | |
local category = values[14] | |
local amountString = values[4]:gsub("%.", ""):gsub(",", ".") | |
if (categoryMapping[category] ~= nil) then | |
local old = category | |
category = categoryMapping[category] | |
print("Category '" .. old .. "' -> '" .. category .. "'") | |
end | |
local transaction = { | |
name = values[1], | |
accountNumber = values[2], | |
bankCode = values[3], | |
amount = tonumber(amountString), | |
currency = values[5], | |
bookingDate = strToDate(values[6]), | |
valueDate = strToDate(values[7]), | |
purpose = values[8], | |
transactionCode = tonumber(values[10]), | |
primanotaNumber = values[11], | |
mandateReference = values[12], | |
endToEndReference = values[13], | |
category = category, | |
comment = values[15] | |
} | |
table.insert(transactions, transaction) | |
end | |
linecount = linecount + 1 | |
end | |
return transactions | |
end | |
-- -- Debug help - Thanks to https://gist.github.com/ripter/4270799 | |
-- function dump(o) | |
-- if type(o) == 'table' then | |
-- local s = '{ ' | |
-- for k, v in pairs(o) do | |
-- if type(k) ~= 'number' then | |
-- k = '"' .. k .. '"' | |
-- end | |
-- s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' | |
-- end | |
-- return s .. '} ' | |
-- else | |
-- return tostring(o) | |
-- end | |
-- end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pecunia export settings: