-
Notifications
You must be signed in to change notification settings - Fork 0
/
izettle2moneybird.py
320 lines (267 loc) · 14.1 KB
/
izettle2moneybird.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import configparser
import decimal
import json
import os
import sys
import dateutil.parser
import datetime
import argparse
import logging
import logging.handlers
from lib import iz, mb, log
parser = argparse.ArgumentParser(description='Sync iZettle to your Moneybird account.')
parser.add_argument('-n', '--noop', dest='noop', action='store_true', help="Only read, do not really change anything")
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help="Print extra output")
parser.add_argument('--startdate', dest='startdatestring', type=str, help="The date to start on, in the "
"example format 31122019 for dec "
"31st, 2019. If not specified, it "
"will be yesterday.")
parser.add_argument('--enddate', dest='enddatestring', type=str, help="The date to start on, in the "
"example format 31122019 for dec "
"31st, 2019. If not specified, it "
"will be tomorrow.")
args = parser.parse_args()
flagNoop = args.noop
flagVerbose = args.verbose
iz.flagVerbose = flagVerbose
mb.flagVerbose = flagVerbose
mb.flagNoop = flagNoop
######################################
# CONFIGURE LOGGING
# ####################################
logger = log.logger(flagVerbose)
######################################
# CHECK REQUIREMENTS
# ####################################
if sys.version_info <= (3, 6, 0):
logger.critical("You are running Python version {0}, but ony >= 3.6 is tested".format(sys.version_info))
exit(1)
######################################
# PARSING INPUT
# ####################################
# SET THE DEFAULT START AND END DATES
date = datetime.datetime.today()
startDate = (date + datetime.timedelta(days=-1))
endDate = (date + datetime.timedelta(days=1))
if args.startdatestring is not None:
try:
startDate = datetime.datetime.strptime(args.startdatestring, "%d%m%Y").date()
except ValueError as err:
logger.exception("Could not convert '{0}' to date: {1}".format(args.startdatestring, err))
exit(1)
if flagVerbose:
logger.info("Starting date: {0}".format(startDate))
if args.enddatestring is not None:
try:
endDate = datetime.datetime.strptime(args.enddatestring, "%d%m%Y").date()
except ValueError as err:
logger.exception("Could not convert '{0}' to date: {1}".format(args.enddatestring, err))
exit(1)
if flagVerbose:
logger.info("Ending date: {0}".format(endDate))
######################################
# GET THE PARAMETERS FROM THE CONFIG FILE
# ####################################
config = configparser.ConfigParser()
config.read('etc/izettle2moneybird.conf')
######################################
# DOWNLOAD ALL REQUIRED DATA
# ####################################
iz.DownloadTransactions(startDate, endDate)
iz.DownloadPurchasesPurchases(startDate, endDate)
mb.DownloadContacts()
mb.DownloadFinancialAccounts()
mb.DownloadLedgerAccounts()
mb.DownloadTaxRates()
mb.DownloadFinanancialMutations(startDate, endDate)
mb.DownloadSalesInvoices(startDate, endDate)
mb.DownloadPurchaseInvoices(startDate, endDate)
######################################
# PROCESS SALES (iZettle purchases)
# ####################################
# Compare the iZettle purchases with the Moneybird purchases
logger.info("Processing the iZettle purchases")
flagMadeChanges = False
for izPurchase in iz.GetPurchases():
globalPurchaseNumber = izPurchase['globalPurchaseNumber']
if len(izPurchase['payments']) == 0:
logger.error("This purchase (globalPurchaseNumber {0}) has no payments, not supported".format(globalPurchaseNumber))
if len(izPurchase['payments']) > 1:
logger.error(
"This purchase (globalPurchaseNumber {0}) has more than one payment, not supported".format(globalPurchaseNumber))
fmreference = "Izettle verkoop {0}".format(globalPurchaseNumber)
logger.info("Processing iZettle purchase {0}".format(globalPurchaseNumber))
flagFound = False
# vergelijk met de Moneybird facturen
for mbFactuur in mb.GetSalesInvoices():
if mbFactuur['reference'] == fmreference:
flagFound = True
if not flagFound:
# Voeg de invoice toe
if flagNoop:
logger.info("NOOP: Sales invoice with reference '{0}' should be added, but read-only mode is preventing updates".format(fmreference))
else:
timestamp = dateutil.parser.parse(izPurchase['timestamp'])
izProducts = izPurchase['products']
details_attributes = []
for izProduct in izProducts:
unitPriceCents = int(izProduct['unitPrice'])
quantity = int(izProduct['quantity'])
totalproductcents = unitPriceCents * quantity
totalproductamount = decimal.Decimal(totalproductcents / 100.0)
products = {"description": izProduct['name'],
"price": totalproductamount,
"tax_rate": izProduct['vatPercentage']
}
details_attributes.append(products)
new_id = mb.AddSalesInvoice(fmreference, timestamp, details_attributes)
logger.info("Created sales invoice ({0})".format(fmreference))
if not new_id is None:
mb.SendInvoice(new_id)
flagMadeChanges = True
if flagFound:
logger.debug("Sales invoice already exists ({0})".format(fmreference))
# all done, now re-download the purchase invoices from moneybird
if flagMadeChanges:
logger.info("Made changes, so re-downloading the Moneybird purchases")
mb.DownloadPurchaseInvoices(startDate, endDate)
######################################
# PROCESS IZETTLE TRANSACTIONS
######################################
# We will first walk through all transactions and set up the purchase invoices and financial statements (so we can link
# them later in another step
flagPurchaseInvoicesChanged = False
flagFinancialStatementsChanged = False
for izTransaction in iz.GetTransactions():
transactioncode = izTransaction['originatorTransactionType']
trans_orig_uuid = izTransaction['originatingTransactionUuid']
fmreference = "IZETTLE_{0}_{1}_{2}".format(transactioncode, izTransaction['timestamp'], trans_orig_uuid)
ProcessThisTransaction = False
IgnoreUnhandled = config['iZettle']['Ignore_Unhandled_Transaction_Codes']
if transactioncode in ['CARD_PAYMENT', 'CARD_PAYMENT_FEE', 'PAYOUT']:
ProcessThisTransaction = True
else:
ProcessThisTransaction = False
if IgnoreUnhandled:
# We can't process this type of transaction, but our program should keep running
logger.warn("Ignoring unhandled transaction code {0}".format(transactioncode))
else:
# We can't process this type of transaction, and our program should exit
logger.error("Transaction code {0} not handled!".format(transactioncode))
exit(10)
if ProcessThisTransaction:
amount_cents = int(izTransaction['amount'])
# always get a positive number to work with
if amount_cents < 0:
amount_cents = 0 - amount_cents
amount_dec = decimal.Decimal(amount_cents / 100.0)
timestamp = dateutil.parser.parse(izTransaction['timestamp'])
##############################
# Check the financial statements
##############################
flagFinancialMutationFound = False
for mbFinancialMutation in mb.GetFinancialMutations():
if mbFinancialMutation['message'] == fmreference:
flagFinancialMutationFound = True
if not flagFinancialMutationFound:
if flagNoop:
logger.info("NOOP: should create financial statement {0}, but in read-only mode.".format(fmreference))
else:
mb.AddFinancialStatementAndMutation(fmreference, transactioncode, timestamp, amount_dec)
logger.info("Created financial statement ({0}".format(fmreference))
flagFinancialStatementsChanged = True
if flagFinancialMutationFound:
logger.debug("Financial statement already exists ({0})".format(fmreference))
##############################
# Create purchase invoices
##############################
if transactioncode == 'CARD_PAYMENT_FEE':
# ###############################################
# check if we have a purchase invoice
purchasereference = "Izettle inkoop {0}".format(trans_orig_uuid)
flagPurchaseInvoiceFound = False
for mbPurchaseinvoice in mb.GetPurchaseInvoices():
if mbPurchaseinvoice['reference'] == purchasereference:
flagPurchaseInvoiceFound = True
if not flagPurchaseInvoiceFound:
if flagNoop:
logger.info("Noop: should create purchase invoice {0}, but in read-only mode".format(fmreference))
else:
mb.AddPurchaseInvoice(purchasereference, timestamp, amount_dec)
logger.info("Created purchase invoice ({0}".format(fmreference))
flagPurchaseInvoicesChanged = True
if flagPurchaseInvoiceFound:
logger.debug("Purchase invoice already exists ({0})".format(purchasereference))
if flagPurchaseInvoicesChanged:
logging.info("Purchase invoices were changed, re-downloading")
mb.DownloadPurchaseInvoices(startDate, endDate)
if flagFinancialStatementsChanged:
logging.info("Financial mutations were changed, re-downloading")
mb.DownloadFinanancialMutations(startDate, endDate)
######################################
# PROCESS LINKS
######################################
# Now we will start the cross-checks to see if stuff needs to be linked.
for fm in mb.GetFinancialMutations():
fmreference = str(fm['message'])
if fmreference.startswith('IZETTLE_'):
# this is one of our iZettle financial statements
fm_payments = fm['payments']
fm_ledger_account_bookings = fm['ledger_account_bookings']
trans_orig_uuid = str(fm['message']).split('_')[-1]
amount = mb.MakePositive(decimal.Decimal(fm['amount']))
if fmreference.startswith('IZETTLE_CARD_PAYMENT_FEE_'):
if len(fm_payments) == 0:
# these are no payments for this financial mutation, so we need to start linking!
# this is a purchase invoice. Find the purchase invoice to go with it.
flagPurchaseInvoiceFound = False
for pi in mb.GetPurchaseInvoices():
pireference = str(pi['reference'])
if pireference.startswith('Izettle inkoop '):
uuid = pireference.split(' ')[-1]
if uuid == trans_orig_uuid:
flagPurchaseInvoiceFound = True
if flagNoop:
logging.info("NOOP: should create link for financial mutation {0}, but in read-only mode.".format(fmreference))
else:
mb.LinkPurchaseInvoice(fm['id'], pi['id'], amount)
logging.info("Created link for financial mutation {0}.".format(fmreference))
if not flagPurchaseInvoiceFound:
logging.info("Could not find a purchange invoice for financial statement {0}, ignoring.".format(fmreference))
if fmreference.startswith('IZETTLE_CARD_PAYMENT_'):
if len(fm_payments) == 0:
# these are no payments for this financial mutation, so we need to start linking!
# this is a sales invoice. Find the sales invoice to go with it.
flagSalesInvoiceFound = False
for si in mb.GetSalesInvoices():
sireference = str(si['reference'])
if sireference.startswith('Izettle verkoop '):
try:
izSalesNumber = int(sireference.split(' ')[-1])
except ValueError:
logging.error("Could not parse a sales invoice with reference '{0}'. I am expecting 'Izettle verkoop <int>'. If you need to do a manual transaction, make sure it does not start with 'Izettle verkoop '".format(sireference))
exit(1)
izSalesPaymentUuid = iz.LookupSalesPaymentUuid(izSalesNumber)
if izSalesPaymentUuid == trans_orig_uuid:
flagSalesInvoiceFound = True
if flagNoop:
logging.info(
"NOOP: should create link for financial mutation {0}, but in read-only mode.".format(
fmreference))
else:
mb.LinkSalesInvoice(fm['id'], si['id'], amount)
logging.info("Created link for financial mutation {0}.".format(fmreference))
if not flagSalesInvoiceFound:
logging.info("Could not find a sales invoice for financial statement {0}, ignoring.".format(
fmreference))
if fmreference.startswith('IZETTLE_PAYOUT_'):
if len(fm_ledger_account_bookings) == 0:
# these are no links for this financial mutation, so we need to start linking!
# this is a payout. Link it to the kruisposten ledger.
if flagNoop:
logging.info("NOOP: should create link for financial mutation {0}, but in read-only mode.".format(
fmreference))
else:
mb.LinkPayout(fm['id'], amount)
logging.info("Created link for financial mutation {0}.".format(fmreference))
logging.info("All done!")