PosArticle.fromJson constructor
Aus der Drahtform /api/v3 (tile {visible, sort}, quantityRule,
askQuantity, maxQuantity, revenueGroupId, stockLocationIds,
number, ean, internalCode, stockTracked).
Liest auch einen Zwischenspeicher der Version 9.x (kasse {sichtbar, sort}, quantityRule stueck|dezimal, askQuantity,
maxQuantity, so schrieb toJson bis 9.x): nach dem Update gehen die
Kacheln des letzten Stands nicht verloren.
Implementation
factory PosArticle.fromJson(Map<String, dynamic> json) {
final alt = !json.containsKey('tile') && json.containsKey('kasse');
final kachel = json[alt ? 'kasse' : 'tile'];
final regel = json[alt ? 'mengenregel' : 'quantityRule'];
final fragen = json[alt ? 'mengeFragen' : 'askQuantity'];
final grenze = json[alt ? 'maxMenge' : 'maxQuantity'];
final erloes = json['revenueGroupId'];
final standorte = json['stockLocationIds'];
return PosArticle(
id: json['id'] is String ? json['id'] as String : '',
name: json['name'] is String ? json['name'] as String : '',
// round, nicht toInt: das Backend rechnet den Preis in JavaScript aus
// Euro hoch, und 19.99 * 100 ergibt dort 1998.9999999999998 -- ein
// abgeschnittener Wert verkaufte den Artikel dauerhaft einen Cent zu
// billig, und zwar auf einen signierten Beleg. Alle Schwesterstellen
// (kasseneck_item, keck_voucher, keck_invoice_item, belege) runden.
unitPriceCents: json['unitPriceCents'] is num ? (json['unitPriceCents'] as num).round() : null,
vatRate: json['vatRate'] is num ? json['vatRate'] as num : null,
unit: json['unit'] is String ? json['unit'] as String : '',
groupId: json['groupId'] is String ? json['groupId'] as String : null,
revenueGroupId: erloes is String && erloes.isNotEmpty ? erloes : null,
// Fehlt die Angabe, ist der Artikel sichtbar: ein Betrieb, der nie
// etwas eingestellt hat, soll seine Artikel trotzdem sehen.
visible: kachel is Map ? kachel[alt ? 'sichtbar' : 'visible'] != false : true,
sort: kachel is Map && kachel['sort'] is num ? (kachel['sort'] as num).toInt() : 0,
active: json['active'] != false,
quantityRule: switch (regel) {
'piece' || 'stueck' => QuantityRule.piece,
'decimal' || 'dezimal' => QuantityRule.decimal,
_ => null,
},
askQuantity: fragen is bool ? fragen : null,
maxQuantity: grenze is num && grenze.isFinite && grenze > 0 ? grenze : null,
// Leere und fremde Eintraege fallen heraus wie im JS-Zwilling; eine
// leere Liste bleibt leer und wird nicht zu null.
stockLocationIds: standorte is List ? [for (final s in standorte) if (s is String && s.isNotEmpty) s] : null,
number: _textOderNull(json['number']),
ean: _textOderNull(json['ean']),
internalCode: _textOderNull(json['internalCode']),
stockTracked: json['stockTracked'] is bool ? json['stockTracked'] as bool : null,
);
}