forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilling.py
More file actions
232 lines (193 loc) · 7.38 KB
/
billing.py
File metadata and controls
232 lines (193 loc) · 7.38 KB
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
"""
SoftLayer.billing
~~~~~~~~~~~~
Billing Manager/helpers
:license: MIT, see LICENSE for more details.
"""
import calendar
import datetime
from SoftLayer import utils
import math
import time
class BillingManager(object):
"""Manages Billing details.
:param SoftLayer.managers.orderingManager
"""
def __init__(self, client):
"""init
:param client
:return: billing account info
"""
self.client = client
self.account = self.client['Account']
self.billing_order = self.client['Billing_Order']
def get_info(self):
"""get info
:return: billing account info
"""
result = self.account.getBillingInfo()
return result
def get_balance(self):
"""get balance
:return: billing account info
"""
result = self.account.getBalance()
return result
def get_next_balance(self):
"""get next balance
:return: billing account info
"""
result = self.account.getNextInvoiceTotalAmount()
return result
def get_latest_bill_date(self):
"""get latest balance
:return: billing account info
"""
result = self.account.getLatestBillDate()
return result
def get_summary(self):
"""get summary
:return: billing account info
"""
result = {
'lastbilldate': self.get_latest_bill_date(),
'balance': self.get_balance(),
'nextbalance': self.get_next_balance()
}
return result
def get_next_bill_items(self):
"""get next bill items
:return: billing account info
"""
result = self.account.getAllBillingItems()
return result
def list_resources(self, from_date=None, to_date=None, **kwargs):
"""Retrieve a list of all ordered resources along with their costing.
:param dict \\*\\*kwargs: response-level option (limit)
:returns: Returns a list of dictionaries representing the
matching ordered resorces by a user along with their costing
"""
params = {}
if 'mask' not in kwargs:
items = set([
'order[items[description,billingItem[id,hostName,'
'hourlyRecurringFee,cancellationDate,createDate,'
'laborFee,oneTimeFee,setupFee]]]'
])
params['mask'] = "mask[%s]" % ','.join(items)
_filter = utils.NestedDict({})
user = self.account.getCurrentUser(mask='mask[id]')
_filter['orders']['userRecordId'] = utils.query_filter(user['id'])
date_format = '%Y-%m-%d'
if from_date and to_date:
_filter['orders']['createDate'] = utils.query_filter_date(
from_date, to_date)
elif from_date:
from_date_filter = '>=' + ' ' + from_date
_filter['orders']['createDate'] = utils.query_filter(
from_date_filter)
elif to_date:
to_date_filter = '<=' + ' ' + to_date
_filter['orders']['createDate'] = utils.query_filter(
to_date_filter)
orders = self.account.getOrders(filter=_filter.to_dict())
result = []
for order in orders:
billing_order = self.client['Billing_Order']
params['id'] = order_id = order['id']
items = billing_order.getItems(**params)
cost = float(0.0)
resource_type = ''
flag = 1
hostname = ''
creation_date = ''
for item in items:
if flag == 1:
resource_type = item['description']
if 'Core' in resource_type and flag == 1:
hostname = item['billingItem']['hostName']
flag = 0
usedmonths = 1
creation_date = item['billingItem']['createDate']
cancellation_date = item['billingItem']['cancellationDate']
if 'hourlyRecurringFee' not in item['billingItem']:
create_date = datetime.datetime.strptime(
format_date(
item['billingItem']['createDate']), date_format)
if cancellation_date:
cancel_date = datetime.datetime.strptime(
format_date(
item['billingItem']['cancellationDate']),
date_format)
usedmonths = get_month_delta(create_date, cancel_date)
else:
now = datetime.datetime.strptime(
format_date(str(datetime.datetime.now())),
date_format)
usedmonths = get_month_delta(create_date, now)
usedmonths += 1
cost += float(
billing_order.getOrderTotalOneTime(
id=order['id']
) + billing_order.getOrderTotalRecurring(
id=order['id']
) * usedmonths
)
elif not cancellation_date:
virtual_guest = self.account.getHourlyVirtualGuests(
filter={
'hourlyVirtualGuests': {
'hourlyVirtualGuests': {
'billingItem': {
'id': {
'operation':
item['billingItem']['id']}}}}})
if virtual_guest:
guest = virtual_guest[0]
cost = guest['billingItem']['currentHourlyCharge']
else:
create_date = datetime.datetime.strptime(
format_date(creation_date), date_format)
cancel_date = datetime.datetime.strptime(
format_date(cancellation_date), date_format)
d1_ts = time.mktime(create_date.timetuple())
d2_ts = time.mktime(cancel_date.timetuple())
usedhours = math.ceil(d2_ts - d1_ts)/3600
cost += usedhours * float(
item['billingItem']['hourlyRecurringFee']
) + float(
item['billingItem']['laborFee']
) + float(
item['billingItem']['oneTimeFee']
) + float(
item['billingItem']['setupFee'])
resource = {
'id': order_id,
'resourceType': resource_type,
'hostName': hostname,
'createDate': creation_date,
'cost': cost
}
result.append(resource)
return result
def format_date(date):
"""format date.
:param date: YY-MM-DD
"""
result = date.replace('T', ' ')
return result[0:10]
def get_month_delta(date1, date2):
"""get month
:param date1: YY-MM-DD
:param date2: YY-MM-DD
:return: delta
"""
delta = 0
while True:
mdays = calendar.monthrange(date1.year, date1.month)[1]
date1 += datetime.timedelta(days=mdays)
if date1 <= date2:
delta += 1
else:
break
return delta