- // returns the checksum of the ean, 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 true if the ean is a valid EAN codebar 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 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 an ean (string encoding an ean)
- // it will check its validity then return an object containing various
- // information about the ean.
- // most importantly :
- // - code : the ean
- // - type : the type of the ean:
- // 'price' | 'weight' | 'product' | 'cashier' | 'client' | 'discount' | 'error'
- //
- // - value : if the id encodes a numerical value, it will be put there
- // - base_code : the ean code with all the encoding parts set to zero; the one put on
- // the product in the backend
-
- parse_ean: function(ean){
- var self = this;
- var parse_result = {
- encoding: 'ean13',
- type:'error',
- code:ean,
- base_code: ean,
- value: 0,
- };