-
Notifications
You must be signed in to change notification settings - Fork 0
/
qbwcCustomerAdd.go
169 lines (152 loc) · 7.7 KB
/
qbwcCustomerAdd.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
package qbwcGo
import (
"bytes"
"encoding/xml"
"github.com/Sirupsen/logrus"
)
//CustomerAddQB will add a customer to the quickbooks database
func CustomerAddQB(workCTX WorkCTX) {
var err error
var escapedQBXML = bytes.Buffer{}
var templateBuff = bytes.Buffer{}
var tPath = `./templates/qbCustomerAdd.t`
var customer = CustomerAddRq{}
var fieldMap = ReadFieldMapping("./fieldMaps/customerAddMapping.json")
var addrFieldMap = ReadFieldMapping("./fieldMaps/addressMapping.json")
switch workCTX.Data.(type) {
case SalesReceiptAdd:
var tempReceiptAdd = workCTX.Data.(SalesReceiptAdd)
customer.BillAddress = tempReceiptAdd.BillAddress
customer.ShipAddress = tempReceiptAdd.ShipAddress
//if a macro was defined add a trailing _c so it will not be a duplicate
if tempReceiptAdd.DefMacro != "" {
tempReceiptAdd.DefMacro = tempReceiptAdd.DefMacro + "_C"
}
workCTX.Data = tempReceiptAdd
var templateBuff = bytes.Buffer{}
var escapedQBXML = bytes.Buffer{}
var tPath = `./templates/qbReceiptAdd.t`
var qbxmlWork = QBXMLWork{} //make([]string, 0)
LoadTemplate(&tPath, tempReceiptAdd, &templateBuff)
//Add the SalesReceiptQBXML to th slice for use in QBXMLMsgsRq, the top level template
qbxmlWork.AppendWork(templateBuff.String())
//Prepare the DataExtAdds
if cfg.DataExtActive {
var dExts = DataExtAddQB(tempReceiptAdd.DefMacro)
qbxmlWork.AppendWork(dExts)
}
//Reset the template buffer, and build and execute the toplevel QBXML template with the preceeding templates as data
templateBuff.Reset()
tPath = "./templates/QBXMLMsgsRq.t"
LoadTemplate(&tPath, qbxmlWork, &templateBuff)
err = xml.EscapeText(&escapedQBXML, templateBuff.Bytes())
if err != nil {
Log.WithFields(logrus.Fields{"error": err}).Error("Error Escaping template xml in CustomerAddQB")
ErrLog.WithFields(logrus.Fields{"error": err}).Error("Error Escaping template xml in CustomerAddQB")
}
//add the QBXML to the work slice
workCTX.Work = escapedQBXML.String()
break
case SalesOrderAdd:
var tempOrderAdd = workCTX.Data.(SalesOrderAdd)
customer.BillAddress = tempOrderAdd.BillAddress
customer.ShipAddress = tempOrderAdd.ShipAddress
//if a macro was defined add a trailing _c so it will not be a duplicate
if tempOrderAdd.DefMacro != "" {
tempOrderAdd.DefMacro = tempOrderAdd.DefMacro + "_C"
}
workCTX.Data = tempOrderAdd
var templateBuff = bytes.Buffer{}
var escapedQBXML = bytes.Buffer{}
var tPath = `./templates/qbSalesOrderAdd.t`
var qbxmlWork = QBXMLWork{} //make([]string, 0)
LoadTemplate(&tPath, tempOrderAdd, &templateBuff)
//Add the SalesOrderQBXML to th slice for use in QBXMLMsgsRq, the top level template
qbxmlWork.AppendWork(templateBuff.String())
//Prepare the DataExtAdds
if cfg.DataExtActive {
var dExts = DataExtAddQB(tempOrderAdd.DefMacro)
qbxmlWork.AppendWork(dExts)
}
//Reset the template buffer, and build and execute the toplevel QBXML template with the preceeding templates as data
templateBuff.Reset()
tPath = "./templates/QBXMLMsgsRq.t"
LoadTemplate(&tPath, qbxmlWork, &templateBuff)
err = xml.EscapeText(&escapedQBXML, templateBuff.Bytes())
if err != nil {
Log.WithFields(logrus.Fields{"error": err}).Error("Error Escaping template xml in CustomerAddQB")
ErrLog.WithFields(logrus.Fields{"error": err}).Error("Error Escaping template xml in CustomerAddQB")
}
//add the QBXML to the work slice
workCTX.Work = escapedQBXML.String()
}
//prepare the shipTo gabs container for a range loop
shipToMapper, err := workCTX.Order.Path("shipTos").Children()
if err != nil {
Log.WithFields(logrus.Fields{"Error": err}).Error("Error getting shipTosMapper Children in MakeSalesReceipt")
ErrLog.WithFields(logrus.Fields{"Error": err}).Error("Error getting shipTosMapper Children in MakeSalesReceipt")
}
var qbShipTos = make([]QBShipToAddress, len(shipToMapper))
for i, shipTo := range shipToMapper {
var qbShipTo = QBShipToAddress{}
//Start shipping address
qbShipTo.Addr1 = FieldCharLimit(addrFieldMap["ShipAddress.Addr1"].Display(shipTo), addrCharLimit)
qbShipTo.Addr2 = FieldCharLimit(addrFieldMap["ShipAddress.Addr2"].Display(shipTo), addrCharLimit)
qbShipTo.Addr3 = FieldCharLimit(addrFieldMap["ShipAddress.Addr3"].Display(shipTo), addrCharLimit)
qbShipTo.City = FieldCharLimit(addrFieldMap["ShipAddress.City"].Display(shipTo), cityCharLimit)
qbShipTo.State = FieldCharLimit(addrFieldMap["ShipAddress.State"].Display(shipTo), stateCharLimit)
qbShipTo.PostalCode = FieldCharLimit(addrFieldMap["ShipAddress.PostalCode"].Display(shipTo), zipCharLimit)
qbShipTo.Country = FieldCharLimit(addrFieldMap["ShipAddress.Country"].Display(shipTo), countryCharLimit)
qbShipTo.Name = qbShipTo.Addr1 //CheckPath("name", shipTo)
//qbShipTo.Note =
//qbShipTo.DefaultShipTo =
qbShipTos[i] = qbShipTo
}
customer.ShipToAddress = append(customer.ShipToAddress, qbShipTos...)
customer.CustomerTypeRef.FullName = fieldMap["CustomerTypeRef.FullName"].Display()
customer.SalesRepRef.FullName = fieldMap["SalesRepRef.FullName"].Display()
customer.Name = fieldMap["Name"].Display(workCTX.Order)
//If NoResendOrder is true we want to add the customer name from the passed in order or receipt struct
if workCTX.NoResendOrder {
switch workCTX.Data.(type) {
case SalesReceiptAdd:
customer.Name = workCTX.Data.(SalesReceiptAdd).CustomerRef.FullName
case SalesOrderAdd:
customer.Name = workCTX.Data.(SalesOrderAdd).CustomerRef.FullName
}
}
customer.AccountNumber = fieldMap["AccountNumber"].Display(workCTX.Order)
customer.Email = fieldMap["Email"].Display(workCTX.Order)
customer.Phone = CheckPath("billing.phone", workCTX.Order)
customer.FirstName = fieldMap["FirstName"].Display(workCTX.Order) //CheckPath("billing.firstName", workCTX.Order)
customer.LastName = fieldMap["LastName"].Display(workCTX.Order) //CheckPath("billing.lastName", workCTX.Order)
customer.Cc = fieldMap["Cc"].Display(workCTX.Order)
customer.ClassRef.FullName = fieldMap["ClassRef.FullName"].Display(workCTX.Order)
customer.ClassRef.ListID = fieldMap["ClassRef.ListID"].Display(workCTX.Order)
customer.CompanyName = fieldMap["CompanyName"].Display(workCTX.Order)
customer.PreferredPaymentMethodRef.FullName = fieldMap["PreferredPaymentMethodRef.FullName"].Display()
customer.TermsRef.FullName = fieldMap["TermsRef.FullName"].Display()
customer.PriceLevelRef.FullName = fieldMap["PriceLevelRef.FullName"].Display()
var ccInfo = CreditCardInfo{}
ccInfo.CreditCardNumber = fieldMap["CreditCardInfo.CreditCardNumber"].Display(workCTX.Order)
ccInfo.ExpirationMonth = fieldMap["CreditCardInfo.ExpirationMonth"].Display(workCTX.Order)
ccInfo.ExpirationYear = fieldMap["CreditCardInfo.ExpirationYear"].Display(workCTX.Order)
ccInfo.NameOnCard = fieldMap["CreditCardInfo.NameOnCard"].Display(workCTX.Order)
ccInfo.CreditCardAddress = fieldMap["CreditCardInfo.CreditCardAddress"].Display(workCTX.Order)
ccInfo.CreditCardPostalCode = fieldMap["CreditCardInfo.CreditCardPostalCode"].Display(workCTX.Order)
customer.CreditCardInfo = ccInfo
LoadTemplate(&tPath, &customer, &templateBuff)
err = xml.EscapeText(&escapedQBXML, templateBuff.Bytes())
if err != nil {
Log.WithFields(logrus.Fields{"error": err}).Error("Error Escaping template in ImportCV3ItemsToQB")
}
//If NoResendOrder is true we want to send the customer add on workChan as opposed to workInsertChan and do not resend the order
if workCTX.NoResendOrder {
ErrLog.Error("noResendOrder")
workChan <- WorkCTX{Work: escapedQBXML.String(), Data: workCTX.Data, Order: workCTX.Order, Type: "customerAddRq", Attempted: workCTX.Attempted}
return
}
//Send prepaired QBXML to the workInsertChan
workInsertChan <- WorkCTX{Work: escapedQBXML.String(), Data: workCTX.Data, Order: workCTX.Order, Type: "customerAddRq", Attempted: workCTX.Attempted}
workInsertChan <- workCTX
}