From 3f6ff87e88c0ec3a884f0019998c840c4c5c71f1 Mon Sep 17 00:00:00 2001 From: borja Date: Wed, 15 Nov 2023 16:20:55 +0100 Subject: [PATCH] Recuento clientes e importar excel --- importarExcel/importarexcel.js | 88 ++++++++++++++++++++++++++++ recuentoClientes/recuentoClientes.js | 71 ++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 importarExcel/importarexcel.js create mode 100644 recuentoClientes/recuentoClientes.js diff --git a/importarExcel/importarexcel.js b/importarExcel/importarexcel.js new file mode 100644 index 0000000..e343484 --- /dev/null +++ b/importarExcel/importarexcel.js @@ -0,0 +1,88 @@ +// Read Excel file and copy the relevant columns to the main table (with proper column types) +const codetoNameEquivalences = { + "AAA": "Hospital Son Espases", + "70007": "Area de Gestión Almería Norte", + "70008": "Consejería de Salud de La Rioja", + "70011": "Jefatura Territorial de Sanidad de Pontevedra", +} +// CONSTANTS + +const ORIGIN_TABLE_NAME = "BASE DE DATOS"; +const ORIGIN_VIEW_NAME = "Default View"; + +const TARGET_TABLE_NAME = "Brutos" +const TARGET_VIEW_NAME = "Default View"; + +// Helper functions +const clientCodeToName = (code) => { + const clientCode = code.toString(); + let clientName = ""; + + +} +const removeDoubleSpace = (str) => { + if (typeof str !== "string") { + return str; + } + return str.replace(/\s\s+/g, ' '); +} + +// Remove leading and ending spaces in string +const removeLeadingAndTrailingSpaces = (str) => { + if (typeof str !== "string") { + return str; + } + return str.trim(); +} + +const matcodeToAbarax = (str) => { + if (str === 211354) { + return "50mg"; + } + return "100mg"; +} + +// get tables and views +const originTable = base.getTableByName(ORIGIN_TABLE_NAME); +const originView = base.getViewByName(originTable, ORIGIN_VIEW_NAME); + +const targetTable = base.getTableByName(TARGET_TABLE_NAME); +const targetView = base.getViewByName(targetTable, TARGET_VIEW_NAME); + +// List of the columns I want to replicate +const selectedColumns = [ + "Material", + "Solic.", + "Present.Concentr.", + "Nom.Solicitante", + "Nom Dest.", + "FechaFact.", + "Cantidad UMB", +] + +const dataMatrix = [] + +// Get all rows and create new element with only the selected columns +const originRows = base.getRows(originTable, originView); + +originRows.forEach(row => { + + // Create new element with only the selected columns + const element = {}; + selectedColumns.forEach(column => { + element[column] = removeLeadingAndTrailingSpaces(row[column]); + element[column] = removeDoubleSpace(element[column]); + }); + + // Material to Abarax + element["Material"] = matcodeToAbarax(element["Material"]); + + // Add element to dataMatrix + dataMatrix.push(element); +}) + +console.log(dataMatrix); + +// Funciones para limpiar los DATOS +// Remove double space +// diff --git a/recuentoClientes/recuentoClientes.js b/recuentoClientes/recuentoClientes.js new file mode 100644 index 0000000..a8dd874 --- /dev/null +++ b/recuentoClientes/recuentoClientes.js @@ -0,0 +1,71 @@ +// Counts how many times a row value is repeated in a table +// +// CONSTANTS + +const ORIGIN_TABLE_NAME = "Brutos"; +const ORIGIN_VIEW_NAME = "Default"; + +const RESULT_TABLE_NAME = "Clientes" +const RESULT_VIEW_NAME = "Default"; + +const originClientColumnName = "Cliente"; +const originQuantityColumnName = "Cantidad"; + +const resultClientColumnName = "Cliente"; +const resultCountColumnName = "Pedidos"; +const resultQuantityColumnName = "Cantidad"; + +const originTable = base.getTableByName(ORIGIN_TABLE_NAME); +const originView = base.getViewByName(originTable, ORIGIN_VIEW_NAME); +const resultTable = base.getTableByName(RESULT_TABLE_NAME); +const resultView = base.getViewByName(resultTable, RESULT_VIEW_NAME); + +// Object to save every reocurring item and its count +const elements = {}; + +// Get all items in Origin table +const originRows = base.getRows(originTable, originView); + +// Fill elements with every item, count and quantity +originRows.forEach(row => { + + const clientName = row[originClientColumnName]; + const quantity = Math.abs(row[originQuantityColumnName]); + const element = { "count": 0, "quantity": 0 }; + + if (elements[clientName]) { + element.count = elements[clientName].count; + element.quantity = elements[clientName].quantity; + element["count"] += 1; + element["quantity"] += quantity; + elements[clientName]["count"] = element.count; + elements[clientName]["quantity"] = element.quantity; + } else { + + elements[clientName] = {}; + elements[clientName].count = 1 + elements[clientName].quantity = quantity; + } +}); + +// Create array of arrays from object 'elements' +clientsList = Object.entries(elements); + +// If Table is not empty, delete all rows +const resultRows = base.getRows(resultTable, resultView); +if (resultRows.length > 0) { + const row_ids = resultRows.map(row => row._id); + row_ids.map(row_id => base.deleteRowById(resultTable, row_id)); +} + +// Add new rows +clientsList.forEach((client) => { + + clientData = { + [resultClientColumnName]: client[0], + [resultCountColumnName]: client[1].count, + [resultQuantityColumnName]: client[1].quantity + }; + base.addRow(resultTable, clientData); + +});