fix(prestashop): resolve bundle item VAT with fallback (1.1.14)
This commit is contained in:
+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 {
|
||||
|
||||
Reference in New Issue
Block a user