summaryrefslogtreecommitdiff
path: root/addons/barcodes/static/src
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2014-11-14 12:39:31 +0100
committerFrédéric van der Essen <fvdessen@gmail.com>2014-11-26 12:11:51 +0100
commitb671824048969f2c42806076f96377f3e36ca2a1 (patch)
tree7005a331b2c6c7b438a09451149e97042115fe67 /addons/barcodes/static/src
parentf2500133290fdc2bb2f2e12f9ad435b67156e7ba (diff)
[FIX] barcodes, stock, point_of_sale: parse_barcode function uses regex, pattern and barcodes may now contain special characters \ { and ., special character * replaced by .
Diffstat (limited to 'addons/barcodes/static/src')
-rw-r--r--addons/barcodes/static/src/js/barcode_parser.js114
1 files changed, 40 insertions, 74 deletions
diff --git a/addons/barcodes/static/src/js/barcode_parser.js b/addons/barcodes/static/src/js/barcode_parser.js
index 18823d645de..3d9a5eb2907 100644
--- a/addons/barcodes/static/src/js/barcode_parser.js
+++ b/addons/barcodes/static/src/js/barcode_parser.js
@@ -162,81 +162,41 @@ openerp.barcodes = function(instance) {
return parsed_result;
}
- function match_pattern(barcode,pattern){
- if(barcode.length < pattern.replace(/[{}]/g, '').length){
- return false; // Match of this pattern is impossible
- }
- var numerical_content = false; // Used to detect when we are between { }
- for(var i = 0, j = 0; i < pattern.length; i++, j++){
- var p = pattern[i];
- if(p === "{" || p === "}"){
- numerical_content = !numerical_content;
- j--;
- continue;
+ function match_pattern(barcode, pattern){
+ var match = {
+ value: 0,
+ base_code: barcode,
+ match: false,
+ };
+ barcode = barcode.replace("\\", "\\\\").replace("{", '\{').replace("}", "\}").replace(".", "\.");
+
+ var numerical_content = pattern.match(/[{][N]*[D]*[}]/);
+ var base_pattern = pattern;
+ if(numerical_content){
+ var num_start = numerical_content.index;
+ var num_length = numerical_content[0].length;
+ var value_string = barcode.substr(num_start, num_length-2);
+ var whole_part_match = numerical_content[0].match("[{][N]*[D}]");
+ var decimal_part_match = numerical_content[0].match("[{N][D]*[}]");
+ var whole_part = value_string.substr(0, whole_part_match.index+whole_part_match[0].length-2);
+ var decimal_part = "0." + value_string.substr(decimal_part_match.index, decimal_part_match[0].length-1);
+ if (whole_part === ''){
+ whole_part = '0';
}
-
- if(!numerical_content && p !== '*' && p !== barcode[j]){
- return false;
+ match['value'] = parseInt(whole_part) + parseFloat(decimal_part);
+ match['base_code'] = barcode.substr(0,num_start);
+ var base_pattern = pattern.substr(0,num_start);
+ for(var i=0;i<(num_length-2);i++) {
+ match['base_code'] += "0";
+ base_pattern += "0";
}
+ match['base_code'] += barcode.substr(num_start+num_length-2,barcode.length-1);
+ base_pattern += pattern.substr(num_start+num_length,pattern.length-1);
+ match['base_code'] = match['base_code'].replace("\\\\", "\\").replace("\{", "{").replace("\}","}").replace("\.",".");
}
- return true;
- }
-
- function get_value(barcode,pattern){
- var value = 0;
- var decimals = 0;
- var numerical_content = false;
- for(var i = 0, j = 0; i < pattern.length; i++, j++){
- var p = pattern[i];
- if(!numerical_content && p !== "{"){
- continue;
- }
- else if(p === "{"){
- numerical_content = true;
- j--;
- continue;
- }
- else if(p === "}"){
- break;
- }
- var v = parseInt(barcode[j]);
- if(p === 'N'){
- value *= 10;
- value += v;
- }else if(p === 'D'){ // FIXME precision ....
- decimals += 1;
- value += v * Math.pow(10,-decimals);
- }
- }
- return value;
- }
-
- function get_basecode(barcode,pattern,encoding){
- var base = '';
- var numerical_content = false;
- for(var i = 0, j = 0; i < pattern.length; i++, j++){
- var p = pattern[i];
- if(p === "{" || p === "}"){
- numerical_content = !numerical_content;
- j--;
- continue;
- }
-
- if(numerical_content){
- base += '0';
- }
- else{
- base += barcode[j];
- }
- }
- for(i=j; i<barcode.length; i++){ // Read the rest of the barcode
- base += barcode[i];
- }
- if(encoding === "ean13"){
- base = self.sanitize_ean(base);
- }
- return base;
+ match['match'] = match['base_code'].substr(0,base_pattern.length).match(base_pattern);
+ return match;
}
// If the nomenclature does not use strict EAN, prepend the barcode with a 0 if it seems
@@ -251,7 +211,8 @@ openerp.barcodes = function(instance) {
if (prepend_zero && rules[i].encoding == 'ean13'){
cur_barcode = '0'+cur_barcode;
}
- if (match_pattern(cur_barcode,rules[i].pattern)) {
+ var match = match_pattern(cur_barcode,rules[i].pattern);
+ if (match.match) {
if(rules[i].type === 'alias') {
barcode = rules[i].alias;
parsed_result.code = barcode;
@@ -260,9 +221,14 @@ openerp.barcodes = function(instance) {
else {
parsed_result.encoding = rules[i].encoding;
parsed_result.type = rules[i].type;
- parsed_result.value = get_value(cur_barcode,rules[i].pattern);
+ parsed_result.value = match.value
parsed_result.code = cur_barcode;
- parsed_result.base_code = get_basecode(cur_barcode,rules[i].pattern,parsed_result.encoding);
+ if (rules[i].encoding === "ean13"){
+ parsed_result.base_code = this.sanitize_ean(match.base_code);
+ }
+ else{
+ parsed_result.base_code = match.base_code;
+ }
return parsed_result;
}
}