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.

186 lines
6.9 KiB

3 years ago
  1. /*
  2. © 2020 Le Filament (<http://www.le-filament.com>)
  3. License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. */
  5. odoo.define('vracoop_balance_ticketing.screens', function (require) {
  6. "use strict";
  7. var chrome = require('point_of_sale.chrome');
  8. var gui = require('point_of_sale.gui');
  9. var models = require('point_of_sale.models');
  10. var screens = require('point_of_sale.screens');
  11. var popups = require('point_of_sale.popups');
  12. var container = require('pos_container.container');
  13. var models_and_db = require('pos_container.models_and_db');
  14. var screen_vrac = require('vracoop_pos_free_balance_v2.container_balance');
  15. var core = require('web.core');
  16. var rpc = require('web.rpc');
  17. var utils = require('web.utils');
  18. var QWeb = core.qweb;
  19. var _t = core._t;
  20. // Screen confirmation de la pesée
  21. screen_vrac.ConfirmationScreen.include({
  22. show: function(){
  23. this._super();
  24. var self = this;
  25. this.render_change();
  26. this.render_receipt();
  27. this.handle_auto_print();
  28. },
  29. handle_auto_print: function() {
  30. if (this.should_auto_print()) {
  31. this.print();
  32. this.click_next();
  33. // }
  34. } else {
  35. this.lock_screen(false);
  36. }
  37. },
  38. get_barcode_data: function () {
  39. var transaction = this.gui.get_current_screen_param('transaction');
  40. var num_ean13 = this.pos.config.prefix + transaction.ean13.slice(2)
  41. return num_ean13;
  42. },
  43. should_auto_print: function() {
  44. return this.pos.config.is_ticket_print_auto && !this.pos.get_order()._printed;
  45. },
  46. lock_screen: function(locked) {
  47. this._locked = locked;
  48. if (locked) {
  49. this.$('.next').removeClass('highlight');
  50. } else {
  51. this.$('.next').addClass('highlight');
  52. }
  53. },
  54. get_receipt_render_env: function() {
  55. var order = this.pos.get_order();
  56. var transaction = this.gui.get_current_screen_param('transaction');
  57. var num_ean13 = this.pos.config.prefix + transaction.ean13.slice(2)
  58. var width_pix = (Math.round(this.pos.config.page_width * 3.78)).toString() + "px";
  59. return {
  60. widget: this,
  61. pos: this.pos,
  62. order: order,
  63. transaction: transaction,
  64. num_ean13: num_ean13,
  65. width: width_pix,
  66. };
  67. },
  68. print_web: function() {
  69. if ($.browser.safari) {
  70. document.execCommand('print', false, null);
  71. } else {
  72. try {
  73. window.print();
  74. } catch(err) {
  75. if (navigator.userAgent.toLowerCase().indexOf("android") > -1) {
  76. this.gui.show_popup('error',{
  77. 'title':_t('Printing is not supported on some android browsers'),
  78. 'body': _t('Printing is not supported on some android browsers due to no default printing protocol is available. It is possible to print your tickets by making use of an IoT Box.'),
  79. });
  80. } else {
  81. throw err;
  82. }
  83. }
  84. }
  85. this.pos.get_order()._printed = true;
  86. },
  87. print_xml: function() {
  88. var receipt = QWeb.render('XmlReceiptBls', this.get_receipt_render_env());
  89. this.pos.proxy.print_receipt(receipt);
  90. this.pos.get_order()._printed = true;
  91. },
  92. print: function() {
  93. var self = this;
  94. if (!this.pos.config.is_ticket_print_via_proxy) { // browser (html) printing
  95. // The problem is that in chrome the print() is asynchronous and doesn't
  96. // execute until all rpc are finished. So it conflicts with the rpc used
  97. // to send the orders to the backend, and the user is able to go to the next
  98. // screen before the printing dialog is opened. The problem is that what's
  99. // printed is whatever is in the page when the dialog is opened and not when it's called,
  100. // and so you end up printing the product list instead of the receipt...
  101. //
  102. // Fixing this would need a re-architecturing
  103. // of the code to postpone sending of orders after printing.
  104. //
  105. // But since the print dialog also blocks the other asynchronous calls, the
  106. // button enabling in the setTimeout() is blocked until the printing dialog is
  107. // closed. But the timeout has to be big enough or else it doesn't work
  108. // 1 seconds is the same as the default timeout for sending orders and so the dialog
  109. // should have appeared before the timeout... so yeah that's not ultra reliable.
  110. this.lock_screen(true);
  111. setTimeout(function(){
  112. self.lock_screen(false);
  113. }, 1000);
  114. this.print_web();
  115. } else { // proxy (xml) printing
  116. this.print_xml();
  117. this.lock_screen(false);
  118. }
  119. },
  120. click_next: function() {
  121. this.set_price(0);
  122. this.pos.proxy.reset_tare();
  123. this.gui.show_screen('presentation');
  124. },
  125. click_back: function() {
  126. // Placeholder method for ReceiptScreen extensions that
  127. // can go back ...
  128. },
  129. renderElement: function() {
  130. var self = this;
  131. this._super();
  132. this.$('.next').click(function(){
  133. if (!self._locked) {
  134. self.click_next();
  135. }
  136. });
  137. this.$('.back').click(function(){
  138. if (!self._locked) {
  139. self.click_back();
  140. }
  141. });
  142. this.$('.button.print').click(function(){
  143. if (!self._locked) {
  144. self.print();
  145. }
  146. });
  147. },
  148. render_change: function() {
  149. var self = this;
  150. this.$('.change-value').html(this.format_currency(this.pos.get_order().get_change()));
  151. var order = this.pos.get_order();
  152. var order_screen_params = order.get_screen_data('params');
  153. var button_print_invoice = this.$('h2.print_invoice');
  154. if (order_screen_params && order_screen_params.button_print_invoice) {
  155. button_print_invoice.show();
  156. } else {
  157. button_print_invoice.hide();
  158. }
  159. },
  160. render_receipt: function() {
  161. this.$('.pos-ticket-container').html(QWeb.render('PosTicketBls', this.get_receipt_render_env()));
  162. },
  163. });
  164. });