From c72674a01e03060a1cb7a24ec25e6bdb9daed588 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 10 Nov 2014 12:28:16 +0100 Subject: [PATCH] [IMP] barcodes,point_of_sale : parse_code js function moved to barcodes module --- addons/barcodes/__openerp__.py | 1 + .../barcodes/static/src/js/barcode_reader.js | 156 +++++++++++++++++ addons/barcodes/static/src/js/main.js | 11 ++ addons/barcodes/views/templates.xml | 11 +- addons/point_of_sale/static/src/js/devices.js | 159 +----------------- addons/point_of_sale/static/src/js/models.js | 71 ++++---- 6 files changed, 210 insertions(+), 199 deletions(-) create mode 100644 addons/barcodes/static/src/js/barcode_reader.js create mode 100644 addons/barcodes/static/src/js/main.js diff --git a/addons/barcodes/__openerp__.py b/addons/barcodes/__openerp__.py index 733f323b153..53a4773b65f 100644 --- a/addons/barcodes/__openerp__.py +++ b/addons/barcodes/__openerp__.py @@ -44,6 +44,7 @@ This module unlocks several advanced barcode features: 'data/barcodes_data.xml', 'barcodes_view.xml', 'security/ir.model.access.csv', + 'views/templates.xml', ], 'installable': True, 'auto_install': False, diff --git a/addons/barcodes/static/src/js/barcode_reader.js b/addons/barcodes/static/src/js/barcode_reader.js new file mode 100644 index 00000000000..e2ff08451db --- /dev/null +++ b/addons/barcodes/static/src/js/barcode_reader.js @@ -0,0 +1,156 @@ +function openerp_barcode_reader(instance,module){ + + module.BarcodeReader = instance.web.Class.extend({ + + // 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(''); + if(code.length !== 13){ + return -1; + } + var oddsum = 0, evensum = 0, total = 0; + code = code.reverse().splice(1); + for(var i = 0; i < code.length; i++){ + if(i % 2 == 0){ + oddsum += Number(code[i]); + }else{ + evensum += Number(code[i]); + } + } + total = oddsum * 3 + evensum; + return Number((10 - total % 10) % 10); + }, + + // 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); + + for(var n = 0, count = (13 - ean.length); n < count; n++){ + ean = ean + '0'; + } + return ean.substr(0,12) + this.ean_checksum(ean); + }, + + // attempts to interpret a barcode (string encoding a barcode Code-128) + // it will return an object containing various information about the barcode. + // most importantly : + // - code : the barcode + // - type : the type of the barcode (e.g. alias, unit product, weighted product...) + // + // - value : if the barcode encodes a numerical value, it will be put there + // - 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, nomenclature){ + var self = this; + var parsed_result = { + encoding: '', + type:'error', + code:barcode, + base_code: barcode, + value: 0, + }; + + if (!nomenclature) { + console.log(this); + console.log("nomenclature does not exist"); + 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; + } + + if(!numerical_content && p !== '*' && p !== barcode[j]){ + return false; + } + } + 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 - + diff --git a/addons/point_of_sale/static/src/js/devices.js b/addons/point_of_sale/static/src/js/devices.js index 976a9845830..23934f0d74d 100644 --- a/addons/point_of_sale/static/src/js/devices.js +++ b/addons/point_of_sale/static/src/js/devices.js @@ -3,6 +3,7 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal "use strict"; var _t = instance.web._t; + var barcode_reader_module = instance.barcode_reader; // the JobQueue schedules a sequence of 'jobs'. each job is // a function returning a deferred. the queue waits for each job to finish @@ -441,7 +442,8 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal // is set-up to act like a keyboard. Use connect() and disconnect() to activate // and deactivate the barcode reader. Use set_action_callbacks to tell it // what to do when it reads a barcode. - module.BarcodeReader = instance.web.Class.extend({ + //module.BarcodeReader = instance.web.Class.extend({ + barcode_reader_module.BarcodeReader.include({ actions:[ 'product', 'cashier', @@ -501,160 +503,9 @@ function openerp_pos_devices(instance,module){ //module is instance.point_of_sal } }, - // 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(''); - if(code.length !== 13){ - return -1; - } - var oddsum = 0, evensum = 0, total = 0; - code = code.reverse().splice(1); - for(var i = 0; i < code.length; i++){ - if(i % 2 == 0){ - oddsum += Number(code[i]); - }else{ - evensum += Number(code[i]); - } - } - total = oddsum * 3 + evensum; - return Number((10 - total % 10) % 10); - }, - - // 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); - - for(var n = 0, count = (13 - ean.length); n < count; n++){ - ean = ean + '0'; - } - return ean.substr(0,12) + this.ean_checksum(ean); - }, - - // attempts to interpret a barcode (string encoding a barcode Code-128) - // it will return an object containing various information about the barcode. - // most importantly : - // - code : the barcode - // - type : the type of the barcode: - // 'price' | 'weight' | 'product' | 'cashier' | 'client' | 'discount' | 'lot' | - // 'package' | 'location' | 'error' - // - // - value : if the barcode encodes a numerical value, it will be put there - // - 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', - code:barcode, - base_code: barcode, - value: 0, - }; - - if (!this.pos.nomenclature) { - 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; - } - - if(!numerical_content && p !== '*' && p !== barcode[j]){ - return false; - } - } - 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