From d561b36417dff6a81f9ed82b634fd44ed3c30d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Kawala?= Date: Wed, 29 Jan 2020 09:30:52 +0100 Subject: [PATCH] On single namespace. --- .../static/src/js/pos_barcode_tare.js | 62 +++++++++++-------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/pos_barcode_tare/static/src/js/pos_barcode_tare.js b/pos_barcode_tare/static/src/js/pos_barcode_tare.js index 7203d4f2..9d238944 100644 --- a/pos_barcode_tare/static/src/js/pos_barcode_tare.js +++ b/pos_barcode_tare/static/src/js/pos_barcode_tare.js @@ -1,26 +1,34 @@ odoo.define('pos_barcode_tare.screens', function (require) { "use strict"; - var screens = require('point_of_sale.screens'); - var gui = require('point_of_sale.gui'); + var chrome = require('point_of_sale.chrome'); var core = require('web.core'); - var _t = core._t; + var devices = require('point_of_sale.devices'); + var gui = require('point_of_sale.gui'); + var models = require('point_of_sale.models'); + var screens = require('point_of_sale.screens'); + var QWeb = core.qweb; + // This configures read action for tare barcode. A tare barcode contains a + // fake product ID and the weight to be subtracted from the product in the + // latest order line. screens.ScreenWidget.include( { barcode_weight_action: function (code) { var self = this; var order = this.pos.get_order(); + // Computes the paid weight var last_order_line = order.get_last_orderline(); var total_weight = last_order_line.get_quantity(); var tare = code.value; var paid_weight = total_weight - tare; - + // Throws a warning popup if the price is negative if (paid_weight <= 0) { self.gui.show_popup('confirm', {'title': _t('Negative weight'), 'body': _t('The calculated weight is negative. ' + 'Did you scan the correct tare label?'), confirm: function () { + // Operator can choose to ignore the warning. last_order_line.set_quantity(paid_weight); }}); } else { @@ -28,6 +36,7 @@ odoo.define('pos_barcode_tare.screens', function (require) { } }, + // Setup the callback action for the "weight" barcodes. show: function () { var self = this; this._super(); @@ -36,14 +45,9 @@ odoo.define('pos_barcode_tare.screens', function (require) { _.bind(self.barcode_weight_action, self)); }, }); -}); - -odoo.define('tare-screen-button.button', function (require) { - "use strict"; - var core = require('web.core'); - var screens = require('point_of_sale.screens'); - var gui = require('point_of_sale.gui'); + // This create a new button on top of action widget. This button links to + // the barcode label printing screen defined below. var TareScreenButton = screens.ActionButtonWidget.extend({ template: 'TareScreenButton', @@ -57,18 +61,12 @@ odoo.define('tare-screen-button.button', function (require) { 'name': 'tareScreenButton', 'widget': TareScreenButton, }); -}); - -odoo.define('tare-screen.screen', function (require) { - "use strict"; - var chrome = require('point_of_sale.chrome'); - var core = require('web.core'); - var devices = require('point_of_sale.devices'); - var gui = require('point_of_sale.gui'); - var models = require('point_of_sale.models'); - var screens = require('point_of_sale.screens'); - var QWeb = core.qweb; + // This is a new screen that reads weight from the electronic scale and + // create a barcode label encoding the weight. The screen shows a preview + // of the label. The user is expected to check if the preview matches what's + // measured on the scale. The barcode image is generated by the report + // module. var TareScreenWidget = screens.ScreenWidget.extend({ template: 'TareScreenWidget', next_screen: 'products', @@ -79,13 +77,14 @@ odoo.define('tare-screen.screen', function (require) { this._super(); var self = this; var queue = this.pos.proxy_queue; - + // The pooling of the scale starts here. queue.schedule(function () { return self.pos.proxy.scale_read().then(function (weight) { self.set_weight(weight.weight); }); }, {duration:150, repeat: true}); - + // Shows a barcode whose weight might be zero, but this is preferred + // for UI/UX reasons. this.render_receipt(); this.lock_screen(true); }, @@ -115,15 +114,26 @@ odoo.define('tare-screen.screen', function (require) { return checksum % 10; }, barcode_data: function (weight) { + // We use EAN13 barcode, it looks like 21 00000 12345 x. First there + // is the prefix, here 21, that is used to decide which type of + // barcode we're dealing with. A weight barcode has then two groups + // of five digits. The first group encodes the product id. Here the + // product id is 00000. The second group encodes the weight in + // grams. Here the weight is 12.345kg. The last digit of the barcode + // is a checksum, here symbolized by x. var padding_size = 5; var default_weight_prefix_id = "21"; var void_product_id = '0'.repeat(padding_size); var weight_in_gram = weight * 10e2; + // Weight has to be padded with zeros. var weight_with_padding = '0'.repeat(padding_size) + weight_in_gram; var padded_weight = weight_with_padding.substr( weight_with_padding.length - padding_size); + // Builds the barcode data (ie. all but the checksum). var barcode_data = default_weight_prefix_id.concat(void_product_id, padded_weight); + // Compute checksum and concat with barcode data to get the actual + // barcode. var checksum = this.ean13_checksum(barcode_data); var barcode = barcode_data.concat(checksum); @@ -153,10 +163,8 @@ odoo.define('tare-screen.screen', function (require) { this.pos.get_order()._printed = true; }, print: function () { - var self = this; - // See comment in print function of ReceiptScreenWidget - + var self = this; this.lock_screen(true); setTimeout(function () {