Merge pull request #1762 from RafaelTaranto/backport/collection-receipts

LAM-230 feat: add cash collection receipts
This commit is contained in:
Rafael Taranto 2024-11-29 13:48:17 +00:00 committed by GitHub
commit c90968b18b
2 changed files with 72 additions and 27 deletions

View file

@ -6,7 +6,7 @@ const camelize = require('./utils')
function createCashboxBatch (deviceId, cashboxCount) { function createCashboxBatch (deviceId, cashboxCount) {
if (_.isEqual(0, cashboxCount)) throw new Error('Cash box is empty. Cash box batch could not be created.') 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 = ` const sql2 = `
UPDATE bills SET cashbox_batch_id=$1 UPDATE bills SET cashbox_batch_id=$1
FROM cash_in_txs FROM cash_in_txs
@ -25,6 +25,7 @@ function createCashboxBatch (deviceId, cashboxCount) {
const q2 = t.none(sql2, [batchId, deviceId]) const q2 = t.none(sql2, [batchId, deviceId])
const q3 = t.none(sql3, [batchId, deviceId]) const q3 = t.none(sql3, [batchId, deviceId])
return t.batch([q1, q2, q3]) return t.batch([q1, q2, q3])
.then(([it]) => it)
}) })
} }
@ -100,14 +101,6 @@ function editBatchById (id, performedBy) {
return db.none(sql, [performedBy, id]) 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) { function logFormatter (data) {
return _.map( return _.map(
it => { 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 = { module.exports = {
createCashboxBatch, createCashboxBatch,
updateMachineWithBatch, updateMachineWithBatch,
getBatches, getBatches,
getBillsByBatchId,
editBatchById, editBatchById,
getBatchById,
getMachineUnbatchedBills,
logFormatter logFormatter
} }

View file

@ -1,40 +1,41 @@
const express = require('express') const express = require('express')
const _ = require('lodash/fp')
const router = express.Router() const router = express.Router()
const cashbox = require('../cashbox-batches') const cashbox = require('../cashbox-batches')
const notifier = require('../notifier') const notifier = require('../notifier')
const { getMachine, setMachine } = require('../machine-loader') const { getMachine, setMachine, getMachineName } = require('../machine-loader')
const { loadLatestConfig } = require('../new-settings-loader') const { loadLatestConfig } = require('../new-settings-loader')
const { getCashInSettings } = require('../new-config-manager') const { getCashInSettings } = require('../new-config-manager')
const { AUTOMATIC } = require('../constants') const { AUTOMATIC } = require('../constants')
const logger = require('../logger') const logger = require('../logger')
function notifyCashboxRemoval (req, res, next) {
function cashboxRemoval (req, res, next) {
const operatorId = res.locals.operatorId 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) return Promise.all([getMachine(req.deviceId), loadLatestConfig()])
.then(() => Promise.all([getMachine(req.deviceId), loadLatestConfig()]))
.then(([machine, config]) => { .then(([machine, config]) => {
logger.info('** DEBUG ** - Cashbox removal - Retrieving system options for cash-in')
const cashInSettings = getCashInSettings(config) const cashInSettings = getCashInSettings(config)
if (cashInSettings.cashboxReset !== AUTOMATIC) { if (cashInSettings.cashboxReset !== AUTOMATIC) {
logger.info('** DEBUG ** - Cashbox removal - Cashbox reset is set to manual. A cashbox batch will NOT be created') return Promise.all([
logger.info(`** DEBUG ** - Cashbox removal - Process finished`) cashbox.getMachineUnbatchedBills(req.deviceId),
return res.status(200).send({ status: 'OK' }) getMachineName(req.deviceId)
])
} }
logger.info('** DEBUG ** - Cashbox removal - Cashbox reset is set to automatic. A cashbox batch WILL be created') return cashbox.createCashboxBatch(req.deviceId, machine.cashbox)
logger.info('** DEBUG ** - Cashbox removal - Creating new batch...') .then(batch => Promise.all([
return cashbox.createCashboxBatch(req.deviceId, machine.cashUnits.cashbox) cashbox.getBatchById(batch.id),
.then(() => { getMachineName(batch.device_id),
logger.info(`** DEBUG ** - Cashbox removal - Process finished`) setMachine({ deviceId: req.deviceId, action: 'emptyCashInBills' }, operatorId)
return res.status(200).send({ status: 'OK' }) ]))
})
}) })
.then(([batch, machineName]) => res.status(200).send({ batch: _.merge(batch, { machineName }), status: 'OK' }))
.catch(next) .catch(next)
} }
router.post('/removal', notifyCashboxRemoval) router.post('/removal', cashboxRemoval)
module.exports = router module.exports = router