fix: cashout detection on cash cassette wizard

fix: cashbox wizard onContinue data
fix: cashbox batch creation query
feat: cashbox batch creation flow on UI
This commit is contained in:
Sérgio Salgado 2021-04-28 16:11:43 +01:00 committed by Josh Harvey
parent e05e44f468
commit 482e1afc3a
5 changed files with 45 additions and 11 deletions

View file

@ -1,8 +1,10 @@
const db = require('./db') const db = require('./db')
const _ = require('lodash/fp') const _ = require('lodash/fp')
const uuid = require('uuid')
function createCashboxBatch (rec) { function createCashboxBatch (deviceId) {
const sql = 'INSERT INTO cashbox_batches (device_id, created) VALUES ($1, now()) RETURNING *' console.log('herererere')
const sql = `INSERT INTO cashbox_batches (id, device_id, created, operation_type) VALUES ($1, $2, now(), 'cash-in-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
@ -11,7 +13,7 @@ function createCashboxBatch (rec) {
bills.cashbox_batch_id IS NULL bills.cashbox_batch_id IS NULL
` `
return db.tx(async t => { return db.tx(async t => {
const newBatch = await t.oneOrNone(sql, [rec.deviceId]) const newBatch = await t.oneOrNone(sql, [uuid.v4(), deviceId])
return t.oneOrNone(sql2, [newBatch.id, newBatch.device_id]) return t.oneOrNone(sql2, [newBatch.id, newBatch.device_id])
}) })
} }

View file

@ -5,6 +5,7 @@ const resolvers = {
cashboxBatches: () => cashbox.getBatches() cashboxBatches: () => cashbox.getBatches()
}, },
Mutation: { Mutation: {
createBatch: (...[, { deviceId }]) => cashbox.createCashboxBatch(deviceId),
editBatch: (...[, { id, performedBy }]) => cashbox.editBatchById(id, performedBy) editBatch: (...[, { id, performedBy }]) => cashbox.editBatchById(id, performedBy)
} }
} }

View file

@ -16,6 +16,7 @@ const typeDef = gql`
} }
type Mutation { type Mutation {
createBatch(deviceId: ID): CashboxBatch
editBatch(id: ID, performedBy: String): CashboxBatch editBatch(id: ID, performedBy: String): CashboxBatch
} }
` `

View file

@ -175,7 +175,7 @@ const CashboxHistory = ({ machines, currency }) => {
{ {
name: 'date', name: 'date',
header: 'Date', header: 'Date',
width: 125, width: 135,
textAlign: 'right', textAlign: 'right',
view: it => moment.utc(it.created).format('YYYY-MM-DD') view: it => moment.utc(it.created).format('YYYY-MM-DD')
}, },
@ -189,7 +189,7 @@ const CashboxHistory = ({ machines, currency }) => {
{ {
name: 'performedBy', name: 'performedBy',
header: 'Performed by', header: 'Performed by',
width: 190, width: 180,
textAlign: 'left', textAlign: 'left',
view: it => { view: it => {
if (!editing) if (!editing)
@ -239,7 +239,12 @@ const CashboxHistory = ({ machines, currency }) => {
return ( return (
<> <>
{!loading && ( {!loading && (
<DataTable name="cashboxHistory" elements={elements} data={batches} /> <DataTable
name="cashboxHistory"
elements={elements}
data={batches}
emptyText="No cashbox batches so far"
/>
)} )}
</> </>
) )

View file

@ -1,3 +1,5 @@
import { useMutation } from '@apollo/react-hooks'
import gql from 'graphql-tag'
import * as R from 'ramda' import * as R from 'ramda'
import React, { useState } from 'react' import React, { useState } from 'react'
import * as Yup from 'yup' import * as Yup from 'yup'
@ -11,22 +13,47 @@ const MODAL_WIDTH = 554
const MODAL_HEIGHT = 520 const MODAL_HEIGHT = 520
const CASHBOX_DEFAULT_CAPACITY = 500 const CASHBOX_DEFAULT_CAPACITY = 500
const CREATE_BATCH = gql`
mutation createBatch($deviceId: ID) {
createBatch(deviceId: $deviceId) {
id
}
}
`
const Wizard = ({ machine, cashoutSettings, locale, onClose, save, error }) => { const Wizard = ({ machine, cashoutSettings, locale, onClose, save, error }) => {
const [{ step, config }, setState] = useState({ const [{ step, config }, setState] = useState({
step: 0, step: 0,
config: { active: true } config: { active: true }
}) })
const LAST_STEP = R.isEmpty(cashoutSettings) ? 1 : 3 const [createBatch] = useMutation(CREATE_BATCH)
const isCashOutDisabled =
R.isEmpty(cashoutSettings) || !cashoutSettings?.active
const LAST_STEP = isCashOutDisabled ? 1 : 3
const title = `Update counts` const title = `Update counts`
const isLastStep = step === LAST_STEP const isLastStep = step === LAST_STEP
const onContinue = it => { const onContinue = it => {
const cashbox = config?.wasCashboxEmptied === 'YES' ? 0 : machine?.cashbox console.log('it?.wasCashboxEmptied', it?.wasCashboxEmptied)
const wasCashboxEmptied = it?.wasCashboxEmptied === 'YES'
const cashbox = wasCashboxEmptied ? 0 : machine?.cashbox
const newConfig = R.merge(config, it) const newConfig = R.merge(config, it)
if (isLastStep) { if (isLastStep) {
if (wasCashboxEmptied) {
console.log('cashbox was emptied')
createBatch({
variables: {
deviceId: machine.id
}
})
}
save( save(
machine.id, machine.id,
parseInt(cashbox), parseInt(cashbox),
@ -73,9 +100,7 @@ const Wizard = ({ machine, cashoutSettings, locale, onClose, save, error }) => {
] ]
const filteredSteps = R.filter(it => { const filteredSteps = R.filter(it => {
return ( return !it.cashoutRequired || (!isCashOutDisabled && it.cashoutRequired)
!it.cashoutRequired || (!R.isEmpty(cashoutSettings) && it.cashoutRequired)
)
}, steps) }, steps)
return ( return (