1 package com.atol.drivers.examples.fptrexample;
3 import android.app.Activity;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.os.AsyncTask;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.LinearLayout;
12 import android.widget.TextView;
18 import java.math.BigDecimal;
19 import java.util.Random;
21 public class FptrExampleActivity
extends Activity {
22 private static final String FPTR_PREFERENCES =
"FPTR_PREFERENCES";
23 private static final int REQUEST_SHOW_SETTINGS = 1;
24 private Button runButton;
25 private Button settingsButton;
26 private Button testButton;
28 private SharedPreferences preferences;
29 private Button cancelButton;
30 private LinearLayout buttonsLayout;
31 private static final boolean PRINT_FISCAL_CHECK =
true;
32 private static final boolean PRINT_NONFISCAL_CHECK =
true;
33 private static final boolean USE_FZ54 =
true;
36 protected void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
39 preferences = getSharedPreferences(FPTR_PREFERENCES, Context.MODE_PRIVATE);
41 setContentView(R.layout.activity_main);
42 buttonsLayout = (LinearLayout) findViewById(R.id.buttons_layout);
43 settingsButton = (Button) findViewById(R.id.settings_button);
44 testButton = (Button) findViewById(R.id.test_button);
45 runButton = (Button) findViewById(R.id.run_button);
46 cancelButton = (Button) findViewById(R.id.cancel_button);
47 log = (TextView) findViewById(R.id.log);
49 settingsButton.setOnClickListener(
new View.OnClickListener() {
51 public void onClick(View v) {
52 Intent intent =
new Intent(FptrExampleActivity.this, SettingsActivity.class);
53 String settings = getSettings();
54 if (settings == null) {
55 settings = getDefaultSettings();
57 intent.putExtra(SettingsActivity.DEVICE_SETTINGS, settings);
58 startActivityForResult(intent, REQUEST_SHOW_SETTINGS);
62 testButton.setOnClickListener(
new View.OnClickListener() {
63 AsyncTask<Void, String, Void> task;
66 public void onClick(View v) {
68 task =
new AsyncTask<Void, String, Void>() {
71 private void checkError() throws DriverException {
72 int rc = fptr.get_ResultCode();
74 String rd = fptr.get_ResultDescription(), bpd = null;
76 bpd = fptr.get_BadParamDescription();
79 throw new DriverException(String.format(
"[%d] %s (%s)", rc, rd, bpd));
81 throw new DriverException(String.format(
"[%d] %s", rc, rd));
87 protected void onPreExecute() {
88 buttonsLayout.setVisibility(View.GONE);
92 protected void onPostExecute(Void result) {
93 buttonsLayout.setVisibility(View.VISIBLE);
97 protected Void doInBackground(Void... params) {
100 fptr.create(getApplication());
101 publishProgress(
"Загрузка настроек");
102 if (fptr.put_DeviceSettings(getSettings()) < 0) {
105 publishProgress(
"Установка соединения...");
106 if (fptr.put_DeviceEnabled(
true) < 0) {
109 publishProgress(
"OK");
110 publishProgress(
"Проверка связи...");
111 if (fptr.GetStatus() < 0) {
114 publishProgress(
"OK");
115 }
catch (Exception e) {
116 publishProgress(e.toString());
124 protected void onProgressUpdate(String... values) {
125 if (values == null || values.length == 0) {
136 runButton.setOnClickListener(
new View.OnClickListener() {
137 AsyncTask<Void, String, Void> task;
140 public void onClick(View v) {
142 task =
new AsyncTask<Void, String, Void>() {
145 private void checkError() throws DriverException {
146 int rc = fptr.get_ResultCode();
148 String rd = fptr.get_ResultDescription(), bpd = null;
150 bpd = fptr.get_BadParamDescription();
153 throw new DriverException(String.format(
"[%d] %s (%s)", rc, rd, bpd));
155 throw new DriverException(String.format(
"[%d] %s", rc, rd));
160 private void printText(String text,
int alignment,
int wrap)
throws DriverException {
161 if (fptr.put_Caption(text) < 0) {
164 if (fptr.put_TextWrap(wrap) < 0) {
167 if (fptr.put_Alignment(alignment) < 0) {
170 if (fptr.PrintString() < 0) {
175 private void printText(String text)
throws DriverException {
176 printText(text, IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
179 private void openCheck(
int type)
throws DriverException {
180 if (fptr.put_Mode(IFptr.MODE_REGISTRATION) < 0) {
183 if (fptr.SetMode() < 0) {
186 if (fptr.put_CheckType(type) < 0) {
189 if (fptr.OpenCheck() < 0) {
194 private void closeCheck(
int typeClose)
throws DriverException {
195 if (fptr.put_TypeClose(typeClose) < 0) {
198 if (fptr.CloseCheck() < 0) {
203 private void registration(String name,
double price,
double quantity)
throws DriverException {
204 if (fptr.put_Quantity(quantity) < 0) {
207 if (fptr.put_Price(price) < 0) {
210 if (fptr.put_TextWrap(IFptr.WRAP_WORD) < 0) {
213 if (fptr.put_Name(name) < 0) {
216 if (fptr.Registration() < 0) {
221 private void registrationFZ54(String name,
double price,
double quantity,
222 double positionSum,
int taxNumber)
throws DriverException {
223 if (fptr.put_TaxNumber(taxNumber) < 0) {
226 if (fptr.put_PositionSum(positionSum) < 0) {
229 if (fptr.put_Quantity(quantity) < 0) {
232 if (fptr.put_Price(price) < 0) {
235 if (fptr.put_TextWrap(IFptr.WRAP_WORD) < 0) {
238 if (fptr.put_Name(name) < 0) {
241 if (fptr.Registration() < 0) {
246 private void payment(
double sum,
int type)
throws DriverException {
247 if (fptr.put_Summ(sum) < 0) {
250 if (fptr.put_TypeClose(type) < 0) {
253 if (fptr.Payment() < 0) {
256 publishProgress(String.format(
"Remainder: %.2f, Change: %.2f", fptr.get_Remainder(), fptr.get_Change()));
259 private void reportZ() throws DriverException {
260 if (fptr.put_Mode(IFptr.MODE_REPORT_CLEAR) < 0) {
263 if (fptr.SetMode() < 0) {
266 if (fptr.put_ReportType(IFptr.REPORT_Z) < 0) {
269 if (fptr.Report() < 0) {
274 private void printFooter() throws DriverException {
275 if (fptr.put_Mode(IFptr.MODE_REPORT_NO_CLEAR) < 0) {
278 if (fptr.SetMode() < 0) {
281 if (fptr.PrintFooter() < 0) {
286 private void printBarcode(
int type, String barcode,
double scale)
throws DriverException {
287 if (fptr.put_Alignment(IFptr.ALIGNMENT_CENTER) < 0) {
290 if (fptr.put_BarcodeType(type) < 0) {
293 if (fptr.put_Barcode(barcode) < 0) {
296 if (type == IFptr.BARCODE_TYPE_QRCODE) {
297 if (fptr.put_Height(0) < 0) {
301 if (fptr.put_Height(50) < 0) {
305 if (fptr.put_BarcodePrintType(IFptr.BARCODE_PRINTTYPE_AUTO) < 0) {
308 if (fptr.put_PrintBarcodeText(
false) < 0) {
311 if (fptr.put_BarcodeControlCode(
true) < 0) {
314 if (fptr.put_Scale(scale) < 0) {
317 if (fptr.put_BarcodeCorrection(0) < 0) {
320 if (fptr.put_BarcodeColumns(3) < 0) {
323 if (fptr.put_BarcodeRows(1) < 0) {
326 if (fptr.put_BarcodeProportions(50) < 0) {
329 if (fptr.put_BarcodeUseProportions(
true) < 0) {
332 if (fptr.put_BarcodePackingMode(IFptr.BARCODE_PDF417_PACKING_MODE_DEFAULT) < 0) {
335 if (fptr.put_BarcodePixelProportions(300) < 0) {
338 if (fptr.PrintBarcode() < 0) {
343 private void discount(
double sum,
int type,
int destination)
throws DriverException {
344 if (fptr.put_Summ(sum) < 0) {
347 if (fptr.put_DiscountType(type) < 0) {
350 if (fptr.put_Destination(destination) < 0) {
353 if (fptr.Discount() < 0) {
358 private void charge(
double sum,
int type,
int destination)
throws DriverException {
359 if (fptr.put_Summ(sum) < 0) {
362 if (fptr.put_DiscountType(type) < 0) {
365 if (fptr.put_Destination(destination) < 0) {
368 if (fptr.Charge() < 0) {
374 protected void onPreExecute() {
375 buttonsLayout.setVisibility(View.GONE);
379 protected void onPostExecute(Void result) {
380 buttonsLayout.setVisibility(View.VISIBLE);
384 protected Void doInBackground(Void... params) {
385 Random random =
new Random();
388 fptr.create(getApplication());
390 publishProgress(
"Загрузка настроек...");
391 if (fptr.put_DeviceSettings(getSettings()) < 0) {
395 publishProgress(
"Установка соединения...");
396 if (fptr.put_DeviceEnabled(
true) < 0) {
399 publishProgress(
"OK");
401 publishProgress(
"Проверка связи...");
402 if (fptr.GetStatus() < 0) {
405 publishProgress(
"OK");
409 publishProgress(
"Отмена чека...");
411 if (fptr.CancelCheck() < 0) {
414 }
catch (DriverException e) {
415 int rc = fptr.get_ResultCode();
416 if (rc != -16 && rc != -3801) {
420 publishProgress(
"OK");
422 if (PRINT_FISCAL_CHECK) {
423 publishProgress(
"Фискальный чек...");
425 publishProgress(
"Открытие чека...");
427 openCheck(IFptr.CHEQUE_TYPE_SELL);
428 }
catch (DriverException e) {
430 if (fptr.get_ResultCode() == -3822) {
432 openCheck(IFptr.CHEQUE_TYPE_SELL);
438 BigDecimal sum =
new BigDecimal(0);
439 for (
int i = -2; i < 5; ++i) {
442 double price = Math.pow(10, i), quantity = 1;
443 publishProgress(String.format(
"Регистрация %d", i + 3));
444 registrationFZ54(String.format(
"Позиция %d", i + 3), price, quantity, price*quantity, IFptr.TAX_VAT_18);
445 sum = sum.add(
new BigDecimal(price).multiply(
new BigDecimal(quantity)));
448 double price = Math.pow(10, i), quantity = 1;
449 publishProgress(String.format(
"Регистрация %d", i + 3));
450 registration(String.format(
"Позиция %d", i + 3), price, quantity);
451 sum = sum.add(
new BigDecimal(price).multiply(
new BigDecimal(quantity)));
453 publishProgress(
"Скидка на позицию");
455 discount(1, IFptr.DISCOUNT_SUMM, IFptr.DESTINATION_POSITION);
457 charge(1, IFptr.DISCOUNT_PERCENT, IFptr.DESTINATION_POSITION);
462 publishProgress(
"Скидка на чек");
465 discount(0, IFptr.DISCOUNT_SUMM, IFptr.DESTINATION_CHECK);
468 discount(1, IFptr.DISCOUNT_PERCENT, IFptr.DESTINATION_CHECK);
472 publishProgress(
"Оплата...");
473 payment(sum.divide(
new BigDecimal(4)).doubleValue(), 1);
474 payment(sum.divide(
new BigDecimal(4)).doubleValue(), 2);
475 payment(sum.divide(
new BigDecimal(4)).doubleValue(), 3);
476 payment(sum.doubleValue(), 0);
478 publishProgress(
"Закрытие чека");
482 if (PRINT_NONFISCAL_CHECK) {
483 publishProgress(
"Печать строк");
484 publishProgress(
"Нефискальный чек...");
485 printText(
"Я сразу смазал карту будня, плеснувши краску из стакана;\n" 486 +
"Я показал на блюде студня косые скулы океана.\n" 487 +
"На чешуе жестяной рыбы прочел я зовы новых губ.", IFptr.ALIGNMENT_LEFT, IFptr.WRAP_WORD);
488 printText(
"А вы", IFptr.ALIGNMENT_LEFT, IFptr.WRAP_WORD);
489 printText(
"ноктюрн сыграть", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_WORD);
490 printText(
"могли бы", IFptr.ALIGNMENT_RIGHT, IFptr.WRAP_WORD);
491 printText(
"на флейте водосточных труб?", IFptr.ALIGNMENT_LEFT, IFptr.WRAP_WORD);
492 publishProgress(
"OK");
494 publishProgress(
"Печать ШК");
495 printText(
"\nEAN8", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
496 printBarcode(IFptr.BARCODE_TYPE_EAN8,
"40182735", 100);
498 printText(
"\nEAN13", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
499 printBarcode(IFptr.BARCODE_TYPE_EAN13,
"4007630000116", 200);
501 printText(
"\nUPCA", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
502 printBarcode(IFptr.BARCODE_TYPE_UPCA,
"697929110035", 100);
504 printText(
"\nCODE39", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
505 printBarcode(IFptr.BARCODE_TYPE_CODE39,
"ATOL.RU", 100);
507 printText(
"\nPDF417", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
508 printBarcode(IFptr.BARCODE_TYPE_PDF417,
"1234567", 200);
510 printText(
"\nQR", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
511 printBarcode(IFptr.BARCODE_TYPE_QRCODE,
"АТОЛ.РУ", 500);
513 printText(
"\nITF-14", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
514 printBarcode(IFptr.BARCODE_TYPE_ITF14,
"00012345600012", 100);
516 printText(
"\nInterleaved 2 of 5", IFptr.ALIGNMENT_CENTER, IFptr.WRAP_LINE);
517 printBarcode(IFptr.BARCODE_TYPE_INTERLEAVED_2_OF_5,
"04062300106659", 100);
520 publishProgress(
"OK");
522 }
catch (Exception e) {
523 publishProgress(e.toString());
531 protected void onProgressUpdate(String... values) {
532 if (values == null || values.length == 0) {
546 protected void onActivityResult(
int requestCode,
int resultCode, Intent data) {
547 if (requestCode == REQUEST_SHOW_SETTINGS) {
548 if (data != null && data.getExtras() != null) {
549 String settings = data.getExtras().getString(SettingsActivity.DEVICE_SETTINGS);
550 setSettings(settings);
555 private String getSettings() {
556 return preferences.getString(SettingsActivity.DEVICE_SETTINGS, getDefaultSettings());
559 private void setSettings(String settings) {
560 SharedPreferences.Editor editor = preferences.edit();
561 editor.putString(SettingsActivity.DEVICE_SETTINGS, settings);
565 private String getDefaultSettings() {
566 IFptr fprint =
new Fptr();
568 String settings = fprint.get_DeviceSettings();
573 private void logAdd(String
string) {
574 String value = (String) log.getText();
575 if (value.length() > 2096) {
576 value = value.substring(0, 2096);
578 log.setText(
string +
"\n" + value);
581 private static class DriverException
extends Exception {
582 public DriverException(String msg) {