Skip to content

Commit e9c1b1e

Browse files
Fixed #884. Sums up invoice detail children for the top level item. Child prices were also incorrectly being dispalyed as their parent price
1 parent 8d73c05 commit e9c1b1e

4 files changed

Lines changed: 64 additions & 19 deletions

File tree

plugin/commands/account/account_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
55
. "github.com/onsi/ginkgo/v2"
66
. "github.com/onsi/gomega"
7+
"github.com/onsi/gomega/format"
78
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/account"
89
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
910
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
@@ -33,8 +34,11 @@ var availableCommands = []string{
3334
"summary",
3435
}
3536

37+
3638
// This test suite exists to make sure commands don't get accidently removed from the actionBindings
3739
var _ = Describe("Test account.GetCommandActionBindings()", func() {
40+
format.TruncatedDiff = false
41+
format.UseStringerRepresentation = false
3842
fakeUI := terminal.NewFakeUI()
3943
fakeSession := testhelpers.NewFakeSoftlayerSession(nil)
4044
slMeta := metadata.NewSoftlayerCommand(fakeUI, fakeSession)

plugin/commands/account/invoice-detail.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,36 @@ func PrintInvoiceDetail(invoiceID int, invoice []datatypes.Billing_Invoice_Item,
8686
if invoiceDetail.Location != nil {
8787
location = utils.FormatStringPointer(invoiceDetail.Location.Name)
8888
}
89+
oneTime, recurring := SumChildItems(invoiceDetail)
8990
table.Add(
9091
utils.FormatIntPointer(invoiceDetail.Id),
9192
Category,
9293
utils.ShortenString(Description),
93-
fmt.Sprintf("%.2f", *invoiceDetail.OneTimeAfterTaxAmount),
94-
fmt.Sprintf("%.2f", *invoiceDetail.RecurringAfterTaxAmount),
95-
utils.FormatSLTimePointer(invoiceDetail.CreateDate),
94+
fmt.Sprintf("%.2f", oneTime),
95+
fmt.Sprintf("%.2f", recurring),
96+
utils.FormatSLTimePointerCustom(invoiceDetail.CreateDate, "2006-01-02"),
9697
location,
9798
)
9899
if details {
100+
// Add in the parent row if we are doing details, so its obvious how the top line item adds up.
101+
if len(invoiceDetail.Children) > 0 {
102+
table.Add(
103+
">>>",
104+
Category,
105+
utils.ShortenString(Description),
106+
fmt.Sprintf("%.2f", *invoiceDetail.OneTimeAfterTaxAmount),
107+
fmt.Sprintf("%.2f", *invoiceDetail.RecurringAfterTaxAmount),
108+
"---",
109+
"---",
110+
)
111+
}
99112
for _, child := range invoiceDetail.Children {
100113
table.Add(
101114
">>>",
102115
utils.FormatStringPointer(child.Category.Name),
103116
utils.ShortenString(utils.FormatStringPointer(child.Description)),
104-
fmt.Sprintf("%.2f", *invoiceDetail.OneTimeAfterTaxAmount),
105-
fmt.Sprintf("%.2f", *invoiceDetail.RecurringAfterTaxAmount),
117+
fmt.Sprintf("%.2f", *child.OneTimeAfterTaxAmount),
118+
fmt.Sprintf("%.2f", *child.RecurringAfterTaxAmount),
106119
"---",
107120
"---",
108121
)
@@ -111,3 +124,16 @@ func PrintInvoiceDetail(invoiceID int, invoice []datatypes.Billing_Invoice_Item,
111124
}
112125
utils.PrintTable(ui, table, outputFormat)
113126
}
127+
128+
func SumChildItems(item datatypes.Billing_Invoice_Item) (oneTime float64, recurring float64) {
129+
oneTime = float64((*item.OneTimeAfterTaxAmount))
130+
recurring = float64((*item.RecurringAfterTaxAmount))
131+
132+
for _, child := range item.Children {
133+
oneTime += float64((*child.OneTimeAfterTaxAmount))
134+
recurring += float64((*child.RecurringAfterTaxAmount))
135+
}
136+
return oneTime, recurring
137+
138+
139+
}

plugin/commands/account/invoice-detail_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,33 @@ var _ = Describe("Account list InvoiceDetail", func() {
5555
It("return account invoice detail", func() {
5656
err := testhelpers.RunCobraCommand(cliCommand.Command, "123")
5757
Expect(err).NotTo(HaveOccurred())
58-
Expect(fakeUI.Outputs()).To(ContainSubstring("Item Id Category Description Single Monthly Create Date Location"))
59-
Expect(fakeUI.Outputs()).To(ContainSubstring("123456789 Server Dual Intel Xeon Silver 4210 (20 Cores, 2.20 GHz) (test-gpu.softlayer-community-f... 10.23 20.34 2022-04-04T05:10:20Z mex01"))
60-
Expect(fakeUI.Outputs()).To(ContainSubstring("123456789123 server Dual E5-2690 v3 (12 Cores, 2.60 GHz) (test-vs.support2.com) 11.23 21.12 2022-04-04T05:10:21Z ams01"))
58+
Expect(fakeUI.Outputs()).To(ContainSubstring("Item Id Category Description Single Monthly Create Date Location"))
59+
Expect(fakeUI.Outputs()).To(ContainSubstring("123456789 Server Dual Intel Xeon Silver 4210 (20 Cores, 2.20 GHz) (test-gpu.softlayer-community-f... 22.59 35.26 2022-04-04 mex01"))
60+
Expect(fakeUI.Outputs()).To(ContainSubstring("123456789123 server Dual E5-2690 v3 (12 Cores, 2.60 GHz) (test-vs.support2.com) 23.81 36.04 2022-04-04 ams01"))
6161
})
6262
It("return account invoice detail with additionals details", func() {
6363
err := testhelpers.RunCobraCommand(cliCommand.Command, "123", "--details")
6464
Expect(err).NotTo(HaveOccurred())
65-
Expect(fakeUI.Outputs()).To(ContainSubstring("Item Id Category Description Single Monthly Create Date Location"))
66-
Expect(fakeUI.Outputs()).To(ContainSubstring("123456789 Server Dual Intel Xeon Silver 4210 (20 Cores, 2.20 GHz) (test-gpu.softlayer-community-f... 10.23 20.34 2022-04-04T05:10:20Z mex01"))
67-
Expect(fakeUI.Outputs()).To(ContainSubstring(">>> Second Processor Intel Xeon (12 Cores, 2.40 GHz) 10.23 20.34 --- ---"))
68-
Expect(fakeUI.Outputs()).To(ContainSubstring(">>> Operating System Virtual (up to 1Gbps) 10.23 20.34 --- ---"))
69-
Expect(fakeUI.Outputs()).To(ContainSubstring("123456789123 server Dual E5-2690 v3 (12 Cores, 2.60 GHz) (test-vs.support2.com) 11.23 21.12 2022-04-04T05:10:21Z ams01"))
70-
Expect(fakeUI.Outputs()).To(ContainSubstring(">>> Second Processor Intel Xeon (12 Cores, 2.40 GHz) 11.23 21.12 --- ---"))
71-
Expect(fakeUI.Outputs()).To(ContainSubstring(">>> Operating System Virtual (up to 1Gbps) 11.23 21.12 --- ---"))
65+
Expect(fakeUI.Outputs()).To(Equal(
66+
`Item Id Category Description Single Monthly Create Date Location
67+
123456789 Server Dual Intel Xeon Silver 4210 (20 Cores, 2.20 GHz) (test-gpu.softlayer-community-f... 22.59 35.26 2022-04-04 mex01
68+
>>> Server Dual Intel Xeon Silver 4210 (20 Cores, 2.20 GHz) (test-gpu.softlayer-community-f... 10.23 20.34 --- ---
69+
>>> Second Processor Intel Xeon (12 Cores, 2.40 GHz) 5.24 6.12 --- ---
70+
>>> Operating System Virtual (up to 1Gbps) 7.12 8.79 --- ---
71+
123456789123 server Dual E5-2690 v3 (12 Cores, 2.60 GHz) (test-vs.support2.com) 23.81 36.04 2022-04-04 ams01
72+
>>> server Dual E5-2690 v3 (12 Cores, 2.60 GHz) (test-vs.support2.com) 11.23 21.12 --- ---
73+
>>> Second Processor Intel Xeon (12 Cores, 2.40 GHz) 5.35 6.23 --- ---
74+
>>> Operating System Virtual (up to 1Gbps) 7.23 8.68 --- ---
75+
`))
7276
})
7377
It("return account invoice detail in format json", func() {
7478
err := testhelpers.RunCobraCommand(cliCommand.Command, "123", "--output", "json")
7579
Expect(err).NotTo(HaveOccurred())
7680
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Item Id": "123456789",`))
7781
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Category": "Server",`))
7882
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Description": "Dual Intel Xeon Silver 4210 (20 Cores, 2.20 GHz) (test-gpu.softlayer-community-f...",`))
79-
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Single": "10.23",`))
80-
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Monthly": "20.34",`))
83+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Single": "22.59",`))
84+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Monthly": "35.26",`))
8185
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Location": "mex01"`))
8286
})
8387
})
@@ -92,8 +96,8 @@ var _ = Describe("Account list InvoiceDetail", func() {
9296
err := testhelpers.RunCobraCommand(cliCommand.Command, "888")
9397
Expect(err).NotTo(HaveOccurred())
9498
output := fakeUI.Outputs()
95-
Expect(output).To(ContainSubstring("2020-05-04T05:11:25Z None"))
96-
Expect(output).To(ContainSubstring("2020-05-04T05:11:25Z tok02"))
99+
Expect(output).To(ContainSubstring("2020-05-04 None"))
100+
Expect(output).To(ContainSubstring("0.00 0.00 2020-05-04 tok02"))
97101
})
98102
})
99103
})

plugin/utils/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ func FormatSLTimePointer(value *datatypes.Time) string {
251251
return value.UTC().Format(time.RFC3339)
252252
}
253253

254+
// Date formats are in '2006-01-02 15:04:05' format
255+
func FormatSLTimePointerCustom(value *datatypes.Time, time_format string) string {
256+
if value == nil {
257+
return EMPTY_VALUE
258+
}
259+
if time_format == "" {
260+
time_format = time.RFC3339
261+
}
262+
return value.UTC().Format(time_format)
263+
}
264+
254265
func Bool2Int(value bool) int {
255266
if value {
256267
return 1

0 commit comments

Comments
 (0)