This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
invoice.go
142 lines (121 loc) · 4.48 KB
/
invoice.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package moneybird
import (
"encoding/json"
"net/http"
)
// Invoice contains all invoice details
type Invoice struct {
ID string `json:"id,omitempty"`
AdministrationID string `json:"administration_id,omitempty"`
InvoiceID string `json:"invoice_id,omitempty"`
Contact Contact `json:"contact,omitempty"`
ContactID string `json:"contact_id,omitempty"`
UpdateContact bool `json:"update_contact,omitempty"`
WorkflowID string `json:"workflow_id,omitempty"`
DocumentStyleID string `json:"document_style_id,omitempty"`
IdentityID string `json:"identity_id,omitempty"`
State string `json:"state,omitempty"`
InvoiceDate string `json:"invoice_date,omitempty"`
DueDate string `json:"due_date,omitempty"`
PaymentConditions string `json:"payment_conditions,omitempty"`
Reference string `json:"reference,omitempty"`
Language string `json:"language,omitempty"`
Currency string `json:"currency,omitempty"`
Discount string `json:"discount,omitempty"`
PaidAt string `json:"paid_at,omitempty"`
SentAt string `json:"sent_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Details []*InvoiceDetails `json:"details_attributes,omitempty"`
TotalPaid string `json:"total_paid,omitempty"`
TotalUnpaid string `json:"total_unpaid,omitempty"`
TotalUnpaidBase string `json:"total_unpaid_base,omitempty"`
PricesAreInclTax bool `json:"prices_are_incl_tax,omitempty"`
TotalPriceExclTax string `json:"total_price_excl_tax,omitempty"`
TotalPriceInclTax string `json:"total_price_incl_tax,omitempty"`
URL string `json:"url,omitempty"`
Notes []*InvoiceNote `json:"notes,omitempty"`
Payments []*InvoicePayment `json:"payments,omitempty"`
}
// InvoiceDetails is a line on an invoice
type InvoiceDetails struct {
ID string `json:"id,omitempty"`
Description string `json:"description"`
Price string `json:"price"`
Period string `json:"period,omitempty"`
Amount string `json:"amount,omitempty"`
TaxRateID string `json:"tax_rate_id,omitempty"`
LedgerAccountID string `json:"ledger_account_id,omitempty"`
Destroy bool `json:"_destroy,omitempty"`
}
// InvoiceGateway encapsulates all /sales_invoices related endpoints
type InvoiceGateway struct {
*Client
}
// Invoice returns a new InvoiceGateway instance
func (c *Client) Invoice() *InvoiceGateway {
return &InvoiceGateway{c}
}
// List returns all invoices
func (c *InvoiceGateway) List() ([]*Invoice, error) {
var invoices []*Invoice
var err error
res, err := c.execute("GET", "sales_invoices", nil)
if err != nil {
return nil, err
}
switch res.StatusCode {
case http.StatusOK:
err = json.NewDecoder(res.Body).Decode(&invoices)
return invoices, err
}
return nil, res.error()
}
// Get returns the invoice with the specified id, or nil
func (c *InvoiceGateway) Get(ID string) (*Invoice, error) {
res, err := c.execute("GET", "sales_invoices/"+ID, nil)
if err != nil {
return nil, err
}
switch res.StatusCode {
case http.StatusOK:
return res.invoice()
}
return nil, res.error()
}
// Update updates the invoice in Moneybird
func (c *InvoiceGateway) Update(invoice *Invoice) (*Invoice, error) {
res, err := c.execute("PATCH", "sales_invoices/"+invoice.ID, &envelope{Invoice: invoice})
if err != nil {
return invoice, err
}
switch res.StatusCode {
case http.StatusOK:
return res.invoice()
}
return nil, res.error()
}
// Create creates the invoice in Moneybird
func (c *InvoiceGateway) Create(invoice *Invoice) (*Invoice, error) {
res, err := c.execute("POST", "sales_invoices", &envelope{Invoice: invoice})
if err != nil {
return invoice, err
}
switch res.StatusCode {
case http.StatusCreated:
return res.invoice()
}
return nil, res.error()
}
// Delete deletes the invoice in Moneybird
func (c *InvoiceGateway) Delete(invoice *Invoice) error {
res, err := c.execute("DELETE", "sales_invoices/"+invoice.ID, nil)
if err != nil {
return err
}
switch res.StatusCode {
case 204:
return nil
}
return res.error()
}