-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEcr.cs
373 lines (306 loc) · 11.8 KB
/
Ecr.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace EcrBluetooth
{
public class Ecr
{
private const int MaxDataSize = 218;
private int _packetSeq = 0x20;
public string Command(int command, string? data)
{
_packetSeq++;
if (_packetSeq > 0x7F)
_packetSeq = 0x20;
return GetPacket(command, data);
}
private static string GetPacket(int command, string? arguments)
{
var len = (uint) (arguments?.Length ?? 0);
var data = ToAnsi(arguments);
for (;;)
{
var buf = new byte[MaxDataSize];
uint offs = 0;
var crc = 0;
if (len > MaxDataSize) throw new ArgumentException("Lenght of the packet exceeds the limits!");
// Set control symbol
buf[offs++] = 0x01;
buf[offs++] = (byte) (0x24 + len);
const int mPacketSeq = 0x20;
// Set packet sequence
buf[offs++] = mPacketSeq;
// Set command
buf[offs++] = (byte) command;
// Set data
if (len > 0)
Array.Copy(data ?? throw new InvalidOperationException(), 0,
buf, offs, len);
//[self toAnsi:data data:&buf[offs]];
offs += len;
// Set control symbol
buf[offs++] = 0x05;
// Calculate checksum
for (var i = 1; i < offs; i++) crc += buf[i] & 0xff;
// Set checksum
buf[offs++] = (byte) (((crc >> 12) & 0xf) + 0x30);
buf[offs++] = (byte) (((crc >> 8) & 0xf) + 0x30);
buf[offs++] = (byte) (((crc >> 4) & 0xf) + 0x30);
buf[offs++] = (byte) (((crc >> 0) & 0xf) + 0x30);
// Set control symbol
buf[offs] = 0x03;
return Encoding.UTF8.GetString(buf, 0, buf.Length);
}
}
private static byte[]? ToAnsi(string? text)
{
if (string.IsNullOrEmpty(text))
return null;
var data = new byte[text.Length];
for (var s = 0; s < text.Length; s++)
{
var c = text[s];
data[s] = (byte) c;
if (c < 0x80)
continue;
data[s] = (byte) c;
}
return data;
}
public string GetReportByteString(ReportType reportType)
{
return reportType switch
{
ReportType.XReport => Command(69, "2"),
ReportType.ZReport => Command(69, "0"),
_ => throw new ArgumentOutOfRangeException(nameof(reportType), reportType, null)
};
}
public string GetCancelReceiptByte()
{
return Command(60, string.Empty);
}
public string GetDeleteArticlesByte()
{
return Command(107, "DA");
}
public string GetCloseReceiptByte()
{
return Command(56, string.Empty);
}
public string GetPrintDuplicateReceiptByte()
{
return Command(109, "1");
}
public string GetSetOperatorNameByte(string operatorName)
{
return Command(102, $"1,000000,{operatorName}");
}
public string GetInitializeReceiptByte()
{
return Command(48, "1;000000;1");
}
public string GetRegisterItemBytes(Item item)
{
item.Price = Math.Round(item.Price, 2, MidpointRounding.AwayFromZero);
var priceString = item.Price.ToString(CultureInfo.InvariantCulture);
if (!string.IsNullOrEmpty(priceString) && priceString.Contains(','))
priceString = priceString.Replace(",", ".");
if (item.Description.Length >= 25)
item.Description = item.Description[..24];
var command =
$"p{Enum.GetName(item.TaxCategory)}{item.ItemId},1,{priceString},10000,..\\t{item.Description}\\t..";
if (string.IsNullOrEmpty(command)) throw new Exception("Data should contain value");
return Command(107, command);
}
public List<string> GetRegisterItemsBytes(List<Item> items)
{
var byteList = new List<string>();
foreach (var item in items)
{
item.Price = Math.Round(item.Price, 2, MidpointRounding.AwayFromZero);
var priceString = item.Price.ToString(CultureInfo.InvariantCulture);
if (!string.IsNullOrEmpty(priceString) && priceString.Contains(','))
priceString = priceString.Replace(",", ".");
if (item.Description.Length >= 25)
item.Description = item.Description[..24];
var command =
$"p{Enum.GetName(item.TaxCategory)}{item.ItemId},1,{priceString},10000,..\\t{item.Description}\\t..";
if (string.IsNullOrEmpty(command)) throw new Exception("Data should contain value");
byteList.Add(Command(107, command));
}
return byteList;
}
public List<string> GetSellItemsBytes(List<Item> items)
{
var byteList = new List<string>();
foreach (var item in items)
{
string isVoid;
string amountString;
item.Price = Math.Round(item.Price, 2, MidpointRounding.AwayFromZero);
item.Rebate = Math.Round(item.Rebate, 2, MidpointRounding.AwayFromZero);
if (item.Amount < 0)
{
isVoid = "D^";
amountString = (item.Amount * -1).ToString("0.000");
}
else
{
isVoid = "D";
amountString = item.Amount.ToString("0.000");
}
var command = $"{isVoid}{item.ItemId}*{amountString}#{item.Price}";
if (item.Rebate != 0)
command += $",-{item.Rebate}";
if (item.Description.Length >= 25)
item.Description = item.Description[..24];
if (string.IsNullOrEmpty(command)) throw new Exception("Data should contain value");
byteList.Add(Command(58, command));
}
return byteList;
}
public List<string> GetPaymentBytes(List<Payment> payments)
{
var paymentList = new List<string>();
foreach (var payment in payments)
{
payment.Value = Math.Round(payment.Value, 2, MidpointRounding.AwayFromZero);
var valueString = payment.Value.ToString(CultureInfo.InvariantCulture);
if (valueString.Contains(','))
valueString = valueString.Replace(",", ".");
var command = $"{Enum.GetName(payment.PaymentMethod)}";
if (payments.Count == 1)
command += valueString;
paymentList.Add(Command(53, command));
}
paymentList.Add(Command(110, string.Empty));
return paymentList;
}
public List<string> GetProgramLinesBytes(ProgramLine line)
{
var programLines = new List<string>();
var forReturn = line.TotalCashPayed - line.TotalInvoiceValue < 0
? "0.00"
: (line.TotalCashPayed -
line.TotalInvoiceValue).ToString("0.00");
var totalCommand = $"Tot. paguar: {line.TotalCashPayed:0.00}; Kusuri: {forReturn}";
programLines.Add(Command(43, totalCommand));
programLines.Add(line.TotalPoints == 0
? Command(43, $"7Ju Faleminderit! / Nr: {line.ReceiptNumber}")
: Command(43, $"7Pikë: {line.BonusPoints}/{line.TotalPoints}; Nr: {line.ReceiptNumber}"));
return programLines;
}
public string GetDecodeResponse(byte[] resultData, List<int> statusResponses)
{
var data = Encoding.UTF8.GetString(resultData);
var fl = statusResponses.Aggregate("", (current, listItems) =>
current + string.Format("{0:x2}", listItems));
return data + " / " + fl;
}
public string GetDecodeResultData(byte[] resultData)
{
return Encoding.UTF8.GetString(resultData);
}
public string GetDecodeResultStatus(byte[] status)
{
return $"{status:x2}";
}
public bool GetHasPaperBytes(byte[] status)
{
string lastBit;
try
{
if (status.Length >= 1)
{
var i = 0;
status = new byte[2];
foreach (var item in status)
{
if (i < 2)
status[i] = 0;
i++;
}
}
var input = status[1].ToString();
var inputAsNumber = Convert.ToInt32(input);
var output = Convert.ToString(inputAsNumber, 2);
output = output.PadLeft(8, '0');
lastBit = output.Substring(7, 1);
}
catch (Exception)
{
lastBit = "0";
}
return lastBit != "1";
}
public List<ReturnValue> GetPrintReceipt(SaleParameters parameters)
{
var operatorNameByteList = GetSetOperatorNameByte(parameters.OperatorName);
var initializeReceiptByte = GetInitializeReceiptByte();
var response = new List<ReturnValue>();
var iterator = 1;
response.Add(new ReturnValue
{
Id = iterator,
Value = operatorNameByteList
});
iterator++;
response.Add(new ReturnValue
{
Id = iterator,
Value = initializeReceiptByte
});
iterator++;
var programLineBytes = GetProgramLinesBytes(parameters.ProgramLine);
foreach (var programLineByte in programLineBytes)
{
response.Add(new ReturnValue
{
Id = iterator,
Value = programLineByte
});
iterator++;
}
var registerItemsBytes = GetRegisterItemsBytes(parameters.Items);
foreach (var registerItems in registerItemsBytes)
{
response.Add(new ReturnValue
{
Id = iterator,
Value = registerItems
});
iterator++;
}
var sellItemsBytes = GetSellItemsBytes(parameters.Items);
foreach (var sellItemsByte in sellItemsBytes)
{
response.Add(new ReturnValue
{
Id = iterator,
Value = sellItemsByte
});
iterator++;
}
var paymentBytes = GetPaymentBytes(parameters.Payments);
foreach (var paymentByte in paymentBytes)
{
response.Add(new ReturnValue
{
Id = iterator,
Value = paymentByte
});
iterator++;
}
var getCloseReceipt = GetCloseReceiptByte();
response.Add(new ReturnValue
{
Id = iterator,
Value = getCloseReceipt
});
return response;
}
}
}