fix: cashbox history optimize and fix query

This commit is contained in:
Rafael Taranto 2024-08-13 09:40:42 +01:00
parent d8ce011b28
commit 0bdbd4cc76
3 changed files with 18 additions and 23 deletions

View file

@ -73,14 +73,22 @@ function updateMachineWithBatch (machineContext, oldCashboxCount) {
function getBatches (from = new Date(0).toISOString(), until = new Date().toISOString()) { function getBatches (from = new Date(0).toISOString(), until = new Date().toISOString()) {
const sql = ` const sql = `
SELECT cuo.id, cuo.device_id, cuo.created, cuo.operation_type, cuo.bill_count_override, cuo.performed_by, json_agg(bi.*) AS bills SELECT
cuo.id,
cuo.device_id,
cuo.created,
cuo.operation_type,
cuo.bill_count_override,
cuo.performed_by,
COUNT(bi.id) AS bill_count,
COALESCE(SUM(bi.fiat), 0) AS fiat_total
FROM cash_unit_operation AS cuo FROM cash_unit_operation AS cuo
LEFT JOIN ( LEFT JOIN (
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 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 SELECT id, fiat, fiat_code, created, cashbox_batch_id, device_id FROM empty_unit_bills
) AS bi ON cuo.id = bi.cashbox_batch_id ) AS bi ON cuo.id = bi.cashbox_batch_id
WHERE cuo.created >= $1 AND cuo.created <= $2 AND cuo.operation_type = 'cash-box-empty' WHERE cuo.created >= $1 AND cuo.created <= $2 AND cuo.operation_type = 'cash-box-empty'
GROUP BY cuo.id GROUP BY cuo.id, cuo.device_id, cuo.created, cuo.operation_type, cuo.bill_count_override, cuo.performed_by
ORDER BY cuo.created DESC ORDER BY cuo.created DESC
` `
@ -103,25 +111,13 @@ function getBillsByBatchId (id) {
function logFormatter (data) { function logFormatter (data) {
return _.map( return _.map(
it => { it => {
const bills = _.filter(
ite => !(_.isNil(ite) || _.isNil(ite.fiat_code) || _.isNil(ite.fiat) || _.isNaN(ite.fiat)),
it.bills
)
return { return {
id: it.id, id: it.id,
deviceId: it.deviceId, deviceId: it.deviceId,
created: it.created, created: it.created,
operationType: it.operationType, operationType: it.operationType,
billCount: _.size(bills), billCount: it.billCount,
fiatTotals: _.reduce( fiatTotal: it.fiatTotal
(acc, value) => {
acc[value.fiat_code] = (acc[value.fiat_code] || 0) + value.fiat
return acc
},
{},
bills
),
billsByDenomination: _.countBy(ite => `${ite.fiat} ${ite.fiat_code}`, bills)
} }
}, },
data data

View file

@ -8,7 +8,8 @@ const typeDef = gql`
operationType: String operationType: String
customBillCount: Int customBillCount: Int
performedBy: String performedBy: String
bills: [Bill] billCount: Int
fiatTotal: Int
} }
type Query { type Query {

View file

@ -24,10 +24,8 @@ const GET_BATCHES = gql`
operationType operationType
customBillCount customBillCount
performedBy performedBy
bills { billCount
fiat fiatTotal
fiatCode
}
} }
} }
` `
@ -168,7 +166,7 @@ const CashboxHistory = ({ machines, currency, timezone }) => {
decimalPlaces: 0 decimalPlaces: 0
}, },
view: it => view: it =>
R.isNil(it.customBillCount) ? it.bills.length : it.customBillCount R.isNil(it.customBillCount) ? it.billCount : it.customBillCount
}, },
{ {
name: 'total', name: 'total',
@ -177,7 +175,7 @@ const CashboxHistory = ({ machines, currency, timezone }) => {
textAlign: 'right', textAlign: 'right',
view: it => ( view: it => (
<span> <span>
{R.sum(R.map(b => R.prop('fiat', b), it.bills))} {currency} {it.fiatTotal} {currency}
</span> </span>
) )
}, },