Fix free sample orders with delivery discounts
This commit is contained in:
@@ -6,6 +6,17 @@ 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]), [
|
||||
@@ -32,7 +43,36 @@ test('mixed or missing VAT requires a verified breakdown instead of a synthetic
|
||||
assert.throws(() => mapOrderDiscounts({ ...source, total_discounts_tax_incl: 'invalid' }, [product]), /brutto/);
|
||||
});
|
||||
|
||||
async function importThroughHooks(providers) {
|
||||
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 = {
|
||||
@@ -59,10 +99,11 @@ async function importThroughHooks(providers) {
|
||||
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/4858') return { order };
|
||||
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);
|
||||
@@ -73,7 +114,7 @@ async function importThroughHooks(providers) {
|
||||
getCustomerNotes: async () => [],
|
||||
};
|
||||
try {
|
||||
await importer.importFromPSOrder('4858', api, { _id: 'shop-1', orderStatus: '000000000000000000000001' });
|
||||
await importer.importFromPSOrder(order.id, api, { _id: 'shop-1', orderStatus: '000000000000000000000001' });
|
||||
return { created };
|
||||
} catch (error) { return { created, error }; }
|
||||
}
|
||||
@@ -85,10 +126,26 @@ test('real hook import delivers corrected discount to an alternative orders prov
|
||||
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']]) {
|
||||
const result = await importThroughHooks(providers);
|
||||
assert.equal(result.created.length, 0);
|
||||
assert.match(result.error.code, /^HOOK_PROVIDER_(UNAVAILABLE|AMBIGUOUS)$/);
|
||||
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)$/);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user