Open the cash drawer for real, and persist the back-office route
Cash drawer - openCashDrawer was a debugPrint. The drawer never opened. - It cannot go through the PDF pipeline: a PDF is rendered by the platform driver, which will not pass raw ESC/POS bytes to the device. So it goes over a socket instead — nearly every network thermal printer listens on 9100 and forwards whatever arrives straight to the print head, which makes the whole protocol five bytes. - Printer IP and port are configurable in Settings with a Test button that saves and fires immediately, because a drawer that does not open is indistinguishable from one that is not wired up. - Every failure explains itself: unreachable, refused, or simply not configured — which is the honest state for a USB printer, since there is no raw path to one from Flutter. - Now fires only on a cash tender. A card-only sale that pops the drawer is a shrinkage risk, and it is the first thing a shop notices. Back-office route - Host, port, TLS and transport persist to the database; username, password and API key go to the platform keystore (Keychain / Credential Manager / Android Keystore). Writing credentials into SQLite would put them in the same file as the bills, on a machine behind a shop counter. - Loaded at startup. Previously the dialog wrote settings that were silently ignored on the next launch, which reads exactly like they never saved — and credentials retyped every morning end up on a sticky note instead. - A saved route never overwrites the terminal's store or terminal id. Those belong to the device, and re-pointing a till at a different broker must not change who it is, or its bills and presence records stop lining up. Tests: 168 -> 176. The drawer test stands up a real socket server and asserts the exact bytes arrive. The config test asserts no credential appears anywhere in the meta table while the non-secret settings do. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,8 @@ class PrinterSettings {
|
||||
this.printerName,
|
||||
this.autoPrint = false,
|
||||
this.openDrawer = true,
|
||||
this.drawerHost,
|
||||
this.drawerPort = 9100,
|
||||
});
|
||||
|
||||
/// Target passed to `directPrintPdf`. Null means "use the system default".
|
||||
@@ -31,7 +33,16 @@ class PrinterSettings {
|
||||
|
||||
final bool openDrawer;
|
||||
|
||||
/// IP address of the receipt printer the drawer is wired to.
|
||||
///
|
||||
/// Separate from [printerUrl] because that is an opaque platform handle for
|
||||
/// the PDF driver, which cannot carry raw ESC/POS bytes. The drawer kick
|
||||
/// needs a socket, so it needs an address.
|
||||
final String? drawerHost;
|
||||
final int drawerPort;
|
||||
|
||||
bool get hasPrinter => printerUrl != null;
|
||||
bool get hasDrawer => (drawerHost ?? '').isNotEmpty;
|
||||
|
||||
PrinterSettings copyWith({
|
||||
String? printerUrl,
|
||||
@@ -39,12 +50,16 @@ class PrinterSettings {
|
||||
bool clearPrinter = false,
|
||||
bool? autoPrint,
|
||||
bool? openDrawer,
|
||||
String? drawerHost,
|
||||
int? drawerPort,
|
||||
}) {
|
||||
return PrinterSettings(
|
||||
printerUrl: clearPrinter ? null : (printerUrl ?? this.printerUrl),
|
||||
printerName: clearPrinter ? null : (printerName ?? this.printerName),
|
||||
autoPrint: autoPrint ?? this.autoPrint,
|
||||
openDrawer: openDrawer ?? this.openDrawer,
|
||||
drawerHost: drawerHost ?? this.drawerHost,
|
||||
drawerPort: drawerPort ?? this.drawerPort,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -69,6 +84,8 @@ class PrinterSettingsController extends StateNotifier<PrinterSettings> {
|
||||
final name = await dao.meta(MetaKeys.printerName);
|
||||
final autoPrint = await dao.meta(MetaKeys.autoPrint);
|
||||
final openDrawer = await dao.meta(MetaKeys.openDrawer);
|
||||
final drawerHost = await dao.meta(MetaKeys.drawerHost);
|
||||
final drawerPort = await dao.meta(MetaKeys.drawerPort);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
@@ -77,6 +94,8 @@ class PrinterSettingsController extends StateNotifier<PrinterSettings> {
|
||||
printerName: name,
|
||||
autoPrint: autoPrint == '1',
|
||||
openDrawer: openDrawer != '0',
|
||||
drawerHost: drawerHost,
|
||||
drawerPort: int.tryParse(drawerPort ?? '') ?? 9100,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -120,6 +139,18 @@ class PrinterSettingsController extends StateNotifier<PrinterSettings> {
|
||||
if (!mounted) return;
|
||||
state = state.copyWith(openDrawer: value);
|
||||
}
|
||||
|
||||
/// Points the drawer kick at a printer.
|
||||
///
|
||||
/// A blank host disables it — which is the honest state for a USB printer,
|
||||
/// since there is no raw path to one from Flutter.
|
||||
Future<void> setDrawerAddress(String host, int port) async {
|
||||
final dao = _ref.read(localStoreProvider).catalogue;
|
||||
await dao.setMeta(MetaKeys.drawerHost, host.trim());
|
||||
await dao.setMeta(MetaKeys.drawerPort, '$port');
|
||||
if (!mounted) return;
|
||||
state = state.copyWith(drawerHost: host.trim(), drawerPort: port);
|
||||
}
|
||||
}
|
||||
|
||||
final printerSettingsProvider =
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../app/providers.dart';
|
||||
import '../../../core/config/sync_config.dart';
|
||||
import '../../../core/constants/app_constants.dart';
|
||||
import '../../../core/services/cash_drawer_service.dart';
|
||||
import '../../../core/theme/app_colors.dart';
|
||||
import '../../../core/theme/app_dimens.dart';
|
||||
import '../../../core/utils/formatters.dart';
|
||||
@@ -26,15 +27,36 @@ class SettingsView extends ConsumerStatefulWidget {
|
||||
}
|
||||
|
||||
class _SettingsViewState extends ConsumerState<SettingsView> {
|
||||
final _drawerHost = TextEditingController();
|
||||
final _drawerPort = TextEditingController(text: '9100');
|
||||
bool _testingDrawer = false;
|
||||
bool _loadedDrawerFields = false;
|
||||
|
||||
bool _scannerSound = true;
|
||||
bool _roundOff = true;
|
||||
bool _autoLoyalty = true;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_drawerHost.dispose();
|
||||
_drawerPort.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final store = ref.watch(currentStoreProvider);
|
||||
final user = ref.watch(currentUserProvider);
|
||||
|
||||
// Seeded once, from whatever was persisted. Assigning on every build would
|
||||
// fight the cashier for the cursor while they type.
|
||||
final printer = ref.watch(printerSettingsProvider);
|
||||
if (!_loadedDrawerFields && printer.hasDrawer) {
|
||||
_loadedDrawerFields = true;
|
||||
_drawerHost.text = printer.drawerHost ?? '';
|
||||
_drawerPort.text = '${printer.drawerPort}';
|
||||
}
|
||||
|
||||
return ModulePage(
|
||||
children: [
|
||||
LayoutBuilder(
|
||||
@@ -290,15 +312,96 @@ class _SettingsViewState extends ConsumerState<SettingsView> {
|
||||
),
|
||||
_toggle(
|
||||
'Open cash drawer on cash sales',
|
||||
'Needs a raw ESC/POS link — see the notes in ReceiptService',
|
||||
settings.hasDrawer
|
||||
? 'Kicks the drawer on ${settings.drawerHost} after a cash '
|
||||
'tender'
|
||||
: 'Enter the printer\'s IP address below to enable this',
|
||||
settings.openDrawer,
|
||||
controller.setOpenDrawer,
|
||||
settings.hasDrawer ? controller.setOpenDrawer : null,
|
||||
),
|
||||
const SizedBox(height: AppSpacing.sm),
|
||||
_drawerAddressField(settings, controller),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The drawer needs a socket, not the print driver.
|
||||
///
|
||||
/// A PDF is rendered by the platform driver, which will not pass raw ESC/POS
|
||||
/// bytes through to the device — so the printer's own address is the only way
|
||||
/// to reach the drawer wired to its RJ11 port.
|
||||
Widget _drawerAddressField(
|
||||
PrinterSettings settings,
|
||||
PrinterSettingsController controller,
|
||||
) {
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: TextFormField(
|
||||
controller: _drawerHost,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Printer IP address',
|
||||
hintText: '192.168.1.50',
|
||||
helperText: 'Leave blank for a USB printer',
|
||||
isDense: true,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
SizedBox(
|
||||
width: 84,
|
||||
child: TextFormField(
|
||||
controller: _drawerPort,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: const InputDecoration(labelText: 'Port', isDense: true),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: AppSpacing.xs),
|
||||
child: OutlinedButton(
|
||||
onPressed: _testingDrawer
|
||||
? null
|
||||
: () => _saveAndTestDrawer(controller),
|
||||
child: _testingDrawer
|
||||
? const SizedBox(
|
||||
width: 14,
|
||||
height: 14,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('Test'),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _saveAndTestDrawer(PrinterSettingsController controller) async {
|
||||
setState(() => _testingDrawer = true);
|
||||
|
||||
final port = int.tryParse(_drawerPort.text.trim()) ?? 9100;
|
||||
await controller.setDrawerAddress(_drawerHost.text, port);
|
||||
|
||||
final result = await ref.read(receiptServiceProvider).openCashDrawer(
|
||||
host: _drawerHost.text,
|
||||
port: port,
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
setState(() => _testingDrawer = false);
|
||||
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(SnackBar(
|
||||
backgroundColor:
|
||||
result.isSuccess ? AppColors.success : AppColors.danger,
|
||||
content: Text(result.message),
|
||||
),);
|
||||
}
|
||||
|
||||
Widget _staffCard(StoreAccount? store, StaffUser? current) => PanelCard(
|
||||
title: 'Users & roles',
|
||||
action: TextButton(
|
||||
|
||||
@@ -95,20 +95,23 @@ class _BackOfficeDialogState extends ConsumerState<_BackOfficeDialog> {
|
||||
// Deliberately left out of the identity store: credentials belong to the
|
||||
// route, not to the machine, and re-pointing a terminal should not rewrite
|
||||
// who it is.
|
||||
ref.read(syncConfigProvider.notifier).state =
|
||||
ref.read(syncConfigProvider).copyWith(
|
||||
transport: _kind,
|
||||
storeId: _storeId.text.trim(),
|
||||
brokerHost: _host.text.trim(),
|
||||
brokerPort: int.tryParse(_port.text.trim()) ?? 8883,
|
||||
useTls: _useTls,
|
||||
username: _username.text.trim().isEmpty
|
||||
? null
|
||||
: _username.text.trim(),
|
||||
password: _password.text.isEmpty ? null : _password.text,
|
||||
httpBaseUrl: _httpUrl.text.trim(),
|
||||
apiKey: _apiKey.text.trim().isEmpty ? null : _apiKey.text.trim(),
|
||||
);
|
||||
final next = ref.read(syncConfigProvider).copyWith(
|
||||
transport: _kind,
|
||||
storeId: _storeId.text.trim(),
|
||||
brokerHost: _host.text.trim(),
|
||||
brokerPort: int.tryParse(_port.text.trim()) ?? 8883,
|
||||
useTls: _useTls,
|
||||
username: _username.text.trim().isEmpty ? null : _username.text.trim(),
|
||||
password: _password.text.isEmpty ? null : _password.text,
|
||||
httpBaseUrl: _httpUrl.text.trim(),
|
||||
apiKey: _apiKey.text.trim().isEmpty ? null : _apiKey.text.trim(),
|
||||
);
|
||||
|
||||
// Non-secret settings to the database, credentials to the OS keystore.
|
||||
// Held only in memory they had to be retyped after every restart, which on
|
||||
// a shop-floor terminal means they end up on a sticky note instead.
|
||||
await store.syncConfig.save(next);
|
||||
ref.read(syncConfigProvider.notifier).state = next;
|
||||
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user