1616 help = "Shows a very detailed list of charges" )
1717@environment .pass_env
1818def 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 )
0 commit comments