-
Notifications
You must be signed in to change notification settings - Fork 7
/
invoiceninja.py
124 lines (105 loc) · 4.44 KB
/
invoiceninja.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
# -*- coding: utf8 -*-
#
# Copyright (C) 2016 Scifabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
import requests
import datetime
from dateutil.relativedelta import relativedelta
class invoiceNinja(object):
"""Class for handling Invoice Ninja API."""
def __init__(self, token, url='https://app.invoiceninja.com/api/v1/'):
"""Add Invoice Ninja token."""
self.token = token
self.url = url
self.headers = {'X-Ninja-Token': self.token}
self.static = self.get_static_data()
self.client = None
self.invoice = None
def get_static_data(self):
"""Get static data from Invoice Ninja."""
res = requests.get(self.url + 'static', headers=self.headers)
if res.status_code == 200:
return res.json()['data']
def exists_client(self, client):
"""Return True if client exists."""
suburl = 'clients?email=' + client['contact']['email']
res = requests.get(self.url + suburl, json=client, headers=self.headers)
if res.status_code == 200:
data = res.json()
if len(data['data']) > 0:
if (len(data['data']) >= 1) and (data['data'][0]['is_deleted'] == True):
return False
d = dict()
d['data'] = data['data'][0]
self.client = d
return d
else:
return False
return False
def create_client(self, client):
"""Create a client in Invoice Ninja."""
client_data = self.exists_client(client)
if not client_data:
res = requests.post(self.url + 'clients', json=client, headers=self.headers)
if res.status_code == 200:
self.client = res.json()
return self.client
else:
return res.json()
else:
return client_data
def create_invoice(self, product):
"""Create an invoice for a client."""
product['client_id'] = self.client['data']['id']
res = requests.post(self.url + 'invoices?include=invitations', json=product,
headers=self.headers)
if res.status_code == 200:
self.invoice = res.json()
return self.invoice
else:
return res.json()
def create_recurring_invoice(self, product):
"""Create a recurring invoice for a client."""
product['is_recurring'] = True
product['client_id'] = self.client['data']['id']
product['auto_bill'] = True
today = datetime.datetime.now().date()
product['start_date'] = today.isoformat()
if product['recurring'] == 'monthly':
end_date = (today + datetime.timedelta(days=365)).isoformat()
end_date = (today + relativedelta(months=1)).isoformat()
product['end_date'] = end_date
product['frequency_id'] = self.get_frequency_id('monthly')
if product['recurring'] == 'annually':
end_date = (today + relativedelta(years=1)).isoformat()
product['end_date'] = end_date
product['frequency_id'] = self.get_frequency_id('annually')
del product['recurring']
res = requests.post(self.url + 'invoices?include=invitations', json=product,
headers=self.headers)
if res.status_code == 200:
self.invoice = res.json()
return self.invoice
else:
return res.json()
def checkout(self, client, product): # pragma: no cover
"""Create a client and an invoice for the client."""
self.create_client(client)
self.create_invoice(product)
def get_frequency_id(self, name):
"""Return frequency ID for name."""
for f in self.static['frequencies']:
if f['name'].lower() == name.lower():
return f['id']