From dbcc93154f6c5bf3b323ad9c776612e7db7bdf3d Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 14 Nov 2014 17:51:33 +0100 Subject: [FIX] barcodes: parse_barcode functions in PY and JS commented --- addons/barcodes/static/src/js/barcode_parser.js | 145 ++++++++---------------- 1 file changed, 45 insertions(+), 100 deletions(-) (limited to 'addons/barcodes/static/src/js/barcode_parser.js') diff --git a/addons/barcodes/static/src/js/barcode_parser.js b/addons/barcodes/static/src/js/barcode_parser.js index 3d9a5eb2907..3ce07be1755 100644 --- a/addons/barcodes/static/src/js/barcode_parser.js +++ b/addons/barcodes/static/src/js/barcode_parser.js @@ -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(); }, - 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) { + // 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; - } - }, + }); }, - ], - - // 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; + // resolves when the barcode parser is operational. + is_loaded: function() { + return self.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; }, }); -} \ No newline at end of file +} -- cgit v1.2.3