Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0] [IMP] barcodes, point_of_sale: Scanning Barcodes (Multiple Rules Apply) #574

Open
wants to merge 3 commits into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions addons/barcodes/static/src/js/barcode_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ var BarcodeParser = Class.extend({
* - base_code: the barcode with all the encoding parts set to zero; the one put on the product in the backend
*/
parse_barcode: function(barcode){
var results = [];
var parsed_result = {
encoding: '',
type:'error',
Expand Down Expand Up @@ -230,27 +231,29 @@ var BarcodeParser = Class.extend({

var match = this.match_pattern(cur_barcode, rules[i].pattern, rule.encoding);
if (match.match) {
var parsed_result_tmp = {...parsed_result};
if(rules[i].type === 'alias') {
barcode = rules[i].alias;
parsed_result.code = barcode;
parsed_result.type = 'alias';
}
else {
parsed_result.encoding = rules[i].encoding;
parsed_result.type = rules[i].type;
parsed_result.value = match.value;
parsed_result.code = cur_barcode;
parsed_result_tmp.encoding = rules[i].encoding;
parsed_result_tmp.type = rules[i].type;
parsed_result_tmp.value = match.value;
parsed_result_tmp.code = cur_barcode;
if (rules[i].encoding === "ean13"){
parsed_result.base_code = this.sanitize_ean(match.base_code);
parsed_result_tmp.base_code = this.sanitize_ean(match.base_code);
}
else{
parsed_result.base_code = match.base_code;
parsed_result_tmp.base_code = match.base_code;
}
return parsed_result;
parsed_result_tmp.rule = rules[i];
results.push(parsed_result_tmp);
}
}
}
return parsed_result;
return results || parsed_result;
},

//--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ odoo.define('point_of_sale.ProductScreen', function(require) {
});
}
await this.currentOrder.add_product(product, options);
return true;
}
_barcodeClientAction(code) {
const partner = this.env.pos.db.get_partner_by_barcode(code.code);
Expand All @@ -324,6 +325,7 @@ odoo.define('point_of_sale.ProductScreen', function(require) {
var last_orderline = this.currentOrder.get_last_orderline();
if (last_orderline) {
last_orderline.set_discount(code.value);
return true;
}
}
/**
Expand All @@ -333,6 +335,9 @@ odoo.define('point_of_sale.ProductScreen', function(require) {
*/
async _barcodeGS1Action(parsed_results) {
const productBarcode = parsed_results.find(element => element.type === 'product');
if (!productBarcode) {
return this._barcodeErrorAction(parsed_results);
}
const lotBarcode = parsed_results.find(element => element.type === 'lot');
const product = await this._getProductByBarcode(productBarcode);
if (!product) {
Expand All @@ -346,6 +351,9 @@ odoo.define('point_of_sale.ProductScreen', function(require) {
// Why? Because once we start declaring barcode actions in different
// screens, these methods will also be declared over and over.
_barcodeErrorAction(code) {
if (this.env.pos.show_barcode_error_modal === false) {
return ;
}
this.showPopup('ErrorBarcodePopup', { code: this._codeRepr(code) });
}
_codeRepr(code) {
Expand Down
34 changes: 23 additions & 11 deletions addons/point_of_sale/static/src/js/barcode_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var BarcodeReader = core.Class.extend({
init: function (attributes) {
this.mutex = new Mutex();
this.pos = attributes.pos;
this.pos.show_barcode_error_modal = true;
this.action_callbacks = {};
this.exclusive_callbacks = {};
this.proxy = attributes.proxy;
Expand Down Expand Up @@ -110,18 +111,29 @@ var BarcodeReader = core.Class.extend({
const callbacks = Object.keys(this.exclusive_callbacks).length
? this.exclusive_callbacks
: this.action_callbacks;
let parsed_result = this.barcode_parser.parse_barcode(code);
if (Array.isArray(parsed_result)) {
[...callbacks.gs1].map(cb => cb(parsed_result));
} else {
if (callbacks[parsed_result.type]) {
for (const cb of callbacks[parsed_result.type]) {
await cb(parsed_result);
let parsed_results = this.barcode_parser.parse_barcode(code);
if (! Array.isArray(parsed_results)) {
parsed_results = [parsed_results];
}
let gs1_parsed_results = parsed_results.filter((result) => !!result.rule.gs1_content_type)
if (gs1_parsed_results.length){
[...callbacks.gs1].map(cb => cb(gs1_parsed_results));
}else {
parsed_results_loop: for (const [index, parsed_result] of parsed_results.filter((result) => !result.rule.gs1_content_type).entries()) {
let is_last_index = index + 1 === parsed_results.length;
this.pos.show_barcode_error_modal = is_last_index;
if (callbacks[parsed_result.type]) {
for (const cb of callbacks[parsed_result.type]) {
let result_callback = await cb(parsed_result);
if (result_callback) {
break parsed_results_loop;
}
}
} else if (callbacks.error) {
[...callbacks.error].map(cb => cb(parsed_result));
} else {
console.warn('Ignored Barcode Scan:', parsed_result);
}
} else if (callbacks.error) {
[...callbacks.error].map(cb => cb(parsed_result));
} else {
console.warn('Ignored Barcode Scan:', parsed_result);
}
}
},
Expand Down