-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.py
63 lines (50 loc) · 2.16 KB
/
migrate.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
from datetime import datetime
import yaml
import csv
import admin
def load():
with open('./import/data.yaml') as f:
import_data = yaml.safe_load(f)
account_id = admin.create_account(import_data['account'])
clients = {}
for client_key, client_details in import_data['clients'].items():
clients[client_key] = admin.create_client(account_id, client_details)
print(f'Created client {client_key}')
with open('import/invoices.csv', 'r') as f:
csvreader = csv.DictReader(f)
for row in csvreader:
client_key = import_data['customer_clients'][row['customer']]
client_id = clients[client_key]
supplier = import_data['suppliers'][row['supplier']]
customer = import_data['customers'][row['customer']]
invoice_id = create_invoice(account_id, client_id, supplier, customer, row)
line_items = process_line_items(invoice_id, row)
print('Created invoice ', invoice_id)
def process_line_items(invoice_id, invoice_row):
invoice_num = invoice_row['invoice_number']
with open(f'./import/{invoice_num}.csv', 'r') as f:
csvreader = csv.reader(f, delimiter='\t')
for row in csvreader:
create_line_item(invoice_id, invoice_row, row)
def create_invoice(account_id, client_id, supplier, customer, row):
return admin.create_invoice(account_id, client_id, {
'number': int(row['invoice_number']),
'supplier': supplier,
'customer': customer,
'issued_on': datetime.strptime(row['create_date'], "%Y-%m-%d"),
'paid_on': datetime.strptime(row['date_paid'], "%Y-%m-%d"),
'terms': row['terms'].replace('\\n', '\n'),
'notes': row['notes'],
'emailed': True,
'currency': 'eur',
})
def create_line_item(invoice_id, invoice_row, row):
return admin.create_line_item(invoice_id, {
'description': row[1],
'rate': {'cost': float(row[2].replace(',', '')), 'unit': 'hour'},
'quantity': float(row[3].replace(',', '')),
'discount_pct': 0,
'taxes': [{'name': 'VAT', 'rate': float(invoice_row['vat'])}],
})
if __name__ == '__main__':
load()