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.

278 lines
10 KiB

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