152 lines
8.4 KiB
JavaScript
152 lines
8.4 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { mapOrderDiscounts } = require('../src/order-discounts');
|
|
const Module = require('node:module');
|
|
const path = require('node:path');
|
|
|
|
const source = { total_discounts: '75.35', total_discounts_tax_excl: '63.51', total_discounts_tax_incl: '75.35' };
|
|
const product = { qty: 3, unitPrice: 62.03, tax: 0.23 };
|
|
const sampleOrder = {
|
|
id: '4888', reference: 'MCBPBQOGI', payment: 'Próbki',
|
|
total_products: '0.000000', total_products_wt: '0.000000',
|
|
total_paid_tax_excl: '0.000000', total_paid_tax_incl: '0.000000', total_paid_real: '0.000000',
|
|
total_wrapping_tax_excl: '0.000000', total_wrapping_tax_incl: '0.000000',
|
|
total_discounts: '14.760000', total_discounts_tax_excl: '12.000000', total_discounts_tax_incl: '14.760000',
|
|
total_shipping_tax_excl: '12.000000', total_shipping_tax_incl: '14.760000', carrier_tax_rate: '23.000',
|
|
associations: { order_rows: [{ product_id: '784', product_attribute_id: '0',
|
|
product_name: 'MULTICOLOR', product_reference: 'NS.40.H.7016',
|
|
product_quantity: '2', unit_price_tax_excl: '0.000000', unit_price_tax_incl: '0.000000' }] },
|
|
};
|
|
|
|
test('6Q3-M9R: inconsistent source totals cannot manufacture 19% VAT', () => {
|
|
assert.deepEqual(mapOrderDiscounts(source, [product]), [
|
|
{ name: 'Rabat', type: 'product', amount: 61.26, gross: 75.35, tax: 0.23 },
|
|
]);
|
|
});
|
|
|
|
test('discounts follow actual VAT, including exempt and fractional rates', () => {
|
|
for (const tax of [0, 0.08, 0.19, 0.077]) {
|
|
const [discount] = mapOrderDiscounts(source, [{ ...product, tax }]);
|
|
assert.equal(discount.tax, tax);
|
|
assert.equal(discount.amount, Math.round(75.35 / (1 + tax) * 100) / 100);
|
|
assert.equal(discount.gross, 75.35);
|
|
}
|
|
assert.equal(mapOrderDiscounts(source, [product, { ...product, unitPrice: 0, tax: 0.08 }])[0].tax, 0.23);
|
|
assert.deepEqual(mapOrderDiscounts({ total_discounts: '0' }, []), []);
|
|
});
|
|
|
|
test('mixed or missing VAT requires a verified breakdown instead of a synthetic rate', () => {
|
|
for (const products of [[], [{ ...product, tax: undefined }], [product, { ...product, tax: 0.08 }],
|
|
[{ ...product, isPack: true, pack: [{ qty: 1, tax: 0.23 }, { qty: 1, tax: 0.08 }] }]]) {
|
|
assert.throws(() => mapOrderDiscounts(source, products), { code: 'DISCOUNT_TAX_AMBIGUOUS' });
|
|
}
|
|
assert.throws(() => mapOrderDiscounts({ ...source, total_discounts_tax_incl: 'invalid' }, [product]), /brutto/);
|
|
});
|
|
|
|
test('4888: zero-price samples retain a delivery discount using the source carrier VAT', () => {
|
|
for (const tax of [0, 0.08, 0.23, 0.077]) {
|
|
const gross = Math.round(12 * (1 + tax) * 100) / 100;
|
|
const order = { ...sampleOrder, carrier_tax_rate: String(tax * 100),
|
|
total_shipping_tax_incl: String(gross), total_discounts_tax_incl: String(gross), total_discounts: String(gross) };
|
|
assert.deepEqual(mapOrderDiscounts(order, [{ ...product, unitPrice: 0, qty: 2 }]), [
|
|
{ name: 'Darmowa dostawa', type: 'delivery', amount: 12, gross, tax },
|
|
]);
|
|
}
|
|
});
|
|
|
|
test('zero-price orders require matching source totals and an explicit delivery VAT', () => {
|
|
for (const overrides of [
|
|
{ total_products: '1' }, { total_products_wt: '1' },
|
|
{ total_paid_tax_excl: '1' }, { total_paid_tax_incl: '1' },
|
|
{ total_wrapping_tax_excl: '1' }, { total_wrapping_tax_incl: '1' },
|
|
{ total_products: undefined }, { total_paid_tax_incl: undefined },
|
|
{ total_discounts_tax_excl: '11' }, { total_shipping_tax_incl: '15' },
|
|
{ carrier_tax_rate: undefined }, { carrier_tax_rate: '' }, { carrier_tax_rate: '19' },
|
|
{ carrier_tax_rate: '-1' }, { carrier_tax_rate: 'invalid' }, { carrier_tax_rate: '101' },
|
|
]) {
|
|
assert.throws(() => mapOrderDiscounts({ ...sampleOrder, ...overrides }, [{ ...product, unitPrice: 0 }]),
|
|
{ code: 'DISCOUNT_TAX_AMBIGUOUS' }, JSON.stringify(overrides));
|
|
}
|
|
assert.throws(() => mapOrderDiscounts(sampleOrder, []), { code: 'DISCOUNT_TAX_AMBIGUOUS' });
|
|
assert.throws(() => mapOrderDiscounts(sampleOrder, [{ ...product, unitPrice: 0, qty: NaN }]),
|
|
{ code: 'DISCOUNT_TAX_AMBIGUOUS' });
|
|
});
|
|
|
|
async function importThroughHooks(providers, overrides = {}) {
|
|
const created = [];
|
|
const original = Module._load;
|
|
const Core = {
|
|
getModuleInfo: () => ({ code: 'core/prestashop' }),
|
|
async execHook(name, envelope) {
|
|
if (name.endsWith('.providers')) return providers.map(instanceId => ({ protocol: 'orbito.contract/1', moduleCode: 'alternative/orders', instanceId }));
|
|
let data;
|
|
if (name === 'orders.status.search') data = { results: [{ _id: '000000000000000000000001' }] };
|
|
else if (name === 'orders.create') { created.push(envelope.params); data = { _id: 'created-1' }; }
|
|
else throw new Error(name);
|
|
return [{ protocol: 'orbito.contract/1', instanceId: envelope.targetInstanceId, ok: true, data }];
|
|
},
|
|
};
|
|
Module._load = function(request, ...args) {
|
|
if (request === '@orbito/api-lib') return { Core, Log: { debug() {}, info() {}, warn() {}, error() {} } };
|
|
if (request === './models/PsOrder') return { update: async () => {}, findOne: async () => null };
|
|
return original.call(this, request, ...args);
|
|
};
|
|
const filename = path.resolve(__dirname, '../src/OrderImporter');
|
|
delete require.cache[require.resolve(filename)];
|
|
let importer;
|
|
try { importer = require(filename); } finally { Module._load = original; }
|
|
const order = { ...source, id: '4858', reference: 'MWSKTWLNP', id_address_delivery: '1', id_customer: '1',
|
|
id_currency: '1', total_shipping_tax_excl: '12', payment: 'B2B', date_add: '2026-10-08 11:40:00',
|
|
associations: { order_rows: [{ product_id: '1', product_name: 'PAVING', product_reference: 'NS.02.J',
|
|
product_attribute_id: '0', product_quantity: '3', unit_price_tax_excl: '62.03', unit_price_tax_incl: '76.30' }] },
|
|
...overrides,
|
|
};
|
|
const api = { isBLSupported: async () => false,
|
|
makeRequest: async endpoint => {
|
|
if (endpoint === `orders/${order.id}`) return { order };
|
|
if (endpoint === 'addresses/1') return { address: { id_country: '1', address1: 'Street', postcode: '00-001' } };
|
|
if (endpoint === 'customers/1') return { customer: { firstname: 'Test', lastname: 'Test', email: 'test@example.com' } };
|
|
throw new Error(endpoint);
|
|
},
|
|
getProduct: async () => ({ name: 'PAVING', type: 'standard' }),
|
|
getProductCoverImageUrl: async () => null,
|
|
getCurrency: async () => ({ iso_code: 'PLN' }), getCountry: async () => ({ iso_code: 'PL' }),
|
|
getCustomerNotes: async () => [],
|
|
};
|
|
try {
|
|
await importer.importFromPSOrder(order.id, api, { _id: 'shop-1', orderStatus: '000000000000000000000001' });
|
|
return { created };
|
|
} catch (error) { return { created, error }; }
|
|
}
|
|
|
|
test('real hook import delivers corrected discount to an alternative orders provider once', async () => {
|
|
const { created, error } = await importThroughHooks(['orders-account-1']);
|
|
assert.equal(error, undefined);
|
|
assert.equal(created.length, 1);
|
|
assert.deepEqual(created[0].discounts, [{ name: 'Rabat', type: 'product', amount: 61.26, gross: 75.35, tax: 0.23 }]);
|
|
});
|
|
|
|
test('4888 imports once through hooks with unchanged zero prices and matching delivery VAT', async () => {
|
|
const { created, error } = await importThroughHooks(['orders-account-1'], sampleOrder);
|
|
assert.equal(error, undefined);
|
|
assert.equal(created.length, 1);
|
|
assert.equal(created[0].source.uniqueId, 'MCBPBQOGI:4888');
|
|
assert.equal(created[0].products[0].unitPrice, 0);
|
|
assert.equal(created[0].products[0].qty, 2);
|
|
assert.deepEqual(created[0].delivery.cost, { amount: 12, tax: 0.23 });
|
|
assert.deepEqual(created[0].discounts, [
|
|
{ name: 'Darmowa dostawa', type: 'delivery', amount: 12, gross: 14.76, tax: 0.23 },
|
|
]);
|
|
assert.deepEqual(created[0].payments, []);
|
|
});
|
|
|
|
test('absent or multiple orders accounts do not create an order', async () => {
|
|
for (const providers of [[], ['orders-account-1', 'orders-account-2']]) {
|
|
for (const order of [{}, sampleOrder]) {
|
|
const result = await importThroughHooks(providers, order);
|
|
assert.equal(result.created.length, 0);
|
|
assert.match(result.error.code, /^HOOK_PROVIDER_(UNAVAILABLE|AMBIGUOUS)$/);
|
|
}
|
|
}
|
|
});
|