forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
employee.go
142 lines (114 loc) · 3.49 KB
/
employee.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 quickbooks
import (
"errors"
"strconv"
)
type Employee struct {
SyncToken string `json:",omitempty"`
Domain string `json:"domain,omitempty"`
DisplayName string `json:",omitempty"`
PrimaryPhone TelephoneNumber `json:",omitempty"`
PrintOnCheckName string `json:",omitempty"`
FamilyName string `json:",omitempty"`
Active bool `json:",omitempty"`
SSN string `json:",omitempty"`
PrimaryAddr PhysicalAddress `json:",omitempty"`
BillableTime bool `json:",omitempty"`
GivenName string `json:",omitempty"`
Id string `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
}
// CreateEmployee creates the given employee within QuickBooks
func (c *Client) CreateEmployee(employee *Employee) (*Employee, error) {
var resp struct {
Employee Employee
Time Date
}
if err := c.post("employee", employee, &resp, nil); err != nil {
return nil, err
}
return &resp.Employee, nil
}
// FindEmployees gets the full list of Employees in the QuickBooks account.
func (c *Client) FindEmployees() ([]Employee, error) {
var resp struct {
QueryResponse struct {
Employees []Employee `json:"Employee"`
MaxResults int
StartPosition int
TotalCount int
}
}
if err := c.query("SELECT COUNT(*) FROM Employee", &resp); err != nil {
return nil, err
}
if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no employees could be found")
}
employees := make([]Employee, 0, resp.QueryResponse.TotalCount)
for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM Employee ORDERBY Id STARTPOSITION " + strconv.Itoa(i+1) + " MAXRESULTS " + strconv.Itoa(queryPageSize)
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Employees == nil {
return nil, errors.New("no employees could be found")
}
employees = append(employees, resp.QueryResponse.Employees...)
}
return employees, nil
}
// FindEmployeeById returns an employee with a given Id.
func (c *Client) FindEmployeeById(id string) (*Employee, error) {
var resp struct {
Employee Employee
Time Date
}
if err := c.get("employee/"+id, &resp, nil); err != nil {
return nil, err
}
return &resp.Employee, nil
}
// QueryEmployees accepts an SQL query and returns all employees found using it
func (c *Client) QueryEmployees(query string) ([]Employee, error) {
var resp struct {
QueryResponse struct {
Employees []Employee `json:"Employee"`
StartPosition int
MaxResults int
}
}
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Employees == nil {
return nil, errors.New("could not find any employees")
}
return resp.QueryResponse.Employees, nil
}
// UpdateEmployee updates the employee
func (c *Client) UpdateEmployee(employee *Employee) (*Employee, error) {
if employee.Id == "" {
return nil, errors.New("missing employee id")
}
existingEmployee, err := c.FindEmployeeById(employee.Id)
if err != nil {
return nil, err
}
employee.SyncToken = existingEmployee.SyncToken
payload := struct {
*Employee
Sparse bool `json:"sparse"`
}{
Employee: employee,
Sparse: true,
}
var employeeData struct {
Employee Employee
Time Date
}
if err = c.post("employee", payload, &employeeData, nil); err != nil {
return nil, err
}
return &employeeData.Employee, err
}