fix(prestashop): resolve bundle item VAT with fallback (1.1.14)
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "core/prestashop",
|
||||
"version": "1.1.13",
|
||||
"version": "1.1.14",
|
||||
"singleton": false,
|
||||
"dependsOn": [
|
||||
"core/orders",
|
||||
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "core.prestashop",
|
||||
"version": "1.1.13",
|
||||
"version": "1.1.14",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "core.prestashop",
|
||||
"version": "1.1.13",
|
||||
"version": "1.1.14",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@orbito/api-lib": "^3.3.3",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "core.prestashop",
|
||||
"version": "1.1.13",
|
||||
"version": "1.1.14",
|
||||
"description": "Integracja zamówień i płatności PrestaShop",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
+38
-3
@@ -7,6 +7,8 @@ const {
|
||||
resolveProductMetadata,
|
||||
} = require('./product-metadata');
|
||||
|
||||
const DEFAULT_PACK_ITEM_TAX = 0.23;
|
||||
|
||||
function getPSMultiLangField(param, langId) {
|
||||
if (Array.isArray(param)) {
|
||||
if (param.length === 0) return '';
|
||||
@@ -50,6 +52,22 @@ function applyCombinationFields(target, combination) {
|
||||
if (Object.hasOwn(combination, 'ean13')) target.ean = combination.ean13 || '';
|
||||
}
|
||||
|
||||
async function resolvePackItemTax(api, productId, combinationId, context) {
|
||||
try {
|
||||
if (typeof api.getProductTaxRate === 'function') {
|
||||
const tax = await api.getProductTaxRate(productId, combinationId, context);
|
||||
if (Number.isFinite(tax) && tax >= 0 && tax <= 1) return tax;
|
||||
}
|
||||
} catch (error) {
|
||||
Log.warn('Could not resolve PrestaShop pack item VAT; using the default rate', {
|
||||
productId,
|
||||
combinationId,
|
||||
error: error?.message,
|
||||
});
|
||||
}
|
||||
return DEFAULT_PACK_ITEM_TAX;
|
||||
}
|
||||
|
||||
async function resolveImportStatusId(config) {
|
||||
const statusResult = await Core.callModule( 'core/orders', 'status.search', {});
|
||||
const statuses = statusResult?.results ?? [];
|
||||
@@ -82,6 +100,20 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
throw new Error(`PrestaShop order ${orderId} not found`);
|
||||
}
|
||||
|
||||
const addressResponse = await api.makeRequest(
|
||||
'addresses/' + order.id_address_delivery,
|
||||
'GET',
|
||||
{ output_format: "JSON" }
|
||||
);
|
||||
const address = addressResponse.address;
|
||||
if (!address) throw new Error(`Delivery address ${order.id_address_delivery} not found`);
|
||||
const productTaxContext = {
|
||||
countryId: address.id_country,
|
||||
stateId: address.id_state,
|
||||
postcode: address.postcode,
|
||||
currencyId: order.id_currency,
|
||||
};
|
||||
|
||||
Log.info( "Importing order: " + order.reference);
|
||||
|
||||
let products = [];
|
||||
@@ -163,6 +195,12 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
}
|
||||
const packWeight = resolveProductWeight(psItem, packCombination);
|
||||
if (packWeight !== undefined) pData.weight = packWeight;
|
||||
pData.tax = await resolvePackItemTax(
|
||||
api,
|
||||
packProductId,
|
||||
packCombinationId,
|
||||
productTaxContext
|
||||
);
|
||||
|
||||
try {
|
||||
const metadata = await resolveProductMetadata(api, psItem, {
|
||||
@@ -209,9 +247,6 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
const customer = res.customer;
|
||||
Log.debug( "Fetched customer details", { email: customer.email, name: customer.firstname + " " + customer.lastname });
|
||||
|
||||
res = await api.makeRequest('addresses/' + order.id_address_delivery, 'GET', { output_format: "JSON" });
|
||||
const address = res.address;
|
||||
|
||||
let carrierName = "Brak wybranej dostawy";
|
||||
if (order.id_carrier) {
|
||||
try {
|
||||
|
||||
@@ -199,6 +199,48 @@ module.exports = class PrestashopApi {
|
||||
return combination;
|
||||
}
|
||||
|
||||
async getProductTaxRate(productId, combinationId = 0, context = {}) {
|
||||
const id = String(productId ?? '').trim();
|
||||
const attributeId = String(combinationId ?? '0').trim() || '0';
|
||||
if (!/^\d+$/.test(id) || id === '0' || !/^\d+$/.test(attributeId)) return null;
|
||||
|
||||
const countryId = String(context.countryId ?? '').trim();
|
||||
const stateId = String(context.stateId ?? '').trim();
|
||||
const currencyId = String(context.currencyId ?? '').trim();
|
||||
const postcode = String(context.postcode ?? '').trim().slice(0, 32);
|
||||
const cacheKey = [
|
||||
'product_tax_rate', id, attributeId, countryId, stateId, postcode, currencyId,
|
||||
].join('_');
|
||||
const cached = this.cache.get(cacheKey);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
const params = { output_format: 'JSON' };
|
||||
const addPriceParams = (alias, useTax) => {
|
||||
params[`price[${alias}][use_tax]`] = useTax ? 1 : 0;
|
||||
params[`price[${alias}][use_reduction]`] = 0;
|
||||
params[`price[${alias}][use_ecotax]`] = 0;
|
||||
params[`price[${alias}][quantity]`] = 1;
|
||||
if (attributeId !== '0') params[`price[${alias}][product_attribute]`] = attributeId;
|
||||
if (/^\d+$/.test(countryId) && countryId !== '0') params[`price[${alias}][country]`] = countryId;
|
||||
if (/^\d+$/.test(stateId) && stateId !== '0') params[`price[${alias}][state]`] = stateId;
|
||||
if (postcode) params[`price[${alias}][postcode]`] = postcode;
|
||||
if (/^\d+$/.test(currencyId) && currencyId !== '0') params[`price[${alias}][currency]`] = currencyId;
|
||||
};
|
||||
addPriceParams('orbito_price_net', false);
|
||||
addPriceParams('orbito_price_gross', true);
|
||||
|
||||
const response = await this.makeRequest(`products/${id}`, 'GET', params, true);
|
||||
const product = response?.product;
|
||||
const net = Number.parseFloat(String(product?.orbito_price_net ?? '').replace(',', '.'));
|
||||
const gross = Number.parseFloat(String(product?.orbito_price_gross ?? '').replace(',', '.'));
|
||||
if (!Number.isFinite(net) || net <= 0 || !Number.isFinite(gross) || gross < net) return null;
|
||||
|
||||
const taxRate = Math.round(((gross - net) / net) * 100000000) / 100000000;
|
||||
if (!Number.isFinite(taxRate) || taxRate < 0 || taxRate > 1) return null;
|
||||
this.cache.set(cacheKey, taxRate);
|
||||
return taxRate;
|
||||
}
|
||||
|
||||
async getManufacturer(manufacturerId) {
|
||||
const id = String(manufacturerId || '').trim();
|
||||
if (!id) return null;
|
||||
|
||||
@@ -83,6 +83,7 @@ test('order import passes verified metadata and pack facts only through core/ord
|
||||
'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 taxLookups = [];
|
||||
const api = {
|
||||
isBLSupported: async () => false,
|
||||
makeRequest: async endpoint => {
|
||||
@@ -93,6 +94,10 @@ test('order import passes verified metadata and pack facts only through core/ord
|
||||
},
|
||||
getProduct: async id => products[id],
|
||||
getProductCombination: async id => combinations[id],
|
||||
getProductTaxRate: async (productId, combinationId, context) => {
|
||||
taxLookups.push({ productId, combinationId, context });
|
||||
return productId === '20' ? 0.08 : null;
|
||||
},
|
||||
getProductCoverImageUrl: async () => null,
|
||||
getCurrency: async () => ({ iso_code: 'PLN' }),
|
||||
getCountry: async () => ({ iso_code: 'PL' }),
|
||||
@@ -111,9 +116,18 @@ test('order import passes verified metadata and pack facts only through core/ord
|
||||
assert.equal(createdOrder.products[0].pack[0].sourceId, 'product:20:combination:200');
|
||||
assert.equal(createdOrder.products[0].pack[0].previousSourceId, '20');
|
||||
assert.equal(createdOrder.products[0].pack[0].baseUnitPrice, 3.25);
|
||||
assert.equal(createdOrder.products[0].pack[0].tax, 0.08);
|
||||
assert.equal(createdOrder.products[0].pack[1].sourceId, 'product:21:combination:0');
|
||||
assert.equal(createdOrder.products[0].pack[1].sku, 'REGULAR-ITEM');
|
||||
assert.equal(createdOrder.products[0].pack[1].baseUnitPrice, 4.5);
|
||||
assert.equal(createdOrder.products[0].pack[1].tax, 0.23, 'missing tax data uses the configured fallback');
|
||||
assert.deepEqual(taxLookups, [{
|
||||
productId: '20', combinationId: '200',
|
||||
context: { countryId: '1', stateId: undefined, postcode: '00-001', currencyId: '1' },
|
||||
}, {
|
||||
productId: '21', combinationId: '0',
|
||||
context: { countryId: '1', stateId: undefined, postcode: '00-001', currencyId: '1' },
|
||||
}]);
|
||||
assert.deepEqual(createdOrder.products.slice(1).map(product => product.sku), [
|
||||
'VARIANT-BLUE', 'VARIANT-RED',
|
||||
]);
|
||||
|
||||
@@ -160,6 +160,39 @@ test('uses a combination image before the base product cover', async () => {
|
||||
assert.match(url, /ws_key=secret/);
|
||||
});
|
||||
|
||||
test('derives a combination VAT rate from the base product tax calculation and caches it', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
const urls = [];
|
||||
Core.fetch = async url => {
|
||||
urls.push(url);
|
||||
return response(200, { product: { orbito_price_net: '100.00', orbito_price_gross: '123.00' } });
|
||||
};
|
||||
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
||||
const context = { countryId: '1', stateId: '0', postcode: '00-001', currencyId: '2' };
|
||||
|
||||
assert.equal(await api.getProductTaxRate('20', '200', context), 0.23);
|
||||
assert.equal(await api.getProductTaxRate('20', '200', context), 0.23);
|
||||
assert.equal(urls.length, 1);
|
||||
assert.match(urls[0], /products\/20\?/);
|
||||
assert.match(urls[0], /price%5Borbito_price_net%5D%5Buse_tax%5D=0/);
|
||||
assert.match(urls[0], /price%5Borbito_price_gross%5D%5Buse_tax%5D=1/);
|
||||
assert.match(urls[0], /price%5Borbito_price_gross%5D%5Bproduct_attribute%5D=200/);
|
||||
assert.match(urls[0], /price%5Borbito_price_gross%5D%5Bcountry%5D=1/);
|
||||
assert.match(urls[0], /price%5Borbito_price_gross%5D%5Bpostcode%5D=00-001/);
|
||||
});
|
||||
|
||||
test('returns no VAT rate when PrestaShop cannot provide comparable net and gross prices', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
Core.fetch = async () => response(200, {
|
||||
product: { orbito_price_net: 'invalid', orbito_price_gross: '123.00' },
|
||||
});
|
||||
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
||||
|
||||
assert.equal(await api.getProductTaxRate('20', '0', { countryId: '1' }), null);
|
||||
});
|
||||
|
||||
test('collects public customer notes from order, cart and customer thread without cross-order leakage', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
|
||||
Reference in New Issue
Block a user