feat: add remote products and qr code for payconic

This commit is contained in:
2026-10-22 00:10:26 +02:00
parent b15d94a805
commit 71af6f9ffe
31 changed files with 2053 additions and 179 deletions
+52 -7
View File
@@ -2,6 +2,7 @@ import 'package:drift/drift.dart';
import 'package:uuid/uuid.dart';
import '../database/app_database.dart';
import '../database/catalog_queries.dart';
import '../database/stock_queries.dart';
import '../models/product.dart';
import '../models/tab_event.dart';
@@ -87,6 +88,8 @@ class DriftProductService implements ProductService {
return _mapRowToProduct(row);
}
/// Queues the new product for the server (which takes this tablet's id), its
/// starting stock as a count, and its picture for upload.
@override
Future<void> createProduct({
required String name,
@@ -114,16 +117,29 @@ class DriftProductService implements ProductService {
imagePath: Value(imagePath),
),
);
await database.queueEvent(
eventFactory.productCreated(
productId: id,
fields: {
'name': name,
'category': _serverCategory(category),
'priceInCents': priceInCents,
'lowStockThreshold': lowStockThreshold,
},
),
);
// The server starts a product it doesn't know as untracked
if (!infiniteStock) {
await database.queueStockSet(
await database.queueEvent(
eventFactory.stockSet(productId: id, quantity: stockQuantity),
);
}
if (imagePath != null) await database.queueImageUpload(id, imagePath);
});
}
/// A changed stock or tracking is queued for the server as a count.
/// Queues what changed for the server: the changed fields, a changed stock
/// or tracking as a count, and a new picture for upload.
@override
Future<void> updateProduct(Product product) async {
await database.transaction(() async {
@@ -147,28 +163,57 @@ class DriftProductService implements ProductService {
);
if (current == null) return;
final fields = <String, Object?>{
if (current.name != product.name) 'name': product.name,
if (current.category != product.category)
'category': _serverCategory(product.category),
if (current.priceInCents != product.priceInCents)
'priceInCents': product.priceInCents,
if (current.lowStockThreshold != product.lowStockThreshold)
'lowStockThreshold': product.lowStockThreshold,
};
if (fields.isNotEmpty) {
await database.queueEvent(
eventFactory.productUpdated(productId: product.id, fields: fields),
);
}
final stockChanged = product.infiniteStock
? !current.infiniteStock
: current.infiniteStock ||
current.stockQuantity != product.stockQuantity;
if (stockChanged) {
await database.queueStockSet(
await database.queueEvent(
eventFactory.stockSet(
productId: product.id,
quantity: product.infiniteStock ? null : product.stockQuantity,
),
);
}
final picture = product.imagePath;
if (picture != null && picture != current.imagePath) {
await database.queueImageUpload(product.id, picture);
}
});
}
/// Only hidden, since open tabs and history still refer to it.
/// Only hidden, since open tabs and history still refer to it; the server
/// archives it for every tablet.
@override
Future<void> deleteProduct(String id) async {
final query = database.update(database.products)
..where((product) => product.id.equals(id));
await database.transaction(() async {
await (database.update(database.products)
..where((product) => product.id.equals(id)))
.write(const ProductsCompanion(active: Value(false)));
await database.queueEvent(eventFactory.productArchived(productId: id));
});
}
await query.write(const ProductsCompanion(active: Value(false)));
/// The server has no category for an empty one.
static String? _serverCategory(String category) {
final trimmed = category.trim();
return trimmed.isEmpty ? null : trimmed;
}
@override