@midlandsbank/node-nacha
Version:
NACHA ACH EFT File Parser/Formatter for CCD+ / PPD+ / CTX+
122 lines (118 loc) • 3.82 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var calculate;
module.exports = calculate = function(options) {
var batch, i, j, len, ref, result, results;
results = {
footer: {},
batchFooters: []
};
options.seq = {
entry: 1
};
ref = options.ach.batches;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
batch = ref[i];
options.batch = batch;
result = calculate.batch(options);
results.batchFooters.push(result.footer);
}
result = calculate.file(options);
results.footer = result.footer;
return results;
};
calculate.file = function(options) {
var batch, batchFooter, batchIndex, file, footer, j, len, ref;
file = options.ach.file;
footer = {
recordType: '9',
batchCount: 0,
entryAndAddendaCount: 0,
totalDebit: 0,
totalCredit: 0,
entryHash: 0
};
ref = options.ach.batches;
for (batchIndex = j = 0, len = ref.length; j < len; batchIndex = ++j) {
batch = ref[batchIndex];
batchFooter = batch.footer;
footer.batchCount++;
footer.entryHash += batchFooter.entryHash;
footer.entryAndAddendaCount += batchFooter.entryAndAddendaCount;
footer.totalDebit += batchFooter.totalDebit;
footer.totalCredit += batchFooter.totalCredit;
if (options.set) {
batch.num = batchFooter.num = batchIndex + 1;
}
}
footer.lineCount = 2 + (footer.batchCount * 2) + footer.entryAndAddendaCount;
footer.blockCount = Number(("" + (Math.ceil(footer.lineCount / 10))).slice(-6));
footer.entryHash = Number(("" + footer.entryHash).slice(-10));
if (options.set) {
file.footer = footer;
}
return {
footer: footer
};
};
calculate.batch = function(options) {
var addenda, batch, entry, footer, i, j, k, len, len1, ref, ref1, ref2, ref3, ref4, sequence;
batch = options.batch;
footer = {
recordType: '8',
entryAndAddendaCount: 0,
totalDebit: 0,
totalCredit: 0,
entryHash: 0,
companyId: batch.companyId,
serviceClassCode: batch.serviceClassCode,
originatingDFIIdentification: batch.originatingDFIIdentification
};
ref = batch.entries;
for (j = 0, len = ref.length; j < len; j++) {
entry = ref[j];
footer.entryHash += Number(entry.receivingDFIIdentification);
footer.entryAndAddendaCount += entry.addenda != null ? 2 : entry.addendas != null ? entry.addendas.length + 1 : 1;
switch (entry.transactionCode[1]) {
case '7':
case '8':
footer.totalDebit += entry.amount;
if (batch.serviceClassCode == null) {
batch.serviceClassCode = footer.serviceClassCode = '225';
} else if (batch.serviceClassCode === '220') {
batch.serviceClassCode = footer.serviceClassCode = '200';
}
break;
case '2':
case '3':
footer.totalCredit += entry.amount;
if (batch.serviceClassCode == null) {
batch.serviceClassCode = footer.serviceClassCode = '220';
} else if (batch.serviceClassCode === '225') {
batch.serviceClassCode = footer.serviceClassCode = '200';
}
}
if (options.set && (((ref1 = options.seq) != null ? ref1.entry : void 0) != null) && (entry.traceNumber == null)) {
sequence = ('0000000' + options.seq.entry).slice(-7);
entry.traceNumber = batch.originatingDFIIdentification + sequence;
}
if (options != null) {
if ((ref2 = options.seq) != null) {
ref2.entry++;
}
}
if (options.set && ((ref3 = entry.addendas) != null ? ref3.length : void 0) > 0) {
ref4 = entry.addendas;
for (i = k = 0, len1 = ref4.length; k < len1; i = ++k) {
addenda = ref4[i];
addenda.sequenceNumber = i;
}
}
}
footer.entryHash = Number(("" + footer.entryHash).slice(-10));
if (options.set) {
batch.footer = footer;
}
return {
footer: footer
};
};