diff --git a/packages/apps/tiny-erp/src/integration/import-product-from-tiny.ts b/packages/apps/tiny-erp/src/integration/import-product-from-tiny.ts index a14ef7cae..69bebf976 100644 --- a/packages/apps/tiny-erp/src/integration/import-product-from-tiny.ts +++ b/packages/apps/tiny-erp/src/integration/import-product-from-tiny.ts @@ -5,6 +5,27 @@ import updateAppData from '@cloudcommerce/firebase/lib/helpers/update-app-data'; import postTiny from './post-tiny-erp'; import parseProduct from './parsers/product-from-tiny'; +const getPriceListData = async (productId: number) => { + const priceListId = process.env.TINYERP_PRICE_LIST_ID; + if (!priceListId) return undefined; + try { + const { registros } = await postTiny('/listas.precos.excecoes.php', { + idListaPreco: Number(priceListId), + idProduto: productId, + }); + if (Array.isArray(registros) && registros.length > 0) { + const registro = registros[0]; + return { + preco: registro.preco, + preco_promocional: registro.preco_promocional, + }; + } + } catch (err) { + logger.warn(`Failed to get price list data for product ${productId}`, err); + } + return undefined; +}; + const importProduct = async ( apiDoc, queueEntry, @@ -117,7 +138,8 @@ const importProduct = async ( if (!canCreateNew) { return null; } - return parseProduct(tinyProduct, appData, tipo, true) + return getPriceListData(tinyProduct.id) + .then((priceListData) => parseProduct(tinyProduct, appData, tipo, true, priceListData)) .then((bodyProduct) => { logger.info(`Creating ${queueSku}`, { bodyProduct }); return api.post('products', bodyProduct); @@ -126,7 +148,7 @@ const importProduct = async ( if (!tinyProduct) return null; return postTiny('/produto.obter.php', { id: (tinyProduct.id || produtoSaldo.id) }) - .then(({ produto }) => { + .then(async ({ produto }) => { let method; let endpoint; let productId = product && product._id; @@ -139,12 +161,14 @@ const importProduct = async ( } else { return null; } + const priceListData = await getPriceListData(produto.id); // @ts-ignore return parseProduct( produto, appData, tipo, method === 'POST', + priceListData, ).then((parsedProduct) => { if (!Number.isNaN(quantity)) { parsedProduct.quantity = quantity >= 0 ? quantity : 0; diff --git a/packages/apps/tiny-erp/src/integration/parsers/product-from-tiny.ts b/packages/apps/tiny-erp/src/integration/parsers/product-from-tiny.ts index de965b60f..820da2d87 100644 --- a/packages/apps/tiny-erp/src/integration/parsers/product-from-tiny.ts +++ b/packages/apps/tiny-erp/src/integration/parsers/product-from-tiny.ts @@ -130,6 +130,7 @@ export default ( appData?: Record, tipo?: string, isNew = true, + priceListData?: { preco?: number; preco_promocional?: number }, ): Promise => new Promise((resolve) => { const sku = tinyProduct.codigo || String(tinyProduct.id); const name = (tinyProduct.nome || sku).trim(); @@ -137,14 +138,18 @@ export default ( const fixToNumber = (shouldBeNumber: any) => { return Number(shouldBeNumber) > 0 ? Number(shouldBeNumber) : 0; }; - const price = fixToNumber(tinyProduct.preco_promocional || tinyProduct.precoPromocional) - || fixToNumber(tinyProduct.preco); + const listPrice = priceListData?.preco; + const listPromoPrice = priceListData?.preco_promocional; + const basePrice = fixToNumber(listPrice) || fixToNumber(tinyProduct.preco); + const price = fixToNumber(listPromoPrice) + || fixToNumber(tinyProduct.preco_promocional || tinyProduct.precoPromocional) + || basePrice; const product: ProductSet = { available: tinyProduct.situacao === 'A', sku, name, price, - base_price: fixToNumber(tinyProduct.preco), + base_price: basePrice, body_html: tinyProduct.descricao_complementar || tinyProduct.descricaoComplementar, }; const costPrice = fixToNumber(tinyProduct.preco_custo || tinyProduct.precoCusto);