Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/apps/tiny-erp/src/integration/import-product-from-tiny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,26 @@ export default (
appData?: Record<string, any>,
tipo?: string,
isNew = true,
priceListData?: { preco?: number; preco_promocional?: number },
): Promise<ProductSet> => new Promise((resolve) => {
const sku = tinyProduct.codigo || String(tinyProduct.id);
const name = (tinyProduct.nome || sku).trim();
const isProduct = tipo === 'produto';
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);
Expand Down
Loading