-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
231 lines (205 loc) · 6.49 KB
/
example_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2019 James Cote
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package intacct_test
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"log"
"github.com/jfcote87/intacct"
v21 "github.com/jfcote87/intacct/v21"
)
// Example Config file.
var sessionConfig = `{
"sender_id": "Your SenderID",
"sender_pwd": "Your Password",
"login": {
"user_id": "xml_gateway",
"company": "Company Name",
"password": "User Password",
"location_id": "XYZ"
},
"session": {}
}`
type Project struct {
RecordNumber intacct.Int `xml:"RECORDNO,omitempty"`
ProjectID string `xml:"PROJECTID"`
Projectname string `xml:"NAME"`
Description string `xml:"DESCRIPTION,omitempty"`
ParentProjectKey intacct.Int `xml:"PARENTKEY,omitempty"`
ParentProjectID string `xml:"PARENTID,omitempty"`
ParentProjectName string `xml:"PARENTNAME,omitempty"`
}
// ExampleService_Exec demonstrates using an intacct service to read
// a project record. It then creates a new project record as a child
// of the first record, and finally runs a query
// reading all projects having orignal project as a parent.
func ExampleService_Exec() {
var projectName = "EX1732"
var ctx context.Context = context.Background()
configReader := bytes.NewReader([]byte(sessionConfig))
sv, err := intacct.ServiceFromConfigJSON(configReader)
if err != nil {
log.Fatalf("Configuration error: %v", err)
}
var projectRecord *Project
resp, err := sv.Exec(ctx, intacct.ReadByName("PROJECT", projectName))
if err != nil {
log.Fatalf("Execution error: %v", err)
}
if err = resp.Decode(&projectRecord); err != nil {
log.Fatalf("result error: %v", err)
}
// empty result set
if projectRecord == nil {
log.Fatalf("No project named %s exists", projectName)
}
// Create new projects
parentNo := projectRecord.RecordNumber
resp, err = sv.Exec(ctx, intacct.Create("PROJECT", []Project{
{
ProjectID: "X2017",
Description: "New sub-project of E1732",
ParentProjectKey: parentNo,
},
{
ProjectID: "X2018",
Description: "New sub-project of E1732",
ParentProjectKey: parentNo,
},
}))
if err == nil {
// pass nil in decode to only check for errors
err = resp.Decode(nil)
}
if err != nil {
log.Fatalf("createProject execution: %v", err)
}
var projectList []Project
// ReadByQuery to read all projects having parent of p. Using GetAll() to return all pages of results
if err = intacct.
ReadByQuery("PROJECT", fmt.Sprintf("PARENTKEY = '%d'", parentNo)).
PageSize(10).
GetAll(ctx, sv, &projectList); err != nil {
log.Fatalf("query full read error: %v", err)
}
fmt.Printf("Total children: %d\n", len(projectList))
}
// Read documents with attachments
// https://developer.intacct.com/api/company-console/attachments/#list-attachments-legacy
func Example_read_supdoc() {
var ctx context.Context = context.Background()
configReader := bytes.NewReader([]byte(sessionConfig))
sv, err := intacct.ServiceFromConfigJSON(configReader)
if err != nil {
log.Fatalf("Configuration error: %v", err)
}
// Read documents with attachment data
resp, err := sv.Exec(ctx, intacct.LegacyFunction{
Payload: &v21.GetList{
ObjectName: "supdoc",
MaxItems: 1,
Filter: &v21.Expression{
Type: v21.ExpressionEqual,
Field: "supdocid",
Value: "DocumentID",
},
},
})
if err != nil {
log.Fatalf("exec err: %v", err)
}
var supDoc = struct {
XMLName xml.Name `xml:"supdoc"`
SupdocID string `xml:"supdocid"`
SupdocName string `xml:"supdocname,omitempty"`
SupdocfolderName string `xml:"supdocfoldername,omitempty"`
Supdocdescription string `xml:"supdocdescription,omitempty"`
Attachments []v21.Attachment `xml:"attachments>attachment,omitempty"`
}{}
if err = resp.Decode(&supDoc); err != nil {
log.Fatalf("decode error: %v", err)
}
for idx, a := range supDoc.Attachments {
log.Printf("Document %s: attachment %d %s total bytes %d", supDoc.SupdocName, idx, a.AttachmentName, len(a.AttachmentData))
}
}
// create document with attachments
// https://developer.intacct.com/api/company-console/attachments/#create-attachment-legacy
func Example_create_doc() {
var ctx context.Context = context.Background()
configReader := bytes.NewReader([]byte(sessionConfig))
sv, err := intacct.ServiceFromConfigJSON(configReader)
if err != nil {
log.Fatalf("Configuration error: %v", err)
}
var buffer []byte
// copy file bytes to buffer
_, err = sv.Exec(ctx, intacct.LegacyFunction{
Payload: &v21.CreateSupdoc{
SupdocID: "NewDocumentID",
SupdocName: "Doc Name",
SupdocfolderName: "FolderName",
Attachments: []v21.Attachment{
{
AttachmentName: "New PDF File",
AttachmentType: "pdf",
AttachmentData: buffer,
},
},
},
})
if err != nil {
log.Fatalf("create supdoc failed: %v", err)
}
log.Printf("attachment added")
}
// ExampleReader_readall shows how to read
// all responses to a Reader function
func ExampleReader_readall() {
var ctx context.Context = context.Background()
configReader := bytes.NewReader([]byte(sessionConfig))
sv, err := intacct.ServiceFromConfigJSON(configReader)
if err != nil {
log.Fatalf("Configuration error: %v", err)
}
var results []Vendor
if err = intacct.ReadByQuery("VENDOR", "").
Fields("RECORDNO", "VENDORID").
GetAll(ctx, sv, &results); err != nil {
log.Fatalf("getall error: %v", err)
}
log.Printf("Total Records: %d", len(results))
}
// ExampleQuery_GetAll demonstrates using an intacct service to query
// records using the new query function. GetAll reads all pages of the
// query.
func ExampleQuery_GetAll() {
var ctx context.Context = context.Background()
configReader := bytes.NewReader([]byte(sessionConfig))
sv, err := intacct.ServiceFromConfigJSON(configReader)
if err != nil {
log.Fatalf("Configuration error: %v", err)
}
var projects []Project
var filter = intacct.NewFilter()
filter.And().EqualTo("STATUS", "active").In("PARENTID", "ID01", "ID02")
var stmt = &intacct.Query{
Object: "PROJECT",
Select: intacct.Select{
Fields: []string{"RECORDNO", "PROJECTID", "NAME", "DESCRIPTION", "PARENTNAME"},
},
Sort: &intacct.QuerySort{Fields: []intacct.OrderBy{{Field: "PROJECTID"}}},
Filter: filter,
}
if err := stmt.GetAll(ctx, sv, &projects); err != nil {
log.Printf("read error %v", err)
return
}
for _, p := range projects {
fmt.Printf("%s", p.Projectname)
}
}