Files
kooltab/lib/models/server_tab.dart
T

135 lines
3.8 KiB
Dart

import 'payment_method.dart';
/// One piece on a line: when it was put on the tab and, if it was taken off
/// again, when. Kept so history can show every drink with its time.
class ServerUnit {
/// Stable across syncs: `<event id>:<index within that event>`.
final String id;
final DateTime addedAt;
final DateTime? removedAt;
/// The order it was rung up in; null for drinks from older app versions.
final String? orderId;
const ServerUnit({
required this.id,
required this.addedAt,
this.removedAt,
this.orderId,
});
factory ServerUnit.fromJson(Map<String, dynamic> json) {
return ServerUnit(
id: json['id'] as String,
addedAt: DateTime.parse(json['addedAt'] as String).toLocal(),
removedAt: switch (json['removedAt']) {
final String value => DateTime.parse(value).toLocal(),
_ => null,
},
orderId: json['orderId'] as String?,
);
}
}
class ServerTabLine {
final String id;
final String productId;
final String productName;
final int unitPriceInCents;
final int quantity;
final List<ServerUnit> units;
const ServerTabLine({
required this.id,
required this.productId,
required this.productName,
required this.unitPriceInCents,
required this.quantity,
this.units = const [],
});
factory ServerTabLine.fromJson(Map<String, dynamic> json) {
return ServerTabLine(
id: json['id'] as String,
productId: json['productId'] as String? ?? '',
productName: json['productName'] as String,
unitPriceInCents: json['unitPriceInCents'] as int,
quantity: json['quantity'] as int,
units: [
for (final unit in json['units'] as List<dynamic>? ?? [])
ServerUnit.fromJson(unit as Map<String, dynamic>),
],
);
}
}
/// An open tab as the server holds it: the venue's source of truth.
class ServerTab {
final String id;
final String customerName;
final String? email;
final DateTime openedAt;
final int version;
final List<ServerTabLine> lines;
const ServerTab({
required this.id,
required this.customerName,
this.email,
required this.openedAt,
required this.version,
this.lines = const [],
});
factory ServerTab.fromJson(Map<String, dynamic> json) {
return ServerTab(
id: json['id'] as String,
customerName: json['customerName'] as String,
email: json['email'] as String?,
openedAt: DateTime.parse(json['openedAt'] as String).toLocal(),
version: json['version'] as int? ?? 0,
lines: [
for (final line in json['lines'] as List<dynamic>? ?? [])
ServerTabLine.fromJson(line as Map<String, dynamic>),
],
);
}
}
/// A tab settled anywhere in the venue (this tablet, another tablet or the
/// dashboard), for the local history.
class ServerSettlement {
/// For a settlement made on a tablet: the id of its `settled` event.
final String id;
final String tabId;
final String customerName;
final DateTime settledAt;
final PaymentMethod paymentMethod;
final List<ServerTabLine> lines;
const ServerSettlement({
required this.id,
required this.tabId,
required this.customerName,
required this.settledAt,
required this.paymentMethod,
this.lines = const [],
});
factory ServerSettlement.fromJson(Map<String, dynamic> json) {
return ServerSettlement(
id: json['id'] as String,
tabId: json['tabId'] as String,
customerName: json['customerName'] as String,
settledAt: DateTime.parse(json['settledAt'] as String).toLocal(),
paymentMethod: PaymentMethod.fromValue(
json['paymentMethod'] as String? ?? 'cash',
),
lines: [
for (final line in json['lines'] as List<dynamic>? ?? [])
ServerTabLine.fromJson(line as Map<String, dynamic>),
],
);
}
}