-
Notifications
You must be signed in to change notification settings - Fork 1
/
itemcach.src
executable file
·375 lines (336 loc) · 13.8 KB
/
itemcach.src
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
370
371
372
373
374
375
'##########################################################################
'
' Item Cache
'
'##########################################################################
'$include:'const.inc'
'$include:'util.def'
'$include:'util.inc'
'$include:'input.def'
'$include:'input.inc'
'$include:'screen.def'
'$include:'screen.inc'
'$include:'com.def'
'$include:'com.inc'
'$include:'itemcach.def'
'-- shared const variable
private ItemCache.ItemDataFileName$[20]
private ItemCache.fileNum%
private ItemCache.size%
private ItemCache.productIds&(ItemCache.RECORDCHUNK)
private ItemCache.productNames$(ItemCache.RECORDCHUNK)[ItemCache.NAMELEN]
private ItemCache.barCodes$(ItemCache.RECORDCHUNK)[ItemCache.BARCODELEN]
private ItemCache.productCodes$(ItemCache.RECORDCHUNK)[ItemCache.CODELEN]
private ItemCache.stockTotals%(ItemCache.RECORDCHUNK)
private ItemCache.stockUnlimiteds%(ItemCache.RECORDCHUNK)
'------------------------------------------------------------
' parse item cache
'------------------------------------------------------------
function ItemCache.ParseItemCache%(byref src$, byref meta$, byref ids&(), byref names$(), byref productCodes$(), byref barCodes$(), byref stockTotals%(), byref stockUnlimiteds%())
private ptr&
private idEnd&
private nameEnd&
private barCodeEnd&
private codeEnd&
private totalEnd&
private unlimitedEnd&
private cnt%
private srcLen&: srcLen&=len(src$)
private tmpLen&
private buf$[30]
ptr&=1 ' string pointer
private metaEnd&
metaEnd&=instr(ptr&,src$,STREOL$)
if metaEnd&>0 then
tmpLen&=Util.Min(metaEnd&-ptr&,Util.MAXIDLEN)
meta$=mid$(src$,ptr&,tmpLen&)
ptr&=metaEnd&+len(STREOL$)
else
meta$=mid$(src$,ptr&)
exit function
end if
cnt%=0 ' array index
while ptr&<srcLen& and ptr&<>0
idEnd&=instr(ptr&,src$,TAB$)
if idEnd&>0 then
tmpLen&=Util.Min(idEnd&-ptr&,ItemCache.IDLEN)
ids&(cnt%)=val(mid$(src$,ptr&,tmpLen&))
nameEnd&=instr(idEnd&+1,src$,TAB$)
if nameEnd&>0 then
tmpLen&=Util.Min(nameEnd&-(idEnd&+1),ItemCache.NAMELEN)
buf$=mid$(src$,idEnd&+1,tmpLen&)
names$(cnt%)=Util.TruncateStr$(buf$,1,tmpLen&)
barCodeEnd&=instr(nameEnd&+1,src$,TAB$)
if barCodeEnd&>0 then
tmpLen&=Util.Min(barCodeEnd&-(nameEnd&+1),ItemCache.BARCODELEN)
barCodes$(cnt%)=mid$(src$,nameEnd&+1,tmpLen&)
codeEnd&=instr(barCodeEnd&+1,src$,TAB$)
if codeEnd& > 0 then
tmpLen&=Util.Min(codeEnd&-(barCodeEnd&+1),ItemCache.CODELEN)
productCodes$(cnt%)=mid$(src$,barCodeEnd&+1,tmpLen&)
totalEnd&=instr(codeEnd&+1,src$,TAB$)
if totalEnd& > 0 then
tmpLen&=Util.Min(totalEnd&-(codeEnd&+1),ItemCache.STOCKLEN)
stockTotals%(cnt%)=val(mid$(src$,codeEnd&+1,tmpLen&))
unlimitedEnd&=instr(totalEnd&+1,src$,STREOL$)
if unlimitedEnd&>0 then
tmpLen&=Util.Min(unlimitedEnd&-(totalEnd&+1),ItemCache.UNLIMITEDLEN)
else
tmpLen&=Util.Min(len(src$)+1-(totalEnd&+1),Util.MAXSTRLEN)
end if
stockUnlimiteds%(cnt%)=val(mid$(src$,totalEnd&+1,tmpLen&))
else
print "no stock total for ",ids&(cnt%)
end if
else
print "no code for ",ids&(cnt%)
end if
else
print "no barcode for ";
print ids&(cnt%);
print " ne:";
print nameEnd&;
print " be:";
print barCodeEnd&
end if
else
print "no name for ",ids&(cnt%)
end if
else
print "no id"
end if
ptr&=instr(ptr&,src$,STREOL$) ' move to next line
if ptr&>0 then
ptr&=ptr&+1
end if
cnt%=cnt%+1
wend
ItemCache.ParseItemCache%=cnt%
end function
'------------------------------------------------------------
' search item
'------------------------------------------------------------
function ItemCache.SearchItem&(key$,flag%)
' search
private rec&
open ItemCache.ItemDataFileName$ AS #ItemCache.fileNum% RECORD ItemCache.RECORDLEN
field #ItemCache.fileNum%, ItemCache.IDLEN as ItemCache.productId$, ItemCache.NAMELEN as ItemCache.productName$, ItemCache.BARCODELEN as ItemCache.barCode$, ItemCache.CODELEN as ItemCache.productCode$, ItemCache.STOCKLEN as ItemCache.stock$, ItemCache.UNLIMITEDLEN as ItemCache.stockUnlimited$
select flag%
case BY.BARCODE
rec&=search (#ItemCache.fileNum%, ItemCache.barCode$, key$, 1)
case BY.PRODUCT.ID
rec&=search (#ItemCache.fileNum%, ItemCache.productId$, key$, 1)
case BY.PRODUCT.CODE
rec&=search (#ItemCache.fileNum%, ItemCache.productCode$, key$, 1)
end select
if rec&>0 then
get #ItemCache.fileNum%,rec&
else
ItemCache.productId$=""
ItemCache.productName$=""
ItemCache.barCode$=""
ItemCache.productCode$=""
ItemCache.stock$=""
ItemCache.stockUnlimited$=""
end if
close #ItemCache.fileNum%
ItemCache.SearchItem&=rec&
end function
'------------------------------------------------------------
' search item by barcode
'------------------------------------------------------------
function ItemCache.SearchItemByBarcode&(key$)
ItemCache.SearchItemByBarcode&=ItemCache.SearchItem&(key$,BY.BARCODE)
end function
'------------------------------------------------------------
' search item by product id
'------------------------------------------------------------
function ItemCache.SearchItemByProductId&(key$)
ItemCache.SearchItemByProductId&=ItemCache.SearchItem&(key$,BY.PRODUCT.ID)
end function
'------------------------------------------------------------
' search item by product code
'------------------------------------------------------------
function ItemCache.SearchItemByProductCode&(key$)
ItemCache.SearchItemByProductCode&=ItemCache.SearchItem&(key$,BY.PRODUCT.CODE)
end function
'------------------------------------------------------------
' append item
'------------------------------------------------------------
function ItemCache.Append%(id$,name$,code$,barcode$,stock&)
private flen&
open ItemCache.ItemDataFileName$ AS #ItemCache.fileNum% RECORD ItemCache.RECORDLEN
field #ItemCache.fileNum%, ItemCache.IDLEN as ItemCache.productId$, ItemCache.NAMELEN as ItemCache.productName$, ItemCache.BARCODELEN as ItemCache.barCode$, ItemCache.CODELEN as ItemCache.productCode$, ItemCache.STOCKLEN as ItemCache.stock$, ItemCache.UNLIMITEDLEN as ItemCache.stockUnlimited$
flen&=lof(#ItemCache.fileNum%)
ItemCache.productId$=id$
ItemCache.productCode$=code$
ItemCache.productName$=name$
ItemCache.barCode$=barcode$
if stock&<>999999 then
ItemCache.stock$=mid$(str$(stock&),2)
ItemCache.stockUnlimited$="0"
else
ItemCache.stock$="0"
ItemCache.stockUnlimited$="1"
end if
put #ItemCache.fileNum%, flen&+1
close #ItemCache.fileNum%
ItemCache.Append%=0
end function
'------------------------------------------------------------
' update barcode
'------------------------------------------------------------
function ItemCache.UpdateBarcode%(id$,name$,code$,barcode$,stock&)
private rec&
rec&=ItemCache.SearchItemByProductId&(id$)
if rec&>0 then
open ItemCache.ItemDataFileName$ AS #ItemCache.fileNum% RECORD ItemCache.RECORDLEN
field #ItemCache.fileNum%, ItemCache.IDLEN as ItemCache.productId$, ItemCache.NAMELEN as ItemCache.productName$, ItemCache.BARCODELEN as ItemCache.barCode$, ItemCache.CODELEN as ItemCache.productCode$, ItemCache.STOCKLEN as ItemCache.stock$, ItemCache.UNLIMITEDLEN as ItemCache.stockUnlimited$
get #ItemCache.fileNum%, rec&
ItemCache.barCode$=barcode$ '-- update barcode
put #ItemCache.fileNum%, rec&
close #ItemCache.fileNum%
ItemCache.UpdateBarcode%=0
else
ItemCache.UpdateBarcode%=ItemCache.Append%(id$,name$,code$,barcode$,stock&)
end if
end function
'------------------------------------------------------------
' null barcode
'------------------------------------------------------------
function ItemCache.NullBarcode%(barcode$)
private rec&
rec&=ItemCache.SearchItemByBarcode&(barcode$)
if rec&>0 then
open ItemCache.ItemDataFileName$ AS #ItemCache.fileNum% RECORD ItemCache.RECORDLEN
field #ItemCache.fileNum%, ItemCache.IDLEN as ItemCache.productId$, ItemCache.NAMELEN as ItemCache.productName$, ItemCache.BARCODELEN as ItemCache.barCode$, ItemCache.CODELEN as ItemCache.productCode$, ItemCache.STOCKLEN as ItemCache.stock$, ItemCache.UNLIMITEDLEN as ItemCache.stockUnlimited$
get #ItemCache.fileNum%, rec&
ItemCache.barCode$="" '-- null barcode
put #ItemCache.fileNum%, rec&
close #ItemCache.fileNum%
end if
ItemCache.NullBarcode%=0
end function
'------------------------------------------------------------
' write item cache data into file
'------------------------------------------------------------
function ItemCache.Write%(byref idx&, total%)
private i%
open ItemCache.ItemDataFileName$ AS #ItemCache.fileNum% RECORD ItemCache.RECORDLEN
for i%=0 to ItemCache.size%-1
ItemCache.productId$=mid$(str$(ItemCache.productIds&(i%)),2)
ItemCache.productName$=ItemCache.productNames$(i%)
ItemCache.productCode$=ItemCache.productCodes$(i%)
ItemCache.barCode$=ItemCache.barCodes$(i%)
ItemCache.stock$=mid$(str$(ItemCache.stockTotals%(i%)),2)
ItemCache.stockUnlimited$=mid$(str$(ItemCache.stockUnlimiteds%(i%)),2)
field #ItemCache.fileNum%, ItemCache.IDLEN as ItemCache.productId$, ItemCache.NAMELEN as ItemCache.productName$, ItemCache.BARCODELEN as ItemCache.barCode$, ItemCache.CODELEN as ItemCache.productCode$, ItemCache.STOCKLEN as ItemCache.stock$, ItemCache.UNLIMITEDLEN as ItemCache.stockUnlimited$
put #ItemCache.fileNum%, idx&
idx&=idx&+1
next
close #ItemCache.fileNum%
locate 1,3
print mid$(str$(idx&-1),2);
print "/";
print total%;
print " "
if idx&>=ItemCache.RECORDLEN or idx&>=total% then
ItemCache.Write%=Com.Status.DataReceivedEnd '-- end
else
ItemCache.Write%=Com.Status.SocketConnected '-- read again
end if
end function
'------------------------------------------------------------
' Get Http and Store into file
'------------------------------------------------------------
function ItemCache.GetStoreFile%
private startTime$[8]
private endTime$[8]
startTime$ = time$ '-- start measuring
private req$[120]
req$=Com.Http.URLPREFIX$+"/cacheItems.php"
private ret$[Com.RECVSIZE]
private status%
status%=Com.Status.Init '-- init status
private offset%
private size%
private total%
private meta$[100]
private idx&
private r$[1]
offset%=0
idx&=1
'-- state machine
while status%<>Com.Status.DeviceClosed
select status%
case Com.Status.Init
status%=Com.TcpIp.OpenDevice%(status%)
case Com.Status.DeviceOpened
status%=Com.TcpIp.Open%(status%)
case Com.Status.Opened
status%=Com.TcpIp.OpenSocket%(status%)
case Com.Status.SocketConnected
status%=Com.Http.SendData%(Com.Http.GetMode,req$+"?o="+mid$(str$(offset%),2),"",status%)
case Com.Status.DataSent
status%=Com.Http.RecvData%(ret$,status%)
case Com.Status.DataReceived
ItemCache.size% = ItemCache.ParseItemCache%(ret$,meta$,ItemCache.productIds&,ItemCache.productNames$,ItemCache.productCodes$,ItemCache.barCodes$,ItemCache.stockTotals%,ItemCache.stockUnlimiteds%)
total%=Util.GetPageInfo%(meta$,offset%,size%)
status%=ItemCache.Write%(idx&,total%)
offset%=offset%+ItemCache.RECORDCHUNK
case Com.Status.DataReceivedEnd
status%=Com.TcpIp.CloseSocket%(status%)
case Com.Status.SocketClosed
status%=Com.TcpIp.Close%(status%)
case Com.Status.Closed
status%=Com.TcpIp.CloseDevice%(status%)
end select
wend
ItemCache.GetStoreFile% = 0
endTime$=time$
APP.ELAPSEDTIME% = Util.GetSeconds%(startTime$, endTime$)
APP.DEBUGMSG$ = ""
end function
'------------------------------------------------------------
' load item cache
'------------------------------------------------------------
function ItemCache.load%
private ret%
call Screen.Init
call Screen.Footer
locate 1,1
print "商品マスタを更新しています..."
ret%= ItemCache.GetStoreFile%
if ret%=-1 then
call Screen.Init
call Screen.Footer
locate 1,1
print "商品が見つかりませんでした"
beep 2
Input.InputAnyKey
ItemCache.load%=0
exit function
end if
beep 1
ItemCache.load% = ItemCache.size%
end function
'------------------------------------------------------------
' init
'------------------------------------------------------------
sub ItemCache.Init
ItemCache.ItemDataFileName$="ITEM.DAT"
ItemCache.fileNum% = 4
end sub
'------------------------------------------------------------
' main
'------------------------------------------------------------
sub ItemCache.Main
private ret%
ItemCache.init '-- init
Com.ReadConfig '-- read communication config file
ret%=ItemCache.load%
end sub
'------------------------------------------------------------
Main
call ItemCache.Main
chain "init.pd4"