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.

256 lines
10 KiB

  1. /*
  2. POS Customer display module for Odoo
  3. Copyright (C) 2014 Aurélien DUMAINE
  4. Copyright (C) 2014 Barroux Abbey (www.barroux.org)
  5. @author: Aurélien DUMAINE
  6. @author: Alexis de Lattre <alexis.delattre@akretion.com>
  7. @author: Father Odilon (Barroux Abbey)
  8. The licence is in the file __openerp__.py
  9. */
  10. openerp.pos_customer_display = function(instance){
  11. module = instance.point_of_sale;
  12. var _t = instance.web._t;
  13. var PosModelSuper = module.PosModel;
  14. module.PosModel = module.PosModel.extend({
  15. prepare_text_customer_display: function(type, data){
  16. if (this.config.iface_customer_display != true)
  17. return;
  18. var line_length = this.config.customer_display_line_length || 20;
  19. var currency_rounding = Math.ceil(Math.log(1.0 / this.currency.rounding) / Math.log(10));
  20. if (type == 'addProduct'){
  21. // in order to not recompute qty in options..., we assume that the new ordeLine is the last of the collection
  22. // addOrderline exists but is not called by addProduct, should we handle it ?
  23. var line = this.get('selectedOrder').getLastOrderline();
  24. var price_unit = line.get_unit_price() * (1.0 - (line.get_discount() / 100.0));
  25. price_unit = price_unit.toFixed(currency_rounding);
  26. var l21 = line.get_quantity_str_with_unit() + ' x ' + price_unit;
  27. var l22 = ' ' + line.get_display_price().toFixed(currency_rounding);
  28. var lines_to_send = new Array(
  29. this.proxy.align_left(line.get_product().display_name, line_length),
  30. this.proxy.align_left(l21, line_length - l22.length) + l22
  31. );
  32. } else if (type == 'removeOrderline') {
  33. // first click on the backspace button set the amount to 0 => we can't precise the deleted qunatity and price
  34. var line = data['line'];
  35. var lines_to_send = new Array(
  36. this.proxy.align_left(_t("Delete Item"), line_length),
  37. this.proxy.align_right(line.get_product().display_name, line_length)
  38. );
  39. } else if (type == 'addPaymentline') {
  40. var total = this.get('selectedOrder').getTotalTaxIncluded().toFixed(currency_rounding);
  41. var lines_to_send = new Array(
  42. this.proxy.align_left(_t("TOTAL: "), line_length),
  43. this.proxy.align_right(total, line_length)
  44. );
  45. } else if (type == 'removePaymentline') {
  46. var line = data['line'];
  47. var amount = line.get_amount().toFixed(currency_rounding);
  48. var lines_to_send = new Array(
  49. this.proxy.align_left(_t("Cancel Payment"), line_length),
  50. this.proxy.align_right(line.cashregister.journal_id[1] , line_length - 1 - amount.length) + ' ' + amount
  51. );
  52. } else if (type == 'update_payment') {
  53. var change = data['change'];
  54. var lines_to_send = new Array(
  55. this.proxy.align_left(_t("Your Change:"), line_length),
  56. this.proxy.align_right(change, line_length)
  57. );
  58. } else if (type == 'pushOrder') {
  59. var lines_to_send = new Array(
  60. this.proxy.align_center(this.config.customer_display_msg_next_l1, line_length),
  61. this.proxy.align_center(this.config.customer_display_msg_next_l2, line_length)
  62. );
  63. } else if (type == 'openPOS') {
  64. var lines_to_send = new Array(
  65. this.proxy.align_center(this.config.customer_display_msg_next_l1, line_length),
  66. this.proxy.align_center(this.config.customer_display_msg_next_l2, line_length)
  67. );
  68. } else if (type = 'closePOS') {
  69. var lines_to_send = new Array(
  70. this.proxy.align_center(this.config.customer_display_msg_closed_l1, line_length),
  71. this.proxy.align_center(this.config.customer_display_msg_closed_l2, line_length)
  72. );
  73. } else {
  74. console.warn('Unknown message type');
  75. return;
  76. }
  77. this.proxy.send_text_customer_display(lines_to_send, line_length);
  78. //console.log('prepare_text_customer_display type=' + type + ' | l1=' + lines_to_send[0] + ' | l2=' + lines_to_send[1]);
  79. },
  80. push_order: function(order){
  81. res = PosModelSuper.prototype.push_order.call(this, order);
  82. if (order) {
  83. this.prepare_text_customer_display('pushOrder', {'order' : order});
  84. }
  85. return res;
  86. },
  87. });
  88. module.ProxyDevice = module.ProxyDevice.extend({
  89. send_text_customer_display: function(data, line_length){
  90. //FIXME : this function is call twice. The first time, it is not called by prepare_text_customer_display : WHY ?
  91. if (_.isEmpty(data) || data.length != 2 || data[0].length != line_length || data[1].length != line_length){
  92. console.warn("send_text_customer_display: Bad Data argument. Data=" + data + ' line_length=' + line_length);
  93. } else {
  94. // alert(JSON.stringify(data));
  95. return this.message('send_text_customer_display', {'text_to_display' : JSON.stringify(data)});
  96. }
  97. },
  98. align_left: function(string, length){
  99. if (string) {
  100. if (string.length > length)
  101. {
  102. string = string.substring(0,length);
  103. }
  104. else if (string.length < length)
  105. {
  106. while(string.length < length)
  107. string = string + ' ';
  108. }
  109. }
  110. else {
  111. string = ' '
  112. while(string.length < length)
  113. string = ' ' + string;
  114. }
  115. return string;
  116. },
  117. align_right: function(string, length){
  118. if (string) {
  119. if (string.length > length)
  120. {
  121. string = string.substring(0,length);
  122. }
  123. else if (string.length < length)
  124. {
  125. while(string.length < length)
  126. string = ' ' + string;
  127. }
  128. }
  129. else {
  130. string = ' '
  131. while(string.length < length)
  132. string = ' ' + string;
  133. }
  134. return string;
  135. },
  136. align_center: function(string, length){
  137. if (string) {
  138. if (string.length > length)
  139. {
  140. string = string.substring(0, length);
  141. }
  142. else if (string.length < length)
  143. {
  144. ini = (length - string.length) / 2;
  145. while(string.length < length - ini)
  146. string = ' ' + string;
  147. while(string.length < length)
  148. string = string + ' ';
  149. }
  150. }
  151. else {
  152. string = ' '
  153. while(string.length < length)
  154. string = ' ' + string;
  155. }
  156. return string;
  157. },
  158. });
  159. var OrderSuper = module.Order;
  160. module.Order = module.Order.extend({
  161. addProduct: function(product, options){
  162. res = OrderSuper.prototype.addProduct.call(this, product, options);
  163. if (product) {
  164. this.pos.prepare_text_customer_display('addProduct', {'product' : product, 'options' : options});
  165. }
  166. return res;
  167. },
  168. removeOrderline: function(line){
  169. if (line) {
  170. this.pos.prepare_text_customer_display('removeOrderline', {'line' : line});
  171. }
  172. return OrderSuper.prototype.removeOrderline.call(this, line);
  173. },
  174. removePaymentline: function(line){
  175. if (line) {
  176. this.pos.prepare_text_customer_display('removePaymentline', {'line' : line});
  177. }
  178. return OrderSuper.prototype.removePaymentline.call(this, line);
  179. },
  180. addPaymentline: function(cashregister){
  181. res = OrderSuper.prototype.addPaymentline.call(this, cashregister);
  182. if (cashregister) {
  183. this.pos.prepare_text_customer_display('addPaymentline', {'cashregister' : cashregister});
  184. }
  185. return res;
  186. },
  187. });
  188. module.PaymentScreenWidget.include({
  189. update_payment_summary: function(){
  190. res = this._super();
  191. var currentOrder = this.pos.get('selectedOrder');
  192. var paidTotal = currentOrder.getPaidTotal();
  193. var dueTotal = currentOrder.getTotalTaxIncluded();
  194. var change = paidTotal > dueTotal ? paidTotal - dueTotal : 0;
  195. if (change) {
  196. change_rounded = change.toFixed(2);
  197. this.pos.prepare_text_customer_display('update_payment', {'change': change_rounded});
  198. }
  199. return res;
  200. },
  201. });
  202. module.PosWidget.include({
  203. close: function(){
  204. this._super();
  205. this.pos.prepare_text_customer_display('closePOS', {});
  206. },
  207. });
  208. module.ProxyStatusWidget.include({
  209. start: function(){
  210. this._super();
  211. this.pos.prepare_text_customer_display('openPOS', {});
  212. },
  213. });
  214. /* Handle Button "Display Total to Customer" */
  215. var _saved_renderElement = module.OrderWidget.prototype.renderElement;
  216. module.OrderWidget.prototype.renderElement = function() {
  217. _saved_renderElement.apply(this, arguments);
  218. var self = this;
  219. if (self.pos.config.iface_customer_display) {
  220. self.el.querySelector('.show-total-to-customer')
  221. .addEventListener('click', function(){
  222. self.pos.prepare_text_customer_display('addPaymentline', {})
  223. });
  224. }
  225. };
  226. };