diff --git a/lib/cashbox-batches.js b/lib/cashbox-batches.js index e07348cc..d5cd4e5c 100644 --- a/lib/cashbox-batches.js +++ b/lib/cashbox-batches.js @@ -47,15 +47,16 @@ function updateMachineWithBatch (machineContext, oldCashboxCount) { }) } -function getBatches () { +function getBatches (from = new Date(0).toISOString(), until = new Date().toISOString()) { 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.created >= $1 AND cb.created <= $2 GROUP BY cb.id ORDER BY cb.created DESC ` - return db.any(sql).then(res => _.map(it => _.mapKeys(ite => _.camelCase(ite), it), res)) + return db.any(sql, [from, until]).then(res => _.map(it => _.mapKeys(ite => _.camelCase(ite), it), res)) } function editBatchById (id, performedBy) { diff --git a/lib/new-admin/graphql/resolvers/cashbox.resolver.js b/lib/new-admin/graphql/resolvers/cashbox.resolver.js index 276e4432..acb29e93 100644 --- a/lib/new-admin/graphql/resolvers/cashbox.resolver.js +++ b/lib/new-admin/graphql/resolvers/cashbox.resolver.js @@ -1,8 +1,11 @@ +const { parseAsync } = require('json2csv') const cashbox = require('../../../cashbox-batches') +const logDateFormat = require('../../../logs').logDateFormat const resolvers = { Query: { - cashboxBatches: () => cashbox.getBatches() + cashboxBatches: () => cashbox.getBatches(), + cashboxBatchesCsv: (...[, { from, until, timezone }]) => cashbox.getBatches(from, until).then(data => parseAsync(logDateFormat(timezone, data, ['created']))) }, Mutation: { createBatch: (...[, { deviceId, cashboxCount }]) => cashbox.createCashboxBatch(deviceId, cashboxCount), diff --git a/lib/new-admin/graphql/types/cashbox.type.js b/lib/new-admin/graphql/types/cashbox.type.js index 19f5b239..17f5131a 100644 --- a/lib/new-admin/graphql/types/cashbox.type.js +++ b/lib/new-admin/graphql/types/cashbox.type.js @@ -12,12 +12,13 @@ const typeDef = gql` } type Query { - cashboxBatches: [CashboxBatch] + cashboxBatches: [CashboxBatch] @auth + cashboxBatchesCsv(from: Date, until: Date, timezone: String): String @auth } type Mutation { - createBatch(deviceId: ID, cashboxCount: Int): CashboxBatch - editBatch(id: ID, performedBy: String): CashboxBatch + createBatch(deviceId: ID, cashboxCount: Int): CashboxBatch @auth + editBatch(id: ID, performedBy: String): CashboxBatch @auth } ` diff --git a/new-lamassu-admin/src/components/LogsDownloaderPopper.js b/new-lamassu-admin/src/components/LogsDownloaderPopper.js index dc37ef88..5d879fe7 100644 --- a/new-lamassu-admin/src/components/LogsDownloaderPopper.js +++ b/new-lamassu-admin/src/components/LogsDownloaderPopper.js @@ -140,7 +140,8 @@ const LogsDownloaderPopover = ({ title, getLogs, timezone, - simplified + simplified, + className }) => { const [selectedRadio, setSelectedRadio] = useState(ALL) const [selectedAdvancedRadio, setSelectedAdvancedRadio] = useState(ADVANCED) @@ -245,7 +246,7 @@ const LogsDownloaderPopover = ({ return ( -
+
0 && ( <> - {buttons.map((button, idx) => ( - - {button.text} - - ))} + {buttons.map((button, idx) => + !R.isNil(button.component) ? ( + button.component + ) : ( + + {button.text} + + ) + )} )}
diff --git a/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js b/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js index 4770792b..57ec5535 100644 --- a/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js +++ b/new-lamassu-admin/src/pages/Maintenance/CashCassettes.js @@ -5,6 +5,7 @@ import * as R from 'ramda' import React, { useState } from 'react' import * as Yup from 'yup' +import LogsDowloaderPopover from 'src/components/LogsDownloaderPopper' import Modal from 'src/components/Modal' import { IconButton, Button } from 'src/components/buttons' import { Table as EditableTable } from 'src/components/editableTable' @@ -147,6 +148,12 @@ const SET_CASSETTE_BILLS = gql` } ` +const GET_BATCHES_CSV = gql` + query cashboxBatchesCsv($from: Date, $until: Date, $timezone: String) { + cashboxBatchesCsv(from: $from, until: $until, timezone: $timezone) + } +` + const CashCassettes = () => { const classes = useStyles() const [showHistory, setShowHistory] = useState(false) @@ -175,6 +182,8 @@ const CashCassettes = () => { refetchQueries: () => ['getData'] }) + const timezone = R.path(['config', 'locale_timezone'], data) + const bills = R.groupBy(bill => bill.deviceId)(R.path(['bills'])(data) ?? []) const deviceIds = R.uniq( R.map(R.prop('deviceId'))(R.path(['bills'])(data) ?? []) @@ -317,6 +326,21 @@ const CashCassettes = () => { icon: HistoryIcon, inverseIcon: ReverseHistoryIcon, toggle: setShowHistory + }, + { + component: showHistory ? ( + R.path(['cashboxBatchesCsv'])(logs)} + timezone={timezone} + args={{ timezone }} + /> + ) : ( + <> + ) } ]} iconClassName={classes.listViewButton} @@ -364,6 +388,7 @@ const CashCassettes = () => { )} { +const CashboxHistory = ({ machines, currency, timezone }) => { const classes = useStyles() /* const [error, setError] = useState(false) @@ -82,10 +76,7 @@ const CashboxHistory = ({ machines, currency }) => { refetchQueries: () => ['cashboxBatches'] }) */ - const { data: configData, loading: configLoading } = useQuery(GET_DATA) - const timezone = R.path(['config', 'locale_timezone'], configData) - - const loading = batchesLoading || configLoading + const loading = batchesLoading const batches = R.path(['cashboxBatches'])(batchesData)