mirror of
https://github.com/inventree/inventree-app.git
synced 2025-06-16 12:15:31 +00:00
Update status codes (#307)
* Extra error info when connecting to server * Adds accessors for various types of status codes * Cleanup / refactor stock status codes - No longer need to duplicate these on the app * improvements to purchase order list - Add option to show closed orders - Render order status * Add purchase order status to order detail widget * Update release notes * Cleanup for imports * linting fixes
This commit is contained in:
112
lib/inventree/status_codes.dart
Normal file
112
lib/inventree/status_codes.dart
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Code for querying the server for various status code data,
|
||||
* so that we do not have to duplicate those codes in the app.
|
||||
*
|
||||
* Ref: https://github.com/inventree/InvenTree/blob/master/InvenTree/InvenTree/status_codes.py
|
||||
*/
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/helpers.dart";
|
||||
|
||||
|
||||
/*
|
||||
* Base class definition for a "status code" definition.
|
||||
*/
|
||||
class InvenTreeStatusCode {
|
||||
|
||||
InvenTreeStatusCode(this.URL);
|
||||
|
||||
final String URL;
|
||||
|
||||
// Internal status code data loaded from server
|
||||
Map<String, dynamic> data = {};
|
||||
|
||||
// Load status code information from the server
|
||||
Future<void> load({bool forceReload = false}) async {
|
||||
|
||||
// Return internally cached data
|
||||
if (data.isNotEmpty && !forceReload) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The server must support this feature!
|
||||
if (!InvenTreeAPI().supportsStatusLabelEndpoints) {
|
||||
return;
|
||||
}
|
||||
|
||||
debug("Loading status codes from ${URL}");
|
||||
|
||||
APIResponse response = await InvenTreeAPI().get(URL);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
Map<String, dynamic> results = response.data as Map<String, dynamic>;
|
||||
|
||||
if (results.containsKey("values")) {
|
||||
data = results["values"] as Map<String, dynamic>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the entry associated with the provided integer status
|
||||
Map<String, dynamic> entry(int status) {
|
||||
for (String key in data.keys) {
|
||||
dynamic _entry = data[key];
|
||||
|
||||
if (_entry is Map<String, dynamic>) {
|
||||
dynamic _status = _entry["key"];
|
||||
|
||||
if (_status is int) {
|
||||
if (status == _status) {
|
||||
return _entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No match - return an empty map
|
||||
return {};
|
||||
}
|
||||
|
||||
// Return the 'label' associated with a given status code
|
||||
String label(int status) {
|
||||
Map<String, dynamic> _entry = entry(status);
|
||||
|
||||
String _label = (_entry["label"] ?? "") as String;
|
||||
|
||||
if (_label.isEmpty) {
|
||||
// If no match found, return the status code
|
||||
debug("No match for status code ${status} at '${URL}'");
|
||||
return status.toString();
|
||||
} else {
|
||||
return _label;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the 'color' associated with a given status code
|
||||
Color color(int status) {
|
||||
Map<String, dynamic> _entry = entry(status);
|
||||
|
||||
String color_name = (_entry["color"] ?? "") as String;
|
||||
|
||||
switch (color_name.toLowerCase()) {
|
||||
case "success":
|
||||
return Colors.green;
|
||||
case "primary":
|
||||
return Colors.blue;
|
||||
case "secondary":
|
||||
return Colors.grey;
|
||||
case "dark":
|
||||
return Colors.black;
|
||||
case "danger":
|
||||
return Colors.red;
|
||||
case "warning":
|
||||
return Colors.orange;
|
||||
case "info":
|
||||
return Colors.lightBlue;
|
||||
default:
|
||||
return Colors.black;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
import "dart:async";
|
||||
|
||||
import "package:flutter/material.dart";
|
||||
import "package:intl/intl.dart";
|
||||
import "package:inventree/helpers.dart";
|
||||
import "package:inventree/inventree/part.dart";
|
||||
@ -122,66 +121,10 @@ class InvenTreeStockItem extends InvenTreeModel {
|
||||
|
||||
InvenTreeStockItem.fromJson(Map<String, dynamic> json) : super.fromJson(json);
|
||||
|
||||
// Stock status codes
|
||||
static const int OK = 10;
|
||||
static const int ATTENTION = 50;
|
||||
static const int DAMAGED = 55;
|
||||
static const int DESTROYED = 60;
|
||||
static const int REJECTED = 65;
|
||||
static const int LOST = 70;
|
||||
static const int QUARANTINED = 75;
|
||||
static const int RETURNED = 85;
|
||||
|
||||
String statusLabel() {
|
||||
|
||||
// TODO: Delete me - The translated status values should be provided by the API!
|
||||
|
||||
switch (status) {
|
||||
case OK:
|
||||
return L10().ok;
|
||||
case ATTENTION:
|
||||
return L10().attention;
|
||||
case DAMAGED:
|
||||
return L10().damaged;
|
||||
case DESTROYED:
|
||||
return L10().destroyed;
|
||||
case REJECTED:
|
||||
return L10().rejected;
|
||||
case LOST:
|
||||
return L10().lost;
|
||||
case QUARANTINED:
|
||||
return L10().quarantined;
|
||||
case RETURNED:
|
||||
return L10().returned;
|
||||
default:
|
||||
return status.toString();
|
||||
}
|
||||
}
|
||||
|
||||
// Return color associated with stock status
|
||||
Color get statusColor {
|
||||
switch (status) {
|
||||
case OK:
|
||||
return Colors.black;
|
||||
case ATTENTION:
|
||||
return Color(0xFFfdc82a);
|
||||
case DAMAGED:
|
||||
case DESTROYED:
|
||||
case REJECTED:
|
||||
return Color(0xFFe35a57);
|
||||
case QUARANTINED:
|
||||
return Color(0xFF0DCAF0);
|
||||
case LOST:
|
||||
default:
|
||||
return Color(0xFFAAAAAA);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String get URL => "stock/";
|
||||
|
||||
// URLs for performing stock actions
|
||||
|
||||
static String transferStockUrl() => "stock/transfer/";
|
||||
|
||||
static String countStockUrl() => "stock/count/";
|
||||
|
Reference in New Issue
Block a user