diff --git a/stores/mpreis.js b/stores/mpreis.js index 692c34d..64db849 100644 --- a/stores/mpreis.js +++ b/stores/mpreis.js @@ -2,22 +2,41 @@ const axios = require("axios"); const utils = require("./utils"); const conversions = { - 'CM': { unit: 'cm', factor: 1 }, - 'DAG': { unit: 'g', factor: 10 }, - 'DL': { unit: 'ml', factor: 10 }, - 'GRM': { unit: 'g', factor: 1 }, - 'H87': { unit: 'ml', factor: 1000 }, - 'HLT': { unit: 'ml', factor: 1000 }, - 'KGM': { unit: 'g', factor: 1000 }, - 'LTR': { unit: 'ml', factor: 1000 }, - 'MLT': { unit: 'ml', factor: 1 }, - 'MTR': { unit: 'm', factor: 1 }, - 'XRO': { unit: 'stk', factor: 1 }, + 'cm': { unit: 'cm', factor: 1 }, + 'dag': { unit: 'g', factor: 10 }, + 'dl': { unit: 'ml', factor: 10 }, + 'grm': { unit: 'g', factor: 1 }, + 'kgm': { unit: 'g', factor: 1000 }, + 'ltr': { unit: 'ml', factor: 1000 }, + 'mlt': { unit: 'ml', factor: 1 }, + 'mtr': { unit: 'm', factor: 1 }, + 'stk': { unit: 'stk', factor: 1 }, + 'stk.': { unit: 'stk', factor: 1 }, + 'g': { unit: 'g', factor: 1 }, + 'anw': { unit: 'stk', factor: 1 }, + 'l': { unit: 'ml', factor: 1000 }, + 'm': { unit: 'cm', factor: 100 }, + 'ml': { unit: 'ml', factor: 1 }, + 'kg': { unit: 'g', factor: 1000 }, + 'paar': { unit: 'stk', factor: 1 }, + 'stück': { unit: 'stk', factor: 1 }, + 'bl.': { unit: 'stk', factor: 1 }, + 'pkg': { unit: 'stk', factor: 1 }, + 'gr': { unit: 'g', factor: 1 }, + 'er': { unit: 'stk', factor: 1 }, }; exports.getCanonical = function(item, today) { let quantity = item.prices[0].presentationPrice.measurementUnit.quantity - let unit = item.prices[0].presentationPrice.measurementUnit.unitCode + let unit = item.prices[0].presentationPrice.measurementUnit.unitCode.toLowerCase() + if(['xro', 'h87', 'hlt'].indexOf(unit)!=-1) { + const q = utils.parseUnitAndQuantityAtEnd(item.mixins.productCustomAttributes.packagingUnit) + quantity = q[0] ?? quantity; + unit = q[1]; + } + if (!(unit in conversions)) { + unit = 'stk'; + } const isWeighted = (item.mixins.productCustomAttributes?.packagingDescription ?? "").startsWith("Gewichtsware"); return utils.convertUnit({ id: item.code, diff --git a/stores/utils.js b/stores/utils.js index a3bca28..39797fc 100644 --- a/stores/utils.js +++ b/stores/utils.js @@ -1,7 +1,7 @@ exports.convertUnit = function (item, units, store) { if(!(item.unit in units)) { - console.error(`Unknown unit in ${store}: '${item.unit}'`); + console.error(`Unknown unit in ${store}: '${item.unit}' in item ${item.name}`); return item; }