-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2ledger_usd.py
executable file
·66 lines (57 loc) · 2.03 KB
/
json2ledger_usd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
from datetime import datetime
import requests
import sys
import json
import re
import privat_fop as privat
from ledger import *
from datetime import date
class PrivatCashAccount(CashAccount):
from_acc = privat.from_acc
class PrivatIncomeAccount(IncomeAccount):
from_payee = privat.income_from_payee
class PrivatExpenseAccount(ExpenseAccount):
from_payee = privat.expense_from_payee
class PrivatTransaction(Transaction):
def clear_date(self, date):
return datetime.strptime(date,'%d.%m.%Y').strftime('%Y/%m/%d')
def __init__(self, **kvargs):
payee=kvargs['AUT_CNTR_NAM']
if re.search('КОМИССИЯ ЗА ДЕБЕТОВАНИЕ', payee):
payee=kvargs['AUT_MY_MFO_NAME']
amount=kvargs['BPL_SUM']
currency=kvargs['BPL_CCY']
date=kvargs['BPL_DAT_OD']
comment=kvargs['BPL_OSND']
acc=kvargs['AUT_MY_ACC']
tran_type=kvargs['TRANTYPE'] # C - income, D - expense
if tran_type == 'C':
debit = PrivatIncomeAccount()
credit = PrivatCashAccount()
debit.from_payee(payee, comment)
credit.from_acc(acc)
elif tran_type == 'D':
debit = PrivatCashAccount()
credit = PrivatExpenseAccount()
credit.from_payee(payee, comment)
debit.from_acc(acc)
else:
raise('Wrong transaction type')
super().__init__(debit=debit,
credit=credit,
date=self.clear_date(date),
money=Money(amount,currency),
payee=payee,
comment=comment)
#f = open("q2.json", "r")
resp = json.load(sys.stdin)
#resp = json.load(f)
transactions = list()
for bankaccounts in resp['StatementsResponse']['statements']:
for statements in bankaccounts.values():
for statement in statements:
for v in statement.values():
transactions.append(PrivatTransaction(**v))
for tran in transactions:
print(tran)