@tallyui/components 3.12.0 → 3.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +466 -28
- package/dist/index.js +3194 -1574
- package/package.json +9 -9
- package/src/cart/cart-columns.tsx +57 -0
- package/src/cart/cart-line.tsx +32 -17
- package/src/cart/index.ts +1 -0
- package/src/checkout/cash-tendered.tsx +5 -7
- package/src/fonts/font-provider.tsx +65 -0
- package/src/fonts/index.ts +1 -0
- package/src/index.ts +40 -2
- package/src/lib/error-text.ts +5 -0
- package/src/order/index.ts +2 -0
- package/src/order/refund-form.tsx +205 -0
- package/src/order/refund-modal.tsx +32 -0
- package/src/product/category-tile.tsx +43 -0
- package/src/product/in-cart-badge.tsx +26 -0
- package/src/product/index.ts +2 -0
- package/src/register/closure-sheet.tsx +13 -2
- package/src/register/handover-slip.tsx +46 -0
- package/src/register/index.ts +2 -0
- package/src/register/movement-sheet.tsx +2 -1
- package/src/register/open-register-card.tsx +2 -1
- package/src/register/register-bar.tsx +57 -11
- package/src/register/register-column.tsx +2 -1
- package/src/register/register-count.tsx +30 -11
- package/src/register/register-panel.tsx +45 -4
- package/src/register/switch-confirm.tsx +102 -0
- package/src/reports/cards.tsx +197 -0
- package/src/reports/filters-bar.tsx +95 -0
- package/src/reports/hero.tsx +111 -0
- package/src/reports/index.ts +4 -0
- package/src/reports/sales-room.tsx +28 -0
- package/src/sale/cart.tsx +37 -21
- package/src/sale/catalogue.tsx +34 -5
- package/src/sale/customer-display-screen.tsx +92 -0
- package/src/sale/index.ts +5 -2
- package/src/sale/open-sale-tabs.tsx +67 -0
- package/src/sale/order-reference.ts +3 -0
- package/src/sale/orders-list.tsx +4 -2
- package/src/sale/parked-sales.tsx +2 -1
- package/src/sale/price-form.tsx +28 -4
- package/src/sale/product-filter-bar.tsx +58 -0
- package/src/sale/receipt.tsx +8 -2
- package/src/sale/tender-pane.tsx +179 -0
- package/src/sale/tender.tsx +20 -35
- package/src/scale/index.ts +3 -0
- package/src/scale/scale-provider.tsx +70 -0
- package/src/scale/use-pointer.ts +23 -0
- package/src/settings/display-settings.tsx +122 -0
- package/src/settings/index.ts +1 -0
- package/src/shell/app-shell.tsx +71 -0
- package/src/shell/index.ts +4 -0
- package/src/shell/nav-drawer.tsx +130 -0
- package/src/shell/nav-item.tsx +70 -0
- package/src/shell/nav-rail.tsx +58 -0
- package/src/shell/shell-context.ts +18 -0
- package/src/ui/icon/menu-icon.tsx +10 -0
- package/src/ui/text/font-text.tsx +25 -0
- package/src/ui/text/index.tsx +5 -4
- package/src/sale/split-tender.tsx +0 -81
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Sales room's cards over unit 10's figures. Port of WCPOS v2 `screens/main/reports/cards/`
|
|
3
|
+
* (`card.tsx`, `primitives.tsx`, `orders.tsx`, `payments.tsx`, `top-products.tsx`,
|
|
4
|
+
* `categories.tsx`, `taxes.tsx`, `refunds.tsx`); copy is v2's English (`core.json`). An unknown
|
|
5
|
+
* figure (margin with no costed line, kept share of no sales) shows `—`, never 0.
|
|
6
|
+
*/
|
|
7
|
+
import type { JSX, ReactNode } from 'react';
|
|
8
|
+
import { Pressable, Text, View } from 'react-native';
|
|
9
|
+
import { formatMoney } from '@tallyui/core';
|
|
10
|
+
import type { SalesFigures } from '@tallyui/pos';
|
|
11
|
+
import { cn } from '@tallyui/theme';
|
|
12
|
+
import { Card } from '../ui/card';
|
|
13
|
+
|
|
14
|
+
export type SalesCardId = 'orders' | 'payments' | 'products' | 'categories' | 'taxes' | 'refunds';
|
|
15
|
+
export interface SalesCardsProps {
|
|
16
|
+
figures: SalesFigures;
|
|
17
|
+
currency: string;
|
|
18
|
+
/** Opens the card's panel. */
|
|
19
|
+
onOpenCard?: (card: SalesCardId) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const SWATCHES = ['bg-primary', 'bg-info', 'bg-success', 'bg-warning', 'bg-destructive'];
|
|
23
|
+
const OTHER_SWATCH = 'bg-muted-foreground';
|
|
24
|
+
const LEGEND_ROWS = 5;
|
|
25
|
+
const TOP_PRODUCTS = 4;
|
|
26
|
+
const UNKNOWN = '—';
|
|
27
|
+
const percent = (fraction: number) => `${(fraction * 100).toFixed(1)}%`;
|
|
28
|
+
|
|
29
|
+
interface Share { label: string; value: number; text: string; note?: string; swatch: string }
|
|
30
|
+
|
|
31
|
+
function ReportCard({ id, name, figure, onOpen, children }: {
|
|
32
|
+
id: SalesCardId; name: string; figure: string; onOpen?: (card: SalesCardId) => void; children: ReactNode;
|
|
33
|
+
}): JSX.Element {
|
|
34
|
+
const head = (
|
|
35
|
+
<View className="flex-row items-baseline justify-between gap-2">
|
|
36
|
+
<Text className="text-sm text-muted-foreground">{name}</Text>
|
|
37
|
+
<Text testID={`card-${id}-figure`} className="text-lg font-semibold text-foreground">{figure}</Text>
|
|
38
|
+
</View>
|
|
39
|
+
);
|
|
40
|
+
return (
|
|
41
|
+
<Card testID={`card-${id}`} className="min-w-72 flex-1 gap-3 p-4">
|
|
42
|
+
{onOpen ? (
|
|
43
|
+
<Pressable testID={`card-${id}-open`} accessibilityRole="button" accessibilityLabel={name} onPress={() => onOpen(id)}>
|
|
44
|
+
{head}
|
|
45
|
+
</Pressable>
|
|
46
|
+
) : head}
|
|
47
|
+
{children}
|
|
48
|
+
</Card>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const Empty = ({ testID, children }: { testID: string; children: string }) => (
|
|
53
|
+
<Text testID={testID} className="text-sm text-muted-foreground">{children}</Text>
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
function StatGrid({ stats }: { stats: { label: string; value: string; testID?: string }[] }): JSX.Element {
|
|
57
|
+
return (
|
|
58
|
+
<View className="flex-row flex-wrap gap-y-2">
|
|
59
|
+
{stats.map((stat) => (
|
|
60
|
+
<View key={stat.label} className="basis-1/3 gap-0.5">
|
|
61
|
+
<Text className="text-xs text-muted-foreground">{stat.label}</Text>
|
|
62
|
+
<Text testID={stat.testID} className="text-sm font-medium text-foreground">{stat.value}</Text>
|
|
63
|
+
</View>
|
|
64
|
+
))}
|
|
65
|
+
</View>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function ProportionalBar({ shares }: { shares: Share[] }): JSX.Element {
|
|
70
|
+
const positive = shares.filter((share) => share.value > 0);
|
|
71
|
+
return (
|
|
72
|
+
<View className="h-2 flex-row gap-0.5 overflow-hidden rounded-md">
|
|
73
|
+
{positive.length ? positive.map((share, i) => (
|
|
74
|
+
<View key={i} className={share.swatch} style={{ flex: share.value }} />
|
|
75
|
+
)) : <View className="flex-1 bg-muted" />}
|
|
76
|
+
</View>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function Legend({ shares }: { shares: Share[] }): JSX.Element {
|
|
81
|
+
return (
|
|
82
|
+
<View className="gap-1.5">
|
|
83
|
+
{shares.map((share, i) => (
|
|
84
|
+
<View key={i} className="flex-row items-center gap-2">
|
|
85
|
+
<View className={cn('h-2.5 w-2.5 rounded-sm', share.swatch)} />
|
|
86
|
+
<Text className="flex-1 text-sm text-foreground" numberOfLines={1}>{share.label}</Text>
|
|
87
|
+
{share.note && <Text className="text-xs text-muted-foreground">{share.note}</Text>}
|
|
88
|
+
<Text className="text-sm font-medium text-foreground">{share.text}</Text>
|
|
89
|
+
</View>
|
|
90
|
+
))}
|
|
91
|
+
</View>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The first rows with their own swatch, the rest folded into Other. */
|
|
96
|
+
function shares<T>(rows: T[], toShare: (row: T) => Omit<Share, 'swatch'>, otherText: (rest: T[]) => string): Share[] {
|
|
97
|
+
const head = rows.slice(0, LEGEND_ROWS).map((row, i) => ({ ...toShare(row), swatch: SWATCHES[i % SWATCHES.length] }));
|
|
98
|
+
const rest = rows.slice(LEGEND_ROWS);
|
|
99
|
+
if (!rest.length) return head;
|
|
100
|
+
return [...head, { label: 'Other', value: rest.reduce((sum, row) => sum + toShare(row).value, 0), text: otherText(rest), swatch: OTHER_SWATCH }];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function SalesCards({ figures, currency, onOpenCard }: SalesCardsProps): JSX.Element {
|
|
104
|
+
const money = (minor: number) => formatMoney({ amount: minor, currency }) ?? '';
|
|
105
|
+
const { totals, payments, products, categories, taxes, refunds } = figures;
|
|
106
|
+
const { margin } = totals;
|
|
107
|
+
const productNet = products.reduce((sum, row) => sum + row.netMinor, 0);
|
|
108
|
+
const categoryNet = categories.reduce((sum, row) => sum + row.netMinor, 0);
|
|
109
|
+
const paymentShares = shares(payments.rows, (row) => ({
|
|
110
|
+
label: row.label || (row.method === 'unpaid' ? 'Unpaid' : row.method === 'unknown' ? 'Unknown' : row.method),
|
|
111
|
+
value: row.amountMinor, text: money(row.amountMinor), note: `${row.orders} orders`,
|
|
112
|
+
}), (rest) => money(rest.reduce((sum, row) => sum + row.amountMinor, 0)));
|
|
113
|
+
const categoryShares = shares(categories, (row) => ({
|
|
114
|
+
label: row.key === '' ? 'Uncategorised' : row.name, value: row.netMinor, text: money(row.netMinor), note: `${row.quantity} sold`,
|
|
115
|
+
}), (rest) => money(rest.reduce((sum, row) => sum + row.netMinor, 0)));
|
|
116
|
+
const taxShares = shares(taxes, (row) => ({
|
|
117
|
+
label: row.label || 'Tax', value: row.taxMinor, text: money(row.taxMinor),
|
|
118
|
+
note: row.netMinor === null ? undefined : `on ${money(row.netMinor)}`,
|
|
119
|
+
}), (rest) => money(rest.reduce((sum, row) => sum + row.taxMinor, 0)));
|
|
120
|
+
const card = { onOpen: onOpenCard };
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<View testID="sales-cards" className="flex-row flex-wrap gap-4">
|
|
124
|
+
<ReportCard id="orders" name="Orders" figure={String(totals.orderCount)} {...card}>
|
|
125
|
+
{totals.orderCount === 0 ? <Empty testID="card-orders-empty">No orders in this period</Empty> : (
|
|
126
|
+
<View className="gap-2">
|
|
127
|
+
<StatGrid stats={[
|
|
128
|
+
{ label: 'Average order', value: money(totals.averageMinor) },
|
|
129
|
+
{ label: 'Items Sold', value: String(totals.items) },
|
|
130
|
+
{ label: 'Items per order', value: (totals.items / totals.orderCount).toFixed(1) },
|
|
131
|
+
{ label: 'Discounts', value: money(totals.discountMinor) },
|
|
132
|
+
{ label: 'Margin %', value: margin.marginPct === null ? UNKNOWN : percent(margin.marginPct), testID: 'card-orders-margin' },
|
|
133
|
+
]} />
|
|
134
|
+
{margin.missingLines > 0 && (
|
|
135
|
+
<Text testID="card-orders-cost-missing" className="text-xs text-muted-foreground">
|
|
136
|
+
{`Cost missing on ${margin.missingQuantity} of ${totals.items} items`}
|
|
137
|
+
</Text>
|
|
138
|
+
)}
|
|
139
|
+
</View>
|
|
140
|
+
)}
|
|
141
|
+
</ReportCard>
|
|
142
|
+
<ReportCard id="payments" name="Payments" figure={money(totals.grossMinor)} {...card}>
|
|
143
|
+
{payments.rows.length === 0 ? <Empty testID="card-payments-empty">No payments in this period</Empty> : (
|
|
144
|
+
<><ProportionalBar shares={paymentShares} /><Legend shares={paymentShares} /></>
|
|
145
|
+
)}
|
|
146
|
+
</ReportCard>
|
|
147
|
+
<ReportCard id="products" name="Top products" figure={String(totals.items)} {...card}>
|
|
148
|
+
{products.length === 0 ? <Empty testID="card-products-empty">No sales in this period</Empty> : (
|
|
149
|
+
<View className="gap-2">
|
|
150
|
+
{products.slice(0, TOP_PRODUCTS).map((row) => {
|
|
151
|
+
const share = productNet > 0 ? Math.min(1, Math.max(0, row.netMinor / productNet)) : 0;
|
|
152
|
+
return (
|
|
153
|
+
<View key={row.key} className="gap-1">
|
|
154
|
+
<View className="flex-row justify-between gap-2">
|
|
155
|
+
<Text className="flex-1 text-sm text-foreground" numberOfLines={1}>{row.name || 'Unknown'}</Text>
|
|
156
|
+
<Text className="text-sm font-medium text-foreground">{money(row.netMinor)}</Text>
|
|
157
|
+
</View>
|
|
158
|
+
<View className="h-1.5 overflow-hidden rounded-md bg-muted">
|
|
159
|
+
<View className="h-full bg-primary" style={{ width: `${share * 100}%` }} />
|
|
160
|
+
</View>
|
|
161
|
+
<Text className="text-xs text-muted-foreground">{`${percent(share)} · ${row.quantity} sold`}</Text>
|
|
162
|
+
</View>
|
|
163
|
+
);
|
|
164
|
+
})}
|
|
165
|
+
</View>
|
|
166
|
+
)}
|
|
167
|
+
</ReportCard>
|
|
168
|
+
<ReportCard id="categories" name="Categories" figure={money(categoryNet)} {...card}>
|
|
169
|
+
{categories.length === 0 ? <Empty testID="card-categories-empty">No sales in this period</Empty> : (
|
|
170
|
+
<><ProportionalBar shares={categoryShares} /><Legend shares={categoryShares} /></>
|
|
171
|
+
)}
|
|
172
|
+
</ReportCard>
|
|
173
|
+
<ReportCard id="taxes" name="Taxes" figure={money(totals.taxMinor)} {...card}>
|
|
174
|
+
{taxes.length === 0 ? <Empty testID="card-taxes-empty">None in this period</Empty> : (
|
|
175
|
+
<>
|
|
176
|
+
<ProportionalBar shares={taxShares} /><Legend shares={taxShares} />
|
|
177
|
+
<StatGrid stats={[
|
|
178
|
+
{ label: 'Net', value: money(totals.netMinor) },
|
|
179
|
+
{ label: 'Tax', value: money(totals.taxMinor) },
|
|
180
|
+
{ label: 'Gross', value: money(totals.grossMinor) },
|
|
181
|
+
]} />
|
|
182
|
+
</>
|
|
183
|
+
)}
|
|
184
|
+
</ReportCard>
|
|
185
|
+
<ReportCard id="refunds" name="Refunds" figure={money(refunds.amountMinor)} {...card}>
|
|
186
|
+
{refunds.count === 0 ? <Empty testID="card-refunds-none">None in this period</Empty> : (
|
|
187
|
+
<StatGrid stats={[
|
|
188
|
+
{ label: 'Refunded', value: money(refunds.amountMinor) },
|
|
189
|
+
{ label: 'Orders', value: `${refunds.orders} of ${totals.orderCount}` },
|
|
190
|
+
{ label: 'Kept', testID: 'card-refunds-kept',
|
|
191
|
+
value: totals.grossMinor === 0 ? UNKNOWN : percent((totals.grossMinor - refunds.amountMinor) / totals.grossMinor) },
|
|
192
|
+
]} />
|
|
193
|
+
)}
|
|
194
|
+
</ReportCard>
|
|
195
|
+
</View>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Sales room's filters: the period presets (worked out in store time by pos `presetRange`),
|
|
3
|
+
* status, cashier and register. Port of WCPOS v2 `screens/main/reports/date-button.tsx` and the
|
|
4
|
+
* filter choices in its `LEDGER.md`; copy is v2's English (`core.json`).
|
|
5
|
+
*/
|
|
6
|
+
import { useState, type JSX } from 'react';
|
|
7
|
+
import { Pressable, Text, View } from 'react-native';
|
|
8
|
+
import { DATE_PRESETS, presetRange, type DatePreset, type DayRange } from '@tallyui/pos';
|
|
9
|
+
import { cn } from '@tallyui/theme';
|
|
10
|
+
|
|
11
|
+
export interface SalesOption { value: string; label: string }
|
|
12
|
+
export interface SalesFilters {
|
|
13
|
+
preset: DatePreset;
|
|
14
|
+
range: DayRange;
|
|
15
|
+
/** A single day's comparison: the day before or the same weekday last week. */
|
|
16
|
+
compare: 'previous' | 'week';
|
|
17
|
+
status: string;
|
|
18
|
+
cashierId: string | null;
|
|
19
|
+
registerId: string | null;
|
|
20
|
+
}
|
|
21
|
+
export interface SalesFiltersBarProps {
|
|
22
|
+
filters: SalesFilters;
|
|
23
|
+
onFiltersChange: (next: SalesFilters) => void;
|
|
24
|
+
/** IANA zone of the store, or 'device'. */
|
|
25
|
+
timezone: string;
|
|
26
|
+
/** The clock presets are worked out from; defaults to the time of the press. */
|
|
27
|
+
now?: () => Date;
|
|
28
|
+
statusOptions: SalesOption[];
|
|
29
|
+
cashierOptions: SalesOption[];
|
|
30
|
+
registerOptions: SalesOption[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const PRESET_LABELS: Record<DatePreset, string> = {
|
|
34
|
+
today: 'Today', yesterday: 'Yesterday', thisWeek: 'This Week',
|
|
35
|
+
lastWeek: 'Last Week', thisMonth: 'This Month', lastMonth: 'Last Month',
|
|
36
|
+
};
|
|
37
|
+
const ALL = 'all';
|
|
38
|
+
|
|
39
|
+
/** `10 Nov` for a store day. */
|
|
40
|
+
export const dayLabel = (day: string): string =>
|
|
41
|
+
new Intl.DateTimeFormat('en-GB', { day: 'numeric', month: 'short', timeZone: 'UTC' }).format(new Date(`${day}T00:00:00Z`));
|
|
42
|
+
|
|
43
|
+
function ChoiceSelect({ id, value, options, onChange }: {
|
|
44
|
+
id: string; value: string; options: SalesOption[]; onChange: (value: string) => void;
|
|
45
|
+
}): JSX.Element {
|
|
46
|
+
const [open, setOpen] = useState(false);
|
|
47
|
+
const chosen = options.find((option) => option.value === value);
|
|
48
|
+
return (
|
|
49
|
+
<View className="gap-1">
|
|
50
|
+
<Pressable testID={id} accessibilityRole="button" onPress={() => setOpen(!open)}
|
|
51
|
+
className="rounded-md border border-border bg-card px-3 py-2">
|
|
52
|
+
<Text className="text-sm text-foreground">{chosen?.label ?? value}</Text>
|
|
53
|
+
</Pressable>
|
|
54
|
+
{open && (
|
|
55
|
+
<View className="rounded-md border border-border bg-card py-1">
|
|
56
|
+
{options.map((option) => (
|
|
57
|
+
<Pressable key={option.value} testID={`${id}-option-${option.value}`} accessibilityRole="radio"
|
|
58
|
+
accessibilityState={{ checked: option.value === value }}
|
|
59
|
+
onPress={() => { setOpen(false); onChange(option.value); }}
|
|
60
|
+
className={cn('px-3 py-2', option.value === value && 'bg-muted')}>
|
|
61
|
+
<Text className="text-sm text-foreground">{option.label}</Text>
|
|
62
|
+
</Pressable>
|
|
63
|
+
))}
|
|
64
|
+
</View>
|
|
65
|
+
)}
|
|
66
|
+
</View>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function SalesFiltersBar({
|
|
71
|
+
filters, onFiltersChange, timezone, now, statusOptions, cashierOptions, registerOptions,
|
|
72
|
+
}: SalesFiltersBarProps): JSX.Element {
|
|
73
|
+
const { range } = filters;
|
|
74
|
+
return (
|
|
75
|
+
<View testID="sales-filters" className="flex-row flex-wrap items-start gap-2">
|
|
76
|
+
<ChoiceSelect id="sales-filter-period" value={filters.preset}
|
|
77
|
+
options={DATE_PRESETS.map((preset) => ({ value: preset, label: PRESET_LABELS[preset] }))}
|
|
78
|
+
onChange={(value) => {
|
|
79
|
+
const preset = value as DatePreset;
|
|
80
|
+
onFiltersChange({ ...filters, preset, range: presetRange(preset, (now ?? (() => new Date()))(), timezone) });
|
|
81
|
+
}} />
|
|
82
|
+
<Text testID="sales-filter-dates" className="py-2 text-sm text-muted-foreground">
|
|
83
|
+
{range.from === range.to ? dayLabel(range.from) : `${dayLabel(range.from)} – ${dayLabel(range.to)}`}
|
|
84
|
+
</Text>
|
|
85
|
+
<ChoiceSelect id="sales-filter-status" value={filters.status} options={statusOptions}
|
|
86
|
+
onChange={(status) => onFiltersChange({ ...filters, status })} />
|
|
87
|
+
<ChoiceSelect id="sales-filter-cashier" value={filters.cashierId ?? ALL}
|
|
88
|
+
options={[{ value: ALL, label: 'Everyone' }, ...cashierOptions]}
|
|
89
|
+
onChange={(value) => onFiltersChange({ ...filters, cashierId: value === ALL ? null : value })} />
|
|
90
|
+
<ChoiceSelect id="sales-filter-register" value={filters.registerId ?? ALL}
|
|
91
|
+
options={[{ value: ALL, label: 'All registers' }, ...registerOptions]}
|
|
92
|
+
onChange={(value) => onFiltersChange({ ...filters, registerId: value === ALL ? null : value })} />
|
|
93
|
+
</View>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Sales room's hero: the period's gross, its change against the comparison period and a
|
|
3
|
+
* daily chart of both. Port of WCPOS v2 `screens/main/reports/hero/index.tsx` (`Difference`, the
|
|
4
|
+
* companions Orders · Average order · Items) and the comparison labels of its `LEDGER.md`.
|
|
5
|
+
* The bars are Views with theme token classes, so the chart takes its colours from the theme.
|
|
6
|
+
*/
|
|
7
|
+
import type { JSX } from 'react';
|
|
8
|
+
import { Pressable, Text, View } from 'react-native';
|
|
9
|
+
import { formatMoney } from '@tallyui/core';
|
|
10
|
+
import type { SalesFigures } from '@tallyui/pos';
|
|
11
|
+
import { cn } from '@tallyui/theme';
|
|
12
|
+
import { Card } from '../ui/card';
|
|
13
|
+
import { dayLabel, type SalesFilters } from './filters-bar';
|
|
14
|
+
|
|
15
|
+
export interface SalesHeroProps {
|
|
16
|
+
figures: SalesFigures;
|
|
17
|
+
filters: SalesFilters;
|
|
18
|
+
onFiltersChange: (next: SalesFilters) => void;
|
|
19
|
+
currency: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const NO_COMPARISON = '—';
|
|
23
|
+
const signed = (diff: number, format: (n: number) => string) =>
|
|
24
|
+
diff === 0 ? '±0' : `${diff > 0 ? '+' : '−'}${format(Math.abs(diff))}`;
|
|
25
|
+
|
|
26
|
+
/** A change against the comparison, or `—` when there is nothing to compare with. */
|
|
27
|
+
function Difference({ testID, text, diff, suffix }: { testID: string; text?: string; diff: number; suffix?: string }): JSX.Element {
|
|
28
|
+
const tone = text === undefined || diff === 0 ? 'text-muted-foreground' : diff > 0 ? 'text-success' : 'text-destructive';
|
|
29
|
+
return (
|
|
30
|
+
<Text testID={testID} className={cn('text-sm', tone)} accessibilityLabel={text === undefined ? 'No comparison' : undefined}>
|
|
31
|
+
{text === undefined ? NO_COMPARISON : suffix ? `${text} ${suffix}` : text}
|
|
32
|
+
</Text>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function compareLabel(filters: SalesFilters): string {
|
|
37
|
+
if (filters.preset === 'thisWeek' || filters.preset === 'lastWeek') return 'vs the week before';
|
|
38
|
+
if (filters.preset === 'thisMonth' || filters.preset === 'lastMonth') return 'vs the month before';
|
|
39
|
+
if (filters.compare === 'previous') return 'vs yesterday';
|
|
40
|
+
const weekday = new Intl.DateTimeFormat('en-GB', { weekday: 'long', timeZone: 'UTC' }).format(new Date(`${filters.range.from}T00:00:00Z`));
|
|
41
|
+
return `vs last ${weekday}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function SalesHero({ figures, filters, onFiltersChange, currency }: SalesHeroProps): JSX.Element {
|
|
45
|
+
const money = (minor: number) => formatMoney({ amount: minor, currency }) ?? '';
|
|
46
|
+
const { totals, series } = figures;
|
|
47
|
+
const before = figures.comparison && figures.comparison.totals.orderCount > 0 ? figures.comparison.totals : undefined;
|
|
48
|
+
const label = compareLabel(filters);
|
|
49
|
+
const grossDiff = before ? totals.grossMinor - before.grossMinor : 0;
|
|
50
|
+
const pct = before && before.grossMinor !== 0 ? Math.round((grossDiff / before.grossMinor) * 1000) / 10 : undefined;
|
|
51
|
+
const companions = [
|
|
52
|
+
{ id: 'orders', name: 'Orders', value: totals.orderCount, prev: before?.orderCount, format: String },
|
|
53
|
+
{ id: 'average', name: 'Average order', value: totals.averageMinor, prev: before?.averageMinor, format: money },
|
|
54
|
+
{ id: 'items', name: 'Items', value: totals.items, prev: before?.items, format: String },
|
|
55
|
+
];
|
|
56
|
+
const compareSeries = figures.comparison?.series ?? [];
|
|
57
|
+
const max = Math.max(0, ...series.map((point) => point.grossMinor), ...compareSeries.map((point) => point.grossMinor));
|
|
58
|
+
const height = (gross: number) => `${max > 0 ? (Math.max(0, gross) / max) * 100 : 0}%` as const;
|
|
59
|
+
const oneDay = filters.preset === 'today' || filters.preset === 'yesterday';
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<Card testID="sales-hero" className="gap-4 p-4">
|
|
63
|
+
<View className="flex-row flex-wrap items-end gap-3">
|
|
64
|
+
<Text testID="hero-total" className="text-4xl font-bold text-foreground">{money(totals.grossMinor)}</Text>
|
|
65
|
+
<Difference testID="hero-delta" diff={pct ?? 0} suffix={label}
|
|
66
|
+
text={pct === undefined ? undefined : pct === 0 ? '±0%' : signed(pct, (n) => `${n.toFixed(1)}%`)} />
|
|
67
|
+
</View>
|
|
68
|
+
{oneDay ? (
|
|
69
|
+
<View className="flex-row gap-2">
|
|
70
|
+
{(['previous', 'week'] as const).map((compare) => (
|
|
71
|
+
<Pressable key={compare} testID={`hero-compare-${compare}`} accessibilityRole="radio"
|
|
72
|
+
accessibilityState={{ checked: filters.compare === compare }}
|
|
73
|
+
onPress={() => onFiltersChange({ ...filters, compare })}
|
|
74
|
+
className={cn('rounded-md border border-border px-3 py-1', filters.compare === compare && 'bg-muted')}>
|
|
75
|
+
<Text className="text-sm text-foreground">{compareLabel({ ...filters, compare })}</Text>
|
|
76
|
+
</Pressable>
|
|
77
|
+
))}
|
|
78
|
+
</View>
|
|
79
|
+
) : (
|
|
80
|
+
<Text testID="hero-compare" className="text-sm text-muted-foreground">{label}</Text>
|
|
81
|
+
)}
|
|
82
|
+
<View className="flex-row flex-wrap gap-6">
|
|
83
|
+
{companions.map(({ id, name, value, prev, format }) => (
|
|
84
|
+
<View key={id} className="gap-0.5">
|
|
85
|
+
<Text className="text-xs text-muted-foreground">{name}</Text>
|
|
86
|
+
<Text testID={`hero-${id}`} className="text-lg font-semibold text-foreground">{format(value)}</Text>
|
|
87
|
+
<Difference testID={`hero-${id}-delta`} diff={prev === undefined ? 0 : value - prev}
|
|
88
|
+
text={prev === undefined ? undefined : signed(value - prev, format)} />
|
|
89
|
+
</View>
|
|
90
|
+
))}
|
|
91
|
+
</View>
|
|
92
|
+
<View testID="hero-chart" className="h-40 flex-row items-end gap-1">
|
|
93
|
+
{series.map((point, i) => (
|
|
94
|
+
<View key={point.day} className="h-full flex-1 flex-row items-end gap-0.5">
|
|
95
|
+
{compareSeries[i] && (
|
|
96
|
+
<View testID={`hero-bar-compare-${i}`} className="flex-1 rounded-sm bg-muted-foreground opacity-30"
|
|
97
|
+
style={{ height: height(compareSeries[i].grossMinor) }} />
|
|
98
|
+
)}
|
|
99
|
+
<View testID={`hero-bar-${i}`} className="flex-1 rounded-sm bg-primary" style={{ height: height(point.grossMinor) }} />
|
|
100
|
+
</View>
|
|
101
|
+
))}
|
|
102
|
+
</View>
|
|
103
|
+
{series.length > 0 && (
|
|
104
|
+
<View className="flex-row justify-between">
|
|
105
|
+
<Text className="text-xs text-muted-foreground">{dayLabel(series[0].day)}</Text>
|
|
106
|
+
{series.length > 1 && <Text className="text-xs text-muted-foreground">{dayLabel(series[series.length - 1].day)}</Text>}
|
|
107
|
+
</View>
|
|
108
|
+
)}
|
|
109
|
+
</Card>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SalesRoom, type SalesRoomProps } from './sales-room';
|
|
2
|
+
export { SalesHero, type SalesHeroProps } from './hero';
|
|
3
|
+
export { SalesCards, type SalesCardsProps, type SalesCardId } from './cards';
|
|
4
|
+
export { SalesFiltersBar, type SalesFiltersBarProps, type SalesFilters, type SalesOption } from './filters-bar';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Reports screen's Sales room: filters, hero and cards over one `SalesFigures` result.
|
|
3
|
+
* Port of the WCPOS v2 Sales room layout (`screens/main/reports/`, `LEDGER.md`). It takes the
|
|
4
|
+
* figures and the filter state as props; the screen works the figures out and owns the state.
|
|
5
|
+
*/
|
|
6
|
+
import type { JSX } from 'react';
|
|
7
|
+
import { View } from 'react-native';
|
|
8
|
+
import type { SalesFigures } from '@tallyui/pos';
|
|
9
|
+
import { SalesCards, type SalesCardId } from './cards';
|
|
10
|
+
import { SalesFiltersBar, type SalesFiltersBarProps } from './filters-bar';
|
|
11
|
+
import { SalesHero } from './hero';
|
|
12
|
+
|
|
13
|
+
export interface SalesRoomProps extends SalesFiltersBarProps {
|
|
14
|
+
figures: SalesFigures;
|
|
15
|
+
currency: string;
|
|
16
|
+
/** Opens a card's panel. */
|
|
17
|
+
onOpenCard?: (card: SalesCardId) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function SalesRoom({ figures, filters, onFiltersChange, currency, onOpenCard, ...bar }: SalesRoomProps): JSX.Element {
|
|
21
|
+
return (
|
|
22
|
+
<View testID="sales-room" className="gap-4">
|
|
23
|
+
<SalesFiltersBar filters={filters} onFiltersChange={onFiltersChange} {...bar} />
|
|
24
|
+
<SalesHero figures={figures} filters={filters} onFiltersChange={onFiltersChange} currency={currency} />
|
|
25
|
+
<SalesCards figures={figures} currency={currency} onOpenCard={onOpenCard} />
|
|
26
|
+
</View>
|
|
27
|
+
);
|
|
28
|
+
}
|
package/src/sale/cart.tsx
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
import { useState, type ReactNode } from 'react';
|
|
2
|
-
import { Pressable, Text, View } from 'react-native';
|
|
2
|
+
import { Pressable, Text, View, useWindowDimensions } from 'react-native';
|
|
3
3
|
import { formatMoney } from '@tallyui/core';
|
|
4
4
|
import { buildReceiptData } from '@tallyui/pos';
|
|
5
5
|
import type { useSale } from '@tallyui/pos';
|
|
6
|
-
import { CartLine, CartLineActions, CartPanel, CartTotal } from '../cart';
|
|
6
|
+
import { CartColumnHeads, CartLine, CartLineActions, CartPanel, CartTotal } from '../cart';
|
|
7
|
+
import { cartShowsPrice } from '../cart/cart-columns';
|
|
7
8
|
import { DiscountChips, DiscountForm } from './discount-form';
|
|
8
9
|
import { PriceForm } from './price-form';
|
|
9
10
|
import { ChargeForm } from './charge-form';
|
|
10
11
|
|
|
11
12
|
/** `taxLabel` names each tax row; VAT isn't universal, so a platform passes its own (medusapos passes `VAT ${rate}%`). */
|
|
12
|
-
export function Cart({ sale, taxLabel = (ratePpm: number) => `Tax ${ratePpm / 10000}%`, canEditPrice = false, onPriceChange, taxClasses, afterSubtotal, couponDiscountMinor }:
|
|
13
|
+
export function Cart({ sale, taxLabel = (ratePpm: number) => `Tax ${ratePpm / 10000}%`, canEditPrice = false, onPriceChange, quickDiscounts, taxClasses, afterSubtotal, couponDiscountMinor, orderActions = true }:
|
|
13
14
|
{ sale: ReturnType<typeof useSale>; taxLabel?: (ratePpm: number) => string;
|
|
14
15
|
taxClasses?: ReadonlyArray<{ id: string; label: string }>;
|
|
15
16
|
/** Shows a Price action on each line. Pass the till's own permission; the control isn't shown without it. */
|
|
16
17
|
canEditPrice?: boolean;
|
|
17
18
|
/** Called after a price is applied, for the app's audit log. The reason is kept only here. */
|
|
18
19
|
onPriceChange?: (change: { lineId: string; fromMinor: number; toMinor: number; reason?: string }) => void;
|
|
20
|
+
/** v2's quick-discount keys on each line's Price form (e.g. the app's `cart.quickDiscounts` setting); needs `canEditPrice`. */
|
|
21
|
+
quickDiscounts?: readonly number[];
|
|
19
22
|
/** Rows under the totals' Subtotal, e.g. the platform's coupon chips (passed to CartTotal). */
|
|
20
23
|
afterSubtotal?: ReactNode;
|
|
21
24
|
/** The coupon amount the app itemises in `afterSubtotal` (e.g. Σ `order.display.coupons`), in integer minor units of
|
|
22
25
|
* the order's currency, in the display mode. When given, the Discount row shows the other discounts only (the
|
|
23
26
|
* totals' discount minus this) and is hidden when they come to 0. Absent: the Discount row is the whole discount. */
|
|
24
27
|
couponDiscountMinor?: number;
|
|
28
|
+
/** Cart's own Order discount and Add charge controls; pass `false` when the app hosts them in its own menu (v2's
|
|
29
|
+
* cart "+" menu). Applied discounts and charges still show and can be removed. */
|
|
30
|
+
orderActions?: boolean;
|
|
25
31
|
}) {
|
|
26
32
|
const { order } = sale;
|
|
27
33
|
const money = (amount: number) => ({ amount, currency: order.currency });
|
|
@@ -36,30 +42,37 @@ export function Cart({ sale, taxLabel = (ratePpm: number) => `Tax ${ratePpm / 10
|
|
|
36
42
|
// One open form: a line's discount or price, the order's discount (lineId null), or a charge.
|
|
37
43
|
const [form, setForm] = useState<{ type: 'discount'; lineId: string | null } | { type: 'price'; lineId: string } | { type: 'charge' } | null>(null);
|
|
38
44
|
const chargesOffered = (sale.capabilities?.orderCreate ?? 0) >= 5;
|
|
45
|
+
const showOrderDiscount = order.lineItems.length > 0 && (orderActions || order.discounts.length > 0);
|
|
46
|
+
const showCharge = orderActions && chargesOffered;
|
|
39
47
|
const discountForm = (lineId: string | null, title: string) => form?.type === 'discount' && form.lineId === lineId
|
|
40
48
|
? <DiscountForm key={lineId ?? 'order'} title={title} currency={order.currency} onClose={() => setForm(null)}
|
|
41
49
|
onApply={(discount) => sale.applyDiscount(lineId, discount)} /> : null;
|
|
42
50
|
const pay = `min-h-12 flex-1 justify-center rounded-md bg-primary px-4 py-3 ${!order.lineItems.length ? 'opacity-50' : ''}`;
|
|
51
|
+
// The Price column folds on a narrow cart or a phone window (v2's rule), in the heads and every line alike.
|
|
52
|
+
const [cartWidth, setCartWidth] = useState<number>();
|
|
53
|
+
const { width: windowWidth, height: windowHeight } = useWindowDimensions();
|
|
54
|
+
const showPrice = cartShowsPrice({ cartWidth, windowWidth, windowHeight });
|
|
43
55
|
const renderItem = (line: typeof order.lineItems[number]) => <>
|
|
56
|
+
<CartLine name={line.name} attributes={line.attributes} quantity={line.quantity} unitPrice={money(line.unitPriceMinor)}
|
|
57
|
+
regularUnitPrice={line.regularUnitPriceMinor === undefined ? undefined : money(line.regularUnitPriceMinor)}
|
|
58
|
+
lineTotal={money(display(line.id).amountMinor)} showPrice={showPrice} />
|
|
59
|
+
<DiscountChips discounts={line.discounts} amounts={display(line.id).discounts} currency={order.currency} onRemove={sale.removeDiscount} />
|
|
44
60
|
<CartLineActions actions={[
|
|
45
61
|
{ id: 'discount', label: 'Discount', color: 'text-primary', onPress: () => setForm({ type: 'discount', lineId: line.id }) },
|
|
46
62
|
...(canEditPrice ? [{ id: 'price', label: 'Price', color: 'text-primary', onPress: () => setForm({ type: 'price', lineId: line.id }) }] : []),
|
|
47
63
|
]}>
|
|
48
|
-
<
|
|
49
|
-
|
|
64
|
+
<View className="flex-row gap-4 px-3 py-2">
|
|
65
|
+
<Pressable accessibilityRole="button" accessibilityLabel={`Decrease ${line.name}`}
|
|
66
|
+
onPress={() => sale.setQuantity(line.id, line.quantity - 1)} className="rounded-md border border-border bg-card px-3 py-2 min-h-11 min-w-11 items-center justify-center"><Text className="text-center text-foreground">−</Text></Pressable>
|
|
67
|
+
<Pressable accessibilityRole="button" accessibilityLabel={`Increase ${line.name}`}
|
|
68
|
+
onPress={() => sale.setQuantity(line.id, line.quantity + 1)} className="rounded-md border border-border bg-card px-3 py-2 min-h-11 min-w-11 items-center justify-center"><Text className="text-center text-foreground">+</Text></Pressable>
|
|
69
|
+
<Pressable accessibilityRole="button" accessibilityLabel={`Remove ${line.name}`}
|
|
70
|
+
onPress={() => sale.remove(line.id)} className="rounded-md border border-border bg-card px-3 py-2 min-h-11 min-w-11 items-center justify-center"><Text className="text-center text-foreground">Remove</Text></Pressable>
|
|
71
|
+
</View>
|
|
50
72
|
</CartLineActions>
|
|
51
|
-
<DiscountChips discounts={line.discounts} amounts={display(line.id).discounts} currency={order.currency} onRemove={sale.removeDiscount} />
|
|
52
|
-
<View className="flex-row gap-4 px-3 py-2">
|
|
53
|
-
<Pressable accessibilityRole="button" accessibilityLabel={`Decrease ${line.name}`}
|
|
54
|
-
onPress={() => sale.setQuantity(line.id, line.quantity - 1)} className="rounded-md border border-border bg-card px-3 py-2 min-h-11 min-w-11 items-center justify-center"><Text className="text-center text-foreground">−</Text></Pressable>
|
|
55
|
-
<Pressable accessibilityRole="button" accessibilityLabel={`Increase ${line.name}`}
|
|
56
|
-
onPress={() => sale.setQuantity(line.id, line.quantity + 1)} className="rounded-md border border-border bg-card px-3 py-2 min-h-11 min-w-11 items-center justify-center"><Text className="text-center text-foreground">+</Text></Pressable>
|
|
57
|
-
<Pressable accessibilityRole="button" accessibilityLabel={`Remove ${line.name}`}
|
|
58
|
-
onPress={() => sale.remove(line.id)} className="rounded-md border border-border bg-card px-3 py-2 min-h-11 min-w-11 items-center justify-center"><Text className="text-center text-foreground">Remove</Text></Pressable>
|
|
59
|
-
</View>
|
|
60
73
|
{discountForm(line.id, `Discount on ${line.name}`)}
|
|
61
74
|
{canEditPrice && form?.type === 'price' && form.lineId === line.id ? <PriceForm key={line.id} lineName={line.name}
|
|
62
|
-
currency={order.currency} currentMinor={line.unitPriceMinor} onClose={() => setForm(null)}
|
|
75
|
+
currency={order.currency} currentMinor={line.unitPriceMinor} quickDiscounts={quickDiscounts} onClose={() => setForm(null)}
|
|
63
76
|
onApply={(amount, reason) => {
|
|
64
77
|
const fromMinor = line.unitPriceMinor;
|
|
65
78
|
const refused = sale.setUnitPrice(line.id, amount);
|
|
@@ -67,7 +80,9 @@ export function Cart({ sale, taxLabel = (ratePpm: number) => `Tax ${ratePpm / 10
|
|
|
67
80
|
return refused;
|
|
68
81
|
}} /> : null}
|
|
69
82
|
</>;
|
|
70
|
-
return <
|
|
83
|
+
return <View dataSet={{ print: 'hide' }} className="flex-1 min-h-0" onLayout={(event) => setCartWidth(event.nativeEvent.layout.width)}>
|
|
84
|
+
<CartColumnHeads showPrice={showPrice} />
|
|
85
|
+
<CartPanel items={order.lineItems} renderItem={renderItem}
|
|
71
86
|
emptyState={<Text testID="cart-empty" className="px-3 py-2 text-muted-foreground">Scan or tap a product to start a sale.</Text>}
|
|
72
87
|
afterItems={<>
|
|
73
88
|
{(['fees', 'shipping'] as const).map((kind) => order[kind]?.map((charge, index) =>
|
|
@@ -79,15 +94,15 @@ export function Cart({ sale, taxLabel = (ratePpm: number) => `Tax ${ratePpm / 10
|
|
|
79
94
|
className="rounded-md border border-border bg-card px-3 py-2 min-h-11 items-center justify-center">
|
|
80
95
|
<Text className="text-foreground">Remove</Text></Pressable>
|
|
81
96
|
</View>))}
|
|
82
|
-
{
|
|
83
|
-
{
|
|
97
|
+
{showOrderDiscount || showCharge ? <View className="gap-3 px-3 py-2">
|
|
98
|
+
{showOrderDiscount ? <>
|
|
84
99
|
<DiscountChips discounts={order.discounts} amounts={orderAmounts} currency={order.currency} onRemove={sale.removeDiscount}
|
|
85
100
|
prefix="Order discount" />
|
|
86
|
-
{form?.type === 'discount' && form.lineId === null ? discountForm(null, 'Order discount') : <Pressable accessibilityRole="button"
|
|
101
|
+
{!orderActions ? null : form?.type === 'discount' && form.lineId === null ? discountForm(null, 'Order discount') : <Pressable accessibilityRole="button"
|
|
87
102
|
onPress={() => setForm({ type: 'discount', lineId: null })} className="self-start rounded-md border border-border bg-card px-4 py-2 min-h-11 justify-center">
|
|
88
103
|
<Text className="text-foreground">Order discount</Text></Pressable>}
|
|
89
104
|
</> : null}
|
|
90
|
-
{
|
|
105
|
+
{showCharge ? form?.type === 'charge' ? <ChargeForm currency={order.currency} lineTax={sale.capabilities?.lineTax}
|
|
91
106
|
taxClasses={taxClasses} shippingTaxFromStore={sale.capabilities?.taxRounding?.granularity === 'woocommerce'}
|
|
92
107
|
onClose={() => setForm(null)} onApply={({ kind, name, amountMinor, taxStatus, taxClass }) => {
|
|
93
108
|
const result = kind === 'custom' ? sale.addCustomLine({ name, priceMinor: amountMinor, taxStatus, taxClass })
|
|
@@ -112,5 +127,6 @@ export function Cart({ sale, taxLabel = (ratePpm: number) => `Tax ${ratePpm / 10
|
|
|
112
127
|
<Text className="text-center font-semibold text-primary-foreground">Card terminal</Text>
|
|
113
128
|
</Pressable>
|
|
114
129
|
</View>
|
|
115
|
-
</View>}
|
|
130
|
+
</View>} />
|
|
131
|
+
</View>;
|
|
116
132
|
}
|