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.
 
 
 
 

199 lines
7.7 KiB

odoo.define('pos_barcode_tare.screens', function (require) {
"use strict";
var core = require('web.core');
var gui = require('point_of_sale.gui');
var models = require('point_of_sale.models');
var screens = require('point_of_sale.screens');
var tools = require('pos_tare.tools');
var QWeb = core.qweb;
var _t = core._t;
var convert_mass = tools.convert_mass;
var format_tare = tools.format_tare;
// 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',
button_click: function () {
this.gui.show_screen('tare');
},
});
screens.define_action_button({
'name': 'tareScreenButton',
'widget': TareScreenButton,
});
// 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',
previous_screen: 'products',
default_tare_value: 0.0,
weight_barcode_prefix: null,
show: function () {
this._super();
// Fetch the barcode prefix from POS barcode parser rules.
this.weight_barcode_prefix = this.get_barcode_prefix();
// Setup the proxy
var queue = this.pos.proxy_queue;
// The pooling of the scale starts here.
var self = this;
queue.schedule(function () {
return self.pos.proxy.scale_read().then(function (weight) {
try {
self.set_tare_weight(weight);
} catch (error) {
var title = _t("Failed to read weight from scale.");
var popup = {title: title, body: error.message};
self.gui.show_popup('error', popup);
}
});
}, {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);
},
get_barcode_prefix: function () {
var barcode_pattern = this.get_barcode_pattern();
return barcode_pattern.substr(0, 2);
},
get_barcode_pattern: function () {
var rules = this.get_barcode_rules();
var rule = rules.filter(
function (r) {
// We select the first (smallest sequence ID) barcode rule
// with the expected type.
return r.type === "tare";
})[0];
return rule.pattern;
},
get_barcode_rules: function () {
return this.pos.barcode_reader.barcode_parser.nomenclature.rules;
},
set_tare_weight: function (scale_measure) {
var weight = scale_measure.weight;
var measure_unit = this.pos.units.filter(
function (u) {
return u.name === scale_measure.unit;
})[0];
if (typeof measure_unit === 'undefined') {
throw new Error(_.str.sprintf(
_t("The scale sent a measure in %s unit. This unit of "+
"measure (UOM) in not found in the point of sale. You " +
"may need to create a new UOM named %s. The UOM name is "+
"case sensitive."), scale_measure.unit,
scale_measure.unit));
}
if (weight > 0) {
var tare_uom = this.pos.config.iface_tare_uom_id[0];
var tare_unit = this.pos.units_by_id[tare_uom];
this.weight_in_tare_unit = convert_mass(weight,
measure_unit, tare_unit);
this.render_receipt();
this.lock_screen(false);
}
},
get_tare_weight: function () {
if (typeof this.weight_in_tare_unit === 'undefined') {
return this.default_tare_value;
}
return this.weight_in_tare_unit;
},
barcode_data: function (weight) {
// We use EAN13 barcode, it looks like 07 00000 12345 x. First there
// is the prefix, here 07, 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 void_product_id = '0'.repeat(padding_size);
var weight_in_gram = weight * 1e3;
if (weight_in_gram >= Math.pow(10, padding_size)) {
throw new RangeError(_t("Maximum tare weight is 99.999kg"));
}
// 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 using a placeholder checksum.
var barcode = this.weight_barcode_prefix
.concat(void_product_id, padded_weight)
.concat(0);
// Compute checksum
var barcode_parser = this.pos.barcode_reader.barcode_parser;
var checksum = barcode_parser.ean_checksum(barcode);
// Replace checksum placeholder by the actual checksum.
return barcode.substr(0, 12).concat(checksum);
},
get_barcode_data: function () {
return this.barcode_data(this.get_tare_weight());
},
lock_screen: function (locked) {
this._locked = locked;
if (locked) {
this.$('.print-label').addClass('disabled');
} else {
this.$('.print-label').removeClass('disabled');
}
},
print_web: function () {
window.print();
this.pos.get_order()._printed = true;
},
print: function () {
// See comment in print function of ReceiptScreenWidget
this.lock_screen(true);
var self = this;
setTimeout(function () {
self.lock_screen(false);
}, 1000);
this.print_web();
this.click_back();
},
click_back: function () {
this.close();
this.gui.show_screen(this.previous_screen);
},
renderElement: function () {
this._super();
var self = this;
this.$('.back').click(function () {
self.click_back();
});
this.$('.print-label').click(function () {
if (!self._locked) {
self.print();
}
});
},
render_receipt: function () {
this.$('.pos-tare-label-container').html(
QWeb.render('PosTareLabel', {widget:this}));
},
close: function () {
this._super();
delete this.weight;
this.pos.proxy_queue.clear();
},
});
gui.define_screen({name:'tare', widget: TareScreenWidget});
});