|
| 1 | +""" |
| 2 | + SoftLayer.billing |
| 3 | + ~~~~~~~~~~~~ |
| 4 | + Billing Manager/helpers |
| 5 | +
|
| 6 | + :license: MIT, see LICENSE for more details. |
| 7 | +""" |
| 8 | +import calendar |
| 9 | +import datetime |
| 10 | + |
| 11 | +from SoftLayer import utils |
| 12 | + |
| 13 | +import math |
| 14 | +import time |
| 15 | + |
| 16 | + |
| 17 | +class BillingManager(object): |
| 18 | + """Manages Billing details. |
| 19 | +
|
| 20 | + :param SoftLayer.managers.orderingManager |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, client): |
| 24 | + """init |
| 25 | +
|
| 26 | + :param client |
| 27 | + :return: billing account info |
| 28 | + """ |
| 29 | + self.client = client |
| 30 | + self.account = self.client['Account'] |
| 31 | + self.billing_order = self.client['Billing_Order'] |
| 32 | + |
| 33 | + def get_info(self): |
| 34 | + """get info |
| 35 | +
|
| 36 | + :return: billing account info |
| 37 | + """ |
| 38 | + result = self.account.getBillingInfo() |
| 39 | + return result |
| 40 | + |
| 41 | + def get_balance(self): |
| 42 | + """get balance |
| 43 | +
|
| 44 | + :return: billing account info |
| 45 | + """ |
| 46 | + result = self.account.getBalance() |
| 47 | + return result |
| 48 | + |
| 49 | + def get_next_balance(self): |
| 50 | + """get next balance |
| 51 | +
|
| 52 | + :return: billing account info |
| 53 | + """ |
| 54 | + result = self.account.getNextInvoiceTotalAmount() |
| 55 | + return result |
| 56 | + |
| 57 | + def get_latest_bill_date(self): |
| 58 | + """get latest balance |
| 59 | +
|
| 60 | + :return: billing account info |
| 61 | + """ |
| 62 | + result = self.account.getLatestBillDate() |
| 63 | + return result |
| 64 | + |
| 65 | + def get_summary(self): |
| 66 | + """get summary |
| 67 | +
|
| 68 | + :return: billing account info |
| 69 | + """ |
| 70 | + result = { |
| 71 | + 'lastbilldate': self.get_latest_bill_date(), |
| 72 | + 'balance': self.get_balance(), |
| 73 | + 'nextbalance': self.get_next_balance() |
| 74 | + } |
| 75 | + return result |
| 76 | + |
| 77 | + def get_next_bill_items(self): |
| 78 | + """get next bill items |
| 79 | +
|
| 80 | + :return: billing account info |
| 81 | + """ |
| 82 | + result = self.account.getAllBillingItems() |
| 83 | + return result |
| 84 | + |
| 85 | + def list_resources(self, from_date=None, to_date=None, **kwargs): |
| 86 | + """Retrieve a list of all ordered resources along with their costing. |
| 87 | +
|
| 88 | + :param dict \\*\\*kwargs: response-level option (limit) |
| 89 | + :returns: Returns a list of dictionaries representing the |
| 90 | + matching ordered resorces by a user along with their costing |
| 91 | + """ |
| 92 | + params = {} |
| 93 | + if 'mask' not in kwargs: |
| 94 | + items = set([ |
| 95 | + 'order[items[description,billingItem[id,hostName,' |
| 96 | + 'hourlyRecurringFee,cancellationDate,createDate,' |
| 97 | + 'laborFee,oneTimeFee,setupFee]]]' |
| 98 | + ]) |
| 99 | + params['mask'] = "mask[%s]" % ','.join(items) |
| 100 | + |
| 101 | + _filter = utils.NestedDict({}) |
| 102 | + user = self.account.getCurrentUser(mask='mask[id]') |
| 103 | + _filter['orders']['userRecordId'] = utils.query_filter(user['id']) |
| 104 | + date_format = '%Y-%m-%d' |
| 105 | + |
| 106 | + if from_date and to_date: |
| 107 | + _filter['orders']['createDate'] = utils.query_filter_date( |
| 108 | + from_date, to_date) |
| 109 | + elif from_date: |
| 110 | + from_date_filter = '>=' + ' ' + from_date |
| 111 | + _filter['orders']['createDate'] = utils.query_filter( |
| 112 | + from_date_filter) |
| 113 | + elif to_date: |
| 114 | + to_date_filter = '<=' + ' ' + to_date |
| 115 | + _filter['orders']['createDate'] = utils.query_filter( |
| 116 | + to_date_filter) |
| 117 | + orders = self.account.getOrders(filter=_filter.to_dict()) |
| 118 | + result = [] |
| 119 | + |
| 120 | + for order in orders: |
| 121 | + billing_order = self.client['Billing_Order'] |
| 122 | + params['id'] = order_id = order['id'] |
| 123 | + items = billing_order.getItems(**params) |
| 124 | + cost = float(0.0) |
| 125 | + resource_type = '' |
| 126 | + flag = 1 |
| 127 | + hostname = '' |
| 128 | + creation_date = '' |
| 129 | + |
| 130 | + for item in items: |
| 131 | + if flag == 1: |
| 132 | + resource_type = item['description'] |
| 133 | + |
| 134 | + if 'Core' in resource_type and flag == 1: |
| 135 | + hostname = item['billingItem']['hostName'] |
| 136 | + flag = 0 |
| 137 | + usedmonths = 1 |
| 138 | + |
| 139 | + creation_date = item['billingItem']['createDate'] |
| 140 | + cancellation_date = item['billingItem']['cancellationDate'] |
| 141 | + |
| 142 | + if 'hourlyRecurringFee' not in item['billingItem']: |
| 143 | + create_date = datetime.datetime.strptime( |
| 144 | + format_date( |
| 145 | + item['billingItem']['createDate']), date_format) |
| 146 | + if cancellation_date: |
| 147 | + cancel_date = datetime.datetime.strptime( |
| 148 | + format_date( |
| 149 | + item['billingItem']['cancellationDate']), |
| 150 | + date_format) |
| 151 | + usedmonths = get_month_delta(create_date, cancel_date) |
| 152 | + else: |
| 153 | + now = datetime.datetime.strptime( |
| 154 | + format_date(str(datetime.datetime.now())), |
| 155 | + date_format) |
| 156 | + usedmonths = get_month_delta(create_date, now) |
| 157 | + |
| 158 | + usedmonths += 1 |
| 159 | + |
| 160 | + cost += float( |
| 161 | + billing_order.getOrderTotalOneTime( |
| 162 | + id=order['id'] |
| 163 | + ) + billing_order.getOrderTotalRecurring( |
| 164 | + id=order['id'] |
| 165 | + ) * usedmonths |
| 166 | + ) |
| 167 | + |
| 168 | + elif not cancellation_date: |
| 169 | + virtual_guest = self.account.getHourlyVirtualGuests( |
| 170 | + filter={ |
| 171 | + 'hourlyVirtualGuests': { |
| 172 | + 'hourlyVirtualGuests': { |
| 173 | + 'billingItem': { |
| 174 | + 'id': { |
| 175 | + 'operation': |
| 176 | + item['billingItem']['id']}}}}}) |
| 177 | + if virtual_guest: |
| 178 | + guest = virtual_guest[0] |
| 179 | + cost = guest['billingItem']['currentHourlyCharge'] |
| 180 | + |
| 181 | + else: |
| 182 | + create_date = datetime.datetime.strptime( |
| 183 | + format_date(creation_date), date_format) |
| 184 | + cancel_date = datetime.datetime.strptime( |
| 185 | + format_date(cancellation_date), date_format) |
| 186 | + d1_ts = time.mktime(create_date.timetuple()) |
| 187 | + d2_ts = time.mktime(cancel_date.timetuple()) |
| 188 | + usedhours = math.ceil(d2_ts - d1_ts)/3600 |
| 189 | + cost += usedhours * float( |
| 190 | + item['billingItem']['hourlyRecurringFee'] |
| 191 | + ) + float( |
| 192 | + item['billingItem']['laborFee'] |
| 193 | + ) + float( |
| 194 | + item['billingItem']['oneTimeFee'] |
| 195 | + ) + float( |
| 196 | + item['billingItem']['setupFee']) |
| 197 | + resource = { |
| 198 | + 'id': order_id, |
| 199 | + 'resourceType': resource_type, |
| 200 | + 'hostName': hostname, |
| 201 | + 'createDate': creation_date, |
| 202 | + 'cost': cost |
| 203 | + } |
| 204 | + result.append(resource) |
| 205 | + return result |
| 206 | + |
| 207 | + |
| 208 | +def format_date(date): |
| 209 | + """format date. |
| 210 | +
|
| 211 | + :param date: YY-MM-DD |
| 212 | + """ |
| 213 | + result = date.replace('T', ' ') |
| 214 | + return result[0:10] |
| 215 | + |
| 216 | + |
| 217 | +def get_month_delta(date1, date2): |
| 218 | + """get month |
| 219 | +
|
| 220 | + :param date1: YY-MM-DD |
| 221 | + :param date2: YY-MM-DD |
| 222 | + :return: delta |
| 223 | + """ |
| 224 | + delta = 0 |
| 225 | + while True: |
| 226 | + mdays = calendar.monthrange(date1.year, date1.month)[1] |
| 227 | + date1 += datetime.timedelta(days=mdays) |
| 228 | + if date1 <= date2: |
| 229 | + delta += 1 |
| 230 | + else: |
| 231 | + break |
| 232 | + return delta |
0 commit comments