From f84ba65f6c3413c5e18e27faa485bd529e18c42d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 5 Apr 2020 15:00:27 +1000 Subject: [PATCH] Some more value getters for Part model --- lib/inventree/model.dart | 2 ++ lib/inventree/part.dart | 55 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 8bf3bee0..5f8c651d 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -43,6 +43,8 @@ class InvenTreeModel { int get parentId => jsondata['parent'] ?? -1; + String get link => jsondata['URL'] ?? ''; + // Create a new object from JSON data (not a constructor!) InvenTreeModel createFromJson(Map json) { diff --git a/lib/inventree/part.dart b/lib/inventree/part.dart index 5c9403c2..d881d9ff 100644 --- a/lib/inventree/part.dart +++ b/lib/inventree/part.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:InvenTree/api.dart'; import 'model.dart'; @@ -34,14 +36,63 @@ class InvenTreePart extends InvenTreeModel { @override String URL = "part/"; - int get categoryId => jsondata['category'] as int ?? -1; + // Get the number of stock on order for this Part + double get onOrder => double.tryParse(jsondata['on_order'].toString() ?? '0'); - String get categoryName => jsondata['category__name'] ?? ''; + // Get the stock count for this Part + double get inStock => double.tryParse(jsondata['total_stock'].toString() ?? '0'); + // Get the number of units being build for this Part + double get building => double.tryParse(jsondata['building'].toString() ?? '0'); + + bool get isAssembly => jsondata['assembly'] ?? false; + + bool get isComponent => jsondata['component'] ?? false; + + bool get isPurchaseable => jsondata['purchaseable'] ?? false; + + bool get isSaleable => jsondata['saleable'] ?? false; + + bool get isActive => jsondata['active'] ?? false; + + bool get isVirtual => jsondata['virtual'] ?? false; + + // Get the IPN (internal part number) for the Part instance + String get IPN => jsondata['IPN'] as String ?? ''; + + // Get the revision string for the Part instance + String get revision => jsondata['revision'] as String ?? ''; + + // Get the category ID for the Part instance (or 'null' if does not exist) + int get categoryId => jsondata['category'] as int ?? null; + + // Get the category name for the Part instance + String get categoryName => jsondata['category_name'] ?? ''; + + // Get the image URL for the Part instance String get _image => jsondata['image'] ?? ''; + // Get the thumbnail URL for the Part instance String get _thumbnail => jsondata['thumbnail'] ?? ''; + // Return the fully-qualified name for the Part instance + String get fullname { + + String fn = jsondata['full_name'] ?? ''; + + if (fn.isNotEmpty) return fn; + + List elements = List(); + + if (IPN.isNotEmpty) elements.add(IPN); + + elements.add(name); + + if (revision.isNotEmpty) elements.add(revision); + + return elements.join(" | "); + } + // Return a path to the image for this Part String get image { // Use thumbnail as a backup