-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
369 lines (319 loc) · 9.9 KB
/
main.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"kiosk-uou-pos/data"
"log"
"net"
"strconv"
"time"
)
func main() {
fmt.Println("※ 키오스크 서버입니다. 창을 닫지 말아주세요. ※")
fmt.Println("※ 웹 페이지의 알람 초기화 버튼을 눌러 알람기능을 동작시켜주세요. ※")
initWeb()
}
func initWeb() {
app := iris.New()
tmpl := iris.Django("./templates", ".html")
tmpl.Reload(true) // reload templates on each request (development mode)
tmpl.AddFunc("greet", func(s string) string { // {{greet(name)}}
return "Greetings " + s + "!"
})
// tmpl.RegisterFilter("myFilter", myFilter) // {{"simple input for filter"|myFilter}}
app.RegisterView(tmpl)
app.HandleDir("/assets", "./assets")
app.Get("/", index)
// 새 주문 들어왔느지 확인
// app.Get("/queue", queue)
// 왼쪽 오더 리스트
app.Get("/orders", refreshedOrderList)
app.Get("/orders/{page:int}", refreshedOrderList)
// 오른쪽 대기번호 조정 패널
app.Get("/waits", refreshWaitNumbers)
// 외부 모니터 대기번호 출력
app.Get("/out-display", outDisplay)
// 정산
app.Get("/jungsan", jungSan)
app.Get("/jungsan/{date:string}", jungSan)
app.Post("/", storeOrderList)
app.Post("/action", action)
app.Connect("/", func(context *context.Context) {
_, _ = context.ResponseWriter().Write([]byte("Hello World"))
})
// http://localhost:8080
_ = app.Listen(":8080")
}
func index(ctx iris.Context) {
_ = ctx.View("index.html")
}
func outDisplay(ctx iris.Context) {
var confirmedOrders []data.Order
var unconfirmedOrders []data.Order
confirmedOrders = data.FindOrderListWithStatus(1, 5)
unconfirmedOrders = data.FindOrderListWithStatus(0, 10)
var unconfirmedOrdersArray [][]data.Order
if len(unconfirmedOrders) > 5 {
unconfirmedOrdersArray = append(unconfirmedOrdersArray, unconfirmedOrders[:5], unconfirmedOrders[5:])
} else {
unconfirmedOrdersArray = append(unconfirmedOrdersArray, unconfirmedOrders)
}
_ = ctx.View("displayWaitingNumbers.html", iris.Map{
"confirmed_orders": confirmedOrders,
"unconfirmed_orders_array": unconfirmedOrdersArray,
})
}
func refreshedOrderList(ctx iris.Context) {
var orders []data.Order
page, err := ctx.Params().GetInt("page")
if err != nil {
// fmt.Print(err)
page = 1
}
data.Paging(page, &orders)
for i, _ := range orders {
var menus []data.Menu
data.GetMenusFromOrder(orders[i], &menus)
orders[i].Menus = make([]data.Menu, len(menus))
copy(orders[i].Menus, menus)
for j, _ := range orders[i].Menus {
var options []data.Option
data.GetOptionsFromMenu(orders[i].Menus[j], &options)
orders[i].Menus[j].Options = make([]data.Option, len(options))
copy(orders[i].Menus[j].Options, options)
}
}
args := map[string]interface{}{
"order_list": orders,
}
buf := new(bytes.Buffer)
ctx.Application().View(buf, "refreshedOrderList.html", "refreshedOrderList.html", args)
ctx.WriteString(buf.String())
}
func refreshWaitNumbers(ctx iris.Context) {
var confirmedOrders []data.Order
var unconfirmedOrders []data.Order
confirmedOrders = data.FindOrderListWithStatus(1, 40)
unconfirmedOrders = data.FindOrderListWithStatus(0, 40)
var confirmedOrdersMashed [][]data.Order
length := len(confirmedOrders)
for i := 0; i < length; i += 3 { // 0,1,2,3,4,5 length:6
if i >= length {
break
}
if length-i < 3 {
if length-i == 1 {
confirmedOrdersMashed = append(confirmedOrdersMashed, []data.Order{confirmedOrders[i]})
} else if length-i == 2 {
confirmedOrdersMashed = append(confirmedOrdersMashed, []data.Order{confirmedOrders[i], confirmedOrders[i+1]})
}
break
}
confirmedOrdersMashed = append(confirmedOrdersMashed, []data.Order{confirmedOrders[i], confirmedOrders[i+1], confirmedOrders[i+2]})
}
var unconfirmedOrdersMashed [][]data.Order
length = len(unconfirmedOrders)
for i := 0; i < length; i += 3 { // 0,1,2,3,4,5 length:6
if i >= length {
break
}
if length-i < 3 {
if length-i == 1 {
unconfirmedOrdersMashed = append(unconfirmedOrdersMashed, []data.Order{unconfirmedOrders[i]})
} else if length-i == 2 {
unconfirmedOrdersMashed = append(unconfirmedOrdersMashed, []data.Order{unconfirmedOrders[i], unconfirmedOrders[i+1]})
}
break
}
unconfirmedOrdersMashed = append(unconfirmedOrdersMashed, []data.Order{unconfirmedOrders[i], unconfirmedOrders[i+1], unconfirmedOrders[i+2]})
}
args := map[string]interface{}{
"unconfirmed_orders": unconfirmedOrdersMashed,
"confirmed_orders": confirmedOrdersMashed,
}
buf := new(bytes.Buffer)
ctx.Application().View(buf, "waitingNumberTable.html", "refreshedOrderList.html", args)
ctx.WriteString(buf.String())
}
func action(ctx iris.Context) {
action := ctx.PostValue("action")
if action == "confirm" {
orderNumber, _ := strconv.Atoi(ctx.PostValue("orderNumber"))
data.UpdateOrderListConfirmation(uint(orderNumber))
_, _ = ctx.Text("confirmed")
return
} else if action == "cancel" {
orderNumber, _ := strconv.Atoi(ctx.PostValue("orderNumber"))
data.CancelOrderList(uint(orderNumber))
_, _ = ctx.Text("canceled")
return
} else if action == "insertbogus" {
data.InsertBogusOrderList()
_, _ = ctx.Text("inserted bogus")
return
} else if action == "printJungsan" {
result := printJungsan(ctx.PostValue("body"))
if result {
ctx.Write([]byte("ok"))
} else {
ctx.Write([]byte("fail"))
}
return
} else if action == "reprint" {
orderNumber, _ := strconv.Atoi(ctx.PostValue("orderNumber"))
order := data.FindOrderList(uint(orderNumber))
var menus []data.Menu
data.GetMenusFromOrder(order, &menus)
order.Menus = menus
for j, _ := range order.Menus {
var options []data.Option
data.GetOptionsFromMenu(order.Menus[j], &options)
order.Menus[j].Options = make([]data.Option, len(options))
copy(order.Menus[j].Options, options)
}
var m map[string]interface{}
jsonBytes, err := json.Marshal(order)
if err != nil {
panic("JSON으로 구조체를 변경하는데 문제가 있습니다")
}
err = json.Unmarshal(jsonBytes, &m)
if err != nil {
panic("JSON을 맵으로 변경하는데 문제가 있습니다")
}
m["action"] = "reprint"
// m["orderNum"] = m["ID"]
m["orderNum"] = m["TodayIndex"]
jsonBytes, err = json.Marshal(m)
if err != nil {
panic("맵을 JSON으로 변경하는데 문제가 있습니다")
}
printWithThermalPrinter(jsonBytes)
_, _ = ctx.Text("reprinted")
return
}
_, _ = ctx.Text("알 수 없는 명령입니다")
return
}
var newOrderAvailable bool = false
func printJungsan(data string) bool {
jungsanData := []byte(data)
fmt.Println(data)
connection, err := net.Dial("tcp", ":13522")
if err != nil {
log.Printf("프린터 서버가 켜져있지 않습니다.")
return false
}
_, err = connection.Write(jungsanData)
if err != nil {
log.Printf("정산 내용이 들어왔습니다. 프린트를 시작합니다.")
}
err = connection.Close()
return true
}
func storeOrderList(ctx iris.Context) {
order, _ := ctx.GetBody()
fmt.Printf("%x\n", md5.Sum(order))
result := data.InsertOrderList(order)
newOrderAvailable = true
response := iris.Map{"state": "OK", "orderNumber": result.TodayIndex}
ctx.JSON(response)
println(response)
var m map[string]interface{}
err := json.Unmarshal(order, &m)
m["orderNum"] = result.TodayIndex
newOrderData, err := json.Marshal(m)
if err != nil {
log.Println("JSON 주문번호 추가 과정에서 에러가 발생했습니다")
}
printWithThermalPrinter(newOrderData)
}
func printWithThermalPrinter(jsonBytes []byte) {
connection, err := net.Dial("tcp", ":13522")
if err != nil {
fmt.Println(err)
log.Printf("프린터 서버가 켜져있지 않습니다.")
} else {
_, err := connection.Write(jsonBytes)
if err != nil {
log.Printf("주문 프린트 요청이 입력되었습니다. 프린트를 시작합니다.")
}
err = connection.Close()
}
}
func queue(ctx iris.Context) {
ctx.JSON(iris.Map{"new": newOrderAvailable})
newOrderAvailable = false
}
func jungSan(ctx iris.Context) {
var orders []data.Order
tmpDate := ctx.Params().GetString("date")
var date time.Time
var err error
isMonth := false
if tmpDate == "" {
date = time.Now()
} else if len(tmpDate) == 8 {
date, err = time.Parse("20060102", ctx.Params().Get("date"))
if err != nil {
date = time.Now()
}
} else if len(tmpDate) == 6 {
isMonth = true
date, err = time.Parse("200601", ctx.Params().Get("date"))
if err != nil {
date = time.Now()
}
} else {
_, _ = ctx.Text("날짜 형식에 문제가 있습니다")
return
}
data.ChangeDBFile(date.Format("200601"))
if isMonth {
orders = data.FindOrderListWithMonth(date)
} else {
orders = data.FindOrderListWithDate(date)
}
totalPrice := 0
canceledCnt, canceledPrice := 0, 0
discountCnt, discountPrice := 0, 0
// 메뉴별 수량, 금액 카운트용
menuTable := make(map[string][]int)
for i := 0; i < len(orders); i++ {
data.Db.Model(&orders[i]).Association("Menus").Find(&orders[i].Menus)
if orders[i].IsConfirmed == 3 {
canceledCnt, canceledPrice = canceledCnt+1, canceledPrice+orders[i].TotalPrice
}
for _, menu := range orders[i].Menus {
if menu.IsTumbler {
discountCnt, discountPrice = discountCnt+1, discountPrice+200
}
if menuTable[menu.Name] == nil {
menuTable[menu.Name] = []int{0, 0}
}
menuTable[menu.Name][0] += 1
menuTable[menu.Name][1] += menu.TotalPrice
}
totalPrice += orders[i].TotalPrice
}
args := map[string]interface{}{
"switch": "일별",
"order_list": orders,
"date": date.Format("2006-01-02"),
"canceledCnt": canceledCnt,
"canceledPrice": canceledPrice,
"discountCnt": discountCnt,
"discountPrice": discountPrice,
"totalPrice": totalPrice,
"menuTable": menuTable,
}
if isMonth {
args["date"] = date.Format("2006-01")
args["switch"] = "월별"
}
data.ChangeDBFile(time.Now().Format("200601"))
_ = ctx.View("jungsan.html", args)
}