diff --git a/README.md b/README.md index 5883f27..e52e024 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,9 @@ produkt. Tożsamość źródłowa ma postać identyfikator kombinacji wynosi `0`. Przy kolejnym imporcie zgodny rekord ze starszym, numerycznym `sourceId` jest bezpiecznie migrowany po SKU. Dane wariantu obejmują jego referencję, EAN, lokalizację magazynową, zdjęcie i wagę produktu -powiększoną o wpływ wagi kombinacji. +powiększoną o wpływ wagi kombinacji. Cena bazowa netto katalogu jest pobierana +z ceny produktu PrestaShop powiększonej o wpływ ceny kombinacji; cena pozycji +zamówienia pozostaje niezależną ceną transakcyjną i nie nadpisuje katalogu. Podczas importu zamówienia moduł zapisuje publiczne wiadomości klienta jako `customer_message`. Obsługuje standardowe zasoby PrestaShop: `messages` po diff --git a/module.json b/module.json index 70b0fbe..43ff9f7 100644 --- a/module.json +++ b/module.json @@ -1,6 +1,6 @@ { "name": "core/prestashop", - "version": "1.1.11", + "version": "1.1.12", "singleton": false, "dependsOn": [ "core/orders", diff --git a/package-lock.json b/package-lock.json index e06cc2d..7eda898 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "core.prestashop", - "version": "1.1.11", + "version": "1.1.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "core.prestashop", - "version": "1.1.11", + "version": "1.1.12", "license": "ISC", "dependencies": { "@orbito/api-lib": "^3.3.3", diff --git a/package.json b/package.json index a2fe764..a6d93c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "core.prestashop", - "version": "1.1.11", + "version": "1.1.12", "description": "Integracja zamówień i płatności PrestaShop", "main": "index.js", "scripts": { diff --git a/src/OrderImporter.js b/src/OrderImporter.js index d7052f1..8526df3 100644 --- a/src/OrderImporter.js +++ b/src/OrderImporter.js @@ -122,12 +122,17 @@ async function importFromPSOrder(orderId, api, config) { } catch (_) { /* no image available */ } try { - Object.assign(p, await resolveProductMetadata(api, psProduct, { + const metadata = await resolveProductMetadata(api, psProduct, { sourceId: productId, sku: p.sku, combination: productCombination, requiresCombination: combinationId !== '0', - })); + }); + if (Object.hasOwn(metadata, 'unitPrice')) { + p.baseUnitPrice = metadata.unitPrice; + delete metadata.unitPrice; + } + Object.assign(p, metadata); } catch (_) { /* optional product metadata must not block order import */ } if (psProduct.type == "pack" && psProduct.associations.product_bundle) { @@ -157,12 +162,17 @@ async function importFromPSOrder(orderId, api, config) { if (packWeight !== undefined) pData.weight = packWeight; try { - Object.assign(pData, await resolveProductMetadata(api, psItem, { + const metadata = await resolveProductMetadata(api, psItem, { sourceId: packProductId, sku: pData.sku, combination: packCombination, requiresCombination: packCombinationId !== '0', - })); + }); + if (Object.hasOwn(metadata, 'unitPrice')) { + pData.baseUnitPrice = metadata.unitPrice; + delete metadata.unitPrice; + } + Object.assign(pData, metadata); } catch (_) { /* optional product metadata must not block order import */ } try { @@ -188,7 +198,10 @@ async function importFromPSOrder(orderId, api, config) { sku: product.sku || '', name: product.name, }; - for (const field of ['unitPrice', 'tax', 'weight', 'ean', 'coverImageUrl', 'brand', 'warehouseLocation']) { + if (Object.hasOwn(product, 'baseUnitPrice') && product.baseUnitPrice !== undefined) { + upsert.unitPrice = product.baseUnitPrice; + } + for (const field of ['tax', 'weight', 'ean', 'coverImageUrl', 'brand', 'warehouseLocation']) { if (Object.hasOwn(product, field) && product[field] !== undefined) upsert[field] = product[field]; } await Core.callModule('core/products', 'upsert', upsert).catch(error => { diff --git a/src/product-metadata.js b/src/product-metadata.js index cce631b..35a4615 100644 --- a/src/product-metadata.js +++ b/src/product-metadata.js @@ -55,6 +55,20 @@ function ownMetadataField(record, field) { return metadataText(record[field]); } +function resolveBaseUnitPrice(product, combination) { + if (!product || !Object.hasOwn(product, 'price')) return undefined; + const basePrice = Number.parseFloat(String(product.price).replace(',', '.')); + if (!Number.isFinite(basePrice) || basePrice < 0) return undefined; + + let impact = 0; + if (combination && Object.hasOwn(combination, 'price')) { + impact = Number.parseFloat(String(combination.price).replace(',', '.')); + if (!Number.isFinite(impact)) return undefined; + } + const unitPrice = basePrice + impact; + return Number.isFinite(unitPrice) && unitPrice >= 0 ? unitPrice : undefined; +} + async function resolveBrand(api, product) { const inlineName = ownMetadataField(product, 'manufacturer_name'); if (inlineName !== undefined) return inlineName; @@ -145,6 +159,8 @@ async function resolveProductMetadata(api, product, { } const metadata = {}; + const unitPrice = resolveBaseUnitPrice(product, combination); + if (unitPrice !== undefined) metadata.unitPrice = unitPrice; try { const brand = await resolveBrand(api, product); if (brand !== undefined) metadata.brand = brand; @@ -212,6 +228,7 @@ module.exports = { buildProductSourceId, parseProductSourceId, normalizeSku, + resolveBaseUnitPrice, chooseStockLocation, resolveProductMetadata, findMetadataBySourceAndSku, diff --git a/test/order-importer-metadata.test.js b/test/order-importer-metadata.test.js index c082a22..2f1c988 100644 --- a/test/order-importer-metadata.test.js +++ b/test/order-importer-metadata.test.js @@ -21,6 +21,7 @@ function loadImporter(Core, PsOrder) { test('order import upserts verified metadata for the ordered product and pack items', async () => { const upserts = []; + let createdOrder = null; const Core = { getModuleInfo: () => ({ code: 'core/prestashop' }), callModule: async (module, method, params) => { @@ -29,7 +30,10 @@ test('order import upserts verified metadata for the ordered product and pack it upserts.push(params); return {}; } - if (module === 'core/orders' && method === 'create') return { _id: 'order-1' }; + if (module === 'core/orders' && method === 'create') { + createdOrder = params; + return { _id: 'order-1' }; + } throw new Error(`Unexpected core call: ${module}.${method}`); }, }; @@ -55,22 +59,22 @@ test('order import upserts verified metadata for the ordered product and pack it const products = { '10': { id: '10', name: 'Pack', reference: 'PACK-BASE', manufacturer_name: 'Pack Brand', location: 'BASE-BIN', - weight: '1', type: 'pack', associations: { product_bundle: [{ id: '20', id_product_attribute: '200', quantity: '2' }] }, + price: '8', weight: '1', type: 'pack', associations: { product_bundle: [{ id: '20', id_product_attribute: '200', quantity: '2' }] }, }, '20': { id: '20', name: 'Item', reference: 'ITEM-BASE', manufacturer_name: 'Item Brand', location: 'ITEM-BASE-BIN', - weight: '2', type: 'standard', associations: {}, + price: '3', weight: '2', type: 'standard', associations: {}, }, '30': { id: '30', name: 'Variant product', reference: 'VARIANT-BASE', manufacturer_name: 'Variant Brand', - location: 'VARIANT-BASE-BIN', weight: '1', type: 'standard', associations: {}, + location: 'VARIANT-BASE-BIN', price: '15', weight: '1', type: 'standard', associations: {}, }, }; const combinations = { - '100': { id: '100', id_product: '10', reference: 'PACK-V', weight: '1.5', location: 'PACK-V-BIN' }, - '200': { id: '200', id_product: '20', reference: 'ITEM-V', weight: '2.5', location: 'ITEM-V-BIN' }, - '300': { id: '300', id_product: '30', reference: 'VARIANT-BLUE', ean13: '5901234123457', weight: '0.1', location: 'BLUE-BIN' }, - '301': { id: '301', id_product: '30', reference: 'VARIANT-RED', ean13: '5901234123464', weight: '0.2', location: 'RED-BIN' }, + '100': { id: '100', id_product: '10', reference: 'PACK-V', price: '1.5', weight: '1.5', location: 'PACK-V-BIN' }, + '200': { id: '200', id_product: '20', reference: 'ITEM-V', price: '0.25', weight: '2.5', location: 'ITEM-V-BIN' }, + '300': { id: '300', id_product: '30', reference: 'VARIANT-BLUE', price: '2', ean13: '5901234123457', weight: '0.1', location: 'BLUE-BIN' }, + '301': { id: '301', id_product: '30', reference: 'VARIANT-RED', price: '3', ean13: '5901234123464', weight: '0.2', location: 'RED-BIN' }, }; const api = { isBLSupported: async () => false, @@ -94,12 +98,12 @@ test('order import upserts verified metadata for the ordered product and pack it assert.equal(upserts.length, 4); assert.deepEqual(upserts.find(product => product.sourceId === 'product:10:combination:100'), { sourceModule: 'core/prestashop', sourceId: 'product:10:combination:100', previousSourceId: '10', - sku: 'PACK-V', name: 'Pack', unitPrice: 10, tax: 0.23, weight: 2.5, + sku: 'PACK-V', name: 'Pack', unitPrice: 9.5, tax: 0.23, weight: 2.5, ean: '', brand: 'Pack Brand', warehouseLocation: 'PACK-V-BIN', }); assert.deepEqual(upserts.find(product => product.sourceId === 'product:20:combination:200'), { sourceModule: 'core/prestashop', sourceId: 'product:20:combination:200', previousSourceId: '20', - sku: 'ITEM-V', name: 'Item', weight: 4.5, + sku: 'ITEM-V', name: 'Item', unitPrice: 3.25, weight: 4.5, brand: 'Item Brand', warehouseLocation: 'ITEM-V-BIN', }); assert.deepEqual( @@ -112,12 +116,14 @@ test('order import upserts verified metadata for the ordered product and pack it ); assert.deepEqual(upserts.find(product => product.sourceId === 'product:30:combination:300'), { sourceModule: 'core/prestashop', sourceId: 'product:30:combination:300', previousSourceId: '30', - sku: 'VARIANT-BLUE', name: 'Variant Blue', unitPrice: 20, tax: 0.23, weight: 1.1, + sku: 'VARIANT-BLUE', name: 'Variant Blue', unitPrice: 17, tax: 0.23, weight: 1.1, ean: '5901234123457', brand: 'Variant Brand', warehouseLocation: 'BLUE-BIN', }); assert.deepEqual(upserts.find(product => product.sourceId === 'product:30:combination:301'), { sourceModule: 'core/prestashop', sourceId: 'product:30:combination:301', previousSourceId: '30', - sku: 'VARIANT-RED', name: 'Variant Red', unitPrice: 21, tax: 0.23, weight: 1.2, + sku: 'VARIANT-RED', name: 'Variant Red', unitPrice: 18, tax: 0.23, weight: 1.2, ean: '5901234123464', brand: 'Variant Brand', warehouseLocation: 'RED-BIN', }); + assert.equal(createdOrder.products[0].unitPrice, 10, 'order keeps its transactional net price'); + assert.equal(createdOrder.products[0].baseUnitPrice, 9.5, 'order also carries the catalog base price'); }); diff --git a/test/product-metadata.test.js b/test/product-metadata.test.js index ed1a179..92ff62d 100644 --- a/test/product-metadata.test.js +++ b/test/product-metadata.test.js @@ -125,6 +125,30 @@ test('product source identities distinguish base products and combinations', () }); }); +test('metadata resolves the net base price including a combination price impact', async () => { + const { resolveProductMetadata, resolveBaseUnitPrice } = require('../src/product-metadata'); + const api = { + getManufacturer: async () => null, + getStockAvailabilities: async () => [], + }; + const product = { + id: '1', reference: 'BASE', price: '19.90', manufacturer_name: '', location: '', + }; + const combination = { + id: '10', id_product: '1', reference: 'VARIANT', price: '2.60', location: '', + }; + + assert.equal(resolveBaseUnitPrice(product), 19.9); + assert.equal(resolveBaseUnitPrice(product, combination), 22.5); + assert.deepEqual(await resolveProductMetadata(api, product, { + sku: 'VARIANT', combination, + }), { + unitPrice: 22.5, + brand: '', + warehouseLocation: '', + }); +}); + test('metadata lookup omits synthetic SKUs and ambiguous variant matches', async () => { let requestedCombinations = 0; const method = loadMethod({