matchReturnCode function
ReturnCodeMatch
matchReturnCode(
- String code, {
- required List<
PosArticle> articles, - required List<
ArticleGroup> groups, - required List<
ReturnCode> learned, - required List<
DepositType> depositTypes,
Ordnet einen Scan im Leergut-Blatt einer Pfandart zu. Reihenfolge: (1)
gelernter Code -> 1 Stueck; (2) gtin eines Pfandeintrags an Artikel oder
Gruppe -> 1 Stueck dieser Pfandart; (3) Artikel-EAN, -Nummer oder -Code mit
wirksamer Pfandliste -> jeder Eintrag mit seiner quantity (Kiste: 1 Kiste
- 20 Flaschen); sonst
unknown. Es zaehlen nur aktive Pfandarten: ein Treffer auf eine stillgelegte oder unbekannte Pfandart istunknown(die Suche endet am ersten Treffer, sie faellt nicht auf die naechste Stufe zurueck). Der Code wird getrimmt, ein leerer istunknown. Zwilling vonmatchReturnCodeim npm-Paket.
Implementation
ReturnCodeMatch matchReturnCode(
String code, {
required List<PosArticle> articles,
required List<ArticleGroup> groups,
required List<ReturnCode> learned,
required List<DepositType> depositTypes,
}) {
const unbekannt = ReturnCodeMatch.unknown();
final gesucht = code.trim();
if (gesucht.isEmpty) return unbekannt;
final aktiv = {for (final t in depositTypes) if (t.active) t.id};
ReturnCodeMatch eins(String id) =>
aktiv.contains(id) ? ReturnCodeMatch.entries([ReturnCodeEntry(depositTypeId: id, quantity: 1)]) : unbekannt;
for (final l in learned) {
if (l.gtin == gesucht) return eins(l.depositTypeId);
}
ArticleGroup? gruppeVon(PosArticle a) => groups.where((g) => g.id == a.groupId).firstOrNull;
for (final a in articles) {
for (final d in depositsFor(a, gruppeVon(a))) {
if (d.gtin == gesucht) return eins(d.depositTypeId);
}
}
for (final g in groups) {
for (final d in g.deposits ?? const <DepositEntry>[]) {
if (d.gtin == gesucht) return eins(d.depositTypeId);
}
}
final artikel = articles.where((a) => a.ean == gesucht || a.number == gesucht || a.internalCode == gesucht).firstOrNull;
if (artikel == null) return unbekannt;
final liste = depositsFor(artikel, gruppeVon(artikel));
if (liste.isEmpty || !liste.every((d) => aktiv.contains(d.depositTypeId))) return unbekannt;
return ReturnCodeMatch.entries([for (final d in liste) ReturnCodeEntry(depositTypeId: d.depositTypeId, quantity: d.quantity)]);
}