-
Notifications
You must be signed in to change notification settings - Fork 6
/
invoice.go
120 lines (107 loc) · 4.02 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
package quickbooks
import (
"encoding/json"
"fmt"
)
// InvoiceObject the complete quickbooks invoice object type
type InvoiceObject struct {
Invoice Invoice `json:"Invoice"`
Time string `json:"time"`
}
// Invoice quickbooks invoice type
type Invoice struct {
ID string `json:"Id,omitempty"`
Deposit int `json:"Deposit,omitempty"`
Domain string `json:"domain,omitempty"`
Sparse bool `json:"sparse,omitempty"`
SyncToken string `json:"SyncToken,omitempty"`
CustomField *[]CustomField `json:"CustomField,omitempty"`
DocNumber string `json:"DocNumber,omitempty"`
TxnDate string `json:"TxnDate,omitempty"`
LinkedTxn *[]LinkedTxn `json:"LinkedTxn,omitempty"`
Line []InvoiceLine `json:"Line"`
TxnTaxDetail *struct {
TxnTaxCodeRef *TaxCodeRef `json:"TxnTaxCodeRef,omitempty"`
TotalTax float64 `json:"TotalTax"`
TaxLine []TaxLine `json:"TaxLine,omitempty"`
} `json:"TxnTaxDetail,omitempty"`
CustomerRef *CustomerRef `json:"CustomerRef"`
CustomerMemo *struct {
Value string `json:"value"`
} `json:"CustomerMemo,omitempty"`
BillAddr *Address `json:"BillAddr"`
ShipAddr *Address `json:"ShipAddr"`
SalesTermRef *struct {
Value string `json:"value"`
} `json:"SalesTermRef,omitempty"`
DueDate string `json:"DueDate,omitempty"`
TotalAmt float64 `json:"TotalAmt,omitempty"`
ApplyTaxAfterDiscount bool `json:"ApplyTaxAfterDiscount,omitempty"`
PrintStatus string `json:"PrintStatus,omitempty"`
EmailStatus string `json:"EmailStatus,omitempty"`
BillEmail *struct {
Address string `json:"Address"`
} `json:"BillEmail,omitempty"`
Balance float64 `json:"Balance,omitempty"`
MetaData *struct {
CreateTime string `json:"CreateTime"`
LastUpdatedTime string `json:"LastUpdatedTime"`
} `json:"MetaData,omitempty"`
}
// InvoiceLine quickbooks invoice line item object
type InvoiceLine struct {
ID string `json:"Id,omitempty"`
LineNum int `json:"LineNum,omitempty"`
Description string `json:"Description,omitempty"`
Amount float64 `json:"Amount"`
DetailType string `json:"DetailType"`
SalesItemLineDetail *SalesItemLineDetail `json:"SalesItemLineDetail,omitempty"`
SubTotalLineDetail interface{} `json:"SubTotalLineDetail,omitempty"`
}
// CustomField quickbooks invoice customer field object
type CustomField struct {
DefinitionID string `json:"DefinitionId"`
Name string `json:"Name,omitempty"`
Type string `json:"Type"`
StringValue string `json:"StringValue,omitempty"`
}
type LinkedTxn struct {
TxnID string `json:"TxnId"`
TxnType string `json:"TxnType"`
}
type TaxCodeRef struct {
Value string `json:"value"`
}
// SalesItemLineDetail quickbooks invoice sales line item details object
type SalesItemLineDetail struct {
ItemRef *ItemRef `json:"ItemRef"`
UnitPrice int `json:"UnitPrice"`
Qty int `json:"Qty"`
TaxCodeRef *TaxCodeRef `json:"TaxCodeRef,omitempty"`
}
// TaxLine quickbooks invoice tax line item object
type TaxLine struct {
Amount float64 `json:"Amount"`
DetailType string `json:"DetailType,omitempty"`
TaxLineDetail *struct {
TaxRateRef *TaxCodeRef `json:"TaxRateRef,omitempty"`
PercentBased bool `json:"PercentBased"`
TaxPercent int `json:"TaxPercent"`
NetAmountTaxable float64 `json:"NetAmountTaxable"`
} `json:"TaxLineDetail,omitempty"`
}
// CreateInvoice creates an invoice on quickbooks
func (q *Quickbooks) CreateInvoice(invoice Invoice) (*InvoiceObject, error) {
endpoint := fmt.Sprintf("/company/%s/invoice", q.RealmID)
res, err := q.makePostRequest(endpoint, invoice)
if err != nil {
return nil, err
}
defer res.Body.Close()
newInvoice := InvoiceObject{}
err = json.NewDecoder(res.Body).Decode(&newInvoice)
if err != nil {
return nil, err
}
return &newInvoice, nil
}