Last active
September 4, 2018 04:09
-
-
Save ReedD/ee67b50eec63fc292ddf0473aa4d2ee8 to your computer and use it in GitHub Desktop.
Sum all transactions since the last statement date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumTransactions(lastStatementDateString) { | |
const lastStatementDate = new Date(lastStatementDateString); | |
return [ | |
'ctl00_ctl00_pagePlaceholder_sectionContent_ucTrans_gvPTV', | |
'ctl00_ctl00_pagePlaceholder_sectionContent_ucPending_gvPending', | |
] | |
.map(id => | |
Array.from(document.querySelectorAll(`#${id} tr`)) | |
.map(row => { | |
const columns = Array.from(row.querySelectorAll('td')).map( | |
r => r.innerText, | |
); | |
if (!columns.length) return 0; | |
if (/payment\-online/i.test(columns[2])) return 0; | |
const date = new Date(columns[1]); | |
const amountMatch = columns[3].match( | |
/(?<negative>[\d\.,]+)\)|(?<positive>[\d\.,]+)/, | |
); | |
if (!amountMatch || date <= lastStatementDate) return 0; | |
const { positive = '0', negative = '0' } = amountMatch.groups; | |
return ( | |
parseFloat(positive.replace(/,/g, '')) + | |
parseFloat(negative.replace(/,/g, '')) * -1 | |
); | |
}) | |
.reduce((sum, value) => value + sum, 0), | |
) | |
.reduce((sum, value) => value + sum, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment