Skip to content

Commit

Permalink
get orgao and mes_ano_referencia from contracheque sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
viniagostini committed May 25, 2019
1 parent a670680 commit 723cf1d
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 31 deletions.
4 changes: 4 additions & 0 deletions src/error_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ module.exports = {
message: `The following error was found when converting the JSON data to CSV: [${error}]`,
code: 4
}),
SPREASHEET_DATA_NOT_FOUND: data => ({
message: `The following data was not found on the spreadsheet: [${data}]`,
code: 5
}),
}
70 changes: 69 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,68 @@ const _getContrachequeData = spreadSheet => {
return _getSheetData(contrachequeModel, _getSheet(CONTRACHEQUE_KEYWORD, spreadSheet));
};


const _getOrgao = contrachequeSheet => {
let orgao = "";
const orgaoLabel = "Órgão";
const found = contrachequeSheet.some(line => {
if (line[0] && line[0].trim() === orgaoLabel) {
orgao = line.reduce((acc, el) => {
return !!el ? el : acc;
}, "");
return true;
}
return false;
});
if (!found) {
const { message, code } = errorMessages.SPREASHEET_DATA_NOT_FOUND(orgaoLabel);
throw new APIError(message, httpStatus.NOT_FOUND, code);
}
return orgao;
};

const excelDateToJSDate = date => new Date(Math.round((date - 25569) * 86400 * 1000));
const isLiteralDateMMYYYY = str => /^(0[\d]|1[0-2])\/?([\d]{4})$/.test(str);
const isLiteralDateMMYY = str => /^(0[\d]|1[0-2])\/?([\d]{2})$/.test(str);

const _getMesReferencia = contrachequeSheet => {
let mesReferencia = "";
const mesLabel = "Mês/Ano de Referência";
const found = contrachequeSheet.some(line => {
if (line[0] && isNaN(line[0]) && line[0].trim() === mesLabel) {
mesReferencia = line.reduce((acc, el, i) => {
if (i === 0) return acc;
if (!!el) {
//el is the excel date format (number of days after 1/1/1900)
if (!isNaN(el) && typeof el !== "object") {
const d = excelDateToJSDate(el);
return `${d.getUTCFullYear()}-${d.getUTCMonth() + 1}`;
//el is a literal string date on format mm/yy
} else if (isLiteralDateMMYY(el)) {
const [mm, yy] = el.split("/");
return `20${yy}-${Number(mm)}`;
//el is a literal string date on format mm/yyyy
} else if (isLiteralDateMMYYYY(el)) {
const [mm, yyyy] = el.split("/");
return `${yyyy}-${Number(mm)}`;
//el is an Date generated by xslx lib
} else {
const d = new Date(el);
return `${d.getUTCFullYear()}-${d.getUTCMonth() + 1}`;
}
}
return acc;
}, "");
return true;
}
return false;
});
if (!found) {
const { message, code } = errorMessages.SPREASHEET_DATA_NOT_FOUND(mesLabel);
throw new APIError(message, httpStatus.NOT_FOUND, code);
}
return mesReferencia;
};
/**
* Get the subsidio data from the spreadsheet.
*
Expand Down Expand Up @@ -367,13 +429,19 @@ const parse = spreadsheet => {
const direitosEventuais = _convertToNameHashTable(_getDireitosEventuaisData(spreadsheet));
const dadosCadastrais = _convertToNameHashTable(_getDadosCadastraisData(spreadsheet));

const contrachequeSheet = _getSheet(CONTRACHEQUE_KEYWORD, spreadsheet);
const orgao = _getOrgao(contrachequeSheet);
const mes_ano_referencia = _getMesReferencia(contrachequeSheet);

return Object.keys(contrachequeData).map(name => {
return {
...contrachequeData[name],
...subsidioData[name],
...indenizacoesData[name],
...direitosEventuais[name],
...dadosCadastrais[name],
orgao,
mes_ano_referencia,
};
});
};
Expand All @@ -382,5 +450,5 @@ module.exports = {
parse, _getHeaderLine, _getSheet, _getSheetData, _getContrachequeData, _getSubsidioData,
_cleanData, _getHeader, _getOutraAndDetalheColumns, _joinOutraColumns, _joinDetalheColumns,
_filterOutraAndDetalheColumns, _getIndenizacoesData, _getDireitosEventuaisData, _getDadosCadastraisData,
_convertToNameHashTable
_convertToNameHashTable, _getOrgao, _getMesReferencia
};
13 changes: 9 additions & 4 deletions src/xlsx_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ const httpStatus = require('http-status');
const errorMessages = require('./error_messages');
const convertSpreadsheetToJson = spreadsheetBuffer => {
try {
const workbook = xlsx.read(spreadsheetBuffer, { type: 'buffer' });
const workbook = xlsx.read(spreadsheetBuffer, {
type: "buffer",
cellDates: true,
cellNF: false,
cellText: false
});
const spreadsheetObj = {};
workbook.SheetNames.forEach(sheetName => {
spreadsheetObj[sheetName] = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName], { header: 1 })
spreadsheetObj[sheetName] = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName], { header: 1, dateNF: "YYYY-MM" })
});
return spreadsheetObj;
} catch (e) {
const {message, code} = errorMessages.XLSX_TO_JSON_ERROR(e);
throw new APIError(message, httpStatus.BAD_REQUEST, code, e.stack);
const { message, code } = errorMessages.XLSX_TO_JSON_ERROR(e);
throw new APIError(message, httpStatus.BAD_REQUEST, code, e.stack);
}
};

Expand Down
Loading

0 comments on commit 723cf1d

Please sign in to comment.