att
This commit is contained in:
@@ -1,41 +1,102 @@
|
||||
var xlsxLoader = {
|
||||
loading: false,
|
||||
callbacks: []
|
||||
};
|
||||
|
||||
function carregarItensDoExcel(fileInputId) {
|
||||
const fileInput = document.getElementById(fileInputId);
|
||||
const file = fileInput ? fileInput.files[0] : null;
|
||||
var fileInput = document.getElementById(fileInputId);
|
||||
var file = fileInput ? fileInput.files[0] : null;
|
||||
if (!file) {
|
||||
FLUIGC.toast({ title: 'Erro', message: 'Nenhum arquivo selecionado.', type: 'danger' });
|
||||
return;
|
||||
}
|
||||
if (typeof XLSX === "undefined") {
|
||||
FLUIGC.toast({ title: 'Erro', message: 'Biblioteca XLSX nao carregada.', type: 'danger' });
|
||||
|
||||
showExcelLoading();
|
||||
ensureXlsxLibrary(function (err) {
|
||||
if (err) {
|
||||
hideExcelLoading();
|
||||
FLUIGC.toast({ title: 'Erro', message: 'Biblioteca XLSX indisponivel.', type: 'danger' });
|
||||
console.error("Erro ao carregar XLSX:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
processarArquivoExcel(file);
|
||||
});
|
||||
}
|
||||
|
||||
function ensureXlsxLibrary(callback) {
|
||||
if (typeof XLSX !== "undefined") {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
showExcelLoading();
|
||||
xlsxLoader.callbacks.push(callback);
|
||||
if (xlsxLoader.loading) {
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
xlsxLoader.loading = true;
|
||||
loadXlsxScript(0);
|
||||
}
|
||||
|
||||
function loadXlsxScript(index) {
|
||||
var urls = [
|
||||
"/portal/resources/js/xlsx.full.min.js",
|
||||
"https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"
|
||||
];
|
||||
|
||||
if (index >= urls.length) {
|
||||
flushXlsxCallbacks(new Error("Biblioteca XLSX nao encontrada."));
|
||||
return;
|
||||
}
|
||||
|
||||
$.getScript(urls[index])
|
||||
.done(function () {
|
||||
if (typeof XLSX === "undefined") {
|
||||
loadXlsxScript(index + 1);
|
||||
return;
|
||||
}
|
||||
flushXlsxCallbacks();
|
||||
})
|
||||
.fail(function () {
|
||||
loadXlsxScript(index + 1);
|
||||
});
|
||||
}
|
||||
|
||||
function flushXlsxCallbacks(err) {
|
||||
var callbacks = xlsxLoader.callbacks.splice(0, xlsxLoader.callbacks.length);
|
||||
xlsxLoader.loading = false;
|
||||
|
||||
for (var i = 0; i < callbacks.length; i++) {
|
||||
callbacks[i](err);
|
||||
}
|
||||
}
|
||||
|
||||
function processarArquivoExcel(file) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function (e) {
|
||||
// Permite o navegador renderizar o overlay antes de processar.
|
||||
setTimeout(function () {
|
||||
try {
|
||||
const data = new Uint8Array(e.target.result);
|
||||
const workbook = XLSX.read(data, { type: 'array' });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
const linhas = XLSX.utils.sheet_to_json(sheet, { defval: "" });
|
||||
var data = new Uint8Array(e.target.result);
|
||||
var workbook = XLSX.read(data, { type: 'array' });
|
||||
var sheetName = workbook.SheetNames[0];
|
||||
var sheet = workbook.Sheets[sheetName];
|
||||
var linhas = XLSX.utils.sheet_to_json(sheet, { defval: "" });
|
||||
|
||||
// Limpa a tabela (sem usar form)
|
||||
const indices = $("input[id^='codigoItem___']").map(function () {
|
||||
var indices = $("input[id^='codigoItem___']").map(function () {
|
||||
return $(this).attr("id").split("___")[1];
|
||||
}).get();
|
||||
$.each(indices, function (_, idx) {
|
||||
fnWdkRemoveChild(idx);
|
||||
});
|
||||
|
||||
const linhasValidas = [];
|
||||
var linhasValidas = [];
|
||||
$.each(linhas, function (_, item) {
|
||||
const codigo = getCellByAliases(item, ["codigoItem", "codigo", "codItem", "sku", "code", "item"]);
|
||||
const quantidade = getCellByAliases(item, ["quantidadeItem", "quantidade", "qtd", "qtde"]);
|
||||
const descricao = getCellByAliases(item, ["descricao", "description", "desc"]);
|
||||
var codigo = getCellByAliases(item, ["codigoItem", "codigo", "codItem", "sku", "code", "item"]);
|
||||
var quantidade = getCellByAliases(item, ["quantidadeItem", "quantidade", "qtd", "qtde"]);
|
||||
var descricao = getCellByAliases(item, ["descricao", "description", "desc"]);
|
||||
|
||||
if (!codigo || !quantidade) {
|
||||
return;
|
||||
@@ -59,23 +120,33 @@ function carregarItensDoExcel(fileInputId) {
|
||||
|
||||
// Adiciona os itens da planilha
|
||||
$.each(linhasValidas, function (_, item) {
|
||||
const idx = wdkAddChild('tabelaItens');
|
||||
const zoomObj = window[`descricao___${idx}`];
|
||||
var idx = wdkAddChild('tabelaItens');
|
||||
var zoomObj = window["descricao___" + idx];
|
||||
if (zoomObj && typeof zoomObj.setValue === "function") {
|
||||
zoomObj.setValue(item.codigo);
|
||||
} else {
|
||||
// Fallback visual caso o objeto zoom ainda nao esteja pronto no momento.
|
||||
$(`#descricao___${idx}`).val(item.codigo);
|
||||
$("#descricao___" + idx).val(item.codigo);
|
||||
}
|
||||
|
||||
$(`#quantidadeItem___${idx}`).val(item.quantidade);
|
||||
$("#quantidadeItem___" + idx).val(item.quantidade);
|
||||
$("#codigoProdutoItem___" + idx).val(item.codigo);
|
||||
|
||||
var descricaoFinal = item.descricao || buscarDescricaoProduto(item.codigo);
|
||||
var produtoInfo = buscarProdutoPorCodigo(item.codigo);
|
||||
if (produtoInfo.id) {
|
||||
$("#productIdItem___" + idx).val(produtoInfo.id);
|
||||
}
|
||||
|
||||
var descricaoFinal = item.descricao || produtoInfo.descricao;
|
||||
if (descricaoFinal) {
|
||||
$(`#codigoItem___${idx}`).val(descricaoFinal);
|
||||
$("#codigoItem___" + idx).val(descricaoFinal);
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof processarConferenciaNfe === "function") {
|
||||
processarConferenciaNfe();
|
||||
}
|
||||
|
||||
FLUIGC.toast({ title: 'Sucesso', message: linhasValidas.length + ' itens carregados com sucesso!', type: 'success' });
|
||||
} catch (err) {
|
||||
FLUIGC.toast({ title: 'Erro', message: 'Falha ao processar Excel: ' + err.message, type: 'danger' });
|
||||
@@ -118,36 +189,46 @@ function normalizeHeader(text) {
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
function buscarDescricaoProduto(codigo) {
|
||||
function buscarProdutoPorCodigo(codigo) {
|
||||
try {
|
||||
if (typeof DatasetFactory === "undefined" || typeof ConstraintType === "undefined") {
|
||||
return "";
|
||||
return { descricao: "", id: "" };
|
||||
}
|
||||
|
||||
var codigoTxt = String(codigo || "").trim();
|
||||
if (!codigoTxt) return "";
|
||||
if (!codigoTxt) return { descricao: "", id: "" };
|
||||
|
||||
var cCodigo = DatasetFactory.createConstraint("Code", codigoTxt, codigoTxt, ConstraintType.MUST);
|
||||
var ds = DatasetFactory.getDataset("ds_rgb_products", null, [cCodigo], null);
|
||||
if (!ds || !ds.values || !ds.values.length) {
|
||||
return "";
|
||||
return { descricao: "", id: "" };
|
||||
}
|
||||
|
||||
for (var i = 0; i < ds.values.length; i++) {
|
||||
var row = ds.values[i] || {};
|
||||
if (String(row.Code || "").trim() === codigoTxt) {
|
||||
return String(row.descricao || row.Description || "").trim();
|
||||
return {
|
||||
descricao: String(row.descricao || row.Description || "").trim(),
|
||||
id: String(row.id || "").trim()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
var first = ds.values[0] || {};
|
||||
return String(first.descricao || first.Description || "").trim();
|
||||
return {
|
||||
descricao: String(first.descricao || first.Description || "").trim(),
|
||||
id: String(first.id || "").trim()
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Erro ao buscar descricao por codigo:", e);
|
||||
return "";
|
||||
return { descricao: "", id: "" };
|
||||
}
|
||||
}
|
||||
|
||||
function buscarDescricaoProduto(codigo) {
|
||||
return buscarProdutoPorCodigo(codigo).descricao;
|
||||
}
|
||||
|
||||
function showExcelLoading() {
|
||||
var overlay = document.getElementById("excelLoadingOverlay");
|
||||
if (overlay) {
|
||||
|
||||
Reference in New Issue
Block a user