Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IFPTRPrinter implements PrinterInterface {
- private Context mContext;
- private static final boolean PRINT_FISCAL_CHECK = true;
- private static final boolean PRINT_NONFISCAL_CHECK = true;
- private static final boolean USE_FZ54 = true;
- public IFPTRPrinter(Context context) {
- this.mContext = context;
- }
- @Override
- public void printCheck(final PrinterCheck check, final PrinterCompletionInterface completion) {
- AsyncTask<Void, String, Void> task;
- task = new AsyncTask<Void, String, Void>() {
- private com.atol.drivers.fptr.IFptr fptr;
- @Override
- protected void onPreExecute() {
- }
- @Override
- protected void onPostExecute(Void result) {
- }
- @Override
- protected Void doInBackground(Void... params) {
- Random random = new Random();
- fptr = new com.atol.drivers.fptr.Fptr();
- try {
- fptr.create(mContext);
- if (fptr.put_DeviceSettings(getSettings()) < 0) {
- checkError(fptr);
- }
- if (fptr.put_DeviceEnabled(true) < 0) {
- checkError(fptr);
- }
- if (fptr.GetStatus() < 0) {
- checkError(fptr);
- }
- // Отменяем чек, если уже открыт. Ошибки "Неверный режим" и "Чек уже закрыт"
- // не являются ошибками, если мы хотим просто отменить чек
- try {
- if (fptr.CancelCheck() < 0) {
- checkError(fptr);
- }
- } catch (DriverException e) {
- int rc = fptr.get_ResultCode();
- if (rc != -16 && rc != -3801) {
- throw e;
- }
- }
- // reportInfo(fptr);
- //
- if (PRINT_FISCAL_CHECK) {
- try {
- openCheck(fptr, com.atol.drivers.fptr.IFptr.CHEQUE_TYPE_SELL);
- } catch (DriverException e) {
- // Проверка на превышение смены
- if (fptr.get_ResultCode() == -3822) {
- reportZ(fptr);
- openCheck(fptr, com.atol.drivers.fptr.IFptr.CHEQUE_TYPE_SELL);
- } else {
- throw e;
- }
- }
- BigDecimal sum = new BigDecimal(0);
- for (int i = 0; i < check.items.size(); i++) {
- if(USE_FZ54)
- {
- // registrationFZ54(fptr, String.format(check.items.get(i).getName()), check.items.get(i).getPriceForPrinter(), check.items.get(i).getQty(), check.items.get(i).getTotalPriceForPrinter(), com.atol.drivers.fptr.IFptr.TAX_VAT_18);
- registration(fptr, String.format(check.items.get(i).getName()), check.items.get(i).getPriceForPrinter(), check.items.get(i).getQty());
- sum = sum.add(new BigDecimal(check.items.get(i).getPriceForPrinter()).multiply(new BigDecimal(check.items.get(i).getQty())));
- }
- }
- payment(fptr, sum.doubleValue(), 0);
- // Закрываем чек
- closeCheck(fptr,0);
- completion.done();
- }
- } catch (Exception e) {
- publishProgress(e.toString());
- } finally {
- fptr.destroy();
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(String... values) {
- if (values == null || values.length == 0) {
- return;
- }
- }
- };
- task.execute();
- }
- private void checkError(com.atol.drivers.fptr.IFptr fptr) throws DriverException {
- int rc = fptr.get_ResultCode();
- if (rc < 0) {
- String rd = fptr.get_ResultDescription(), bpd = null;
- if (rc == -6) {
- bpd = fptr.get_BadParamDescription();
- }
- if (bpd != null) {
- throw new DriverException(String.format("[%d] %s (%s)", rc, rd, bpd));
- } else {
- throw new DriverException(String.format("[%d] %s", rc, rd));
- }
- }
- }
- // private void printText(String text, int alignment, int wrap) throws DriverException {
- // if (fptr.put_Caption(text) < 0) {
- // checkError();
- // }
- // if (fptr.put_TextWrap(wrap) < 0) {
- // checkError();
- // }
- // if (fptr.put_Alignment(alignment) < 0) {
- // checkError();
- // }
- // if (fptr.PrintString() < 0) {
- // checkError();
- // }
- // }
- // private void printText(String text) throws DriverException {
- // printText(text, com.atol.drivers.fptr.IFptr.ALIGNMENT_CENTER, com.atol.drivers.fptr.IFptr.WRAP_LINE);
- // }
- private void openCheck(com.atol.drivers.fptr.IFptr fptr, int type) throws DriverException {
- if (fptr.put_Mode(com.atol.drivers.fptr.IFptr.MODE_REGISTRATION) < 0) {
- checkError(fptr);
- }
- if (fptr.SetMode() < 0) {
- checkError(fptr);
- }
- if (fptr.put_CheckType(type) < 0) {
- checkError(fptr);
- }
- if (fptr.OpenCheck() < 0) {
- checkError(fptr);
- }
- }
- private void closeCheck(com.atol.drivers.fptr.IFptr fptr, int typeClose) throws DriverException {
- if (fptr.put_TypeClose(typeClose) < 0) {
- checkError(fptr);
- }
- if (fptr.CloseCheck() < 0) {
- checkError(fptr);
- }
- }
- private void registration(com.atol.drivers.fptr.IFptr fptr, String name, double price, double quantity) throws DriverException {
- if (fptr.put_Quantity(quantity) < 0) {
- checkError(fptr);
- }
- if (fptr.put_Price(price) < 0) {
- checkError(fptr);
- }
- if (fptr.put_TextWrap(com.atol.drivers.fptr.IFptr.WRAP_WORD) < 0) {
- checkError(fptr);
- }
- if (fptr.put_Name(name) < 0) {
- checkError(fptr);
- }
- if (fptr.Registration() < 0) {
- checkError(fptr);
- }
- }
- private void registrationFZ54(com.atol.drivers.fptr.IFptr fptr, String name, double price, double quantity,
- double positionSum, int taxNumber) throws DriverException {
- if (fptr.put_TaxNumber(taxNumber) < 0) {
- checkError(fptr);
- }
- if (fptr.put_PositionSum(positionSum) < 0) {
- checkError(fptr);
- }
- if (fptr.put_Quantity(quantity) < 0) {
- checkError(fptr);
- }
- if (fptr.put_Price(price) < 0) {
- checkError(fptr);
- }
- if (fptr.put_TextWrap(com.atol.drivers.fptr.IFptr.WRAP_WORD) < 0) {
- checkError(fptr);
- }
- if (fptr.put_Name(name) < 0) {
- checkError(fptr);
- }
- if (fptr.Registration() < 0) {
- checkError(fptr);
- }
- }
- private void payment(com.atol.drivers.fptr.IFptr fptr, double sum, int type) throws DriverException {
- if (fptr.put_Summ(sum) < 0) {
- checkError(fptr);
- }
- if (fptr.put_TypeClose(type) < 0) {
- checkError(fptr);
- }
- if (fptr.Payment() < 0) {
- checkError(fptr);
- }
- }
- private void reportZ(com.atol.drivers.fptr.IFptr fptr) throws DriverException {
- if (fptr.put_Mode(com.atol.drivers.fptr.IFptr.MODE_REPORT_CLEAR) < 0) {
- checkError(fptr);
- }
- if (fptr.SetMode() < 0) {
- checkError(fptr);
- }
- if (fptr.put_ReportType(com.atol.drivers.fptr.IFptr.REPORT_Z) < 0) {
- checkError(fptr);
- }
- if (fptr.Report() < 0) {
- checkError(fptr);
- }
- }
- private void reportInfo(com.atol.drivers.fptr.IFptr fptr) throws DriverException {
- if (fptr.put_Mode(0) < 0) {
- checkError(fptr);
- }
- if (fptr.SetMode() < 0) {
- checkError(fptr);
- }
- if (fptr.put_ReportType(com.atol.drivers.fptr.IFptr.REPORT_PRINT_INFO) < 0) {
- checkError(fptr);
- }
- if (fptr.Report() < 0) {
- checkError(fptr);
- }
- }
- // private void printFooter() throws DriverException {
- // if (fptr.put_Mode(com.atol.drivers.fptr.IFptr.MODE_REPORT_NO_CLEAR) < 0) {
- // checkError();
- // }
- // if (fptr.SetMode() < 0) {
- // checkError();
- // }
- // if (fptr.PrintFooter() < 0) {
- // checkError();
- // }
- // }
- // private void printBarcode(int type, String barcode, double scale) throws DriverException {
- // if (fptr.put_Alignment(com.atol.drivers.fptr.IFptr.ALIGNMENT_CENTER) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodeType(type) < 0) {
- // checkError();
- // }
- // if (fptr.put_Barcode(barcode) < 0) {
- // checkError();
- // }
- // if (type == com.atol.drivers.fptr.IFptr.BARCODE_TYPE_QRCODE) {
- // if (fptr.put_Height(0) < 0) {
- // checkError();
- // }
- // } else {
- // if (fptr.put_Height(50) < 0) {
- // checkError();
- // }
- // }
- // if (fptr.put_BarcodePrintType(com.atol.drivers.fptr.IFptr.BARCODE_PRINTTYPE_AUTO) < 0) {
- // checkError();
- // }
- // if (fptr.put_PrintBarcodeText(false) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodeControlCode(true) < 0) {
- // checkError();
- // }
- // if (fptr.put_Scale(scale) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodeCorrection(0) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodeColumns(3) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodeRows(1) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodeProportions(50) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodeUseProportions(true) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodePackingMode(com.atol.drivers.fptr.IFptr.BARCODE_PDF417_PACKING_MODE_DEFAULT) < 0) {
- // checkError();
- // }
- // if (fptr.put_BarcodePixelProportions(300) < 0) {
- // checkError();
- // }
- // if (fptr.PrintBarcode() < 0) {
- // checkError();
- // }
- // }
- //
- // private void discount(double sum, int type, int destination) throws DriverException {
- // if (fptr.put_Summ(sum) < 0) {
- // checkError();
- // }
- // if (fptr.put_DiscountType(type) < 0) {
- // checkError();
- // }
- // if (fptr.put_Destination(destination) < 0) {
- // checkError();
- // }
- // if (fptr.Discount() < 0) {
- // checkError();
- // }
- // }
- // private void charge(double sum, int type, int destination) throws DriverException {
- // if (fptr.put_Summ(sum) < 0) {
- // checkError();
- // }
- // if (fptr.put_DiscountType(type) < 0) {
- // checkError();
- // }
- // if (fptr.put_Destination(destination) < 0) {
- // checkError();
- // }
- // if (fptr.Charge() < 0) {
- // checkError();
- // }
- // }
- private String getSettings() {
- String model = String.valueOf(com.atol.drivers.fptr.IFptr.MODEL_ATOL_50F);
- String port = com.atol.drivers.fptr.Fptr.SETTING_PORT_USB;
- return "<settings version=\"5\">\n" +
- " <value name=\"AccessPassword\">0</value>\n" +
- " <value name=\"AutoDisableBluetooth\">0</value>\n" +
- " <value name=\"AutoEnableBluetooth\">1</value>\n" +
- " <value name=\"BaudRate\">9600</value>\n" +
- " <value name=\"Bits\">8</value>\n" +
- " <value name=\"ConnectionType\">1</value>\n" +
- " <value name=\"DeviceName\">Device #1</value>\n" +
- " <value name=\"IPAddress\"></value>\n" +
- " <value name=\"IPPort\">5555</value>\n" +
- " <value name=\"MACAddress\"></value>\n" +
- " <value name=\"Model\">"+ model + "</value>\n" +
- " <value name=\"OfdPort\">NONE</value>\n" +
- " <value name=\"Parity\">0</value>\n" +
- " <value name=\"Port\">"+ port +"</value>\n" +
- " <value name=\"Protocol\">0</value>\n" +
- " <value name=\"StopBits\">0</value>\n" +
- " <value name=\"TTYSuffix\">ttyACM0</value>\n" +
- " <value name=\"UseJournal\">0</value>\n" +
- " <value name=\"UserPassword\">30</value>\n" +
- " <value name=\"Vid\">10514</value>\n" +
- " <value name=\"Pid\">5</value>\n" +
- " </settings>";
- // return preferences.getString(SettingsActivity.DEVICE_SETTINGS, getDefaultSettings());
- }
- private static class DriverException extends Exception {
- public DriverException(String msg) {
- super(msg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement