You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
10 KiB

8 years ago
8 years ago
8 years ago
  1. /* Copyright 2015 igallyamov <https://github.com/igallyamov>
  2. Copyright 2016 ufaks <https://github.com/ufaks>
  3. Copyright 2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
  4. Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/kolushovalexandr>
  5. Copyright 2019 ssaid <https://github.com/ssaid>
  6. License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). */
  7. /* eslint-disable complexity */
  8. odoo.define('pos_keyboard.pos', function (require) {
  9. "use strict";
  10. var core = require('web.core');
  11. var gui = require('point_of_sale.gui');
  12. var models = require('point_of_sale.models');
  13. var screens = require('point_of_sale.screens');
  14. var PopupWidget = require('point_of_sale.popups');
  15. gui.Gui.prototype.popup_classes.filter(function(c){
  16. return c.name === 'password';
  17. })[0].widget.include({
  18. init: function(parent, args) {
  19. this._super(parent, args);
  20. this.popup_type = 'password';
  21. },
  22. });
  23. PopupWidget.include({
  24. keypad_action: function(data){
  25. var type = this.pos.keypad.type;
  26. if (data.type === type.numchar){
  27. this.click_keyboard(data.val);
  28. } else if (data.type === type.backspace){
  29. this.click_keyboard('BACKSPACE');
  30. } else if (data.type === type.enter){
  31. // some pop-ups might throw an error due to lack of some income data
  32. try {
  33. return this.click_confirm();
  34. } catch (error){
  35. return;
  36. }
  37. } else if (data.type === type.escape){
  38. this.click_cancel();
  39. }
  40. },
  41. click_keyboard: function(value){
  42. if (typeof this.inputbuffer === 'undefined') {
  43. return;
  44. }
  45. var newbuf = this.gui.numpad_input(
  46. this.inputbuffer,
  47. value,
  48. {'firstinput': this.firstinput});
  49. this.firstinput = (newbuf.length === 0);
  50. var $value = this.$('.value');
  51. if (newbuf !== this.inputbuffer) {
  52. this.inputbuffer = newbuf;
  53. $value.text(this.inputbuffer);
  54. }
  55. if (this.popup_type === 'password' && newbuf) {
  56. $value.text($value.text().replace(/./g, '•'));
  57. }
  58. },
  59. show: function(options){
  60. this._super(options);
  61. this.$('input,textarea').focus();
  62. },
  63. });
  64. screens.NumpadWidget.include({
  65. keypad_action: function(data){
  66. var type = this.pos.keypad.type;
  67. if (data.type === type.numchar){
  68. this.state.appendNewChar(data.val);
  69. } else if (data.type === type.bmode) {
  70. this.state.changeMode(data.val);
  71. } else if (data.type === type.sign){
  72. this.clickSwitchSign();
  73. } else if (data.type === type.backspace){
  74. this.clickDeleteLastChar();
  75. }
  76. }
  77. });
  78. screens.PaymentScreenWidget.include({
  79. show: function(){
  80. this._super();
  81. this.pos.keypad.disconnect();
  82. },
  83. hide: function(){
  84. this._super();
  85. this.pos.keypad.connect();
  86. }
  87. });
  88. // this module mimics a keypad-only cash register. Use connect() and
  89. // disconnect() to activate and deactivate it.
  90. var Keypad = core.Class.extend({
  91. init: function(attributes){
  92. this.pos = attributes.pos;
  93. /*this.pos_widget = this.pos.pos_widget;*/
  94. this.type = {
  95. numchar: 'number, dot',
  96. bmode: 'quantity, discount, price',
  97. sign: '+, -',
  98. backspace: 'backspace',
  99. enter: 'enter',
  100. escape: 'escape',
  101. };
  102. this.data = {
  103. // type: undefined,
  104. // val: undefined
  105. };
  106. delete this.action_callback;
  107. this.active = false;
  108. },
  109. save_callback: function(){
  110. this.saved_callback_stack.push(this.action_callback);
  111. },
  112. restore_callback: function(){
  113. if (this.saved_callback_stack.length > 0) {
  114. this.action_callback = this.saved_callback_stack.pop();
  115. }
  116. },
  117. set_action_callback: function(callback){
  118. this.action_callback = callback;
  119. },
  120. //remove action callback
  121. reset_action_callback: function(){
  122. delete this.action_callback;
  123. },
  124. // starts catching keyboard events and tries to interpret keystrokes,
  125. // calling the callback when needed.
  126. connect: function(){
  127. var self = this;
  128. if (self.active) {
  129. return;
  130. }
  131. // --- additional keyboard ---//
  132. // KeyCode: + or - (Keypad '+')
  133. var KC_PLU = 107;
  134. // KeyCode: Quantity (Keypad '/')
  135. var KC_QTY = 111;
  136. // KeyCode: Price (Keypad '*')
  137. var KC_AMT = 106;
  138. // KeyCode: Discount Percentage [0..100] (Keypad '-')
  139. var KC_DISC = 109;
  140. // --- basic keyboard --- //
  141. // KeyCode: sign + or - (Keypad 's')
  142. var KC_PLU_1 = 83;
  143. // KeyCode: Quantity (Keypad 'q')
  144. var KC_QTY_1 = 81;
  145. // KeyCode: Price (Keypad 'p')
  146. var KC_AMT_1 = 80;
  147. // KeyCode: Discount Percentage [0..100] (Keypad 'd')
  148. var KC_DISC_1 = 68;
  149. // KeyCode: Backspace (Keypad 'backspace')
  150. var KC_BACKSPACE = 8;
  151. // KeyCode: Enter (Keypad 'enter')
  152. var KC_ENTER = 13;
  153. // KeyCode: Escape (Keypad 'esc')
  154. var KC_ESCAPE = 27;
  155. var kc_lookup = {
  156. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4',
  157. 53: '5', 54: '6', 55: '7', 56: '8', 57: '9',
  158. 80: 'p', 83: 's', 68: 'd', 190: '.', 81: 'q',
  159. 96: '0', 97: '1', 98: '2', 99: '3', 100: '4',
  160. 101: '5', 102: '6', 103: '7', 104: '8', 105: '9',
  161. 106: '*', 107: '+', 109: '-', 110: '.', 111: '/'
  162. };
  163. //usb keyboard keyup event
  164. var rx = /INPUT|SELECT|TEXTAREA/i;
  165. var ok = false;
  166. var timeStamp = 0;
  167. $('body').on('keyup', '', function (e){
  168. var statusHandler = !rx.test(e.target.tagName) ||
  169. e.target.disabled || e.target.readOnly;
  170. if (statusHandler){
  171. var is_number = false;
  172. var type = self.type;
  173. var buttonMode = {
  174. qty: 'quantity',
  175. disc: 'discount',
  176. price: 'price'
  177. };
  178. var token = e.keyCode;
  179. if (((token >= 96 && token <= 105) || token === 110) ||
  180. ((token >= 48 && token <= 57) || token === 190)) {
  181. self.data.type = type.numchar;
  182. self.data.val = kc_lookup[token];
  183. is_number = true;
  184. ok = true;
  185. } else if (token === KC_PLU || token === KC_PLU_1) {
  186. self.data.type = type.sign;
  187. ok = true;
  188. } else if (token === KC_QTY || token === KC_QTY_1) {
  189. self.data.type = type.bmode;
  190. self.data.val = buttonMode.qty;
  191. ok = true;
  192. } else if (token === KC_AMT || token === KC_AMT_1) {
  193. self.data.type = type.bmode;
  194. self.data.val = buttonMode.price;
  195. ok = true;
  196. } else if (token === KC_DISC || token === KC_DISC_1) {
  197. self.data.type = type.bmode;
  198. self.data.val = buttonMode.disc;
  199. ok = true;
  200. } else if (token === KC_BACKSPACE) {
  201. self.data.type = type.backspace;
  202. ok = true;
  203. } else if (token === KC_ENTER) {
  204. self.data.type = type.enter;
  205. ok = true;
  206. } else if (token === KC_ESCAPE) {
  207. self.data.type = type.escape;
  208. ok = true;
  209. } else {
  210. delete self.data.type;
  211. delete self.data.val;
  212. ok = false;
  213. }
  214. if (is_number) {
  215. if (timeStamp + 50 > new Date().getTime()) {
  216. ok = false;
  217. }
  218. }
  219. timeStamp = new Date().getTime();
  220. setTimeout(function(){
  221. if (ok) {
  222. self.action_callback(self.data);
  223. }
  224. }, 50);
  225. }
  226. });
  227. self.active = true;
  228. },
  229. // stops catching keyboard events
  230. disconnect: function(){
  231. $('body').off('keyup', '');
  232. self.active = false;
  233. }
  234. });
  235. var _super_posmodel = models.PosModel.prototype;
  236. models.PosModel = models.PosModel.extend({
  237. initialize: function (session, attributes) {
  238. var self = this;
  239. this.keypad = new Keypad({'pos': this});
  240. _super_posmodel.initialize.call(this, session, attributes);
  241. this.ready.then(function(){
  242. self.keypad.set_action_callback(function(data){
  243. var current_screen = self.gui.current_screen;
  244. var current_popup = self.gui.current_popup;
  245. if (current_popup) {
  246. current_popup.keypad_action(data);
  247. } else if (current_screen.numpad && current_screen.numpad.keypad_action) {
  248. current_screen.numpad.keypad_action(data);
  249. }
  250. });
  251. });
  252. }
  253. });
  254. return {
  255. Keypad: Keypad
  256. };
  257. });