account_invoices.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  // Invoice structs reflect an invoice for billable activity on the account.
  12  type Invoice struct {
  13  	ID            int                 `json:"id"`
  14  	Label         string              `json:"label"`
  15  	Total         float32             `json:"total"`
  16  	Date          *time.Time          `json:"-"`
  17  	Tax           float32             `json:"tax"`
  18  	Subtotal      float32             `json:"subtotal"`
  19  	BillingSource string              `json:"billing_source"`
  20  	TaxSummary    []InvoiceTaxSummary `json:"tax_summary"`
  21  }
  22  
  23  type InvoiceTaxSummary struct {
  24  	Tax  float32 `json:"tax"`
  25  	Name string  `json:"name"`
  26  }
  27  
  28  // InvoiceItem structs reflect a single billable activity associate with an Invoice
  29  type InvoiceItem struct {
  30  	Label     string     `json:"label"`
  31  	Type      string     `json:"type"`
  32  	UnitPrice string     `json:"unit_price"`
  33  	Quantity  int        `json:"quantity"`
  34  	Amount    float32    `json:"amount"`
  35  	Tax       float32    `json:"tax"`
  36  	Region    *string    `json:"region"`
  37  	From      *time.Time `json:"-"`
  38  	To        *time.Time `json:"-"`
  39  	Total     float32    `json:"total"`
  40  }
  41  
  42  // ListInvoices gets a paginated list of Invoices against the Account
  43  func (c *Client) ListInvoices(ctx context.Context, opts *ListOptions) ([]Invoice, error) {
  44  	return getPaginatedResults[Invoice](ctx, c, "account/invoices", opts)
  45  }
  46  
  47  // UnmarshalJSON implements the json.Unmarshaler interface
  48  func (i *Invoice) UnmarshalJSON(b []byte) error {
  49  	type Mask Invoice
  50  
  51  	p := struct {
  52  		*Mask
  53  
  54  		Date *parseabletime.ParseableTime `json:"date"`
  55  	}{
  56  		Mask: (*Mask)(i),
  57  	}
  58  
  59  	if err := json.Unmarshal(b, &p); err != nil {
  60  		return err
  61  	}
  62  
  63  	i.Date = (*time.Time)(p.Date)
  64  
  65  	return nil
  66  }
  67  
  68  // UnmarshalJSON implements the json.Unmarshaler interface
  69  func (i *InvoiceItem) UnmarshalJSON(b []byte) error {
  70  	type Mask InvoiceItem
  71  
  72  	p := struct {
  73  		*Mask
  74  
  75  		From *parseabletime.ParseableTime `json:"from"`
  76  		To   *parseabletime.ParseableTime `json:"to"`
  77  	}{
  78  		Mask: (*Mask)(i),
  79  	}
  80  
  81  	if err := json.Unmarshal(b, &p); err != nil {
  82  		return err
  83  	}
  84  
  85  	i.From = (*time.Time)(p.From)
  86  	i.To = (*time.Time)(p.To)
  87  
  88  	return nil
  89  }
  90  
  91  // GetInvoice gets a single Invoice matching the provided ID
  92  func (c *Client) GetInvoice(ctx context.Context, invoiceID int) (*Invoice, error) {
  93  	e := formatAPIPath("account/invoices/%d", invoiceID)
  94  	return doGETRequest[Invoice](ctx, c, e)
  95  }
  96  
  97  // ListInvoiceItems gets the invoice items associated with a specific Invoice
  98  func (c *Client) ListInvoiceItems(ctx context.Context, invoiceID int, opts *ListOptions) ([]InvoiceItem, error) {
  99  	return getPaginatedResults[InvoiceItem](ctx, c, formatAPIPath("account/invoices/%d/items", invoiceID), opts)
 100  }
 101