-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpaypal.py
62 lines (49 loc) · 2.6 KB
/
paypal.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
import csv
import sys
import datetime
def read_statement(filename):
source_data = []
with open('source.csv', 'rb') as sourcefile:
reader = csv.DictReader(sourcefile)
for row in reader:
source_data.append(row)
return source_data
def normalize_source(source_data):
normalized_data = []
# Remove transactions without reference ID -- we don't want currency conversion transactions
for row in source_data:
if row[' Reference Txn ID'] == '':
normalized_data.append(row)
transactions = {row[' Transaction ID']: row for row in normalized_data}
# Replace USD values with GBP values if transaction currency was converted
for row in source_data:
if row[' Currency'] == 'USD' and row[' Reference Txn ID'] != '':
id = row[' Reference Txn ID']
if id in transactions:
transactions[id][' Fee'] = row[' Fee']
transactions[id][' Gross'] = row[' Gross']
transactions[id][' Net'] = row[' Net']
transactions[id][' Currency'] = row[' Currency']
return transactions.values()
def write_csv(data):
fieldnames = ['Description on bank statement', 'Item', 'Category', 'Date', 'Money in USD', 'Money in GBP', 'Money out USD', 'Money out GBP', 'Account', 'Is refunded?']
output_filename = 'paypal_{}.csv'.format(datetime.datetime.now().strftime('%Y%m%d'))
with open(output_filename, 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(fieldnames)
for row in data:
name = '{} / {}'.format(row[' Name'], row[' Transaction ID'])
if row[' Currency'] == 'GBP' and '-' in row[' Net']:
writer.writerow([name, '', '', row['Date'], '', '', '', row[' Net'].replace('-', ''), 'PayPal', '-'])
elif row[' Currency'] == 'GBP' and not '-' in row[' Net']:
writer.writerow([name, '', '', row['Date'], '', row[' Net'], '', '', 'PayPal', '-'])
elif row[' Currency'] == 'USD' and '-' in row[' Net']:
writer.writerow([name, '', '', row['Date'], '', row[' Net'].replace('-', ''), '', '', 'PayPal', '-'])
elif row[' Currency'] == 'USD' and not '-' in row[' Net']:
writer.writerow([name, '', '', row['Date'], row[' Net'], '', '', '', 'PayPal', '-'])
print 'Transaction {} on {} processed'.format(name, row['Date'])
print 'CSV saved to {}'.format(output_filename)
if __name__ == "__main__":
source_data = read_statement(sys.argv[1])
normalized_data = normalize_source(source_data)
write_csv(normalized_data)