/
rma_update.go
295 lines (257 loc) · 9.95 KB
/
rma_update.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"fmt"
"net/http"
"strconv"
"text/template"
"time"
"gopkg.in/mgo.v2/bson"
"github.com/albrow/forms"
"github.com/go-zoo/bone"
)
// Update the RMA.
func rmaUpdateHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
// Get the value of the "id" parameters.
rmaNum := bone.GetValue(r, "id")
rma := getRma(rmaNum)
rmaData := &RmaUpdate{}
rmaData.RMA = *rma
rmaData.ProductList = *getProductList()
rmaData.StatusList = getStatusList(rma.Status)
rmaData.BillableList = getBillableList(rma.Billable)
rmaData.RmaTypeList = getRmaTypeList(rma.RmaType)
if len(rmaData.RMA.Notes) > 50000 {
rmaData.RMA.Notes = rmaData.RMA.Notes[0:50000]
}
displayRmaUpdateTemplate(w, rmaData)
} else {
fmt.Printf("Request: %v", r)
// Parse the form
formData, err := forms.Parse(r)
CheckError(err)
fmt.Printf("Button clicked: %s\n", formData.Get("SubmitButton"))
fmt.Printf("Selected Product: %s\n", bson.ObjectId(formData.Get("AddProduct")))
//fmt.Printf("Selected Product: %s\n", formData.Get("AddProduct"))
// Check token
token := r.Form.Get("token")
if token != "" {
// check token validity
fmt.Println("Good token")
} else {
// give error if no token
fmt.Println("Bad token")
}
// Validate data
val := formData.Validator()
val.Require("RmaNumber")
fmt.Printf("FormData %v\n", formData)
// Use data to create a user object
rma := getRma(formData.Get("OriginalRmaNumber"))
fmt.Printf("Set RMA %s Orignal RMA Num %s\n", formData.Get("RmaNumber"), formData.Get("OriginalRmaNumber"))
rma.RmaNumber = formData.Get("RmaNumber")
rma.RmaType = formData.Get("RmaType")
rma.OrigSalesOrder = formData.Get("OrigSalesOrder")
rma.Company = formData.Get("Company")
rma.ContactName = formData.Get("ContactName")
rma.ContactEmail = formData.Get("ContactEmail")
rma.ContactPhone = formData.Get("ContactPhone")
rma.ContactAddress = formData.Get("ContactAddress")
rma.ContactAddress2 = formData.Get("ContactAddress2")
rma.ContactAddressCityStateZip = formData.Get("ContactAddressCityStateZip")
rma.ContactAddressCountry = formData.Get("ContactAddressCountry")
rma.ProductDesc = formData.Get("ProductDesc")
rma.ReasonReturn = formData.Get("ReasonReturn")
rma.ReturnCompany = formData.Get("ReturnCompany")
rma.ReturnContact = formData.Get("ReturnContact")
rma.ReturnAddress = formData.Get("ReturnAddress")
rma.ReturnAddressCont = formData.Get("ReturnAddressCont")
rma.ReturnAddressCityStateZip = formData.Get("ReturnAddressCityStateZip")
rma.ReturnAddressCountry = formData.Get("ReturnAddressCountry")
rma.ReturnPhone = formData.Get("ReturnPhone")
rma.ReturnEmail = formData.Get("ReturnEmail")
rma.ReceiveInfo = formData.Get("ReceiveInfo")
rma.ReceiveUser = formData.Get("ReceiveUser")
rma.InspectionInfo = formData.Get("InspectionInfo")
rma.InspectionUser = formData.Get("InspectionUser")
rma.RepairInfo = formData.Get("RepairInfo")
rma.RepairUser = formData.Get("RepairUser")
rma.RepairEstHours = formData.GetInt("RepairEstHours")
rma.Billable = formData.Get("Billable")
rma.QuoteNum = formData.Get("QuoteNum")
rma.OriginalRmaNum = formData.Get("OriginalRmaNum")
rma.SerialNumber = formData.Get("SerialNumber")
rma.Notes = formData.Get("Notes")
rma.Modified = time.Now().Local()
rma.InspectionDate = formData.Get("InspectionDate")
rma.ReceiveDate = formData.Get("ReceiveDate")
rma.RepairDate = formData.Get("RepairDate")
rma.RmaDate = formData.Get("RmaDate")
rma.ShipDate = formData.Get("ShipDate")
rma.Status = formData.Get("Status")
// Accumulate the Products
for i := range rma.Products {
rma.Products[i].PartNumber = r.Form["ProductPartNumber"][i]
rma.Products[i].SerialNumber = r.Form["ProductSerialNumber"][i]
qty, err := strconv.Atoi(r.Form["ProductQty"][i])
if err == nil {
rma.Products[i].Qty = qty
}
}
// Accumulate the RepairProducts
for i := range rma.RepairProducts {
rma.RepairProducts[i].PartNumber = r.Form["ProductPartNumber"][i]
rma.RepairProducts[i].SerialNumber = r.Form["ProductSerialNumber"][i]
qty, err := strconv.Atoi(r.Form["ProductQty"][i])
if err == nil {
rma.RepairProducts[i].Qty = qty
}
}
// Add the new product to the RMA
if formData.Get("SubmitButton") == "ADD" {
fmt.Printf("Add product to RMA: %s\n", rma.RmaNumber)
//
// // Add the product to the list
// rmaProduct := &RmaProduct{}
// rmaProduct.PartNumber = formData.Get("AddProductPartNumber")
// rmaProduct.Qty = formData.GetInt("AddProductQty")
// rmaProduct.SerialNumber = formData.Get("AddProductSerialNumber")
// product := getProductPartNumber(rmaProduct.PartNumber)
// if product != nil {
// rma.Products = append(rma.Products, *rmaProduct)
// }
// // Update the RMA in DB
// updateRma(rma)
addRmaProduct(formData.Get("AddProductPartNumber"),
formData.GetInt("AddProductQty"),
formData.Get("AddProductSerialNumber"),
rma)
// Go back to the update page
http.Redirect(w, r, "/rma/update/"+rma.RmaNumber, http.StatusFound)
} else if formData.Get("SubmitButton") == "ADD REPAIR" {
fmt.Printf("Add Repair product to RMA: %s\n", rma.RmaNumber)
//
// Add the repair to the list
addRmaRepairProduct(formData.Get("AddRepairProductPartNumber"),
formData.GetInt("AddRepairProductQty"),
formData.Get("AddRepairProductSerialNumber"),
rma)
// Go back to the update page
http.Redirect(w, r, "/rma/update/"+rma.RmaNumber, http.StatusFound)
} else {
fmt.Printf("RMA Update: %s\n", rma.RmaNumber)
// Update the RMA in DB
updateRma(rma)
// Go to the list of RMA
http.Redirect(w, r, "/rma", http.StatusFound)
}
}
}
// Add a product to the RMA Repair list.
func addRmaRepairProduct(partNum string, qty int, serialNum string, rma *RMA) {
fmt.Printf("Add Repair product to RMA: %s\n", rma.RmaNumber)
// Add the product to the list
rmaProduct := &RmaProduct{}
rmaProduct.PartNumber = partNum
rmaProduct.Qty = qty
rmaProduct.SerialNumber = serialNum
product := getProductPartNumber(rmaProduct.PartNumber)
if product != nil {
rma.RepairProducts = append(rma.RepairProducts, *rmaProduct)
}
fmt.Printf("RMA %v\n", rma.RepairProducts)
// Update the RMA in DB
updateRma(rma)
}
// Add a product to the received parts list.
func addRmaProduct(partNum string, qty int, serialNum string, rma *RMA) {
fmt.Printf("Add product to RMA: %s\n", rma.RmaNumber)
// Add the product to the list
rmaProduct := &RmaProduct{}
rmaProduct.PartNumber = partNum
rmaProduct.Qty = qty
rmaProduct.SerialNumber = serialNum
product := getProductPartNumber(rmaProduct.PartNumber)
if product != nil {
rma.Products = append(rma.Products, *rmaProduct)
}
// Update the RMA in DB
updateRma(rma)
}
// Display the template
func displayRmaUpdateTemplate(w http.ResponseWriter, rmaData *RmaUpdate) {
// Generate token
rmaData.Token = genToken()
// Redirect back to the page with error message
t, _ := template.ParseFiles("header.html", "rma_update.html", "footer.html")
t.ExecuteTemplate(w, "header", nil)
t.ExecuteTemplate(w, "content", rmaData)
t.ExecuteTemplate(w, "footer", nil)
t.Execute(w, rmaData)
}
// Find the RMA from the database
func getRma(rmaNum string) *RMA {
fmt.Println("Get RMA - RMANumber: ", rmaNum)
var data RMA
err := Vault.Mongo.C("RMAs").Find(bson.M{"RmaNumber": rmaNum}).One(&data)
if err != nil {
fmt.Printf("Can't find RMA %v\n", err)
}
fmt.Println("RMA: ", data.RmaNumber)
fmt.Println("SerialNum: ", data.SerialNumber)
fmt.Println("ReasonReturn: ", data.ReasonReturn)
fmt.Println("ID:", data.ID)
return &data
}
// Update the RMA data.
func updateRma(rma *RMA) {
fmt.Println("updateRMA - ID", rma.ID)
fmt.Printf("Products: %v\n", rma.Products)
//err := Vault.Mongo.C("adcps").Update(bson.M{"_id": adcp._id}, bson.M{"$inc": bson.M{"Customer": adcp.Customer}})
err := Vault.Mongo.C("RMAs").Update(bson.M{"_id": rma.ID}, bson.M{"$set": bson.M{
"RmaNumber": rma.RmaNumber,
"RmaType": rma.RmaType,
"RmaDate": rma.RmaDate,
"OrigSalesOrder": rma.OrigSalesOrder,
"Company": rma.Company,
"ContactName": rma.ContactName,
"ContactEmail": rma.ContactEmail,
"ContactPhone": rma.ContactPhone,
"ContactAddress": rma.ContactAddress,
"ContactAddress2": rma.ContactAddress2,
"ContactAddressCityStateZip": rma.ContactAddressCityStateZip,
"ContactAddressCountry": rma.ContactAddressCountry,
"ProductDesc": rma.ProductDesc,
"ReasonReturn": rma.ReasonReturn,
"ReturnCompany": rma.ReturnCompany,
"ReturnContact": rma.ReturnContact,
"ReturnAddress": rma.ReturnAddress,
"ReturnAddressCont": rma.ReturnAddressCont,
"ReturnAddressCityStateZip": rma.ReturnAddressCityStateZip,
"ReturnAddressCountry": rma.ReturnAddressCountry,
"ReturnPhone": rma.ReturnPhone,
"ReturnEmail": rma.ReturnEmail,
"ReceiveInfo": rma.ReceiveInfo,
"ReceiveUser": rma.ReceiveUser,
"ReceiveDate": rma.ReceiveDate,
"InspectionInfo": rma.InspectionInfo,
"InspectionUser": rma.InspectionUser,
"InspectionDate": rma.InspectionDate,
"RepairInfo": rma.RepairInfo,
"RepairUser": rma.RepairUser,
"RepairDate": rma.RepairDate,
"RepairEstHours": rma.RepairEstHours,
"RepairProducts": rma.RepairProducts,
"Billable": rma.Billable,
"QuoteNum": rma.QuoteNum,
"OriginalRmaNum": rma.OriginalRmaNum,
"SerialNumber": rma.SerialNumber,
"Products": rma.Products,
"Notes": rma.Notes,
"ShipDate": rma.ShipDate,
"Modified": rma.Modified,
"Status": rma.Status}})
if err != nil {
fmt.Printf("Can't update RMA %v\n", err)
}
}