-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestunit.pas
296 lines (231 loc) · 8.71 KB
/
requestunit.pas
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
unit RequestUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, JsonTools, TypesUnit, AtolKKTUnit;
type
{ TRequest }
TRequest = class(TObject)
public
class function getRequestTypeByRequest(const fRequest: string): RequestType;
class function getStringByJSONNode(jn: TJsonNode): string;
class function requestDeviceDataToAtolDeviceDataType(js: TJsonNode): AtolDeviceDataType;
class function GenerateReadableText(js: TJsonNode): string;
class function requestComandToRequestComandType(const st: string): RequestComandType;
class procedure getPrintTStringListRequestData(var lst: TStringList; var setmode_one: boolean; var use_print_string: boolean; const data: string);
class procedure setTStringListFromRequestDataJSONobject(js: TJsonNode; const field_name: string; var lst: TStringList);
class function getAtolFontByJSON(js: TJsonNode): TAtolFont;
class procedure getPrintStringListRequestData(var text: string; var setmode_one: boolean; var use_print_string: boolean; const data: string);
class procedure getIncomeRequestData(var header_lst, footer_lst: TStringList; var cheque_data: ChequeDataType; const data: string);
class function getChequeGoodsTypeByJSON(js: TJsonNode): ChequeGoodsType;
class function getTypeCloseTypeByStr(const tc: string): TypeCloseType;
class function getNdsTypeByStr(const nds: string): NDSType;
class function getCashIncomeOutcomeRequestData(const data: string): real;
class function getSegmentRequestData(const text: string): RequestSegmentType;
end;
implementation
{ TRequest }
class function TRequest.getRequestTypeByRequest(const fRequest: string): RequestType;
var js: TJsonNode;
cmd: RequestComandType;
data: string;
req: RequestType;
begin
js := TJsonNode.Create();
js.Parse(AnsiToUtf8(fRequest));
cmd := requestComandToRequestComandType(js.Find('comand').AsString);
data := getStringByJSONNode(js.Find('data'));
req.comand := cmd;
req.data := data;
req.atol_device_data := requestDeviceDataToAtolDeviceDataType(js.Find('atol_device_data'));
js.Free;
result := req;
end;
class function TRequest.getStringByJSONNode(jn: TJsonNode): string;
begin
result := Utf8ToAnsi(GenerateReadableText(jn));
end;
class function TRequest.requestDeviceDataToAtolDeviceDataType(js: TJsonNode
): AtolDeviceDataType;
begin
result.atol_device_number := round(js.Find('atol_device_number').AsNumber);
result.atol_operator_password := js.Find('atol_operator_password').AsString;
end;
class function TRequest.GenerateReadableText(js: TJsonNode): string;
begin
result := js.AsJson;
end;
class function TRequest.requestComandToRequestComandType(const st: string
): RequestComandType;
begin
result := rctUnknown;
if AnsiCompareText(st,'PRINT_TEXT_CHEQUE')=0 then
result := rctPrintTestCheque;
if AnsiCompareText(st,'CASH_REGISTER_INF')=0 then
result := rctCashRegisterInf;
if AnsiCompareText(st,'PRINT_TSTRING_LIST')=0 then
result := rctPrintTStringList;
if AnsiCompareText(st,'PRINT_STRING_LIST')=0 then
result := rctPrintStringList;
if AnsiCompareText(st,'CHEQUE_PARTIAL_CUT')=0 then
result := rctChequePartialCut;
if AnsiCompareText(st,'CHEQUE_FULL_CUT')=0 then
result := rctChequeFullCut;
if AnsiCompareText(st,'RESET_SUMMARY')=0 then
result := rctResetSummary;
if AnsiCompareText(st,'TECH_RESET')=0 then
result := rctTechReset;
if AnsiCompareText(st,'INCOME')=0 then
result := rctIncome;
if AnsiCompareText(st,'RETURN')=0 then
result := rctReturn;
if AnsiCompareText(st,'XREPORT')=0 then
result := rctXReport;
if AnsiCompareText(st,'ZREPORT')=0 then
result := rctZReport;
if AnsiCompareText(st,'CASH_INCOME')=0 then
result := rctCashIncome;
if AnsiCompareText(st,'CASH_OUTCOME')=0 then
result := rctCashOutcome;
if AnsiCompareText(st,'OPEN_SESSION')=0 then
result := rctOpenSession;
if AnsiCompareText(st,'PRINT_LAST_CHEQUE_COPY')=0 then
result := rctPrintLastCheckCopy;
if AnsiCompareText(st,'SEGMENT')=0 then
result := rctSegment;
end;
class procedure TRequest.getPrintTStringListRequestData(var lst: TStringList;
var setmode_one: boolean; var use_print_string: boolean; const data: string);
var js: TJsonNode;
begin
js := TJsonNode.Create;
js.Parse(AnsiToUtf8(data));
setmode_one := js.Find('setmode_one').AsBoolean;
use_print_string := js.Find('use_print_string').AsBoolean;
setTStringListFromRequestDataJSONobject(js, 'text', lst);
js.free;
end;
class procedure TRequest.setTStringListFromRequestDataJSONobject(
js: TJsonNode; const field_name: string; var lst: TStringList);
var chd: TJsonNode;
st: string;
af: TAtolFont;
begin
for chd in js.Find(field_name) do
begin
af := getAtolFontByJSON(chd.Find('font'));
st := chd.Find('text').AsString;
lst.AddObject(st, af);
end;
end;
class function TRequest.getAtolFontByJSON(js: TJsonNode): TAtolFont;
begin
result := TAtolFont.Create;
result.FontBold := js.Find('b').AsBoolean;
result.FontItalic := js.Find('i').AsBoolean;
result.FontNegative := js.Find('n').AsBoolean;
result.FontUnderline := js.Find('u').AsBoolean;
result.FontDblHeight := js.Find('dh').AsBoolean;
result.FontDblWidth := js.Find('dw').AsBoolean;
result.TextWrap := round(js.Find('w').AsNumber);
result.Alignment := round(js.Find('a').AsNumber);
end;
class procedure TRequest.getPrintStringListRequestData(var text: string;
var setmode_one: boolean; var use_print_string: boolean; const data: string);
var js: TJsonNode;
begin
js := TJsonNode.Create;
js.Parse(AnsiToUtf8(data));
setmode_one := js.Find('setmode_one').AsBoolean;
use_print_string := js.Find('use_print_string').AsBoolean;
text := js.Find('text').AsString;
js.free;
end;
class procedure TRequest.getIncomeRequestData(var header_lst,
footer_lst: TStringList; var cheque_data: ChequeDataType; const data: string);
var js, ijs: TJsonNode;
i: integer;
g: ChequeGoodsType;
begin
js := TJsonNode.Create;
js.Parse(AnsiToUtf8(data));
setTStringListFromRequestDataJSONobject(js, 'header_text', header_lst);
setTStringListFromRequestDataJSONobject(js, 'footer_text', footer_lst);
SetLength(cheque_data.goods, 0);
for ijs in js.Find('goods') do
begin
g := getChequeGoodsTypeByJSON(ijs);
SetLength(cheque_data.goods, Length(cheque_data.goods)+1);
cheque_data.goods[Length(cheque_data.goods)-1] := g;
end;
cheque_data.check_sm := js.Find('check_sm').AsNumber;
cheque_data.check_discount_sm := js.Find('check_discount_sm').AsNumber;
cheque_data.type_close := getTypeCloseTypeByStr(js.Find('type_close').AsString);
cheque_data.cash_sm := js.Find('cash_sm').AsNumber;
cheque_data.tr_id := round(js.Find('tr_id').AsNumber);
js.free;
end;
class function TRequest.getChequeGoodsTypeByJSON(js: TJsonNode): ChequeGoodsType;
begin
result.name := js.Find('name').AsString;
result.price := js.Find('price').AsNumber;
result.kol := round(js.Find('kol').AsNumber);
result.nds := getNdsTypeByStr(js.Find('nds').AsString);
result.barcode := js.Find('barcode').AsString;
result.dep := round(js.Find('dep').AsNumber);
result.discount := js.Find('discount').AsNumber;
end;
class function TRequest.getTypeCloseTypeByStr(const tc: string): TypeCloseType;
begin
result := tcCash;
if AnsiCompareText(tc, 'tcCash')=0 then
result := tcCash;
if AnsiCompareText(tc, 'tc1')=0 then
result := tc1;
if AnsiCompareText(tc, 'tc2')=0 then
result := tc2;
if AnsiCompareText(tc, 'tc3')=0 then
result := tc3;
if AnsiCompareText(tc, 'tc4')=0 then
result := tc4;
if AnsiCompareText(tc, 'tc5')=0 then
result := tc5;
if AnsiCompareText(tc, 'tc6')=0 then
result := tc6;
end;
class function TRequest.getNdsTypeByStr(const nds: string): NDSType;
begin
result := ndsNo;
if AnsiCompareText(nds, 'ndsSection')=0 then
result := ndsSection;
if AnsiCompareText(nds, 'nds0')=0 then
result := nds0;
if AnsiCompareText(nds, 'nds10')=0 then
result := nds10;
if AnsiCompareText(nds, 'nds18')=0 then
result := nds18;
if AnsiCompareText(nds, 'nds18118')=0 then
result := nds18118;
if AnsiCompareText(nds, 'nds10110')=0 then
result := nds10110;
end;
class function TRequest.getCashIncomeOutcomeRequestData(const data: string): real;
var js: TJsonNode;
begin
js := TJsonNode.Create;
js.Parse(AnsiToUtf8(data));
result := js.Find('sm').AsNumber;
js.Free;
end;
class function TRequest.getSegmentRequestData(const text: string): RequestSegmentType;
var js: TJsonNode;
begin
js := TJsonNode.Create;
js.Parse(AnsiToUtf8(text));
result.guid := js.Find('segment_guid').AsString;
result.num := round(js.Find('segment_num').AsNumber);
result.count := round(js.Find('segment_count').AsNumber);
result.text := js.Find('text').AsString;
js.Free;
end;
end.