feat: one way to count sales in every report; exports for Excel; the report kit and Today on v2 (platform-v2 step 6.12a)

- services/sales.py: sold = active + paid lines; a line's value net of what
  was refunded of it; system lines (the delivery fee) counted apart as fees;
  cost/profit with has_gap. Products, categories, tables, traffic, revenue
  trends, business days (list and current), per-waiter sales, order history
  totals and the printed products/categories/tables reports all use it (they
  used to differ: closed-only vs all orders, fee as a product, refunds never
  subtracted)
- the overview's day summary values lines with their price change (discounts
  were valued at full price)
- services/csv_export.py: UTF-8 BOM, times in venue time with the zone in the
  header, ';' and decimal commas for a Greek venue (',' and '.' otherwise);
  every report export uses it; orders export has products / fees / refunded
- fix: /api/reports/shift/orders with business_day_id crashed (no window)
- manager: src/pages/reports/kit (page with tabs, period picker, panels,
  figures, sortable table, export button, order dialog reusing the order
  page's parts, hour chart on tokens) and reports/today on it; v1 Today removed
- tests: test_sales_accounting.py (a day with a fee and a partial refund adds
  up the same in every report), test_reports_export.py (Excel format),
  reports.test.mjs, e2e manager.mjs (Today, order dialog, export, Greek)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 08:51:13 +02:00
co-authored by Claude Opus 5.5
parent 6643614ff6
commit 4df59c33ef
21 changed files with 1197 additions and 558 deletions
+40
View File
@@ -0,0 +1,40 @@
// The report kit's plain data (plan step 6.12a).
import { test } from 'node:test'
import assert from 'node:assert/strict'
import {
addDays, dayChoices, defaultPeriod, hourSeries, margin, orderTotals, periodParams, periodSlug, quickRange, rangeProblem, ratio, sortRows,
} from '../src/pages/reports/kit/reportLogic.js'
test('periods: the default, quick ranges, the parameters sent (venue time), file names', () => {
assert.deepEqual(defaultPeriod('2026-11-19'), { mode: 'range', from: '2026-10-21', to: '2026-11-19', dayId: '' })
assert.deepEqual(quickRange('week', '2026-11-19'), { from: '2026-11-13', to: '2026-11-19' })
assert.deepEqual(quickRange('month', '2026-11-19'), { from: '2026-11-01', to: '2026-11-19' })
assert.equal(addDays('2026-03-01', -1), '2026-02-28')
assert.deepEqual(periodParams({ mode: 'range', from: '2026-11-01', to: '2026-11-19' }), { from: '2026-11-01T00:00:00', to: '2026-11-19T23:59:59' })
assert.deepEqual(periodParams({ mode: 'day', dayId: '12' }), { business_day_id: '12' })
assert.deepEqual(periodParams({ mode: 'day', dayId: '' }), {})
assert.equal(periodSlug({ mode: 'range', from: '2026-11-01', to: '2026-11-19' }), '2026-11-01_2026-11-19')
assert.equal(rangeProblem({ mode: 'range', from: '2026-11-19', to: '2026-11-01' }), 'order')
assert.equal(rangeProblem({ mode: 'range', from: '2024-01-01', to: '2026-11-01' }), 'long')
assert.equal(rangeProblem({ mode: 'day' }), null)
})
test('numbers: ratios, margins, sorting (numbers, text, empty last, stable)', () => {
assert.equal(ratio(1, 4), 0.25)
assert.equal(ratio(1, 0), 0)
assert.equal(margin(6, 13), 6 / 13)
assert.equal(margin(0, 0), null)
const rows = [{ n: 'b', v: 2 }, { n: 'a', v: null }, { n: 'c', v: 10 }, { n: 'd', v: 2 }]
assert.deepEqual(sortRows(rows, 'v').map(r => r.n), ['c', 'b', 'd', 'a'])
assert.deepEqual(sortRows(rows, 'v', 'asc').map(r => r.n), ['b', 'd', 'c', 'a'])
assert.deepEqual(sortRows(rows, 'n', 'asc').map(r => r.n), ['a', 'b', 'c', 'd'])
assert.deepEqual(sortRows(rows, r => r.n.charCodeAt(0), 'desc').map(r => r.n), ['d', 'c', 'b', 'a'])
})
test('a day by the hour, an order\'s totals, business days newest first', () => {
const s = hourSeries([{ hour: 12, revenue: 30, orders: 2 }, { hour: 3, revenue: 5 }], { from: 11, to: 13 })
assert.deepEqual(s.map(h => [h.hour, h.revenue]), [[3, 5], ...Array.from({ length: 7 }, (_, i) => [4 + i, 0]), [11, 0], [12, 30], [13, 0]]) // widened to 03:00
assert.equal(orderTotals({ totals: { revenue: 13, fees: 2.5 } }).fees, 2.5)
assert.equal(orderTotals({ items: [{ status: 'paid', unit_price: 4, price_adjustment: -1, quantity: 2 }, { status: 'cancelled', unit_price: 9, quantity: 1 }] }).revenue, 6)
assert.deepEqual(dayChoices([{ id: 1, opened_at: '2026-11-17' }, { id: 2, opened_at: '2026-11-19' }]).map(d => d.id), [2, 1])
})