forked from nextgenhealthcare/cloudhealth-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
customer_statements.go
90 lines (82 loc) · 2.97 KB
/
customer_statements.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cloudhealth
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// BillingArtifacts represents all Customer Statements enabled in CloudHealth with their details.
type BillingArtifacts struct {
BillingArtifacts []BillingArtifact `json:"billing_artifacts"`
}
// BillingArtifact represents the configuration of a Customer Statement in CloudHealth with its details.
type BillingArtifact struct {
CustomerID int `json:"customer_id"`
CloudProvider string `json:"cloud"`
BillingPeriod string `json:"billing_period"`
TotalAmount float64 `json:"total_amount"`
Status string `json:"status"`
DetailedBillingRecordsGenerationTime string `json:"detailed_billing_records_generation_time"`
StatementGenerationTime string `json:"statement_generation_time"`
StatementSummaryGenerationTime string `json:"statement_summary_generation_time"`
Currency Currency `json:"currency"`
InvoiceId string `json:"invoice_id,omitempty"`
InvoiceDate string `json:"invoice_date,omitempty"`
}
// Currency represents the currency used for billing.
type Currency struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
}
// GetSingleCustomerStatements gets all statements for a specific Customer ID.
func (s *Client) GetSingleCustomerStatements(id int) (*BillingArtifacts, error) {
var billingArtifacts = new(BillingArtifacts)
page := 1
for {
params := url.Values{"page": {strconv.Itoa(page)}, "per_page": {"100"}, "api_key": {s.APIKey}}
relativeURL, _ := url.Parse(fmt.Sprintf("customer_statements/?client_api_id=%d&%s", id, params.Encode()))
responseBody, err := getResponsePage(s, relativeURL)
if err != nil {
return nil, err
}
var ba = new(BillingArtifacts)
err = json.Unmarshal(responseBody, &ba)
if err != nil {
return nil, err
}
for _, a := range ba.BillingArtifacts {
billingArtifacts.BillingArtifacts = append(billingArtifacts.BillingArtifacts, a)
}
if len(ba.BillingArtifacts) < 100 {
break
}
page++
}
return billingArtifacts, nil
}
// GetCustomerStatements gets all Statements.
func (s *Client) GetCustomerStatements() (*BillingArtifacts, error) {
billingArtifacts := new(BillingArtifacts)
page := 1
for {
params := url.Values{"page": {strconv.Itoa(page)}, "per_page": {"100"}, "api_key": {s.APIKey}}
relativeURL, _ := url.Parse(fmt.Sprintf("customer_statements/?%s", params.Encode()))
responseBody, err := getResponsePage(s, relativeURL)
if err != nil {
return nil, err
}
var ba = new(BillingArtifacts)
err = json.Unmarshal(responseBody, &ba)
if err != nil {
return nil, err
}
for _, a := range ba.BillingArtifacts {
billingArtifacts.BillingArtifacts = append(billingArtifacts.BillingArtifacts, a)
}
if len(ba.BillingArtifacts) < 100 {
break
}
page++
}
return billingArtifacts, nil
}