-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_invoice
executable file
·55 lines (49 loc) · 1.85 KB
/
csv_to_invoice
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
#!/usr/bin/env python
import FBAPI as fb
import csv
import argparse
import sys
import datetime
from config import *
class HourLine:
def __init__(self, name, date, category, ticket, notes, hours):
self.name = name
self.category = category
self.date = date
self.ticket = ticket
self.notes = notes
self.hours = float(hours)
parser = argparse.ArgumentParser(prog='csv_to_invoice', description='Convert a CSV file from Redmine into a FreshBooks invoice.')
parser.add_argument('-r', '--rate', required=True, help='rate (in your base currency) per hour')
parser.add_argument('-c', '--client', required=True, help='FreshBooks ID of the client to bill (see list_clients)')
parser.add_argument('-f', '--filename', required=True, help='CSV file to read data from')
parser.add_argument('-d', '--date', help='date of record for the invoice (defaults to today)')
args = parser.parse_args()
if args.date:
date = args.date
else:
date = datetime.date.today().strftime('%Y-%m-%d')
lines = []
with open(args.filename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
header_read = False
for row in reader:
if not header_read:
header_read = True
continue
try:
ticket = row[4].split(' ')[1][1:-1]
except:
ticket = ''
line = HourLine(name=row[2], date=row[1], category=row[3], ticket=ticket, notes=row[5], hours=row[6])
description = "[" + line.date + " - " + line.name + "]"
if hasattr(line, 'ticket'):
if line.ticket.isdigit():
description = description + " " + redmine_base + "/" + line.ticket
if hasattr(line, 'notes'):
description = description + " " + line.notes
lines.append(fb.FBLineItem(name=line.category, unit_cost=args.rate, quantity=line.hours, description=description))
api = fb.FBAPI(url, token)
invoice = fb.FBInvoice(client_id=args.client, date=date, lines=lines)
xml = invoice.to_xml()
api.x_request('invoice.create', xml)