/
rma_add.go
342 lines (301 loc) · 13.3 KB
/
rma_add.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"fmt"
"net/http"
"strconv"
"text/template"
"time"
"github.com/albrow/forms"
"gopkg.in/mgo.v2/bson"
)
// RmaProduct will store a product for RMA.
// type RmaProduct struct {
// PartNumber string `bson:"PartNumber" json:"PartNumber"`
// Qty int `bson:"Qty" json:"Qty"`
// SerialNumber string `bson:"SerialNumber" json:"SerialNumber"`
// }
// RMA will keep track of the sales order information.
// RMA Type is the first 3 digits
// RMA Number is the unique number for all RMA.
// RMA Types:
// 29 = Warrenty
// 28 = Billable
// 259 = Demo Repair
// type RMA struct {
// ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
// OrigSalesOrder string `bson:"OrigSalesOrder,omitempty" json:"OrigSalesOrder"`
// RmaDate string `bson:"RmaDate" json:"RmaDate"`
// RmaType string `bson:"RmaType" json:"RmaType"`
// RmaNumber string `bson:"RmaNumber" json:"RmaNumber"`
// Company string `bson:"Company" json:"Company"`
// ContactName string `bson:"ContactName" json:"ContactName"`
// ContactAddress string `bson:"ContactAddress" json:"ContactAddress"`
// ContactAddress2 string `bson:"ContactAddress2" json:"ContactAddress2"`
// ContactAddressCityStateZip string `bson:"ContactAddressCityStateZip" json:"ContactAddressCityStateZip"`
// ContactAddressCountry string `bson:"ContactAddressCountry" json:"ContactAddressCountry"`
// ContactEmail string `bson:"ContactEmail" json:"ContactEmail"`
// ContactPhone string `bson:"ContactPhone" json:"ContactPhone"`
// ProductDesc string `bson:"ProductDesc" json:"ProductDesc"`
// ProductID bson.ObjectId `bson:"ProductID,omitempty" json:"ProductID"`
// Products []RmaProduct `bson:"Products" json:"Products"`
// SerialNumber string `bson:"SerialNumber" json:"SerialNumber"`
// ReasonReturn string `bson:"ReasonReturn" json:"ReasonReturn"`
// ReturnCompany string `bson:"ReturnCompany" json:"ReturnCompany"`
// ReturnContact string `bson:"ReturnContact" json:"ReturnContact"`
// ReturnAddress string `bson:"ReturnAddress" json:"ReturnAddress"`
// ReturnAddressCont string `bson:"ReturnAddressCont" json:"ReturnAddressCont"`
// ReturnAddressCityStateZip string `bson:"ReturnAddressCityStateZip" json:"ReturnAddressCityStateZip"`
// ReturnAddressCountry string `bson:"ReturnAddressCountry" json:"ReturnAddressCountry"`
// ReturnPhone string `bson:"ReturnPhone" json:"ReturnPhone"`
// ReturnEmail string `bson:"ReturnEmail" json:"ReturnEmail"`
// ReceiveDate string `bson:"ReceiveDate" json:"ReceiveDate"`
// ReceiveInfo string `bson:"ReceiveInfo" json:"ReceiveInfo"`
// ReceiveUser string `bson:"ReceiveUser" json:"ReceiveUser"`
// InspectionDate string `bson:"InspectionDate" json:"InspectionDate"`
// InspectionInfo string `bson:"InspectionInfo" json:"InspectionInfo"`
// InspectionUser string `bson:"InspectionUser" json:"InspectionUser"`
// RepairDate string `bson:"RepairDate" json:"RepairDate"`
// RepairInfo string `bson:"RepairInfo" json:"RepairInfo"`
// RepairUser string `bson:"RepairUser" json:"RepairUser"`
// RepairEstHours int `bson:"RepairEstHours" json:"RepairEstHours"`
// RepairMaterial []string `bson:"RepairMaterial" json:"RepairMaterial"`
// RepairMaterialID []bson.ObjectId `bson:"RepairMaterialID,omitempty" json:"RepairMaterialID"`
// RepairProducts []RmaProduct `bson:"RepairProducts" json:"RepairProducts"`
// Billable string `bson:"Billable" json:"Billable"`
// QuoteNum string `bson:"QuoteNum" json:"QuoteNum"`
// OriginalRmaNum string `bson:"OriginalRmaNum" json:"OriginalRmaNum"`
// Notes string `bson:"Notes" json:"Notes"`
// Status string `bson:"Status" json:"Status"`
// ShipDate string `bson:"ShipDate" json:"ShipDate"`
// Created time.Time `bson:"Created" json:"Created"`
// Modified time.Time `bson:"Modified" json:"Modified"`
// }
// RmaUpdate will contain the RMA data.
type RmaUpdate struct {
RMA RMA
AddProduct bson.ObjectId
ProductList []Product
StatusList []OptionItem
BillableList []OptionItem
RmaTypeList []OptionItem
Token string
}
// Add the RMA.
func rmaAddHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
// Display blank form
rmaData := &RmaUpdate{}
rmaData.ProductList = *getProductList()
rmaData.StatusList = getStatusList("Reported")
rmaData.BillableList = getBillableList("Warranty")
rmaData.RmaTypeList = getRmaTypeList("290")
rmaData.RMA.RmaNumber = getNewRmaNumber()
displayRmaTemplate(w, rmaData)
} else {
// Parse the form
formData, err := forms.Parse(r)
CheckError(err)
// 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("Company")
// val.Require("ContactName")
// val.Require("ContactEmail")
// val.Require("ContactPhone")
// val.Require("SerialNumber")
// val.Require("ReasonReturn")
// Use data to create a user object
rma := &RMA{
RmaType: formData.Get("RmaType"),
RmaNumber: formData.Get("RmaNumber"),
OrigSalesOrder: formData.Get("OrigSalesOrder"),
Company: formData.Get("Company"),
ContactName: formData.Get("ContactName"),
ContactEmail: formData.Get("ContactEmail"),
ContactPhone: formData.Get("ContactPhone"),
ContactAddress: formData.Get("ContactAddress"),
ContactAddress2: formData.Get("ContactAddress2"),
ContactAddressCityStateZip: formData.Get("ContactAddressCityStateZip"),
ContactAddressCountry: formData.Get("ContactAddressCountry"),
ProductDesc: formData.Get("ProductDesc"),
ReasonReturn: formData.Get("ReasonReturn"),
ReturnCompany: formData.Get("ReturnCompany"),
ReturnContact: formData.Get("ReturnContact"),
ReturnAddress: formData.Get("ReturnAddress"),
ReturnAddressCont: formData.Get("ReturnAddressCont"),
ReturnAddressCityStateZip: formData.Get("ReturnAddressCityStateZip"),
ReturnAddressCountry: formData.Get("ReturnAddressCountry"),
ReturnPhone: formData.Get("ReturnPhone"),
ReturnEmail: formData.Get("ReturnEmail"),
ReceiveInfo: formData.Get("ReceiveInfo"),
ReceiveUser: formData.Get("ReceiveUser"),
InspectionInfo: formData.Get("InspectionInfo"),
InspectionUser: formData.Get("InspectionUser"),
RepairInfo: formData.Get("RepairInfo"),
RepairUser: formData.Get("RepairUser"),
RepairEstHours: formData.GetInt("RepairEstHours"),
Billable: formData.Get("Billable"),
QuoteNum: formData.Get("QuoteNum"),
OriginalRmaNum: formData.Get("OriginalRmaNum"),
SerialNumber: formData.Get("SerialNumber"),
Notes: formData.Get("Notes"),
RmaDate: formData.Get("RmaDate"),
Created: time.Now().Local(),
Modified: time.Now().Local(),
InspectionDate: formData.Get("InspectionDate"),
ReceiveDate: formData.Get("ReceiveDate"),
RepairDate: formData.Get("RepairDate"),
ShipDate: formData.Get("ShipDate"),
Status: formData.Get("Status"),
}
fmt.Printf("Submit Button: %s\n", formData.Get("SubmitButton"))
// Add the new product to the RMA
if formData.Get("SubmitButton") == "ADD" {
//if !val.HasErrors() {
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)
}
rmaData := &RmaUpdate{}
rmaData.ProductList = *getProductList()
rmaData.StatusList = getStatusList(rma.Status)
rmaData.BillableList = getBillableList(rma.Billable)
rmaData.RmaTypeList = getRmaTypeList(rma.RmaType)
rmaData.RMA = *rma
displayRmaTemplate(w, rmaData)
} else if formData.Get("SubmitButton") == "ADD REPAIR" {
//if !val.HasErrors() {
fmt.Printf("Add repair product to RMA: %s\n", rma.RmaNumber)
// Add the product to the repair list
rmaProduct := &RmaProduct{}
rmaProduct.PartNumber = formData.Get("AddRepairProductPartNumber")
rmaProduct.Qty = formData.GetInt("AddRepairProductQty")
rmaProduct.SerialNumber = formData.Get("AddRepairProductSerialNumber")
product := getProductPartNumber(rmaProduct.PartNumber)
if product != nil {
rma.RepairProducts = append(rma.Products, *rmaProduct)
}
rmaData := &RmaUpdate{}
rmaData.ProductList = *getProductList()
rmaData.StatusList = getStatusList(rma.Status)
rmaData.BillableList = getBillableList(rma.Billable)
rmaData.RmaTypeList = getRmaTypeList(rma.RmaType)
rmaData.RMA = *rma
displayRmaTemplate(w, rmaData)
} else {
fmt.Printf("RMA Add: %s\n", rma.RmaNumber)
// 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, err1 := strconv.Atoi(r.Form["ProductQty"][i])
if err1 == nil {
rma.Products[i].Qty = qty
}
}
// Accumulate the Repair Products
for i := range rma.RepairProducts {
rma.RepairProducts[i].PartNumber = r.Form["RepairProductPartNumber"][i]
rma.RepairProducts[i].SerialNumber = r.Form["RepairProductSerialNumber"][i]
qty, err2 := strconv.Atoi(r.Form["RepairProductQty"][i])
if err2 == nil {
rma.RepairProducts[i].Qty = qty
}
}
// Add the RMAs to the DB
err = Vault.Mongo.C("RMAs").Insert(rma)
CheckError(err)
// Go to the list of RMAs
http.Redirect(w, r, "/rma", http.StatusFound)
}
}
}
// Display the template
func displayRmaTemplate(w http.ResponseWriter, rmaData *RmaUpdate) {
// Generate token
rmaData.Token = genToken()
t, _ := template.ParseFiles("header.html", "rma_add.html", "footer.html")
t.ExecuteTemplate(w, "header", nil)
t.ExecuteTemplate(w, "content", rmaData)
t.ExecuteTemplate(w, "footer", nil)
t.Execute(w, rmaData)
}
// Create a Status slice. Then set the selected flag
// based off the status value given.
func getStatusList(status string) []OptionItem {
options := []OptionItem{
OptionItem{"Reported", "Reported", false},
OptionItem{"Received", "Received", false},
OptionItem{"Inspected", "Inspected", false},
OptionItem{"Repaired", "Repaired", false},
OptionItem{"Returned", "Returned", false},
OptionItem{"Completed", "Completed", false},
}
// Set the selected value based off the status given
for i := range options {
if options[i].Value == status {
options[i].Selected = true
}
}
return options
}
// Create a Billable slice. Then set the selected flag
// based off the billable value given.
func getBillableList(billable string) []OptionItem {
options := []OptionItem{
OptionItem{"Billable", "Billable", false},
OptionItem{"Warranty", "Warranty", false},
OptionItem{"N/A", "N/A", false},
}
// Set the selected value based off the string given
for i := range options {
if options[i].Value == billable {
options[i].Selected = true
}
}
return options
}
// Create a RmaType slice. Then set the selected flag
// based off the RmaType value given.
func getRmaTypeList(rmaType string) []OptionItem {
options := []OptionItem{
OptionItem{"280", "280 - Billable", false},
OptionItem{"290", "290 - Warranty", false},
OptionItem{"259", "259 - Demo Repair", false},
}
// Set the selected value based off the string given
for i := range options {
if options[i].Value == rmaType {
options[i].Selected = true
}
}
return options
}
// Get the next RMA number. Find the last RMA number
// and return it plus 1.
func getNewRmaNumber() string {
// Init
var rma RMA
// Get data form DB
err := Vault.Mongo.C("RMAs").Find(bson.M{}).Sort("-RmaNumber").One(&rma)
CheckError(err)
fmt.Println("RMA: ", rma.Company)
fmt.Println("RMA Number: ", rma.RmaNumber)
fmt.Println("RMA Orig Sales Order: ", rma.OrigSalesOrder)
num, err := strconv.Atoi(rma.RmaNumber)
return strconv.Itoa(num + 1)
}