| 1 |
import { patch } from "@web/core/utils/patch"; |
| 2 |
import { BarcodeParser } from "@barcodes/js/barcode_parser"; |
| 3 |
import { _t } from "@web/core/l10n/translation"; |
| 4 |
export class GS1BarcodeError extends Error {}; |
| 5 |
|
| 6 |
export const FNC1_CHAR = String.fromCharCode(29); |
| 7 |
|
| 8 |
patch(BarcodeParser, { |
| 9 |
barcodeNomenclatureFields: [ |
| 10 |
...BarcodeParser.barcodeNomenclatureFields, |
| 11 |
"is_gs1_nomenclature", |
| 12 |
"gs1_separator_fnc1", |
| 13 |
], |
| 14 |
barcodeRuleFields: [ |
| 15 |
...BarcodeParser.barcodeRuleFields, |
| 16 |
"gs1_content_type", |
| 17 |
"gs1_decimal_usage", |
| 18 |
"associated_uom_id", |
| 19 |
], |
| 20 |
}); |
| 21 |
|
| 22 |
patch(BarcodeParser.prototype, { |
| 23 |
setup(attributes) { |
| 24 |
super.setup(...arguments); |
| 25 |
// Use the nomenclature's separaor regex, else use an impossible one. |
| 26 |
const nomenclatureSeparator = this.nomenclature && this.nomenclature.gs1_separator_fnc1; |
| 27 |
this.gs1SeparatorRegex = new RegExp(nomenclatureSeparator || '.^', 'g'); |
| 28 |
}, |
| 29 |
|
| 30 |
/** |
| 31 |
* Convert YYMMDD GS1 date into a Date object |
| 32 |
* |
| 33 |
* @param {string} gs1Date YYMMDD string date, length must be 6 |
| 34 |
* @returns {Date} |
| 35 |
*/ |
| 36 |
gs1_date_to_date(gs1Date) { |
| 37 |
// See 7.12 Determination of century in dates: |
| 38 |
// https://www.gs1.org/sites/default/files/docs/barcodes/GS1_General_Specifications.pdfDetermination of century |
| 39 |
const now = new Date(); |
| 40 |
const substractYear = parseInt(gs1Date.slice(0, 2)) - (now.getFullYear() % 100); |
| 41 |
let century = Math.floor(now.getFullYear() / 100); |
| 42 |
if (51 <= substractYear && substractYear <= 99) { |
| 43 |
century--; |
| 44 |
} else if (-99 <= substractYear && substractYear <= -50) { |
| 45 |
century++; |
| 46 |
} |
| 47 |
const year = century * 100 + parseInt(gs1Date.slice(0, 2)); |
| 48 |
const date = new Date(year, parseInt(gs1Date.slice(2, 4) - 1)); |
| 49 |
|
| 50 |
if (gs1Date.slice(-2) === '00'){ |
| 51 |
// Day is not mandatory, when not set -> last day of the month |
| 52 |
date.setDate(new Date(year, parseInt(gs1Date.slice(2, 4)), 0).getDate()); |
| 53 |
} else { |
| 54 |
date.setDate(parseInt(gs1Date.slice(-2))); |
| 55 |
} |
| 56 |
return date; |
| 57 |
}, |
| 58 |
|
| 59 |
/** |
| 60 |
* Perform interpretation of the barcode value depending of the rule.gs1_content_type |
| 61 |
* |
| 62 |
* @param {Array} match Result of a regex match with atmost 2 groups (ia and value) |
| 63 |
* @param {Object} rule Matched Barcode Rule |
| 64 |
* @returns {Object|null} |
| 65 |
*/ |
| 66 |
parse_gs1_rule_pattern(match, rule) { |
| 67 |
const result = { |
| 68 |
rule: Object.assign({}, rule), |
| 69 |
ai: match[1], |
| 70 |
string_value: match[2], |
| 71 |
code: match[2], |
| 72 |
base_code: match[2], |
| 73 |
type: rule.type |
| 74 |
}; |
| 75 |
if (rule.gs1_content_type === 'measure'){ |
| 76 |
let decimalPosition = 0; // Decimal position begin at the end, 0 means no decimal |
| 77 |
if (rule.gs1_decimal_usage){ |
| 78 |
decimalPosition = parseInt(match[1][match[1].length - 1]); |
| 79 |
} |
| 80 |
if (decimalPosition > 0) { |
| 81 |
const integral = match[2].slice(0, match[2].length - decimalPosition); |
| 82 |
const decimal = match[2].slice(match[2].length - decimalPosition); |
| 83 |
result.value = parseFloat( integral + "." + decimal); |
| 84 |
} else { |
| 85 |
result.value = parseInt(match[2]); |
| 86 |
} |
| 87 |
} else if (rule.gs1_content_type === 'identifier'){ |
| 88 |
if (parseInt(match[2][match[2].length - 1]) !== this.get_barcode_check_digit("0".repeat(18 - match[2].length) + match[2])){ |
| 89 |
throw new Error(_t("Invalid barcode: the check digit is incorrect")); |
| 90 |
// return {error: _t("Invalid barcode: the check digit is incorrect")}; |
| 91 |
} |
| 92 |
result.value = match[2]; |
| 93 |
} else if (rule.gs1_content_type === 'date'){ |
| 94 |
if (match[2].length !== 6){ |
| 95 |
throw new Error(_t("Invalid barcode: can't be formated as date")); |
| 96 |
// return {error: _t("Invalid barcode: can't be formated as date")}; |
| 97 |
} |
| 98 |
result.value = this.gs1_date_to_date(match[2]); |
| 99 |
} else { |
| 100 |
result.value = match[2]; |
| 101 |
} |
| 102 |
return result; |
| 103 |
}, |
| 104 |
|
| 105 |
/** |
| 106 |
* Try to decompose the gs1 extanded barcode into several unit of information using gs1 rules. |
| 107 |
* |
| 108 |
* @param {string} barcode |
| 109 |
* @returns {Array} Array of object |
| 110 |
*/ |
| 111 |
gs1_decompose_extended(barcode) { |
| 112 |
const results = []; |
| 113 |
const rules = this.nomenclature.rules.filter(rule => rule.encoding === 'gs1-128'); |
| 114 |
const separatorReg = `(?:${FNC1_CHAR}+)?`; |
| 115 |
barcode = this._convertGS1Separators(barcode); |
| 116 |
barcode = this.cleanBarcode(barcode); |
| 117 |
|
| 118 |
while (barcode.length > 0) { |
| 119 |
const barcodeLength = barcode.length; |
| 120 |
for (const rule of rules) { |
| 121 |
const match = barcode.match("^" + rule.pattern + separatorReg); |
| 122 |
if (match && match.length >= 3) { |
| 123 |
const res = this.parse_gs1_rule_pattern(match, rule); |
| 124 |
if (res) { |
| 125 |
barcode = barcode.slice(match.index + match[0].length); |
| 126 |
results.push(res); |
| 127 |
if (barcode.length === 0) { |
| 128 |
return results; // Barcode completly parsed, no need to keep looping. |
| 129 |
} |
| 130 |
} else { |
| 131 |
throw new GS1BarcodeError(_t("This barcode can't be parsed by any barcode rules.")); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
if (barcodeLength === barcode.length) { |
| 136 |
throw new GS1BarcodeError(_t("This barcode can't be partially or fully parsed.")); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return results; |
| 141 |
}, |
| 142 |
|
| 143 |
/** |
| 144 |
* @override |
| 145 |
* @returns {Object|Array|null} If nomenclature is GS1, returns an array or null |
| 146 |
*/ |
| 147 |
parseBarcodeNomenclature(barcode) { |
| 148 |
if (this.nomenclature && this.nomenclature.is_gs1_nomenclature) { |
| 149 |
return this.gs1_decompose_extended(barcode); |
| 150 |
} |
| 151 |
return super.parseBarcodeNomenclature(...arguments); |
| 152 |
}, |
| 153 |
|
| 154 |
/** |
| 155 |
* Makes all needed operations to clean and prepare the barcode. |
| 156 |
* @param {string} barcode |
| 157 |
* @returns {string} |
| 158 |
*/ |
| 159 |
cleanBarcode(barcode) { |
| 160 |
if (barcode[0] === FNC1_CHAR) { |
| 161 |
// If first character is the separator, remove it to be able to parse the barcode. |
| 162 |
barcode = barcode.slice(1); |
| 163 |
} |
| 164 |
return barcode; |
| 165 |
}, |
| 166 |
|
| 167 |
/** |
| 168 |
* The FNC1 is the default GS1 separator character, but through the field `gs1_separator_fnc1`, |
| 169 |
* the user has the possibility to define one or multiple characters to use as separator as |
| 170 |
* a regex. This method replaces all of the matches in the given barcode by the FNC1. |
| 171 |
* |
| 172 |
* @param {string} barcode |
| 173 |
* @returns {string} |
| 174 |
*/ |
| 175 |
_convertGS1Separators: function (barcode) { |
| 176 |
barcode = barcode.replace(this.gs1SeparatorRegex, FNC1_CHAR); |
| 177 |
return barcode; |
| 178 |
}, |
| 179 |
}); |