[FIX] barcodes: parse_barcode functions in PY and JS commented

This commit is contained in:
Aaron Bohy
2014-11-26 12:11:55 +01:00
committed by Frédéric van der Essen
parent 658f310223
commit dbcc93154f
2 changed files with 63 additions and 112 deletions
+17 -11
View File
@@ -88,6 +88,12 @@ class barcode_nomenclature(osv.osv):
'base_code': barcode,
'value': 0}
# 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
def match_pattern(barcode, pattern):
match = {
"value": 0,
@@ -96,24 +102,24 @@ class barcode_nomenclature(osv.osv):
}
barcode = barcode.replace("\\", "\\\\").replace("{", '\{').replace("}", "\}").replace(".", "\.")
numerical_content = re.search("[{][N]*[D]*[}]", pattern)
numerical_content = re.search("[{][N]*[D]*[}]", pattern) # look for numerical content in pattern
if numerical_content:
num_start = numerical_content.start()
num_end = numerical_content.end()
value_string = barcode[num_start:num_end-2]
if numerical_content: # the pattern encodes a numerical content
num_start = numerical_content.start() # start index of numerical content
num_end = numerical_content.end() # end index of numerical content
value_string = barcode[num_start:num_end-2] # numerical content in barcode
whole_part_match = re.search("[{][N]*[D}]", numerical_content.group())
decimal_part_match = re.search("[{N][D]*[}]", numerical_content.group())
whole_part = value_string[:whole_part_match.end()-2]
decimal_part = "0." + value_string[decimal_part_match.start():decimal_part_match.end()-1]
whole_part_match = re.search("[{][N]*[D}]", numerical_content.group()) # looks for whole part of numerical content
decimal_part_match = re.search("[{N][D]*[}]", numerical_content.group()) # looks for decimal part
whole_part = value_string[:whole_part_match.end()-2] # retrieve whole part of numerical content in barcode
decimal_part = "0." + value_string[decimal_part_match.start():decimal_part_match.end()-1] # retrieve decimal part
if whole_part == '':
whole_part = '0'
match['value'] = int(whole_part) + float(decimal_part)
match['base_code'] = barcode[:num_start] + (num_end-num_start-2)*"0" + barcode[num_end-2:]
match['base_code'] = barcode[:num_start] + (num_end-num_start-2)*"0" + barcode[num_end-2:] # replace numerical content by 0's in barcode
match['base_code'] = match['base_code'].replace("\\\\", "\\").replace("\{", "{").replace("\}","}").replace("\.",".")
pattern = pattern[:num_start] + (num_end-num_start-2)*"0" + pattern[num_end:]
pattern = pattern[:num_start] + (num_end-num_start-2)*"0" + pattern[num_end:] # replace numerical content by 0's in pattern to match
match['match'] = re.match(pattern, match['base_code'][:len(pattern)])
+46 -101
View File
@@ -4,107 +4,43 @@ openerp.barcodes = function(instance) {
instance.barcodes = {};
var module = instance.barcodes;
// The BarcodeParser is used to detect what is the category
// of a barcode (product, partner, ...) and extract an encoded value
// (like weight, price, etc.)
module.BarcodeParser = instance.web.Class.extend({
init: function(attributes) {
var self = this;
this.nomenclature_id = attributes.nomenclature_id;
this.loaded = this.load_server_data();
this.loaded = this.load();
},
// This loads the barcode nomenclature and barcode rules which are
// necessary to parse the barcodes. The BarcodeParser is operational
// only when those data have been loaded
load: function(){
var self = this;
return new instance.web.Model('barcode.nomenclature')
.query(['name','rule_ids','strict_ean'])
.filter([['id','=',this.nomenclature_id[0]]])
.first()
.then(function(nomenclature){
self.nomenclature = nomenclature;
return new instance.web.Model('barcode.rule')
.query(['name','sequence','type','encoding','pattern','alias'])
.filter([['barcode_nomenclature_id','=',self.nomenclature.id ]])
.all()
}).then(function(rules){
rules = rules.sort(function(a,b){ return a.sequence - b.sequence; });
self.nomenclature.rules = rules;
});
},
// resolves when the barcode parser is operational.
is_loaded: function() {
return self.loaded;
},
models: [
{
model: 'barcode.nomenclature',
fields: ['name','rule_ids', 'strict_ean'],
domain: function(self){ return [] },
loaded: function(self,nomenclatures){
if (self.nomenclature_id) {
for (var i = 0; i < nomenclatures.length; i++) {
if (nomenclatures[i].id === self.nomenclature_id[0]) {
self.nomenclature = nomenclatures[i];
}
}
}
self.nomenclature = self.nomenclature || null;
},
}, {
model: 'barcode.rule',
fields: ['name','sequence','type','encoding','pattern','alias'],
domain: function(self){ return [['barcode_nomenclature_id','=',self.nomenclature ? self.nomenclature.id : 0]]; },
loaded: function(self,rules){
if (self.nomenclature) {
rules = rules.sort(function(a,b){ return a.sequence - b.sequence; });
self.nomenclature.rules = rules;
}
},
},
],
// loads all the needed data on the sever. returns a deferred indicating when all the data has loaded.
load_server_data: function(){
var self = this;
var loaded = new $.Deferred();
var progress = 0;
var progress_step = 1.0 / self.models.length;
var tmp = {}; // this is used to share a temporary state between models loaders
function load_model(index){
if(index >= self.models.length){
loaded.resolve();
}else{
var model = self.models[index];
//self.pos_widget.loading_message(_t('Loading')+' '+(model.label || model.model || ''), progress);
var cond = typeof model.condition === 'function' ? model.condition(self,tmp) : true;
if (!cond) {
load_model(index+1);
return;
}
var fields = typeof model.fields === 'function' ? model.fields(self,tmp) : model.fields;
var domain = typeof model.domain === 'function' ? model.domain(self,tmp) : model.domain;
var context = typeof model.context === 'function' ? model.context(self,tmp) : model.context;
progress += progress_step;
if( model.model ){
new instance.web.Model(model.model).query(fields).filter(domain).context(context).all()
.then(function(result){
try{ // catching exceptions in model.loaded(...)
$.when(model.loaded(self,result,tmp))
.then(function(){ load_model(index + 1); },
function(err){ loaded.reject(err); });
}catch(err){
console.error(err.stack);
loaded.reject(err);
}
},function(err){
loaded.reject(err);
});
}else if( model.loaded ){
try{ // catching exceptions in model.loaded(...)
$.when(model.loaded(self,tmp))
.then( function(){ load_model(index +1); },
function(err){ loaded.reject(err); });
}catch(err){
loaded.reject(err);
}
}else{
load_model(index + 1);
}
}
}
try{
load_model(0);
}catch(err){
loaded.reject(err);
}
return loaded;
},
// returns the checksum of the ean13, or -1 if the ean has not the correct length, ean must be a string
ean_checksum: function(ean){
var code = ean.split('');
@@ -162,6 +98,12 @@ openerp.barcodes = function(instance) {
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,
@@ -170,20 +112,22 @@ openerp.barcodes = function(instance) {
};
barcode = barcode.replace("\\", "\\\\").replace("{", '\{').replace("}", "\}").replace(".", "\.");
var numerical_content = pattern.match(/[{][N]*[D]*[}]/);
var numerical_content = pattern.match(/[{][N]*[D]*[}]/); // look for numerical content in pattern
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(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++) {
@@ -192,6 +136,7 @@ openerp.barcodes = function(instance) {
}
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("\.",".");
}
@@ -236,4 +181,4 @@ openerp.barcodes = function(instance) {
return parsed_result;
},
});
}
}