16
16
help = "Shows a very detailed list of charges" )
17
17
@environment .pass_env
18
18
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
+ """
20
26
21
27
manager = AccountManager (env .client )
22
28
top_items = manager .get_billing_items (identifier )
@@ -49,16 +55,31 @@ def get_invoice_table(identifier, top_items, details):
49
55
description = nice_string (item .get ('description' ))
50
56
if fqdn != '.' :
51
57
description = "%s (%s)" % (item .get ('description' ), fqdn )
58
+ total_recur , total_single = sum_item_charges (item )
52
59
table .add_row ([
53
60
item .get ('id' ),
54
61
category ,
55
62
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} " ,
58
65
utils .clean_time (item .get ('createDate' ), out_format = "%Y-%m-%d" ),
59
66
utils .lookup (item , 'location' , 'name' )
60
67
])
61
68
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
+ ])
62
83
for child in item .get ('children' , []):
63
84
table .add_row ([
64
85
'>>>' ,
@@ -70,3 +91,16 @@ def get_invoice_table(identifier, top_items, details):
70
91
'---'
71
92
])
72
93
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 )
0 commit comments