summaryrefslogtreecommitdiff
path: root/addons/barcodes/static/src/js/barcode_parser.js
diff options
context:
space:
mode:
Diffstat (limited to 'addons/barcodes/static/src/js/barcode_parser.js')
-rw-r--r--addons/barcodes/static/src/js/barcode_parser.js163
1 files changed, 104 insertions, 59 deletions
diff --git a/addons/barcodes/static/src/js/barcode_parser.js b/addons/barcodes/static/src/js/barcode_parser.js
index 3ce07be1755..13052288f8f 100644
--- a/addons/barcodes/static/src/js/barcode_parser.js
+++ b/addons/barcodes/static/src/js/barcode_parser.js
@@ -20,7 +20,7 @@ openerp.barcodes = function(instance) {
load: function(){
var self = this;
return new instance.web.Model('barcode.nomenclature')
- .query(['name','rule_ids','strict_ean'])
+ .query(['name','rule_ids','upc_ean_conv'])
.filter([['id','=',this.nomenclature_id[0]]])
.first()
.then(function(nomenclature){
@@ -60,12 +60,44 @@ openerp.barcodes = function(instance) {
return Number((10 - total % 10) % 10);
},
+ // returns the checksum of the ean8, or -1 if the ean has not the correct length, ean must be a string
+ ean8_checskum: function(ean){
+ var code = ean.split('');
+ if (code.length !== 8) {
+ return -1;
+ }
+ var sum1 = Number(code[1]) + Number(code[3]) + Number(code[5]);
+ var sum2 = Number(code[0]) + Number(code[2]) + Number(code[4]) + Number(code[6]);
+ var total = sum1 + 3 * sum2;
+ return Number((10 - total % 10) % 10);
+ },
+
+
// returns true if the ean is a valid EAN barcode number by checking the control digit.
// ean must be a string
check_ean: function(ean){
return /^\d+$/.test(ean) && this.ean_checksum(ean) === Number(ean[ean.length-1]);
},
+ // returns true if the barcode string is encoded with the provided encoding.
+ check_encoding: function(barcode, encoding) {
+ var len = barcode.length;
+ var allnum = /^\d+$/.test(barcode);
+ var check = Number(barcode[len-1]);
+
+ if (encoding === 'ean13') {
+ return len === 13 && allnum && this.ean_checksum(barcode) === check;
+ } else if (encoding === 'ean8') {
+ return len === 8 && allnum && this.ean8_checksum(barcode) === check;
+ } else if (encoding === 'upca') {
+ return len === 12 && allnum && this.ean_checksum('0'+barcode) === check;
+ } else if (encoding === 'any') {
+ return true;
+ } else {
+ return false;
+ }
+ },
+
// returns a valid zero padded ean13 from an ean prefix. the ean prefix must be a string.
sanitize_ean: function(ean){
ean = ean.substr(0,13);
@@ -75,6 +107,57 @@ openerp.barcodes = function(instance) {
}
return ean.substr(0,12) + this.ean_checksum(ean);
},
+
+ // Returns a valid zero padded UPC-A from a UPC-A prefix. the UPC-A prefix must be a string.
+ sanitize_upc: function(upc) {
+ return this.sanitize_ean('0'+upc).substr(1,12);
+ },
+
+ // Checks if barcode matches the pattern
+ // Additionnaly retrieves the optional numerical content in barcode
+ // Returns an object containing:
+ // - value: the numerical value encoded in the barcode (0 if no value encoded)
+ // - base_code: the barcode in which numerical content is replaced by 0's
+ // - match: boolean
+ match_pattern: function (barcode, pattern){
+ var match = {
+ value: 0,
+ base_code: barcode,
+ match: false,
+ };
+ barcode = barcode.replace("\\", "\\\\").replace("{", '\{').replace("}", "\}").replace(".", "\.");
+
+ var numerical_content = pattern.match(/[{][N]*[D]*[}]/); // look for numerical content in pattern
+ var base_pattern = pattern;
+ if(numerical_content){ // the pattern encodes a numerical content
+ var num_start = numerical_content.index; // start index of numerical content
+ var num_length = numerical_content[0].length; // length of numerical content
+ var value_string = barcode.substr(num_start, num_length-2); // numerical content in barcode
+ var whole_part_match = numerical_content[0].match("[{][N]*[D}]"); // looks for whole part of numerical content
+ var decimal_part_match = numerical_content[0].match("[{N][D]*[}]"); // looks for decimal part
+ var whole_part = value_string.substr(0, whole_part_match.index+whole_part_match[0].length-2); // retrieve whole part of numerical content in barcode
+ var decimal_part = "0." + value_string.substr(decimal_part_match.index, decimal_part_match[0].length-1); // retrieve decimal part
+ if (whole_part === ''){
+ whole_part = '0';
+ }
+ match['value'] = parseInt(whole_part) + parseFloat(decimal_part);
+
+ // replace numerical content by 0's in barcode and pattern
+ 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("\.",".");
+ }
+
+ match['match'] = match['base_code'].substr(0,base_pattern.length).match(base_pattern);
+ return match;
+ },
// attempts to interpret a barcode (string encoding a barcode Code-128)
// it will return an object containing various information about the barcode.
@@ -86,7 +169,6 @@ openerp.barcodes = function(instance) {
// - 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 self = this;
var parsed_result = {
encoding: '',
type:'error',
@@ -94,69 +176,32 @@ openerp.barcodes = function(instance) {
base_code: barcode,
value: 0,
};
- if (!self.nomenclature) {
- return parsed_result;
- }
-
- // Checks if barcode matches the pattern
- // Additionnaly retrieves the optional numerical content in barcode
- // Returns an object containing:
- // - value: the numerical value encoded in the barcode (0 if no value encoded)
- // - base_code: the barcode in which numerical content is replaced by 0's
- // - match: boolean
- 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]*[}]/); // look for numerical content in pattern
- var base_pattern = pattern;
- if(numerical_content){ // the pattern encodes a numerical content
- var num_start = numerical_content.index; // start index of numerical content
- var num_length = numerical_content[0].length; // length of numerical content
- var value_string = barcode.substr(num_start, num_length-2); // numerical content in barcode
- var whole_part_match = numerical_content[0].match("[{][N]*[D}]"); // looks for whole part of numerical content
- var decimal_part_match = numerical_content[0].match("[{N][D]*[}]"); // looks for decimal part
- var whole_part = value_string.substr(0, whole_part_match.index+whole_part_match[0].length-2); // retrieve whole part of numerical content in barcode
- var decimal_part = "0." + value_string.substr(decimal_part_match.index, decimal_part_match[0].length-1); // retrieve decimal part
- if (whole_part === ''){
- whole_part = '0';
- }
- match['value'] = parseInt(whole_part) + parseFloat(decimal_part);
-
- // replace numerical content by 0's in barcode and pattern
- 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("\.",".");
- }
- match['match'] = match['base_code'].substr(0,base_pattern.length).match(base_pattern);
- return match;
+ if (!this.nomenclature) {
+ return parsed_result;
}
- // If the nomenclature does not use strict EAN, prepend the barcode with a 0 if it seems
- // that it has been striped by the barcode scanner, when trying to match an EAN13 rule
- var prepend_zero = false;
- if(!self.strict_ean && barcode.length === 12 && self.check_ean("0"+barcode)){
- prepend_zero = true;
- }
- var rules = self.nomenclature.rules;
+ var rules = this.nomenclature.rules;
for (var i = 0; i < rules.length; i++) {
+ var rule = rules[i];
var cur_barcode = barcode;
- if (prepend_zero && rules[i].encoding == 'ean13'){
- cur_barcode = '0'+cur_barcode;
+
+ if ( rule.encoding === 'ean13' &&
+ this.check_encoding(barcode,'upca') &&
+ this.nomenclature.upc_ean_conv in {'upc2ean':'','always':''} ){
+ cur_barcode = '0' + cur_barcode;
+ } else if (rule.encoding === 'upca' &&
+ this.check_encoding(barcode,'ean13') &&
+ barcode[0] === '0' &&
+ this.upc_ean_conv in {'ean2upc':'','always':''} ){
+ cur_barcode = cur_barcode.substr(1,12);
+ }
+
+ if (!this.check_encoding(cur_barcode,rule.encoding)) {
+ continue;
}
- var match = match_pattern(cur_barcode,rules[i].pattern);
+
+ var match = this.match_pattern(cur_barcode,rules[i].pattern);
if (match.match) {
if(rules[i].type === 'alias') {
barcode = rules[i].alias;