CSVConverterFirefly3/transaction.py
2021-08-10 01:05:52 +07:00

161 lines
4.7 KiB
Python

import re
import json
from enum import Enum
import math
from models.transactions import *
from datetime import datetime
import api
"""
Transaction to be added in Firefly III
Need to have the following roles:
- Description
- Source Account
- Destination Account
- Date
- Amount
- Category
"""
Month = {
"JAN": "01",
"FEB": "02",
"MAR": "03",
"APR": "04",
"MAY": "05",
"JUN": "06",
"JUL": "07",
"AUG": "08",
"SEP": "09",
"OCT": "10",
"NOV": "11",
"DEC": "12"
}
class Accounts(Enum):
HSBC_ATM_VND = "HSBC Vietnam [Shared] Current Account (VND)"
HSBC_CreditCard_VND = "HSBC Vietnam [Shared] Credit Card (VND)"
HSBC_Savings_USD = "HSBC Vietnam [Shared] Savings Account (USD)"
HSBC_Greg_Savings_USD = "HSBC Vietnam [Perso] Gregory Savings Account (USD)"
SG_Greg_EUR = "Societe Generale [Perso] Gregory Current Account (EUR)"
Regex = {
"HSBC_ATM_VND": '([^,]*),([^,]*),(?:"?)(?:-?)(0|[1-9][0-9]{0,2}(?:(?:,[0-9]{3})*|[0-9]*)(?:\.[0-9]+){0,1})(?:"?),',
"HSBC_CreditCard_VND": '(?:"?)([^,]*)",(?:"?)([^,]*)",([^,]*)(?:"?),(?:"?)(?:-?)((?!0+\.00)(?=.{1,9}(?:\.|$))\d{1,3}(?:,\d{3})*(?:\.\d+)?)(?:"?)',
"HSBC_Savings_USD": '([^,]*),([^,]*),(?:"?)(?:-?)((?!0+\.00)(?=.{1,9}(?:\.|$))\d{1,3}(?:,\d{3})*(?:\.\d+)?)(?:"?)',
"HSBC_Greg_Savings_USD": '([^,]*),([^,]*),(?:"?)(?:-?)((?!0+\.00)(?=.{1,9}(?:\.|$))\d{1,3}(?:,\d{3})*(?:\.\d+)?)(?:"?)',
"SG_Greg_EUR": "'?(.*?);(.*?);(.*?);(.*);",
}
def createTransactionCurrentVND(t):
regex = Regex[t.account_short_name]
new_line = re.search(regex, t.raw_line, re.IGNORECASE)
t.description = new_line.group(2)
t.payment_date = datetime.strptime(new_line.group(1), '%d/%m/%Y')
t.payment_date = t.payment_date.date().isoformat()
if convertDate(t) is not None:
t.payment_date = datetime.strptime(convertDate(t), '%d/%m/%Y')
t.payment_date = t.payment_date.date().isoformat()
t.amount = new_line.group(3)
t.amount = float(t.amount.replace(",", ""))
searchDestinationAccount(t)
api.uploadTransaction(t)
def createTransactionCreditCardVND(t):
regex = Regex[t.account_short_name]
new_line = re.search(regex, t.raw_line, re.IGNORECASE)
t.payment_date = datetime.strptime(new_line.group(2), '%d/%m/%Y')
t.payment_date = t.payment_date.date().isoformat()
t.description = new_line.group(3)
t.amount = new_line.group(4)
t.amount = t.amount.replace(",", "")
t.amount = float(t.amount)
searchDestinationAccount(t)
api.uploadTransaction(t)
def createTransactionSGEUR(t):
regex = Regex[t.account_short_name]
new_line = re.search(regex, t.raw_line, re.IGNORECASE)
t.payment_date = datetime.strptime(new_line.group(1), '%d/%m/%Y')
t.payment_date = t.payment_date.date().isoformat()
t.description = new_line.group(2)
t.amount = new_line.group(3).replace(",",".")
t.amount = float(t.amount)
searchDestinationAccount(t)
api.uploadTransaction(t)
"""
Convert date from format 14APR21 to 14/04/2021
"""
def convertDate(t):
if re.search(r"(\d{2})([a-zA-Z]{3})(\d{2})", t.raw_line, re.IGNORECASE) is not None:
payment_date = re.search(r"(\d{2})([a-zA-Z]{3})(\d{2,4})", t.raw_line, re.IGNORECASE)
payment_date = f"{payment_date.group(1)}/{Month[payment_date.group(2)]}/20{payment_date.group(3)[-2:]}"
elif re.search(r"(2021|2022)(\d{2})(\d{2})", t.raw_line, re.IGNORECASE) is not None:
payment_date = re.search(r"(2021|2022)(\d{2})(\d{2})", t.raw_line, re.IGNORECASE)
payment_date = f"{payment_date.group(3)}/{payment_date.group(2)}/{payment_date.group(1)}"
else:
payment_date = None
return payment_date
def harmonizeDate(t):
date = re.search(r'(\d+)/(\d+)/(\d+)', t.date, re.IGNORECASE)
if len(date.group(1)) == 1:
new_date = "0" + date.group(1)
else:
new_date = date.group(1)
if len(date.group(2)) == 1:
new_date += "/0" + date.group(2)
else:
new_date += "/" + date.group(2)
new_date += "/" + date.group(3)
return new_date
def searchDestinationAccount(t):
t.opposing_account = "Other"
t.category = "Other"
t.type = "withdrawal"
with open('data/master.json') as master:
companies = json.load(master)
for company in companies:
match = re.search(company['regex'], t.raw_line)
if match:
t.type = company['type']
t.opposing_account = company['opposing_account']
t.description = company['description']
t.category = company['default_category']
t.tag = company['tag']
if company['asset_account'] != "":
t.asset_account = company['asset_account']
break
return t