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.

190 lines
6.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. odoo.define('pos_keyboard.pos', function (require) {
  2. "use strict";
  3. var core = require('web.core');
  4. var models = require('point_of_sale.models');
  5. var screens = require('point_of_sale.screens');
  6. var _super_posmodel = models.PosModel.prototype;
  7. models.PosModel = models.PosModel.extend({
  8. initialize: function (session, attributes) {
  9. this.keypad = new Keypad({'pos': this});
  10. return _super_posmodel.initialize.call(this, session, attributes);
  11. }
  12. });
  13. screens.NumpadWidget.include({
  14. start: function() {
  15. this._super();
  16. var self = this;
  17. this.pos.keypad.set_action_callback(function(data){
  18. self.keypad_action(data, self.pos.keypad.type);
  19. });
  20. },
  21. keypad_action: function(data, type){
  22. if (data.type === type.numchar){
  23. this.state.appendNewChar(data.val);
  24. }
  25. else if (data.type === type.bmode) {
  26. this.state.changeMode(data.val);
  27. }
  28. else if (data.type === type.sign){
  29. this.clickSwitchSign();
  30. }
  31. else if (data.type === type.backspace){
  32. this.clickDeleteLastChar();
  33. }
  34. }
  35. });
  36. screens.PaymentScreenWidget.include({
  37. show: function(){
  38. this._super();
  39. this.pos.keypad.disconnect();
  40. },
  41. hide: function(){
  42. this._super();
  43. this.pos.keypad.connect();
  44. }
  45. });
  46. // this module mimics a keypad-only cash register. Use connect() and
  47. // disconnect() to activate and deactivate it.
  48. var Keypad = core.Class.extend({
  49. init: function(attributes){
  50. this.pos = attributes.pos;
  51. /*this.pos_widget = this.pos.pos_widget;*/
  52. this.type = {
  53. numchar: 'number, dot',
  54. bmode: 'quantity, discount, price',
  55. sign: '+, -',
  56. backspace: 'backspace'
  57. };
  58. this.data = {
  59. type: undefined,
  60. val: undefined
  61. };
  62. this.action_callback = undefined;
  63. },
  64. save_callback: function(){
  65. this.saved_callback_stack.push(this.action_callback);
  66. },
  67. restore_callback: function(){
  68. if (this.saved_callback_stack.length > 0) {
  69. this.action_callback = this.saved_callback_stack.pop();
  70. }
  71. },
  72. set_action_callback: function(callback){
  73. this.action_callback = callback
  74. },
  75. //remove action callback
  76. reset_action_callback: function(){
  77. this.action_callback = undefined;
  78. },
  79. // starts catching keyboard events and tries to interpret keystrokes,
  80. // calling the callback when needed.
  81. connect: function(){
  82. var self = this;
  83. // --- additional keyboard ---//
  84. var KC_PLU = 107; // KeyCode: + or - (Keypad '+')
  85. var KC_QTY = 111; // KeyCode: Quantity (Keypad '/')
  86. var KC_AMT = 106; // KeyCode: Price (Keypad '*')
  87. var KC_DISC = 109; // KeyCode: Discount Percentage [0..100] (Keypad '-')
  88. // --- basic keyboard --- //
  89. var KC_PLU_1 = 83; // KeyCode: sign + or - (Keypad 's')
  90. var KC_QTY_1 = 81; // KeyCode: Quantity (Keypad 'q')
  91. var KC_AMT_1 = 80; // KeyCode: Price (Keypad 'p')
  92. var KC_DISC_1 = 68; // KeyCode: Discount Percentage [0..100] (Keypad 'd')
  93. var KC_BACKSPACE = 8; // KeyCode: Backspace (Keypad 'backspace')
  94. var kc_lookup = {
  95. 48: '0', 49: '1', 50: '2', 51: '3', 52: '4',
  96. 53: '5', 54: '6', 55: '7', 56: '8', 57: '9',
  97. 80: 'p', 83: 's', 68: 'd', 190: '.', 81: 'q',
  98. 96: '0', 97: '1', 98: '2', 99: '3', 100: '4',
  99. 101: '5', 102: '6', 103: '7', 104: '8', 105: '9',
  100. 106: '*', 107: '+', 109: '-', 110: '.', 111: '/'
  101. };
  102. //usb keyboard keyup event
  103. var rx = /INPUT|SELECT|TEXTAREA/i;
  104. var ok = false;
  105. var timeStamp = 0;
  106. $('body').on('keyup', '', function (e){
  107. var statusHandler = !rx.test(e.target.tagName) ||
  108. e.target.disabled || e.target.readOnly;
  109. if (statusHandler){
  110. var is_number = false;
  111. var type = self.type;
  112. var buttonMode = {
  113. qty: 'quantity',
  114. disc: 'discount',
  115. price: 'price'
  116. };
  117. var token = e.keyCode;
  118. if ((token >= 96 && token <= 105 || token == 110) ||
  119. (token >= 48 && token <= 57 || token == 190)) {
  120. self.data.type = type.numchar;
  121. self.data.val = kc_lookup[token];
  122. is_number = true;
  123. ok = true;
  124. }
  125. else if (token == KC_PLU || token == KC_PLU_1) {
  126. self.data.type = type.sign;
  127. ok = true;
  128. }
  129. else if (token == KC_QTY || token == KC_QTY_1) {
  130. self.data.type = type.bmode;
  131. self.data.val = buttonMode.qty;
  132. ok = true;
  133. }
  134. else if (token == KC_AMT || token == KC_AMT_1) {
  135. self.data.type = type.bmode;
  136. self.data.val = buttonMode.price;
  137. ok = true;
  138. }
  139. else if (token == KC_DISC || token == KC_DISC_1) {
  140. self.data.type = type.bmode;
  141. self.data.val = buttonMode.disc;
  142. ok = true;
  143. }
  144. else if (token == KC_BACKSPACE) {
  145. self.data.type = type.backspace;
  146. ok = true;
  147. }
  148. else {
  149. self.data.type = undefined;
  150. self.data.val = undefined;
  151. ok = false;
  152. }
  153. if (is_number) {
  154. if (timeStamp + 50 > new Date().getTime()) {
  155. ok = false;
  156. }
  157. }
  158. timeStamp = new Date().getTime();
  159. setTimeout(function(){
  160. if (ok) {self.action_callback(self.data);}
  161. }, 50);
  162. }
  163. });
  164. },
  165. // stops catching keyboard events
  166. disconnect: function(){
  167. $('body').off('keyup', '')
  168. }
  169. });
  170. return {
  171. Keypad: Keypad
  172. };
  173. });