docparse-dumpinvoices
Version:
takes a list of couchdb invoice ids and exports the data contained within those invoices to an Excel spreadsheet
103 lines (97 loc) • 3.26 kB
JavaScript
var getNotes = require('./lib/getNotes')
var costsWithoutSalesTax = require('./lib/costsWithoutSalesTax')
var costsWithoutLatePayments = require('./lib/costsWithoutLatePayments')
var getSalesTaxCostValue = require('./lib/getSalesTaxCostValue')
var getLatePaymentChargesValue = require('./lib/getLatePaymentChargesValue')
var getLatePaymentCreditsValue = require('./lib/getLatePaymentCreditsValue')
var num = require('num')
var moment = require('moment')
var es = require('event-stream')
var async = require('async')
var inspect = require('eyespect').inspector({maxLength: 9999999})
var logger = require('loggly-console-logger')
module.exports = function (data, callback) {
var invoiceIDs = data.invoiceIDs
var db = data.db
var opts = {
include_docs: true,
keys: invoiceIDs
}
async.map(
invoiceIDs,
function(id, cb) {
logger.debug('getting invoice by id',{
invoiceID: id,
section: 'getFeed',
role: 'export'
})
db.get(id, function(err, doc) {
if (err) {
return cb({
message: 'failed to get invoice by id',
id: id,
error: err,
stack: new Error().stack
})
}
var invoice = doc.json
var fromDate = moment(invoice.fromDate).utc().startOf('day')
var toDate = moment(invoice.toDate).utc().startOf('day')
invoice.fromDate = fromDate.format('YYYY-MM-DD')
invoice.toDate = toDate.format('YYYY-MM-DD')
invoice = parseInvoice(invoice)
cb(null, invoice)
})
},
function(err, invoices) {
if (err) { return callback(err) }
invoiceIDs = null
var counts = invoices.reduce(function (prev, doc) {
var invoiceType = doc.invoiceType
var typeOfSupply = invoiceType.type
var value = prev[typeOfSupply] || 0
prev[typeOfSupply] = value + 1
return prev
}, {})
inspect(counts, 'counts')
var reader = es.readArray(invoices)
callback(null, reader)
reader = null
})
}
function parseInvoice(invoice) {
// trim spaces
if (invoice.costDelivery) {
invoice.costDelivery = invoice.costDelivery.replace(/\s/g,'')
}
if (invoice.costSupply) {
invoice.costSupply = invoice.costSupply.replace(/\s/g,'')
}
// negative to pay values should output as 0
if (invoice.toPay) {
var value = num(invoice.toPay)
if (value.lt(0)) {
invoice.toPay = '0'
}
}
var costs = invoice.costOther.costs.map(function (cost) {
var value = cost.value.replace(/\s/g,'')
cost.value = value
return cost
})
var notesCosts = costsWithoutSalesTax(costs)
invoice.latePaymentCharges = getLatePaymentChargesValue(notesCosts)
invoice.latePaymentCredits = getLatePaymentCreditsValue(notesCosts)
var otherCosts = costsWithoutLatePayments(notesCosts)
invoice.costSalesTax = getSalesTaxCostValue(costs)
invoice.costSupplyWithoutSalesTax = num(invoice.costSupply).sub(invoice.costSalesTax).toString()
invoice.notes = getNotes(notesCosts)
var total = otherCosts.reduce(function (a, b) {
return a.add(b.value)
}, num(0)).toString()
invoice.costOther.total = total
// if (costs.length > 0) {
// inspect(costs,'costs')
// }
return invoice
}