@gameroom/cli
Version:
A command line tool for Gameroom
89 lines (83 loc) • 5.27 kB
JavaScript
const Table = require('tabley'),
{ models: { Payment, Sale, Store } } = require('@gameroom/kit'),
{ dateAddDays, dateLocaleString, dateString, getAll, grGreen, printTable } = require('../../helpers'),
{ spinner } = require('../../refs')
module.exports = async ({ start_at, end_at }) => {
spinner.start()
spinner.info(`generating ${grGreen('payments')} report`)
spinner.info(`from ${grGreen(start_at.toLocaleString())} to ${grGreen(end_at.toLocaleString())}`)
const filter = { and: [
{ key: 'updated_at', comparison: '>=', value: start_at },
{ key: 'updated_at', comparison: '<', value: end_at }
] }
spinner.text = `getting ${grGreen('stores')}`
const stores = await Store.get({ sort: { name: 1 }})
spinner.succeed(`got ${stores.length} ${grGreen('stores')}`)
spinner.text = `getting ${grGreen('sales')}`
const sales = await getAll(Sale, { filter })
spinner.succeed(`got ${sales.length} ${grGreen('sales')}`)
spinner.text = `getting ${grGreen('payments')}`
const payments = await getAll(Payment, { filter })
spinner.succeed(`got ${payments.length} ${grGreen('payments')}`)
stores.push({ name: 'All Stores' })
// formatter
const value = (v) => `${(v * .01).toFixed(2)}`
for (const store of stores) {
const store_sales = store.id ? sales.filter(s => s.store_id === store.id) : sales
if (!store_sales.length) continue
const store_payments = store.id ? payments.filter(p => store_sales.find(s => s.id === p.sale_id)) : payments
const total = {
day: 'Total',
card_in: store_payments.map(p => p.gateway === 'stripe_terminal' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0) + store_payments.map(p => p.gateway === 'stripe' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
stripe_out: store_payments.map(p => p.gateway === 'stripe' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p, 0),
cash_in: store_payments.map(p => p.gateway === 'cash' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
cash_out: store_payments.map(p => p.gateway === 'cash' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p, 0),
store_credit_in: store_payments.map(p => p.gateway === 'store_credit' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
store_credit_out: store_payments.map(p => p.gateway === 'store_credit' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p, 0),
gift_certificate_in: store_payments.map(p => p.gateway === 'gift_certificate' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
// gift_certificate_out: store_payments.map(p => p.gateway === 'gift_certificate' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p)
}
const rows = []
let start = start_at
while (start < end_at) {
const end = dateAddDays(start, 1)
// const day_sales = store_sales.filter(s => start < s.created_at && s.created_at <= end)
const day_payments = store_payments.filter(p => start < p.created_at.toDate() && p.created_at.toDate() <= end)
const row = {
day: start.toLocaleDateString(),
card_in: day_payments.map(p => p.gateway === 'stripe_terminal' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0) + day_payments.map(p => p.gateway === 'stripe' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
stripe_out: day_payments.map(p => p.gateway === 'stripe' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p, 0),
cash_in: day_payments.map(p => p.gateway === 'cash' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
cash_out: day_payments.map(p => p.gateway === 'cash' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p, 0),
store_credit_in: day_payments.map(p => p.gateway === 'store_credit' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
store_credit_out: day_payments.map(p => p.gateway === 'store_credit' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p, 0),
gift_certificate_in: day_payments.map(p => p.gateway === 'gift_certificate' && p.amount > 0 ? p.amount : 0).reduce((t, p) => t += p, 0),
// gift_certificate_out: day_payments.map(p => p.gateway === 'gift_certificate' && p.amount < 0 ? Math.abs(p.amount) : 0).reduce((t, p) => t += p)
}
rows.push(row)
start = end
}
const table = new Table(rows, {
title: grGreen(store.name),
columns: [
{ key: 'day', title: 'Day' },
{ key: 'card_in', title: 'Card In', value },
{ key: 'stripe_out', title: 'Manual Card Out', value, hidden: total.stripe_out === 0 },
{ key: 'cash_in', title: 'Cash In', value },
{ key: 'cash_out', title: 'Cash Out', value },
{ key: 'store_credit_in', title: 'Credit In', value },
{ key: 'store_credit_out', title: 'Credit Out', value },
{ key: 'gift_certificate_in', title: 'Gift Cert In', value },
// { key: 'online_in', title: 'Online', value },
],
meta: [ total ],
align: 'center',
margin: 4
})
spinner.stop()
console.log()
table.print()
console.log()
}
spinner.succeed(`generated ${grGreen('payments')} report`).stop()
}