diff --git a/lib/cashbox-batches.js b/lib/cashbox-batches.js index 52f41dc61..35a03d9ed 100644 --- a/lib/cashbox-batches.js +++ b/lib/cashbox-batches.js @@ -6,7 +6,7 @@ const camelize = require('./utils') function createCashboxBatch (deviceId, cashboxCount) { if (_.isEqual(0, cashboxCount)) throw new Error('Cash box is empty. Cash box batch could not be created.') - const sql = `INSERT INTO cash_unit_operation (id, device_id, created, operation_type) VALUES ($1, $2, now(), 'cash-box-empty')` + const sql = `INSERT INTO cash_unit_operation (id, device_id, created, operation_type) VALUES ($1, $2, now(), 'cash-box-empty') RETURNING *` const sql2 = ` UPDATE bills SET cashbox_batch_id=$1 FROM cash_in_txs @@ -25,6 +25,7 @@ function createCashboxBatch (deviceId, cashboxCount) { const q2 = t.none(sql2, [batchId, deviceId]) const q3 = t.none(sql3, [batchId, deviceId]) return t.batch([q1, q2, q3]) + .then(([it]) => it) }) } @@ -100,14 +101,6 @@ function editBatchById (id, performedBy) { return db.none(sql, [performedBy, id]) } -function getBillsByBatchId (id) { - const sql = `SELECT bi.* FROM ( - SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, cit.device_id AS device_id FROM bills b LEFT OUTER JOIN (SELECT id, device_id FROM cash_in_txs) AS cit ON cit.id = b.cash_in_txs_id UNION - SELECT id, fiat, fiat_code, created, cashbox_batch_id, device_id FROM empty_unit_bills - ) AS bi WHERE bi.cashbox_batch_id=$1` - return db.any(sql, [id]) -} - function logFormatter (data) { return _.map( it => { @@ -124,11 +117,62 @@ function logFormatter (data) { ) } +function getMachineUnbatchedBills (deviceId) { + const sql = ` + SELECT now() AS created, cash_in_txs.device_id, json_agg(b.*) AS bills FROM bills b LEFT OUTER JOIN cash_in_txs + ON b.cash_in_txs_id = cash_in_txs.id + WHERE b.cashbox_batch_id IS NULL AND cash_in_txs.device_id = $1 + GROUP BY cash_in_txs.device_id + ` + + return db.oneOrNone(sql, [deviceId]) + .then(res => _.mapKeys(it => _.camelCase(it), res)) + .then(logFormatterSingle) +} + +function getBatchById (id) { + const sql = ` + SELECT cb.id, cb.device_id, cb.created, cb.operation_type, cb.bill_count_override, cb.performed_by, json_agg(b.*) AS bills + FROM cashbox_batches AS cb + LEFT JOIN bills AS b ON cb.id = b.cashbox_batch_id + WHERE cb.id = $1 + GROUP BY cb.id + ` + + return db.oneOrNone(sql, [id]).then(res => _.mapKeys(it => _.camelCase(it), res)) + .then(logFormatterSingle) +} + +function logFormatterSingle (data) { + const bills = _.filter( + it => !(_.isNil(it) || _.isNil(it.fiat_code) || _.isNil(it.fiat) || _.isNaN(it.fiat)), + data.bills + ) + + return { + id: data.id, + deviceId: data.deviceId, + created: data.created, + operationType: data.operationType, + billCount: _.size(bills), + fiatTotals: _.reduce( + (acc, value) => { + acc[value.fiat_code] = (acc[value.fiat_code] || 0) + value.fiat + return acc + }, + {}, + bills + ), + billsByDenomination: _.countBy(it => `${it.fiat} ${it.fiat_code}`, bills) + } +} + module.exports = { createCashboxBatch, updateMachineWithBatch, getBatches, - getBillsByBatchId, editBatchById, + getBatchById, + getMachineUnbatchedBills, logFormatter } diff --git a/lib/routes/cashboxRoutes.js b/lib/routes/cashboxRoutes.js index cec844163..6fcc88bf2 100644 --- a/lib/routes/cashboxRoutes.js +++ b/lib/routes/cashboxRoutes.js @@ -1,40 +1,41 @@ const express = require('express') +const _ = require('lodash/fp') const router = express.Router() const cashbox = require('../cashbox-batches') const notifier = require('../notifier') -const { getMachine, setMachine } = require('../machine-loader') +const { getMachine, setMachine, getMachineName } = require('../machine-loader') const { loadLatestConfig } = require('../new-settings-loader') const { getCashInSettings } = require('../new-config-manager') const { AUTOMATIC } = require('../constants') const logger = require('../logger') -function notifyCashboxRemoval (req, res, next) { + +function cashboxRemoval (req, res, next) { const operatorId = res.locals.operatorId - logger.info(`** DEBUG ** - Cashbox removal - Received a cashbox opening request from device ${req.deviceId}`) + notifier.cashboxNotify(req.deviceId).catch(logger.error) - return notifier.cashboxNotify(req.deviceId) - .then(() => Promise.all([getMachine(req.deviceId), loadLatestConfig()])) + return Promise.all([getMachine(req.deviceId), loadLatestConfig()]) .then(([machine, config]) => { - logger.info('** DEBUG ** - Cashbox removal - Retrieving system options for cash-in') const cashInSettings = getCashInSettings(config) if (cashInSettings.cashboxReset !== AUTOMATIC) { - logger.info('** DEBUG ** - Cashbox removal - Cashbox reset is set to manual. A cashbox batch will NOT be created') - logger.info(`** DEBUG ** - Cashbox removal - Process finished`) - return res.status(200).send({ status: 'OK' }) + return Promise.all([ + cashbox.getMachineUnbatchedBills(req.deviceId), + getMachineName(req.deviceId) + ]) } - logger.info('** DEBUG ** - Cashbox removal - Cashbox reset is set to automatic. A cashbox batch WILL be created') - logger.info('** DEBUG ** - Cashbox removal - Creating new batch...') - return cashbox.createCashboxBatch(req.deviceId, machine.cashUnits.cashbox) - .then(() => { - logger.info(`** DEBUG ** - Cashbox removal - Process finished`) - return res.status(200).send({ status: 'OK' }) - }) + return cashbox.createCashboxBatch(req.deviceId, machine.cashbox) + .then(batch => Promise.all([ + cashbox.getBatchById(batch.id), + getMachineName(batch.device_id), + setMachine({ deviceId: req.deviceId, action: 'emptyCashInBills' }, operatorId) + ])) }) + .then(([batch, machineName]) => res.status(200).send({ batch: _.merge(batch, { machineName }), status: 'OK' })) .catch(next) } -router.post('/removal', notifyCashboxRemoval) +router.post('/removal', cashboxRemoval) module.exports = router