Skip to content

Commit 413fa88

Browse files
Fixed #2201 Added invoice item rollup to 'account invoice-detail' to better match invoices as dispalyed in the portal
1 parent e7de42b commit 413fa88

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

SoftLayer/CLI/account/invoice_detail.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
help="Shows a very detailed list of charges")
1717
@environment.pass_env
1818
def cli(env, identifier, details):
19-
"""Invoice details"""
19+
"""Invoice details
20+
21+
Will display the top level invoice items for a given invoice. The cost displayed is the sum of the item's
22+
cost along with all its child items.
23+
The --details option will display any child items a top level item may have. Parent items will appear
24+
in this list as well to display their specific cost.
25+
"""
2026

2127
manager = AccountManager(env.client)
2228
top_items = manager.get_billing_items(identifier)
@@ -49,16 +55,31 @@ def get_invoice_table(identifier, top_items, details):
4955
description = nice_string(item.get('description'))
5056
if fqdn != '.':
5157
description = "%s (%s)" % (item.get('description'), fqdn)
58+
total_recur, total_single = sum_item_charges(item)
5259
table.add_row([
5360
item.get('id'),
5461
category,
5562
nice_string(description),
56-
"$%.2f" % float(item.get('oneTimeAfterTaxAmount')),
57-
"$%.2f" % float(item.get('recurringAfterTaxAmount')),
63+
f"${total_single:,.2f}",
64+
f"${total_recur:,.2f}",
5865
utils.clean_time(item.get('createDate'), out_format="%Y-%m-%d"),
5966
utils.lookup(item, 'location', 'name')
6067
])
6168
if details:
69+
# This item has children, so we want to print out the parent item too. This will match the
70+
# invoice from the portal. https://github.com/softlayer/softlayer-python/issues/2201
71+
if len(item.get('children')) > 0:
72+
single = float(item.get('oneTimeAfterTaxAmount', 0.0))
73+
recurring = float(item.get('recurringAfterTaxAmount', 0.0))
74+
table.add_row([
75+
'>>>',
76+
category,
77+
nice_string(description),
78+
f"${single:,.2f}",
79+
f"${recurring:,.2f}",
80+
'---',
81+
'---'
82+
])
6283
for child in item.get('children', []):
6384
table.add_row([
6485
'>>>',
@@ -70,3 +91,16 @@ def get_invoice_table(identifier, top_items, details):
7091
'---'
7192
])
7293
return table
94+
95+
96+
def sum_item_charges(item: dict) -> (float, float):
97+
"""Takes a billing Item, sums up its child items and returns recurring, one_time prices"""
98+
99+
# API returns floats as strings in this case
100+
single = float(item.get('oneTimeAfterTaxAmount', 0.0))
101+
recurring = float(item.get('recurringAfterTaxAmount', 0.0))
102+
for child in item.get('children', []):
103+
single = single + float(child.get('oneTimeAfterTaxAmount', 0.0))
104+
recurring = recurring + float(child.get('recurringAfterTaxAmount', 0.0))
105+
106+
return (recurring, single)

SoftLayer/testing/xmlrpc.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
print(f"Server running on http://{my_server.server_name}:{my_server.server_port}")
1616
---
1717
$> python quick-server.py
18-
$> curl -X POST -d "<?xml version='1.0' encoding='iso-8859-1'?><methodCall><methodName>getInvoiceTopLevelItems</methodName><params><param><value><struct><member><name>headers</name><value><struct><member><name>SoftLayer_Billing_InvoiceInitParameters</name><value><struct><member><name>id</name><value><string>1234</string></value></member></struct></value></member></struct></value></member></struct></value></param></params></methodCall>" http://127.0.0.1:4321/SoftLayer_Billing_Invoice
18+
$> curl -X POST -d "<?xml version='1.0' encoding='iso-8859-1'?><methodCall><methodName> \
19+
getInvoiceTopLevelItems</methodName><params><param><value><struct><member><name>headers</name> \
20+
<value><struct><member><name>SoftLayer_Billing_InvoiceInitParameters</name><value><struct> \
21+
<member><name>id</name><value><string>1234</string></value></member></struct></value></member> \
22+
</struct></value></member></struct></value></param></params></methodCall>" \
23+
http://127.0.0.1:4321/SoftLayer_Billing_Invoice
1924
2025
:license: MIT, see LICENSE for more details.
2126
"""
@@ -99,10 +104,10 @@ def do_POST(self):
99104
self.end_headers()
100105
response_body = '''<error>OverflowError in XML response.</error>'''
101106
self.wfile.write(response_body.encode('utf-8'))
102-
logging.exception(f"Error while handling request: {str(ex)}")
107+
logging.exception("Error while handling request: %s", ex)
103108
except Exception as ex:
104109
self.send_response(500)
105-
logging.exception(f"Error while handling request: {str(ex)}")
110+
logging.exception("Error while handling request: %s", ex)
106111

107112
def log_message(self, fmt, *args):
108113
"""Override log_message."""

tests/CLI/modules/account_tests.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ def test_event_jsonraw_output(self):
4444
command = '--format jsonraw account events'
4545
command_params = command.split()
4646
result = self.run_command(command_params)
47-
4847
json_text_tables = result.stdout.split('\n')
49-
print(f"RESULT: {result.output}")
5048
# removing an extra item due to an additional Newline at the end of the output
5149
json_text_tables.pop()
5250
# each item in the json_text_tables should be a list
5351
for json_text_table in json_text_tables:
54-
print(f"TESTING THIS: \n{json_text_table}\n")
5552
json_table = json.loads(json_text_table)
5653
self.assertIsInstance(json_table, list)
5754

@@ -66,6 +63,18 @@ def test_invoice_detail_details(self):
6663
self.assert_no_fail(result)
6764
self.assert_called_with('SoftLayer_Billing_Invoice', 'getInvoiceTopLevelItems', identifier='1234')
6865

66+
def test_invoice_detail_sum_children(self):
67+
result = self.run_command(['--format=json', 'account', 'invoice-detail', '1234', '--details'])
68+
self.assert_no_fail(result)
69+
json_out = json.loads(result.output)
70+
self.assertEqual(len(json_out), 7)
71+
self.assertEqual(json_out[0]['Item Id'], 724951323)
72+
self.assertEqual(json_out[0]['Single'], '$55.50')
73+
self.assertEqual(json_out[0]['Monthly'], '$0.10')
74+
self.assertEqual(json_out[3]['Item Id'], 1111222)
75+
self.assertEqual(json_out[3]['Single'], '$0.00')
76+
self.assertEqual(json_out[3]['Monthly'], '$30.36')
77+
6978
def test_invoice_detail_csv_output_format(self):
7079
result = self.run_command(["--format", "csv", 'account', 'invoice-detail', '1234'])
7180
result_output = result.output.replace('\r', '').split('\n')
@@ -74,7 +83,7 @@ def test_invoice_detail_csv_output_format(self):
7483
'"Create Date","Location"')
7584
self.assertEqual(result_output[1], '724951323,"Private (only) Secondary VLAN IP Addresses",'
7685
'"64 Portable Private IP Addresses (bleg.beh.com)",'
77-
'"$0.00","$0.00","2018-04-04","fra02"')
86+
'"$55.50","$0.10","2018-04-04","fra02"')
7887

7988
# slcli account invoices
8089
def test_invoices(self):

0 commit comments

Comments
 (0)