forked from XeroAPI/xerogolang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tax_rate.go
152 lines (115 loc) · 4.58 KB
/
tax_rate.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
143
144
145
146
147
148
149
150
151
152
package accounting
import (
"encoding/json"
"encoding/xml"
"github.com/XeroAPI/xerogolang"
"github.com/markbates/goth"
)
//TaxRate is a rate at which an item or service is taxed when sold or purchased
type TaxRate struct {
// Name of tax rate
Name string `json:"Name,omitempty" xml:"Name,omitempty"`
// See Tax Types – can only be used on update calls
TaxType string `json:"TaxType,omitempty" xml:"TaxType,omitempty"`
// See TaxComponents
TaxComponents []TaxComponent `json:"TaxComponents,omitempty" xml:"TaxComponents>TaxComponent,omitempty"`
// See Status Codes
Status string `json:"Status,omitempty" xml:"Status,omitempty"`
// See ReportTaxTypes
ReportTaxType string `json:"ReportTaxType" xml:"ReportTaxType"`
// Boolean to describe if tax rate can be used for asset accounts i.e. true,false
CanApplyToAssets bool `json:"CanApplyToAssets,omitempty" xml:"CanApplyToAssets,omitempty"`
// Boolean to describe if tax rate can be used for equity accounts i.e. true,false
CanApplyToEquity bool `json:"CanApplyToEquity,omitempty" xml:"CanApplyToEquity,omitempty"`
// Boolean to describe if tax rate can be used for expense accounts i.e. true,false
CanApplyToExpenses bool `json:"CanApplyToExpenses,omitempty" xml:"CanApplyToExpenses,omitempty"`
// Boolean to describe if tax rate can be used for liability accounts i.e. true,false
CanApplyToLiabilities bool `json:"CanApplyToLiabilities,omitempty" xml:"CanApplyToLiabilities,omitempty"`
// Boolean to describe if tax rate can be used for revenue accounts i.e. true,false
CanApplyToRevenue bool `json:"CanApplyToRevenue,omitempty" xml:"CanApplyToRevenue,omitempty"`
// Tax Rate (decimal to 4dp) e.g 12.5000
DisplayTaxRate float64 `json:"DisplayTaxRate,omitempty" xml:"DisplayTaxRate,omitempty"`
// Effective Tax Rate (decimal to 4dp) e.g 12.5000
EffectiveRate float64 `json:"EffectiveRate,omitempty" xml:"EffectiveRate,omitempty"`
}
type TaxRates struct {
TaxRates []TaxRate `json:"TaxRates" xml:"TaxRate"`
}
func unmarshalTaxRate(taxRateResponseBytes []byte) (*TaxRates, error) {
var taxRateResponse *TaxRates
err := json.Unmarshal(taxRateResponseBytes, &taxRateResponse)
if err != nil {
return nil, err
}
return taxRateResponse, err
}
//Create will create taxRates given an TaxRates struct
func (t *TaxRates) Create(provider xerogolang.IProvider, session goth.Session) (*TaxRates, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
"Content-Type": "application/xml",
}
body, err := xml.MarshalIndent(t, " ", " ")
if err != nil {
return nil, err
}
taxRateResponseBytes, err := provider.Create(session, "TaxRates", additionalHeaders, body)
if err != nil {
return nil, err
}
return unmarshalTaxRate(taxRateResponseBytes)
}
//Update will update an taxRate given an TaxRates struct
//This will only handle a single taxRate - you cannot update multiple taxRates in a single call
func (t *TaxRates) Update(provider xerogolang.IProvider, session goth.Session) (*TaxRates, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
"Content-Type": "application/xml",
}
body, err := xml.MarshalIndent(t, " ", " ")
if err != nil {
return nil, err
}
taxRateResponseBytes, err := provider.Update(session, "TaxRates", additionalHeaders, body)
if err != nil {
return nil, err
}
return unmarshalTaxRate(taxRateResponseBytes)
}
//FindTaxRates will get all TaxRates.
//additional querystringParameters such as taxType, where and order can be added as a map
func FindTaxRates(provider xerogolang.IProvider, session goth.Session, querystringParameters map[string]string) (*TaxRates, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
taxRateResponseBytes, err := provider.Find(session, "TaxRates", additionalHeaders, querystringParameters)
if err != nil {
return nil, err
}
return unmarshalTaxRate(taxRateResponseBytes)
}
//GenerateExampleTaxRate Creates an Example taxRate
func GenerateExampleTaxRate() *TaxRates {
taxComponent1 := TaxComponent{
Name: "State Tax",
Rate: 7.5,
IsCompound: false,
}
taxComponent2 := TaxComponent{
Name: "Local Sales Tax",
Rate: 0.625,
IsCompound: false,
}
taxRate := TaxRate{
Name: "Newman's Tax",
TaxComponents: []TaxComponent{},
ReportTaxType: "OUTPUT",
}
taxRate.TaxComponents = append(taxRate.TaxComponents, taxComponent1)
taxRate.TaxComponents = append(taxRate.TaxComponents, taxComponent2)
taxRateCollection := &TaxRates{
TaxRates: []TaxRate{},
}
taxRateCollection.TaxRates = append(taxRateCollection.TaxRates, taxRate)
return taxRateCollection
}