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

137 lines
5.9 KiB
Python

import requests
import math
from models.transactions import *
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIyMyIsImp0aSI6IjU1NjM3NTAzMTBhMzU0OGEwODY0NTc2NGM4MTMyZTQxYmIzZWY2ODI0ZTM1YjZmNWQ3NWYwNjZjMjE1OWZkOTIwY2ZkNjQ0NTUyN2I1MWIyIiwiaWF0IjoxNjI1MDM0NDE3Ljc0OTA4NSwibmJmIjoxNjI1MDM0NDE3Ljc0OTA4NywiZXhwIjoxNjU2NTcwNDE3LjczOTMyMiwic3ViIjoiMSIsInNjb3BlcyI6W119.U8CaPuvjw_ZTrEevH6Qhi_O9Mu7eQcDhzwXrLw8lR1tC_NBm2i6JOmRK_uRSA4xt4-J_AJtsBv7PnWE-p8FznsbS6gABGmzrU2GX6u0ByiwcawbkmkPhN28gdBxf-wwvFevsUbFtT71xt3n4xOzpuD91uxKsHDOBz3Hw0X0dsqiw1s-MuHfRNBNoG1qKwtv3lGoBIuAyiqqxyDG53hDdrx7_A23Jop7XtSktWLfdhLNXG4rquK24ETDwtdfwEqdneL1QWf8Q-fcJGne-j5Rar9x8aupSM7rcNe-Tzw4iYzRk6fzB6ERYj4nfRbPTmIGfjw5Km3aysTvOjVvS3fL_poEay4dD7X_TQj-k5g4ZfRiGq6aPhoN4IEq0DMVAC17Za9uhjbOq_Fn-MfMAaOlX6bP-1bMZoclqsw8x1IQIyAbkPvFORS4wsx7QqrmYjTTsXGbUudfWbvaSLPLhF6h50BvHoVV719qulsq69yLBAkDs8ZIuiJ8VtH5-vHTmHVfvQUjg0WZKi6ZiQXEZvydyaJ6Hk8Fscg6e-tz3xcGUAwzIqrIRYggGkhiGRHa49JmgVf8-MMqFUb6rak4N5xhMZVX9cWt4S7vDuCQcyfHryV-wZ4qp0kLueon_abu_zgmEUu-nn1FBtCRY3h8ODK_aFIgD0N3UQtYebfbJx34yoAg"
baseUrl = "https://accounts.rouggy.com"
endPoint = "api/v1/transactions"
url = baseUrl + "/" + endPoint
my_headers = {'Authorization': f"Bearer {token}", 'accept': 'application/json', 'Content-Type': 'application/json'}
def uploadTransaction(t):
global data
transaction = Transactions()
# Get last id in the database and increment it.
try:
transaction = Transactions.select().order_by(Transactions.id.desc()).get()
t.id = transaction.id + 1
except:
t.id = 1
if t.amount < 0:
t.amount = abs(t.amount)
if t.type == "withdrawal":
data = {
"error_if_duplicate_hash": "false",
"apply_rules": "true",
"transactions": [
{
"type": "withdrawal",
"date": t.payment_date,
"amount": t.amount,
"description": t.description,
"currency_code": t.currency,
"category_name": t.category,
"source_name": t.asset_account,
"destination_name": t.opposing_account,
"tags": t.tag,
"external_id": t.id
}
]
}
elif t.type == "transfer":
if t.opposing_account == "HSBC Vietnam [Shared] Credit Card (VND)" or t.opposing_account == "HSBC Vietnam [Shared] Current Account (VND)":
t.foreign_currency_code = "VND"
elif t.opposing_account == "Societe Generale [Perso] Gregory Current Account (EUR)":
t.foreign_currency_code = "EUR"
elif t.opposing_account == "HSBC Vietnam [Shared] Savings Account (USD)" or t.opposing_account == "HSBC Vietnam [Perso] Gregory Savings Account (USD)":
t.foreign_currency_code = "USD"
if t.asset_account == "HSBC Vietnam [Shared] Savings Account (USD)" and t.opposing_account == "HSBC Vietnam [Shared] Credit Card (VND)" or t.opposing_account == "HSBC Vietnam [Shared] Current Account (VND)":
t.save(force_insert=True)
return
if t.currency != t.foreign_currency_code and t.foreign_currency_code is not None:
if t.currency == "VND" and t.foreign_currency_code == "USD":
t.foreign_currency_amount = t.amount / 23100
t.foreign_currency_amount = (math.ceil(t.amount / 100) * 100)
data = {
"error_if_duplicate_hash": "false",
"apply_rules": "true",
"transactions": [
{
"type": "transfer",
"date": t.payment_date,
"amount": t.amount,
"description": t.description,
"currency_code": t.currency,
"category_name": t.category,
"foreign_amount": t.foreign_currency_amount,
"foreign_currency_code": t.foreign_currency_code,
"source_name": t.asset_account,
"destination_name": t.opposing_account,
"tags": t.tag,
"external_id": t.id
}
]
}
else:
data = {
"error_if_duplicate_hash": "false",
"apply_rules": "true",
"transactions": [
{
"type": "transfer",
"date": t.payment_date,
"amount": t.amount,
"description": t.description,
"currency_code": t.currency,
"category_name": t.category,
"source_name": t.asset_account,
"destination_name": t.opposing_account,
"tags": t.tag,
"external_id": t.id
}
]
}
elif t.type == "deposit":
data = {
"error_if_duplicate_hash": "false",
"apply_rules": "true",
"transactions": [
{
"type": "deposit",
"date": t.payment_date,
"amount": t.amount,
"description": t.description,
"currency_code": t.currency,
"category_name": t.category,
"source_name": t.asset_account,
"destination_name": t.opposing_account,
"tags": t.tag,
"external_id": t.id
}
]
}
post = requests.post(url, headers=my_headers, json=data)
print(f"Imported into Firefly the transaction {t.description} for an amount of: {t.currency}{t.amount} from "
f"{t.asset_account} to {t.opposing_account}")
print(f"Results: {post.text}")
t.save(force_insert=True)