Merge pull request #1893 from siiky/fix/lam-1462/exclude-bills-of-unpaired-machines
LAM-1462 fix: exclude bills of unpaired machines
This commit is contained in:
commit
25f7bab775
2 changed files with 50 additions and 28 deletions
|
|
@ -139,10 +139,9 @@ const CashCassettes = () => {
|
||||||
|
|
||||||
const timezone = R.path(['config', 'locale_timezone'], data)
|
const timezone = R.path(['config', 'locale_timezone'], data)
|
||||||
|
|
||||||
const bills = R.groupBy(bill => bill.deviceId)(R.path(['bills'])(data) ?? [])
|
const bills = data?.bills ?? []
|
||||||
const deviceIds = R.uniq(
|
const billsByDeviceID = R.groupBy(bill => bill.deviceId)(bills)
|
||||||
R.map(R.prop('deviceId'))(R.path(['bills'])(data) ?? []),
|
const deviceIds = R.keys(billsByDeviceID)
|
||||||
)
|
|
||||||
const cashout = data?.config && fromNamespace('cashOut')(data.config)
|
const cashout = data?.config && fromNamespace('cashOut')(data.config)
|
||||||
const locale = data?.config && fromNamespace('locale')(data.config)
|
const locale = data?.config && fromNamespace('locale')(data.config)
|
||||||
const fiatCurrency = locale?.fiatCurrency
|
const fiatCurrency = locale?.fiatCurrency
|
||||||
|
|
@ -195,7 +194,7 @@ const CashCassettes = () => {
|
||||||
const InnerCashUnitDetails = ({ it }) => (
|
const InnerCashUnitDetails = ({ it }) => (
|
||||||
<CashUnitDetails
|
<CashUnitDetails
|
||||||
machine={it}
|
machine={it}
|
||||||
bills={bills[it.id] ?? []}
|
bills={billsByDeviceID[it.id] ?? []}
|
||||||
currency={fiatCurrency}
|
currency={fiatCurrency}
|
||||||
config={config}
|
config={config}
|
||||||
/>
|
/>
|
||||||
|
|
@ -291,7 +290,7 @@ const CashCassettes = () => {
|
||||||
currencyCode={fiatCurrency}
|
currencyCode={fiatCurrency}
|
||||||
machines={machines}
|
machines={machines}
|
||||||
config={config}
|
config={config}
|
||||||
bills={R.path(['bills'])(data)}
|
bills={bills}
|
||||||
deviceIds={deviceIds}
|
deviceIds={deviceIds}
|
||||||
/>
|
/>
|
||||||
{wizard && (
|
{wizard && (
|
||||||
|
|
|
||||||
|
|
@ -3,32 +3,55 @@ const pgp = require('pg-promise')()
|
||||||
|
|
||||||
const db = require('../../db')
|
const db = require('../../db')
|
||||||
|
|
||||||
const getBills = filters => {
|
const AND = (...clauses) => clauses.filter(clause => !!clause).join(' AND ')
|
||||||
const deviceStatement = !_.isNil(filters.deviceId)
|
|
||||||
? `WHERE device_id = ${pgp.as.text(filters.deviceId)}`
|
const getBatchIDCondition = filter => {
|
||||||
: ``
|
switch (filter) {
|
||||||
const batchStatement = filter => {
|
case 'none':
|
||||||
switch (filter) {
|
return 'b.cashbox_batch_id IS NULL'
|
||||||
case 'none':
|
case 'any':
|
||||||
return `WHERE b.cashbox_batch_id IS NULL`
|
return 'b.cashbox_batch_id IS NOT NULL'
|
||||||
case 'any':
|
default:
|
||||||
return `WHERE b.cashbox_batch_id IS NOT NULL`
|
return _.isNil(filter)
|
||||||
default:
|
? ''
|
||||||
return _.isNil(filter)
|
: `b.cashbox_batch_id = ${pgp.as.text(filter)}`
|
||||||
? ``
|
|
||||||
: `WHERE b.cashbox_batch_id = ${pgp.as.text(filter)}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const sql = `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 (
|
const getBills = filters => {
|
||||||
SELECT id, device_id FROM cash_in_txs ${deviceStatement}
|
const deviceIDCondition = !_.isNil(filters.deviceId)
|
||||||
) AS cit ON cit.id = b.cash_in_txs_id ${batchStatement(filters.batch)} ${_.isNil(batchStatement(filters.batch)) ? `WHERE` : `AND`} b.destination_unit = 'cashbox'`
|
? `device_id = ${pgp.as.text(filters.deviceId)}`
|
||||||
|
: ''
|
||||||
|
const batchIDCondition = getBatchIDCondition(filters.batch)
|
||||||
|
|
||||||
const sql2 = `SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, b.device_id FROM empty_unit_bills b ${deviceStatement} ${!_.isNil(filters.deviceId) && !_.isNil(filters.batch) ? `AND ${_.replace('WHERE', '', batchStatement(filters.batch))}` : `${batchStatement(filters.batch)}`}`
|
const cashboxBills = `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
|
||||||
|
WHERE ${AND(
|
||||||
|
deviceIDCondition,
|
||||||
|
'device_id NOT IN (SELECT device_id FROM unpaired_devices)',
|
||||||
|
)}
|
||||||
|
) AS cit
|
||||||
|
ON cit.id = b.cash_in_txs_id
|
||||||
|
WHERE ${AND(
|
||||||
|
batchIDCondition,
|
||||||
|
"b.destination_unit = 'cashbox'",
|
||||||
|
'cit.device_id IS NOT NULL',
|
||||||
|
)}`
|
||||||
|
|
||||||
return Promise.all([db.any(sql), db.any(sql2)]).then(
|
const recyclerBills = `SELECT b.id, b.fiat, b.fiat_code, b.created, b.cashbox_batch_id, b.device_id
|
||||||
([bills, operationalBills]) =>
|
FROM empty_unit_bills b
|
||||||
_.map(_.mapKeys(_.camelCase), _.concat(bills, operationalBills)),
|
WHERE ${AND(
|
||||||
|
deviceIDCondition,
|
||||||
|
batchIDCondition,
|
||||||
|
'b.device_id NOT IN (SELECT device_id FROM unpaired_devices)',
|
||||||
|
)}`
|
||||||
|
|
||||||
|
return Promise.all([db.any(cashboxBills), db.any(recyclerBills)]).then(
|
||||||
|
([cashboxBills, recyclerBills]) =>
|
||||||
|
[].concat(cashboxBills, recyclerBills).map(_.mapKeys(_.camelCase)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue