/* Financial Transaction Pane ** ** This outline pane allows a user to interact with a transaction ** downloaded from a bank statement, annotting it with classes and comments, ** trips, etc */ import * as UI from 'solid-ui' import * as $rdf from 'rdflib' const ns = UI.ns export default { // icon: (module.__dirname || __dirname) + '22-pixel-068010-3d-transparent-glass-icon-alphanumeric-dollar-sign.png', icon: UI.icons.iconBase + 'noun_106746.svg', name: 'transaction', audience: [ns.solid('PowerUser')], // Does the subject deserve this pane? label: function (subject, context) { const kb = context.session.store const t = kb.findTypeURIs(subject) if (t['http://www.w3.org/2000/10/swap/pim/qif#Transaction']) return '$$' if (kb.any(subject, UI.ns.qu('amount'))) return '$$$' // In case schema not picked up // if (t['http://www.w3.org/2000/10/swap/pim/qif#Period']) return "period $" if (t['http://www.w3.org/ns/pim/trip#Trip']) return 'Trip $' return null // No under other circumstances (while testing at least!) }, render: function (subject, context) { const dom = context.dom const kb = context.session.store const fetcher = kb.fetcher const Q = $rdf.Namespace('http://www.w3.org/2000/10/swap/pim/qif#') const TRIP = $rdf.Namespace('http://www.w3.org/ns/pim/trip#') const div = dom.createElement('div') div.setAttribute('class', 'transactionPane') const mention = function mention (message, style) { if (style === undefined) style = 'color: grey' const pre = dom.createElement('pre') pre.setAttribute('style', style) div.appendChild(pre) pre.appendChild(dom.createTextNode(message)) } const complain = function complain (message) { return mention(message, 'color: #100; background-color: #fee') } /* var thisPane = this var rerender = function (div) { var parent = div.parentNode var div2 = thisPane.render(subject, dom) parent.replaceChild(div2, div) } */ // ////////////////////////////////////////////////////////////////////////////// const plist = kb.statementsMatching(subject) const qlist = kb.statementsMatching(undefined, undefined, subject) const t = kb.findTypeURIs(subject) // var me = authn.currentUser() const predicateURIsDone = {} const donePredicate = function (pred) { predicateURIsDone[pred.uri] = true } const setPaneStyle = function (account) { let mystyle = 'padding: 0.5em 1.5em 1em 1.5em; ' if (account) { const backgroundColor = kb.any(account, UI.ns.ui('backgroundColor')) if (backgroundColor) { mystyle += 'background-color: ' + backgroundColor.value + '; ' } } div.setAttribute('style', mystyle) } // setPaneStyle() // Functions for displaying lists of transactions // Click on the transaction line to expand it into a pane // Shift-click to expand without collapsing others const d2 = function (n) { if (n === undefined) return '' const s = '' + n if (s.indexOf('.') >= 0) { return s.split('.')[0] + '.' + (s.split('.')[1] + '00').slice(0, 2) } return s + '.00' } const numericCell = function numericCell (amount, suppressZero) { const td = dom.createElement('td') if (!(0.0 + amount === 0.0 && suppressZero)) { td.textContent = d2(amount) } td.setAttribute('style', 'width: 6em; text-align: right; ') return td } const headerCell = function headerCell (str) { const td = dom.createElement('th') td.textContent = str td.setAttribute('style', 'text-align: right; ') return td } const oderByDate = function (x, y) { const dx = kb.any(x, ns.qu('date')) const dy = kb.any(y, ns.qu('date')) if (dx !== undefined && dy !== undefined) { if (dx.value < dy.value) return -1 if (dx.value > dy.value) return 1 } if (x.uri < y.uri) return -1 // Arbitrary but repeatable if (x.uri > y.uri) return 1 return 0 } const insertedPane = function (context, subject, paneName) { const p = context.session.paneRegistry.byName(paneName) const d = p.render(subject, context) d.setAttribute('style', 'border: 0.1em solid green;') return d } const expandAfterRow = function (dom, row, subject, paneName, solo) { const siblings = row.parentNode.children if (solo) { for (let j = siblings.length - 1; j >= 0; j--) { if (siblings[j].expanded) { siblings[j].parentNode.removeChild(siblings[j].expanded) siblings[j].expanded = false } } } const tr = dom.createElement('tr') const td = tr.appendChild(dom.createElement('td')) td.setAttribute( 'style', 'width: 98%; padding: 1em; border: 0.1em solid grey;' ) const cols = row.children.length if (row.nextSibling) { row.parentNode.insertBefore(tr, row.nextSibling) } else { row.parentNode.appendChild(tr) } row.expanded = tr td.setAttribute('colspan', '' + cols) td.appendChild(insertedPane(context, subject, paneName)) } const expandAfterRowOrCollapse = function ( dom, row, subject, paneName, solo ) { if (row.expanded) { row.parentNode.removeChild(row.expanded) row.expanded = false } else { expandAfterRow(dom, row, subject, paneName, solo) } } const transactionTable = function (dom, list, filter) { const table = dom.createElement('table') table.setAttribute( 'style', 'padding-left: 0.5em; padding-right: 0.5em; font-size: 9pt; width: 85%;' ) const transactionRow = function (dom, x) { const tr = dom.createElement('tr') const setTRStyle = function (tr, account) { // var mystyle = "padding: 0.5em 1.5em 1em 1.5em; " let mystyle = '\'padding-left: 0.5em; padding-right: 0.5em; padding-top: 0.1em;' if (account) { const backgroundColor = kb.any(account, UI.ns.ui('backgroundColor')) if (backgroundColor) { mystyle += 'background-color: ' + backgroundColor.value + '; ' } } tr.setAttribute('style', mystyle) } const account = kb.any(x, ns.qu('toAccount')) setTRStyle(tr, account) const c0 = tr.appendChild(dom.createElement('td')) const date = kb.any(x, ns.qu('date')) c0.textContent = date ? date.value.slice(0, 10) : '???' c0.setAttribute('style', 'width: 7em;') const c1 = tr.appendChild(dom.createElement('td')) c1.setAttribute('style', 'width: 36em;') const payee = kb.any(x, ns.qu('payee')) c1.textContent = payee ? payee.value : '???' const a1 = c1.appendChild(dom.createElement('a')) a1.textContent = ' ➜' a1.setAttribute('href', x.uri) const c3 = tr.appendChild(dom.createElement('td')) const amount = kb.any(x, ns.qu('in_USD')) c3.textContent = amount ? d2(amount.value) : '???' c3.setAttribute('style', 'width: 6em; text-align: right; ') // @@ decimal alignment? tr.addEventListener( 'click', function (e) { // solo unless shift key expandAfterRowOrCollapse(dom, tr, x, 'transaction', !e.shiftKey) }, false ) return tr } const list2 = filter ? list.filter(filter) : list.slice() // don't sort a paramater passed in place list2.sort(oderByDate) for (let i = 0; i < list2.length; i++) { table.appendChild(transactionRow(dom, list2[i])) } return table } // Render a single transaction // This works only if enough metadata about the properties can drive the RDFS // (or actual type statements whichtypically are NOT there on) if ( t['http://www.w3.org/2000/10/swap/pim/qif#Transaction'] || kb.any(subject, ns.qu('toAccount')) ) { // var trip = kb.any(subject, WF('trip')) donePredicate(ns.rdf('type')) const account = kb.any(subject, UI.ns.qu('toAccount')) setPaneStyle(account) if (!account) { complain( '(Error: There is no bank account known for this transaction <' + subject.uri + '>,\n -- every transaction needs one.)' ) } let store = null const statement = kb.any(subject, UI.ns.qu('accordingTo')) if (statement === undefined) { complain( '(Error: There is no back link to the original data source foir this transaction <' + subject.uri + '>,\nso I can\'t tell how to annotate it.)' ) } else { store = statement !== undefined ? kb.any(statement, UI.ns.qu('annotationStore')) : null if (store === undefined) { complain( '(There is no annotation document for this statement\n<' + statement.uri + '>,\nso you cannot classify this transaction.)' ) } } const nav = dom.createElement('div') nav.setAttribute('style', 'float:right') div.appendChild(nav) const navLink = function (pred, label) { donePredicate(pred) const obj = kb.any(subject, pred) if (!obj) return const a = dom.createElement('a') a.setAttribute('href', obj.uri) a.setAttribute('style', 'float:right') nav.appendChild(a).textContent = label || UI.utils.label(obj) nav.appendChild(dom.createElement('br')) } navLink(UI.ns.qu('toAccount')) navLink(UI.ns.qu('accordingTo'), 'Statement') navLink(TRIP('trip')) // Basic data: const table = dom.createElement('table') div.appendChild(table) const preds = ['date', 'payee', 'amount', 'in_USD', 'currency'].map(Q) const inner = preds .map(function (p) { donePredicate(p) const value = kb.any(subject, p) const s = value ? UI.utils.labelForXML(value) : '' return ( '