Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.77 KB | None | 0 0
  1. public class IFPTRPrinter implements PrinterInterface {
  2.  
  3. private Context mContext;
  4.  
  5. private static final boolean PRINT_FISCAL_CHECK = true;
  6. private static final boolean PRINT_NONFISCAL_CHECK = true;
  7. private static final boolean USE_FZ54 = true;
  8.  
  9. public IFPTRPrinter(Context context) {
  10. this.mContext = context;
  11. }
  12.  
  13. @Override
  14. public void printCheck(final PrinterCheck check, final PrinterCompletionInterface completion) {
  15. AsyncTask<Void, String, Void> task;
  16.  
  17. task = new AsyncTask<Void, String, Void>() {
  18. private com.atol.drivers.fptr.IFptr fptr;
  19.  
  20. @Override
  21. protected void onPreExecute() {
  22. }
  23.  
  24. @Override
  25. protected void onPostExecute(Void result) {
  26. }
  27.  
  28. @Override
  29. protected Void doInBackground(Void... params) {
  30. Random random = new Random();
  31. fptr = new com.atol.drivers.fptr.Fptr();
  32. try {
  33. fptr.create(mContext);
  34.  
  35. if (fptr.put_DeviceSettings(getSettings()) < 0) {
  36. checkError(fptr);
  37. }
  38.  
  39. if (fptr.put_DeviceEnabled(true) < 0) {
  40. checkError(fptr);
  41. }
  42.  
  43. if (fptr.GetStatus() < 0) {
  44. checkError(fptr);
  45. }
  46.  
  47. // Отменяем чек, если уже открыт. Ошибки "Неверный режим" и "Чек уже закрыт"
  48. // не являются ошибками, если мы хотим просто отменить чек
  49.  
  50.  
  51. try {
  52. if (fptr.CancelCheck() < 0) {
  53. checkError(fptr);
  54. }
  55. } catch (DriverException e) {
  56. int rc = fptr.get_ResultCode();
  57. if (rc != -16 && rc != -3801) {
  58. throw e;
  59. }
  60. }
  61.  
  62. // reportInfo(fptr);
  63. //
  64. if (PRINT_FISCAL_CHECK) {
  65. try {
  66. openCheck(fptr, com.atol.drivers.fptr.IFptr.CHEQUE_TYPE_SELL);
  67. } catch (DriverException e) {
  68. // Проверка на превышение смены
  69. if (fptr.get_ResultCode() == -3822) {
  70. reportZ(fptr);
  71. openCheck(fptr, com.atol.drivers.fptr.IFptr.CHEQUE_TYPE_SELL);
  72. } else {
  73. throw e;
  74. }
  75. }
  76.  
  77. BigDecimal sum = new BigDecimal(0);
  78. for (int i = 0; i < check.items.size(); i++) {
  79. if(USE_FZ54)
  80. {
  81. // 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);
  82. registration(fptr, String.format(check.items.get(i).getName()), check.items.get(i).getPriceForPrinter(), check.items.get(i).getQty());
  83. sum = sum.add(new BigDecimal(check.items.get(i).getPriceForPrinter()).multiply(new BigDecimal(check.items.get(i).getQty())));
  84. }
  85. }
  86.  
  87. payment(fptr, sum.doubleValue(), 0);
  88. // Закрываем чек
  89. closeCheck(fptr,0);
  90. completion.done();
  91. }
  92. } catch (Exception e) {
  93. publishProgress(e.toString());
  94. } finally {
  95. fptr.destroy();
  96. }
  97. return null;
  98. }
  99.  
  100. @Override
  101. protected void onProgressUpdate(String... values) {
  102. if (values == null || values.length == 0) {
  103. return;
  104. }
  105. }
  106.  
  107. };
  108. task.execute();
  109. }
  110.  
  111. private void checkError(com.atol.drivers.fptr.IFptr fptr) throws DriverException {
  112. int rc = fptr.get_ResultCode();
  113. if (rc < 0) {
  114. String rd = fptr.get_ResultDescription(), bpd = null;
  115. if (rc == -6) {
  116. bpd = fptr.get_BadParamDescription();
  117. }
  118. if (bpd != null) {
  119. throw new DriverException(String.format("[%d] %s (%s)", rc, rd, bpd));
  120. } else {
  121. throw new DriverException(String.format("[%d] %s", rc, rd));
  122. }
  123. }
  124. }
  125.  
  126. // private void printText(String text, int alignment, int wrap) throws DriverException {
  127. // if (fptr.put_Caption(text) < 0) {
  128. // checkError();
  129. // }
  130. // if (fptr.put_TextWrap(wrap) < 0) {
  131. // checkError();
  132. // }
  133. // if (fptr.put_Alignment(alignment) < 0) {
  134. // checkError();
  135. // }
  136. // if (fptr.PrintString() < 0) {
  137. // checkError();
  138. // }
  139. // }
  140.  
  141. // private void printText(String text) throws DriverException {
  142. // printText(text, com.atol.drivers.fptr.IFptr.ALIGNMENT_CENTER, com.atol.drivers.fptr.IFptr.WRAP_LINE);
  143. // }
  144.  
  145. private void openCheck(com.atol.drivers.fptr.IFptr fptr, int type) throws DriverException {
  146. if (fptr.put_Mode(com.atol.drivers.fptr.IFptr.MODE_REGISTRATION) < 0) {
  147. checkError(fptr);
  148. }
  149. if (fptr.SetMode() < 0) {
  150. checkError(fptr);
  151. }
  152. if (fptr.put_CheckType(type) < 0) {
  153. checkError(fptr);
  154. }
  155. if (fptr.OpenCheck() < 0) {
  156. checkError(fptr);
  157. }
  158. }
  159.  
  160. private void closeCheck(com.atol.drivers.fptr.IFptr fptr, int typeClose) throws DriverException {
  161. if (fptr.put_TypeClose(typeClose) < 0) {
  162. checkError(fptr);
  163. }
  164. if (fptr.CloseCheck() < 0) {
  165. checkError(fptr);
  166. }
  167. }
  168.  
  169. private void registration(com.atol.drivers.fptr.IFptr fptr, String name, double price, double quantity) throws DriverException {
  170. if (fptr.put_Quantity(quantity) < 0) {
  171. checkError(fptr);
  172. }
  173. if (fptr.put_Price(price) < 0) {
  174. checkError(fptr);
  175. }
  176. if (fptr.put_TextWrap(com.atol.drivers.fptr.IFptr.WRAP_WORD) < 0) {
  177. checkError(fptr);
  178. }
  179. if (fptr.put_Name(name) < 0) {
  180. checkError(fptr);
  181. }
  182. if (fptr.Registration() < 0) {
  183. checkError(fptr);
  184. }
  185. }
  186.  
  187. private void registrationFZ54(com.atol.drivers.fptr.IFptr fptr, String name, double price, double quantity,
  188. double positionSum, int taxNumber) throws DriverException {
  189. if (fptr.put_TaxNumber(taxNumber) < 0) {
  190. checkError(fptr);
  191. }
  192. if (fptr.put_PositionSum(positionSum) < 0) {
  193. checkError(fptr);
  194. }
  195. if (fptr.put_Quantity(quantity) < 0) {
  196. checkError(fptr);
  197. }
  198. if (fptr.put_Price(price) < 0) {
  199. checkError(fptr);
  200. }
  201. if (fptr.put_TextWrap(com.atol.drivers.fptr.IFptr.WRAP_WORD) < 0) {
  202. checkError(fptr);
  203. }
  204. if (fptr.put_Name(name) < 0) {
  205. checkError(fptr);
  206. }
  207. if (fptr.Registration() < 0) {
  208. checkError(fptr);
  209. }
  210. }
  211.  
  212. private void payment(com.atol.drivers.fptr.IFptr fptr, double sum, int type) throws DriverException {
  213. if (fptr.put_Summ(sum) < 0) {
  214. checkError(fptr);
  215. }
  216. if (fptr.put_TypeClose(type) < 0) {
  217. checkError(fptr);
  218. }
  219. if (fptr.Payment() < 0) {
  220. checkError(fptr);
  221. }
  222. }
  223.  
  224. private void reportZ(com.atol.drivers.fptr.IFptr fptr) throws DriverException {
  225. if (fptr.put_Mode(com.atol.drivers.fptr.IFptr.MODE_REPORT_CLEAR) < 0) {
  226. checkError(fptr);
  227. }
  228. if (fptr.SetMode() < 0) {
  229. checkError(fptr);
  230. }
  231. if (fptr.put_ReportType(com.atol.drivers.fptr.IFptr.REPORT_Z) < 0) {
  232. checkError(fptr);
  233. }
  234. if (fptr.Report() < 0) {
  235. checkError(fptr);
  236. }
  237. }
  238.  
  239. private void reportInfo(com.atol.drivers.fptr.IFptr fptr) throws DriverException {
  240. if (fptr.put_Mode(0) < 0) {
  241. checkError(fptr);
  242. }
  243. if (fptr.SetMode() < 0) {
  244. checkError(fptr);
  245. }
  246. if (fptr.put_ReportType(com.atol.drivers.fptr.IFptr.REPORT_PRINT_INFO) < 0) {
  247. checkError(fptr);
  248. }
  249. if (fptr.Report() < 0) {
  250. checkError(fptr);
  251. }
  252. }
  253.  
  254. // private void printFooter() throws DriverException {
  255. // if (fptr.put_Mode(com.atol.drivers.fptr.IFptr.MODE_REPORT_NO_CLEAR) < 0) {
  256. // checkError();
  257. // }
  258. // if (fptr.SetMode() < 0) {
  259. // checkError();
  260. // }
  261. // if (fptr.PrintFooter() < 0) {
  262. // checkError();
  263. // }
  264. // }
  265.  
  266. // private void printBarcode(int type, String barcode, double scale) throws DriverException {
  267. // if (fptr.put_Alignment(com.atol.drivers.fptr.IFptr.ALIGNMENT_CENTER) < 0) {
  268. // checkError();
  269. // }
  270. // if (fptr.put_BarcodeType(type) < 0) {
  271. // checkError();
  272. // }
  273. // if (fptr.put_Barcode(barcode) < 0) {
  274. // checkError();
  275. // }
  276. // if (type == com.atol.drivers.fptr.IFptr.BARCODE_TYPE_QRCODE) {
  277. // if (fptr.put_Height(0) < 0) {
  278. // checkError();
  279. // }
  280. // } else {
  281. // if (fptr.put_Height(50) < 0) {
  282. // checkError();
  283. // }
  284. // }
  285. // if (fptr.put_BarcodePrintType(com.atol.drivers.fptr.IFptr.BARCODE_PRINTTYPE_AUTO) < 0) {
  286. // checkError();
  287. // }
  288. // if (fptr.put_PrintBarcodeText(false) < 0) {
  289. // checkError();
  290. // }
  291. // if (fptr.put_BarcodeControlCode(true) < 0) {
  292. // checkError();
  293. // }
  294. // if (fptr.put_Scale(scale) < 0) {
  295. // checkError();
  296. // }
  297. // if (fptr.put_BarcodeCorrection(0) < 0) {
  298. // checkError();
  299. // }
  300. // if (fptr.put_BarcodeColumns(3) < 0) {
  301. // checkError();
  302. // }
  303. // if (fptr.put_BarcodeRows(1) < 0) {
  304. // checkError();
  305. // }
  306. // if (fptr.put_BarcodeProportions(50) < 0) {
  307. // checkError();
  308. // }
  309. // if (fptr.put_BarcodeUseProportions(true) < 0) {
  310. // checkError();
  311. // }
  312. // if (fptr.put_BarcodePackingMode(com.atol.drivers.fptr.IFptr.BARCODE_PDF417_PACKING_MODE_DEFAULT) < 0) {
  313. // checkError();
  314. // }
  315. // if (fptr.put_BarcodePixelProportions(300) < 0) {
  316. // checkError();
  317. // }
  318. // if (fptr.PrintBarcode() < 0) {
  319. // checkError();
  320. // }
  321. // }
  322. //
  323. // private void discount(double sum, int type, int destination) throws DriverException {
  324. // if (fptr.put_Summ(sum) < 0) {
  325. // checkError();
  326. // }
  327. // if (fptr.put_DiscountType(type) < 0) {
  328. // checkError();
  329. // }
  330. // if (fptr.put_Destination(destination) < 0) {
  331. // checkError();
  332. // }
  333. // if (fptr.Discount() < 0) {
  334. // checkError();
  335. // }
  336. // }
  337.  
  338. // private void charge(double sum, int type, int destination) throws DriverException {
  339. // if (fptr.put_Summ(sum) < 0) {
  340. // checkError();
  341. // }
  342. // if (fptr.put_DiscountType(type) < 0) {
  343. // checkError();
  344. // }
  345. // if (fptr.put_Destination(destination) < 0) {
  346. // checkError();
  347. // }
  348. // if (fptr.Charge() < 0) {
  349. // checkError();
  350. // }
  351. // }
  352.  
  353. private String getSettings() {
  354.  
  355. String model = String.valueOf(com.atol.drivers.fptr.IFptr.MODEL_ATOL_50F);
  356. String port = com.atol.drivers.fptr.Fptr.SETTING_PORT_USB;
  357.  
  358. return "<settings version=\"5\">\n" +
  359. " <value name=\"AccessPassword\">0</value>\n" +
  360. " <value name=\"AutoDisableBluetooth\">0</value>\n" +
  361. " <value name=\"AutoEnableBluetooth\">1</value>\n" +
  362. " <value name=\"BaudRate\">9600</value>\n" +
  363. " <value name=\"Bits\">8</value>\n" +
  364. " <value name=\"ConnectionType\">1</value>\n" +
  365. " <value name=\"DeviceName\">Device #1</value>\n" +
  366. " <value name=\"IPAddress\"></value>\n" +
  367. " <value name=\"IPPort\">5555</value>\n" +
  368. " <value name=\"MACAddress\"></value>\n" +
  369. " <value name=\"Model\">"+ model + "</value>\n" +
  370. " <value name=\"OfdPort\">NONE</value>\n" +
  371. " <value name=\"Parity\">0</value>\n" +
  372. " <value name=\"Port\">"+ port +"</value>\n" +
  373. " <value name=\"Protocol\">0</value>\n" +
  374. " <value name=\"StopBits\">0</value>\n" +
  375. " <value name=\"TTYSuffix\">ttyACM0</value>\n" +
  376. " <value name=\"UseJournal\">0</value>\n" +
  377. " <value name=\"UserPassword\">30</value>\n" +
  378. " <value name=\"Vid\">10514</value>\n" +
  379. " <value name=\"Pid\">5</value>\n" +
  380. " </settings>";
  381. // return preferences.getString(SettingsActivity.DEVICE_SETTINGS, getDefaultSettings());
  382. }
  383.  
  384. private static class DriverException extends Exception {
  385. public DriverException(String msg) {
  386. super(msg);
  387. }
  388. }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement