feat: add cash collection receipts

This commit is contained in:
Sérgio Salgado 2022-07-14 22:11:15 +01:00 committed by Rafael
parent d0573daa74
commit eac9f2cf69
2 changed files with 72 additions and 27 deletions

View file

@ -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