-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHSBCBusiness.py
98 lines (86 loc) · 3.95 KB
/
HSBCBusiness.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os, datetime
import csv, hashlib, uuid
from TransactionGatewayAbstract import TransactionGatewayAbstract, Transaction
from TransactionGatewayAbstract import PartnerGatewayAbstract, Partner
class HSBCBusiness(TransactionGatewayAbstract, PartnerGatewayAbstract):
""" The supported HSBC export is csv. The csv header is:
'Date Type Description Paid out Paid in Balance'
Date format is: %d %b %Y e.g. 12 Mar 2018
"""
def __init__(self):
self.transactions = []
self.partners = []
@staticmethod
def get_name():
return "HSBC Business"
@staticmethod
def get_short_name():
return "HSBCB"
def init(self):
pass
def fetchTransactions(self):
self.hsbc_combine_exports()
pass
def hsbc_combine_exports(self):
"""Get all transaction export csv files, concatenate them and
read into self.transactions
:param None
:return: list of payments
"""
self.hsbc_transactions = []
for root, dirs, statement_exports in os.walk('./exports/hsbc'):
pass
for statement_export in statement_exports:
with open(''.join(['./exports/hsbc/', statement_export]), 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
header = next(reader) # Take header
for row in reader:
date = row.pop(0)
row.insert(0, datetime.datetime.strptime(date, "%d %b %Y").date())
self.hsbc_transactions.append(row)
# Transform to generic Transaction tuple
if len(row[3]) is not 1:
amount = ''.join(['-',row[3]]) #HSBC payout
if len(row[4]) is not 1: #HSBC payment inward
amount = row[4]
transaction = Transaction(source_gateway='HSBCB', source_id=row[2],source_type=row[1],
date=row[0], amount=amount, reference=row[2], currency='GBP')
if transaction not in self.transactions:
self.transactions.append(transaction)
def fetchPartners(self):
hsbc_partners = self.hsbc_fetch_partners()
def hsbc_fetch_partners(self):
"""Get all transaction export csv files, concatenate them and then
extract information relevant to a Partner record.
Read records into self.partners
:param None
:return: list of partners
"""
self.hsbc_partners = []
for root, dirs, statement_exports in os.walk('./exports/hsbc'):
pass
for statement_export in statement_exports:
with open(''.join(['./exports/hsbc/', statement_export]), 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
header = next(reader) # Take header
for row in reader:
date = row.pop(0)
row.insert(0, datetime.datetime.strptime(date, "%d %b %Y").date())
self.hsbc_partners.append(row)
# Transform to generic Partner tuple
if len(row[3]) is not 1:
amount = ''.join(['-',row[3]]) #HSBC payout
if len(row[4]) is not 1: #HSBC payment inward
amount = row[4]
source_id = str(hashlib.sha512(str(row)).digest())
source_type = row[1]
company_name = row[2]
partner = Partner(uid = str(uuid.uuid4()),
created_at = date,
source_gateway = self.get_short_name(),
source_id = source_id,
source_type = source_type,
company_name = company_name,
)
if partner not in self.partners:
self.partners.append(partner)