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
7.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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_url: function () {
  39. var transaction = this.gui.get_current_screen_param('transaction');
  40. var num_ean13 = this.pos.config.prefix + transaction.ean13.slice(2);
  41. var url_barcode = "/report/barcode/?type=EAN13&value=" + num_ean13 + "&width=600&height=150"
  42. return url_barcode;
  43. },
  44. should_auto_print: function() {
  45. return this.pos.config.is_ticket_print_auto && !this.pos.get_order()._printed;
  46. },
  47. lock_screen: function(locked) {
  48. this._locked = locked;
  49. if (locked) {
  50. this.$('.next').removeClass('highlight');
  51. } else {
  52. this.$('.next').addClass('highlight');
  53. }
  54. },
  55. get_receipt_render_env: function() {
  56. var order = this.pos.get_order();
  57. var transaction = this.gui.get_current_screen_param('transaction');
  58. var num_ean13 = this.pos.config.prefix + transaction.ean13.slice(2)
  59. var width_pix = (Math.round(this.pos.config.page_width * 3.78)).toString() + "px";
  60. var url_barcode = "/report/barcode/?type=EAN13&value=" + num_ean13 + "&width=600&height=150"
  61. return {
  62. widget: this,
  63. pos: this.pos,
  64. order: order,
  65. transaction: transaction,
  66. num_ean13: num_ean13,
  67. width: width_pix,
  68. url_barcode: url_barcode,
  69. };
  70. },
  71. print_web: function() {
  72. if ($.browser.safari) {
  73. document.execCommand('print', false, null);
  74. } else {
  75. try {
  76. window.print();
  77. } catch(err) {
  78. if (navigator.userAgent.toLowerCase().indexOf("android") > -1) {
  79. this.gui.show_popup('error',{
  80. 'title':_t('Printing is not supported on some android browsers'),
  81. '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.'),
  82. });
  83. } else {
  84. throw err;
  85. }
  86. }
  87. }
  88. this.pos.get_order()._printed = true;
  89. },
  90. print_xml: function() {
  91. var receipt = QWeb.render('XmlReceiptBls', this.get_receipt_render_env());
  92. this.pos.proxy.print_receipt(receipt);
  93. this.pos.get_order()._printed = true;
  94. },
  95. print: function() {
  96. var self = this;
  97. if (!this.pos.config.is_ticket_print_via_proxy) { // browser (html) printing
  98. // The problem is that in chrome the print() is asynchronous and doesn't
  99. // execute until all rpc are finished. So it conflicts with the rpc used
  100. // to send the orders to the backend, and the user is able to go to the next
  101. // screen before the printing dialog is opened. The problem is that what's
  102. // printed is whatever is in the page when the dialog is opened and not when it's called,
  103. // and so you end up printing the product list instead of the receipt...
  104. //
  105. // Fixing this would need a re-architecturing
  106. // of the code to postpone sending of orders after printing.
  107. //
  108. // But since the print dialog also blocks the other asynchronous calls, the
  109. // button enabling in the setTimeout() is blocked until the printing dialog is
  110. // closed. But the timeout has to be big enough or else it doesn't work
  111. // 1 seconds is the same as the default timeout for sending orders and so the dialog
  112. // should have appeared before the timeout... so yeah that's not ultra reliable.
  113. this.lock_screen(true);
  114. setTimeout(function(){
  115. self.lock_screen(false);
  116. }, 1000);
  117. this.print_web();
  118. } else { // proxy (xml) printing
  119. this.print_xml();
  120. this.lock_screen(false);
  121. }
  122. },
  123. click_next: function() {
  124. this.set_price(0);
  125. this.pos.proxy.reset_tare();
  126. this.gui.show_screen('presentation');
  127. },
  128. click_back: function() {
  129. // Placeholder method for ReceiptScreen extensions that
  130. // can go back ...
  131. },
  132. renderElement: function() {
  133. var self = this;
  134. this._super();
  135. this.$('.next').click(function(){
  136. if (!self._locked) {
  137. self.click_next();
  138. }
  139. });
  140. this.$('.back').click(function(){
  141. if (!self._locked) {
  142. self.click_back();
  143. }
  144. });
  145. this.$('.button.print').click(function(){
  146. if (!self._locked) {
  147. self.print();
  148. }
  149. });
  150. },
  151. render_change: function() {
  152. var self = this;
  153. this.$('.change-value').html(this.format_currency(this.pos.get_order().get_change()));
  154. var order = this.pos.get_order();
  155. var order_screen_params = order.get_screen_data('params');
  156. var button_print_invoice = this.$('h2.print_invoice');
  157. if (order_screen_params && order_screen_params.button_print_invoice) {
  158. button_print_invoice.show();
  159. } else {
  160. button_print_invoice.hide();
  161. }
  162. },
  163. render_receipt: function() {
  164. this.$('.pos-ticket-container').html(QWeb.render('PosTicketBls', this.get_receipt_render_env()));
  165. },
  166. });
  167. });