@adncorp/account-book-parser-adapter
Version:
parsing sheet adapter for account book
174 lines (146 loc) • 5.21 kB
JavaScript
;
/* eslint-disable spellcheck/spell-checker */
/* eslint-disable camelcase */
/**
* Build by modifying te following package source code : https://github.com/rahil471/node-xls-json
*/
var xlsx = require('xlsx');
var cvcsv = require('csv');
var fileUtils = require('../utils/fileUtils.js');
var xlsxTocsv = require('../utils/xlsxTocsv.js'); // eslint-disable-next-line camelcase
exports = module.exports = XLSXJson;
function XLSXJson(config, callback) {
if (!config.input) {
console.error('You miss an input file');
process.exit(1);
} // eslint-disable-next-line no-unused-vars
var cv = new CV(config, callback);
}
function CV(config, callback) {
this.loadXlsx(config.input).then(function (wss) {
var wb = wss;
var ws = getWS(config, wb); // console.log(ws)
// skip line and start à line
var range = xlsx.utils.decode_range(ws['!ref']);
range.s.r += config.skip === undefined || config.skip === null ? 0 : config.skip;
ws['!ref'] = xlsx.utils.encode_range(range);
var csv = getCSV(ws);
cvjson(csv, config.lowerCaseHeaders, config.mapping, callback);
})["catch"](function (err) {
if (err) {
callback(err, [], config.lowerCaseHeaders);
} else {
callback(null, [], config.lowerCaseHeaders);
}
});
}
CV.prototype.loadXlsx = function (input) {
if (fileUtils.isUri(input)) {
return new Promise(function (resolve, reject) {
fileUtils.readFileUri(input).then(function (data) {
resolve(xlsx.read(data, {
type: 'buffer'
}));
})["catch"](function (err) {
reject(err);
});
});
}
return new Promise(function (resolve) {
resolve(xlsx.readFile(input));
});
};
var getWS = function getWS(config, wb) {
var targetSheet = config.sheet;
if (targetSheet == null) {
targetSheet = wb.SheetNames[0];
}
var ws = wb.Sheets[targetSheet];
return ws;
};
var getCSV = function getCSV(ws) {
var csvFile = xlsxTocsv.sheet_to_csv(ws);
return csvFile;
};
var cvjson = function cvjson(csv, lowerCaseHeaders, mapping, callback, settings) {
var record = [];
var accountbookobject = {};
var account = '';
var values = [];
var account_label = '';
var tour = false;
var fisrtSave = false;
var columnMap = mapping === undefined || mapping === null ? [] : mapping.slice(0); // clone
var header = columnMap;
var validateHeader = true;
cvcsv().from.string(csv).on('record', function (row, index) {
if (index === 7) {
if (row[0] === undefined || row[0] === '' || row[0] === null || row[10] === undefined || row[10] === '' || row[10] === null || row[13] === undefined || row[13] === '' || row[13] === null) {
validateHeader = false;
} else if (row[0].replace(/\n|\r/g, '').trim().toLowerCase() !== 'date' && row[10].replace(/\n|\r/g, '').trim().toLowerCase() !== 'mouvement débit' && row[13].replace(/\n|\r/g, '').trim().toLowerCase() !== 'mouvement crédit') {
validateHeader = false;
}
}
if (validateHeader) {
if (index > 10) {
if (tour === false) {
if (row[0] !== '') {
account = row[0].trim().toLowerCase();
account_label = row[2].trim().toLowerCase();
tour = true;
}
} else {
if (row[1].toLowerCase() !== 'total compte' && tour) {
if (row[0].toLowerCase() !== '') {
var object = {};
row.forEach(function (column, index) {
if (index === 0) {
object['date'] = row[index];
}
if (index === 1) {
object['journal'] = row[index];
}
if (index === 2) {
object['reference'] = row[index];
}
if (index === 11) {
object['debit'] = row[index];
}
if (index === 14) {
object['credit'] = row[index];
}
if (index === 17) {
object['cumul_bal'] = row[index];
}
}); // ajout des données relatives à un account
values.push(object);
fisrtSave = true;
}
} else {
if (fisrtSave) {
// console.log(row[0].toLowerCase())
accountbookobject.account = account;
accountbookobject.account_label = account_label;
accountbookobject.values = values;
accountbookobject.cumul_bal_debit = row[11];
accountbookobject.cumul_bal_credit = row[14];
accountbookobject.cumul_bal_total = row[17];
record.push(accountbookobject);
values = [];
accountbookobject = {};
tour = false;
fisrtSave = false;
}
}
}
}
}
}).on('end', function (count) {
// when writing to a file, use the 'close' event
// the 'end' event may fire before the file has been written
callback(null, record, header);
}).on('error', function (error) {
console.error(error.message);
});
};
//# sourceMappingURL=parser.js.map