Драйверы торгового оборудования v.9
FptrExample.java
1 package com.atol.drivers.fptr.examples;
2 
3 import com.atol.drivers.fptr.Fptr;
4 import com.atol.drivers.fptr.IFptr;
5 
6 import java.math.BigDecimal;
7 import java.util.Random;
8 
9 public class FptrExample {
10  private static final boolean USE_SHOWPROPERTIES = false;
11  private static final boolean USE_FZ54 = true;
12  private static final boolean PRINT_FISCAL_CHECK = true;
13  private static final boolean PRINT_NONFISCAL_CHECK = true;
14 
15  private static void checkError(IFptr fptr) throws DriverException {
16  int rc = fptr.get_ResultCode();
17  if (rc < 0) {
18  String rd = fptr.get_ResultDescription(), bpd = null;
19  if (rc == -6) {
20  bpd = fptr.get_BadParamDescription();
21  }
22  if (bpd != null)
23  throw new DriverException(String.format("[%d] %s (%s)", rc, rd, bpd));
24  else
25  throw new DriverException(String.format("[%d] %s", rc, rd));
26  }
27  }
28 
29  private static void printText(IFptr fptr, String text, int alignment, int wrap) throws DriverException {
30  if (fptr.put_Caption(text) < 0)
31  checkError(fptr);
32  if (fptr.put_TextWrap(wrap) < 0)
33  checkError(fptr);
34  if (fptr.put_Alignment(alignment) < 0)
35  checkError(fptr);
36  if (fptr.PrintString() < 0)
37  checkError(fptr);
38  }
39 
40  private static void printText(IFptr fptr, String text) throws DriverException {
41  printText(fptr, text, IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
42  }
43 
44  private static void openCheck(IFptr fptr, int type) throws DriverException {
45  if (fptr.put_Mode(IFptr.MODE_REGISTRATION) < 0)
46  checkError(fptr);
47  if (fptr.SetMode() < 0)
48  checkError(fptr);
49  if (fptr.put_CheckType(type) < 0)
50  checkError(fptr);
51  if (fptr.OpenCheck() < 0)
52  checkError(fptr);
53  }
54 
55  private static void closeCheck(IFptr fptr, int typeClose) throws DriverException {
56  if (fptr.put_TypeClose(typeClose) < 0)
57  checkError(fptr);
58  if (fptr.CloseCheck() < 0)
59  checkError(fptr);
60  }
61 
62  private static void registration(IFptr fptr, String name, double price, double quantity) throws DriverException {
63  if (fptr.put_Quantity(quantity) < 0)
64  checkError(fptr);
65  if (fptr.put_Price(price) < 0)
66  checkError(fptr);
67  if (fptr.put_TextWrap(IFptr.WRAP_WORD) < 0)
68  checkError(fptr);
69  if (fptr.put_Name(name) < 0)
70  checkError(fptr);
71  if (fptr.Registration() < 0)
72  checkError(fptr);
73  }
74 
75  private static void registrationFZ54(IFptr fptr, String name, double price, double quantity,
76  double positionSum, int taxNumber) throws DriverException {
77  if (fptr.put_PositionSum(positionSum) < 0)
78  checkError(fptr);
79  if (fptr.put_Quantity(quantity) < 0)
80  checkError(fptr);
81  if (fptr.put_Price(price) < 0)
82  checkError(fptr);
83  if (fptr.put_TaxNumber(taxNumber) < 0)
84  checkError(fptr);
85  if (fptr.put_TextWrap(IFptr.WRAP_WORD) < 0)
86  checkError(fptr);
87  if (fptr.put_Name(name) < 0)
88  checkError(fptr);
89  if (fptr.Registration() < 0)
90  checkError(fptr);
91  }
92 
93 
94  private static void payment(IFptr fptr, double sum, int type) throws DriverException {
95  if (fptr.put_Summ(sum) < 0)
96  checkError(fptr);
97  if (fptr.put_TypeClose(type) < 0)
98  checkError(fptr);
99  if (fptr.Payment() < 0)
100  checkError(fptr);
101  System.out.println(String.format("Remainder: %.2f, Change: %.2f", fptr.get_Remainder(), fptr.get_Change()));
102  }
103 
104  private static void reportZ(IFptr fptr) throws DriverException {
105  if (fptr.put_Mode(IFptr.MODE_REPORT_CLEAR) < 0)
106  checkError(fptr);
107  if (fptr.SetMode() < 0)
108  checkError(fptr);
109  if (fptr.put_ReportType(IFptr.REPORT_Z) < 0)
110  checkError(fptr);
111  if (fptr.Report() < 0)
112  checkError(fptr);
113  }
114 
115  private static void printFooter(IFptr fptr) throws DriverException {
116  if (fptr.put_Mode(IFptr.MODE_REPORT_NO_CLEAR) < 0)
117  checkError(fptr);
118  if (fptr.SetMode() < 0)
119  checkError(fptr);
120  if (fptr.PrintFooter() < 0)
121  checkError(fptr);
122  }
123 
124  private static void printBarcode(IFptr fptr, int type, String barcode, double scale) throws DriverException {
125  if (fptr.put_Alignment(IFptr.ALIGNMENT_CENTER) < 0)
126  checkError(fptr);
127  if (fptr.put_BarcodeType(type) < 0)
128  checkError(fptr);
129  if (fptr.put_Barcode(barcode) < 0)
130  checkError(fptr);
131  if (fptr.put_Height(0) < 0)
132  checkError(fptr);
133  if (fptr.put_BarcodeVersion(0) < 0)
134  checkError(fptr);
135  if (fptr.put_BarcodePrintType(IFptr.BARCODE_PRINTTYPE_AUTO) < 0)
136  checkError(fptr);
137  if (fptr.put_PrintBarcodeText(false) < 0)
138  checkError(fptr);
139  if (fptr.put_BarcodeControlCode(true) < 0)
140  checkError(fptr);
141  if (fptr.put_Scale(scale) < 0)
142  checkError(fptr);
143  if (fptr.put_BarcodeCorrection(0) < 0)
144  checkError(fptr);
145  if (fptr.put_BarcodeColumns(3) < 0)
146  checkError(fptr);
147  if (fptr.put_BarcodeRows(1) < 0)
148  checkError(fptr);
149  if (fptr.put_BarcodeProportions(50) < 0)
150  checkError(fptr);
151  if (fptr.put_BarcodeUseProportions(true) < 0)
152  checkError(fptr);
153  if (fptr.put_BarcodePackingMode(IFptr.BARCODE_PDF417_PACKING_MODE_DEFAULT) < 0)
154  checkError(fptr);
155  if (fptr.put_BarcodePixelProportions(300) < 0)
156  checkError(fptr);
157  if (fptr.PrintBarcode() < 0)
158  checkError(fptr);
159  }
160 
161  private static void discount(IFptr fptr, double sum, int type, int destination) throws DriverException {
162  if (fptr.put_Summ(sum) < 0)
163  checkError(fptr);
164  if (fptr.put_DiscountType(type) < 0)
165  checkError(fptr);
166  if (fptr.put_Destination(destination) < 0)
167  checkError(fptr);
168  if (fptr.Discount() < 0)
169  checkError(fptr);
170  }
171 
172  private static void charge(IFptr fptr, double sum, int type, int destination) throws DriverException {
173  if (fptr.put_Summ(sum) < 0)
174  checkError(fptr);
175  if (fptr.put_DiscountType(type) < 0)
176  checkError(fptr);
177  if (fptr.put_Destination(destination) < 0)
178  checkError(fptr);
179  if (fptr.Charge() < 0)
180  checkError(fptr);
181  }
182 
183  public static void main(String[] argv) {
184  Random random = new Random();
185  IFptr fptr = new Fptr();
186 
187  try {
188  fptr.create();
189 
190  // Выставляем рабочий каталог. В нем дККМ будет искать требуемые ему библиотеки.
191  fptr.put_DeviceSingleSetting(fptr.SETTING_SEARCHDIR, System.getProperty("java.library.path"));
192  fptr.ApplySingleSettings();
193 
194  if (USE_SHOWPROPERTIES) {
195  // Для настройки драйвера можно вызвать графическое окно настроек
196  if (fptr.ShowProperties() < 0)
197  checkError(fptr);
198  } else {
199  // Или настроить без него
200  // COM17
201  //if (fptr.put_DeviceSingleSetting(IFptr.SETTING_PORT, 17) < 0)
202  // checkError(fptr);
203  // USB. Можно указать положение на шине (USB$1-1.3, например - брать из /sys/bus/usb/devices/),
204  // но тогда не нужно указывать Vid и Pid
205  // Работа напрямую с USB - Linux Only!
206  if (fptr.put_DeviceSingleSetting(IFptr.SETTING_PORT, IFptr.SETTING_PORT_USB) < 0)
207  checkError(fptr);
208  if (fptr.put_DeviceSingleSetting(IFptr.SETTING_VID, 0x2912) < 0)
209  checkError(fptr);
210  if (fptr.put_DeviceSingleSetting(IFptr.SETTING_PID, 0x0005) < 0)
211  checkError(fptr);
212  if (USE_FZ54) {
213  if (fptr.put_DeviceSingleSetting(IFptr.SETTING_MODEL, IFptr.MODEL_ATOL_55F) < 0)
214  checkError(fptr);
215  } else {
216  if (fptr.put_DeviceSingleSetting(IFptr.SETTING_MODEL, IFptr.MODEL_FPRINT_55) < 0)
217  checkError(fptr);
218  }
219  if (fptr.put_DeviceSingleSetting(IFptr.SETTING_BAUDRATE, 115200) < 0)
220  checkError(fptr);
221  if (fptr.ApplySingleSettings() < 0)
222  checkError(fptr);
223  }
224 
225  // Подключаемся к устройству
226  if (fptr.put_DeviceEnabled(true) < 0)
227  checkError(fptr);
228 
229  // Проверка связи
230  if (fptr.GetStatus() < 0)
231  checkError(fptr);
232 
233  // Убедились, что настройки подходят и касса отвечает - вытаскиваем актуальные настройки из драйвера
234  if (true) {
235  String settings = fptr.get_DeviceSettings();
236  // Тут можно их сохранить в файл, базу, т.п.
237  // При следующем запуске их можно передать в драйвер чере put_DeviceSettings()
238  }
239 
240  // Отменяем чек, если уже открыт. Ошибки "Неверный режим" и "Чек уже закрыт"
241  // не являются ошибками, если мы хотим просто отменить чек
242  try {
243  if (fptr.CancelCheck() < 0)
244  checkError(fptr);
245  } catch (DriverException e) {
246  int rc = fptr.get_ResultCode();
247  if (rc != -16 && rc != -3801)
248  throw e;
249  }
250 
251  if (PRINT_FISCAL_CHECK) {
252  // Открываем чек продажи, попутно обработав превышение смены
253  try {
254  openCheck(fptr, IFptr.CHEQUE_TYPE_SELL);
255  } catch (DriverException e) {
256  // Проверка на превышение смены
257  if (fptr.get_ResultCode() == -3822) {
258  reportZ(fptr);
259  openCheck(fptr, IFptr.CHEQUE_TYPE_SELL);
260  } else {
261  throw e;
262  }
263  }
264 
265  BigDecimal sum = new BigDecimal(0);
266  for (int i = -2; i < 5; ++i) {
267  if (USE_FZ54) {
268  double price = Math.pow(10, i), quantity = 1;
269  registrationFZ54(fptr, String.format("Позиция %d", i + 3), price, quantity, price * quantity, IFptr.TAX_VAT_18);
270  sum = sum.add(new BigDecimal(price).multiply(new BigDecimal(quantity)));
271  } else {
272  double price = Math.pow(10, i), quantity = 1;
273  registration(fptr, String.format("Позиция %d", i + 3), price, quantity);
274  sum = sum.add(new BigDecimal(price).multiply(new BigDecimal(quantity)));
275  // Скидка на позицию
276  if (i % 2 == 0) {
277  discount(fptr, 1, IFptr.DISCOUNT_SUMM, IFptr.DESTINATION_POSITION);
278  } else {
279  charge(fptr, 1, IFptr.DISCOUNT_PERCENT, IFptr.DESTINATION_POSITION);
280  }
281  }
282  }
283  // Скидка на чек
284  if (USE_FZ54) {
285  // Можно сбрасывать в пределах копеек. При подаче 0 копейки отбрасываются полностью
286  discount(fptr, 0, IFptr.DISCOUNT_SUMM, IFptr.DESTINATION_CHECK);
287  } else {
288  discount(fptr, 1, IFptr.DISCOUNT_PERCENT, IFptr.DESTINATION_CHECK);
289  }
290  // Оплачиваем
291  payment(fptr, sum.divide(new BigDecimal(4)).doubleValue(), 1);
292  payment(fptr, sum.divide(new BigDecimal(4)).doubleValue(), 2);
293  payment(fptr, sum.divide(new BigDecimal(4)).doubleValue(), 3);
294  payment(fptr, sum.doubleValue(), 0);
295  // Закрываем чек
296  closeCheck(fptr, 0);
297  }
298 
299  // Нефискальный чек
300  // Его можно не открывать, а сразу начинать печатать
301  if (PRINT_NONFISCAL_CHECK) {
302  printText(fptr, "Я сразу смазал карту будня, плеснувши краску из стакана;\n"
303  + "Я показал на блюде студня косые скулы океана.\n"
304  + "На чешуе жестяной рыбы прочел я зовы новых губ.", IFptr.ALIGNMENT_LEFT, IFptr.WRAP_WORD);
305  printText(fptr, "А вы", IFptr.ALIGNMENT_LEFT, IFptr.WRAP_WORD);
306  printText(fptr, "ноктюрн сыграть", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_WORD);
307  printText(fptr, "могли бы", IFptr.ALIGNMENT_RIGHT, IFptr.WRAP_WORD);
308  printText(fptr, "на флейте водосточных труб?", IFptr.ALIGNMENT_LEFT, IFptr.WRAP_WORD);
309 
310  printText(fptr, "\nEAN8", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
311  printBarcode(fptr, IFptr.BARCODE_TYPE_EAN8, "40182735", 100);
312  printText(fptr, "");
313  printText(fptr, "\nEAN13", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
314  printBarcode(fptr, IFptr.BARCODE_TYPE_EAN13, "4007630000116", 200);
315  printText(fptr, "");
316  printText(fptr, "\nUPCA", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
317  printBarcode(fptr, IFptr.BARCODE_TYPE_UPCA, "697929110035", 100);
318  printText(fptr, "");
319  printText(fptr, "\nCODE39", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
320  printBarcode(fptr, IFptr.BARCODE_TYPE_CODE39, "ATOL.RU", 100);
321  printText(fptr, "");
322  printText(fptr, "\nPDF417", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
323  printBarcode(fptr, IFptr.BARCODE_TYPE_PDF417, "1234567", 100);
324  printText(fptr, "");
325  printText(fptr, "\nQR", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
326  printBarcode(fptr, IFptr.BARCODE_TYPE_QRCODE, "АТОЛ.РУ", 500);
327  printText(fptr, "");
328  printText(fptr, "\nITF-14", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
329  printBarcode(fptr, IFptr.BARCODE_TYPE_ITF14, "00012345600012", 100);
330  printText(fptr, "");
331  printText(fptr, "\nInterleaved 2 of 5", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
332  printBarcode(fptr, IFptr.BARCODE_TYPE_INTERLEAVED_2_OF_5, "04062300106659", 100);
333  printText(fptr, "");
334  printFooter(fptr);
335  }
336  } catch (Exception e) {
337  System.out.println(e);
338  } finally {
339  fptr.destroy();
340  }
341  }
342 
343  private static class DriverException extends Exception {
344  private static final long serialVersionUID = 6164921645357791803L;
345 
346  public DriverException(String msg) {
347  super(msg);
348  }
349  }
350 }