From d2a01a0286960adf2d09afbb3dbdb38e660ef781 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Thu, 27 Jul 2023 10:16:36 +1000
Subject: [PATCH] Supplier part fix (#408)

* Change supplier part fields based on API version

* Display packaging info on supplier part page

* Icon consolidation

* Bump version number
---
 assets/release_notes.md              |  5 +++++
 lib/api_form.dart                    |  2 +-
 lib/inventree/company.dart           | 22 ++++++++++++++++++++--
 lib/widget/stock_detail.dart         |  2 +-
 lib/widget/supplier_part_detail.dart | 12 ++++++++++++
 pubspec.yaml                         |  2 +-
 6 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/assets/release_notes.md b/assets/release_notes.md
index 5d6bb714..99fc78f2 100644
--- a/assets/release_notes.md
+++ b/assets/release_notes.md
@@ -1,3 +1,8 @@
+### 0.12.7 - July 2023
+---
+
+- Bug fix for Supplier Part editing page
+
 ### 0.12.6 - July 2023
 --- 
 
diff --git a/lib/api_form.dart b/lib/api_form.dart
index fb058a89..237b1d57 100644
--- a/lib/api_form.dart
+++ b/lib/api_form.dart
@@ -299,7 +299,7 @@ class APIFormField {
       default:
         return ListTile(
           title: Text(
-            "Unsupported field type: '${type}'",
+            "Unsupported field type: '${type}' for field '${name}'",
             style: TextStyle(
                 color: COLOR_DANGER,
                 fontStyle: FontStyle.italic),
diff --git a/lib/inventree/company.dart b/lib/inventree/company.dart
index 78f83d39..032ee41e 100644
--- a/lib/inventree/company.dart
+++ b/lib/inventree/company.dart
@@ -122,14 +122,22 @@ class InvenTreeSupplierPart extends InvenTreeModel {
 
   @override
   Map<String, dynamic> formFields() {
-    return {
+    Map<String, dynamic> fields = {
       "supplier": {},
       "SKU": {},
       "link": {},
       "note": {},
       "packaging": {},
-      "pack_size": {},
     };
+
+    // At some point, pack_size was changed to pack_quantity
+    if (InvenTreeAPI().apiVersion < 117) {
+      fields["pack_size"] = {};
+    } else {
+      fields["pack_quantity"] = {};
+    }
+
+    return fields;
   }
 
   Map<String, String> _filters() {
@@ -178,6 +186,16 @@ class InvenTreeSupplierPart extends InvenTreeModel {
   
   String get note => getString("note");
 
+  String get packaging => getString("packaging");
+
+  String get pack_quantity {
+    if (InvenTreeAPI().apiVersion < 117) {
+      return getString("pack_size");
+    } else {
+      return getString("pack_quantity");
+    }
+  }
+
   @override
   InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreeSupplierPart.fromJson(json);
 }
diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart
index 2b6f983f..9e2f517b 100644
--- a/lib/widget/stock_detail.dart
+++ b/lib/widget/stock_detail.dart
@@ -636,7 +636,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
         ListTile(
           title: Text(L10().packaging),
           subtitle: Text(widget.item.packaging),
-          leading: FaIcon(FontAwesomeIcons.box),
+          leading: FaIcon(FontAwesomeIcons.boxesPacking),
         )
       );
     }
diff --git a/lib/widget/supplier_part_detail.dart b/lib/widget/supplier_part_detail.dart
index 9821bfb1..fe39c803 100644
--- a/lib/widget/supplier_part_detail.dart
+++ b/lib/widget/supplier_part_detail.dart
@@ -192,6 +192,18 @@ class _SupplierPartDisplayState extends RefreshableState<SupplierPartDetailWidge
       );
     }
 
+    // Packaging
+    if (widget.supplierPart.packaging.isNotEmpty || widget.supplierPart.pack_quantity.isNotEmpty) {
+      tiles.add(
+        ListTile(
+          title: Text(L10().packaging),
+          subtitle: widget.supplierPart.packaging.isNotEmpty ? Text(widget.supplierPart.packaging) : null,
+          leading: FaIcon(FontAwesomeIcons.boxesPacking),
+          trailing: widget.supplierPart.pack_quantity.isNotEmpty ? Text(widget.supplierPart.pack_quantity) : null,
+        )
+      );
+    }
+
     if (widget.supplierPart.link.isNotEmpty) {
       tiles.add(
         ListTile(
diff --git a/pubspec.yaml b/pubspec.yaml
index 1fb2f36a..20b00ec8 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
 name: inventree
 description: InvenTree stock management
 
-version: 0.12.6+73
+version: 0.12.7+74
 
 environment:
   sdk: ">=2.19.5 <3.0.0"