mirror of
https://github.com/inventree/inventree-app.git
synced 2026-11-27 15:59:09 +00:00
Fetch TransferOrder status codes from the server (#913)
Remove hard-coded local values
This commit is contained in:
1 parent
4430bfa9c7
commit
9d00be5364
10 files changed
+172
-119
No files matched your search
@@ -2222,11 +2222,17 @@ class InvenTreeAPI {
|
||||
InvenTreeStatusCode get BuildOrderStatus =>
|
||||
_get_status_class("build/status/");
|
||||
|
||||
// Transfer orders have no dedicated status endpoint,
|
||||
// so use the generic endpoint (lookup by class name requires API v405+)
|
||||
InvenTreeStatusCode get TransferOrderStatus =>
|
||||
_get_status_class("generic/status/TransferOrderStatus/");
|
||||
|
||||
void clearStatusCodeData() {
|
||||
StockStatus.data.clear();
|
||||
PurchaseOrderStatus.data.clear();
|
||||
SalesOrderStatus.data.clear();
|
||||
BuildOrderStatus.data.clear();
|
||||
TransferOrderStatus.data.clear();
|
||||
}
|
||||
|
||||
Future<void> fetchStatusCodeData({bool forceReload = true}) async {
|
||||
@@ -2234,6 +2240,10 @@ class InvenTreeAPI {
|
||||
PurchaseOrderStatus.load(forceReload: forceReload);
|
||||
SalesOrderStatus.load(forceReload: forceReload);
|
||||
BuildOrderStatus.load(forceReload: forceReload);
|
||||
|
||||
if (supportsTransferOrders) {
|
||||
TransferOrderStatus.load(forceReload: forceReload);
|
||||
}
|
||||
}
|
||||
|
||||
int notification_counter = 0;
|
||||
|
||||
@@ -111,12 +111,15 @@ class InvenTreeBuildOrder extends InvenTreeOrder {
|
||||
// Status code handling
|
||||
// Note: These map to BuildStatus in backend/status_codes.py
|
||||
bool get isWaitingForBuild =>
|
||||
api.BuildOrderStatus.isNameIn(status, ["PENDING"]);
|
||||
api.BuildOrderStatus.isNameIn(logicalStatus, ["PENDING"]);
|
||||
bool get isInProgress =>
|
||||
api.BuildOrderStatus.isNameIn(status, ["PRODUCTION"]);
|
||||
bool get isComplete => api.BuildOrderStatus.isNameIn(status, ["COMPLETE"]);
|
||||
bool get isCancelled => api.BuildOrderStatus.isNameIn(status, ["CANCELLED"]);
|
||||
bool get isOnHold => api.BuildOrderStatus.isNameIn(status, ["ON_HOLD"]);
|
||||
api.BuildOrderStatus.isNameIn(logicalStatus, ["PRODUCTION"]);
|
||||
bool get isComplete =>
|
||||
api.BuildOrderStatus.isNameIn(logicalStatus, ["COMPLETE"]);
|
||||
bool get isCancelled =>
|
||||
api.BuildOrderStatus.isNameIn(logicalStatus, ["CANCELLED"]);
|
||||
bool get isOnHold =>
|
||||
api.BuildOrderStatus.isNameIn(logicalStatus, ["ON_HOLD"]);
|
||||
|
||||
// Can this build order be completed?
|
||||
bool get canCompleteOrder {
|
||||
|
||||
@@ -340,6 +340,9 @@ class InvenTreeModel {
|
||||
|
||||
// Return the effective status of this object
|
||||
// If a custom status is defined, return that, otherwise return the logical status
|
||||
// Note: Use this for *display* (label / color) only.
|
||||
// Business logic must check against logicalStatus, as custom states have
|
||||
// user-defined names which do not match the built-in status names.
|
||||
int get status {
|
||||
if (customStatus > 0) {
|
||||
return customStatus;
|
||||
|
||||
@@ -95,18 +95,19 @@ class InvenTreePurchaseOrder extends InvenTreeOrder {
|
||||
|
||||
int get destinationId => getInt("destination");
|
||||
|
||||
bool get isOpen => api.PurchaseOrderStatus.isNameIn(status, [
|
||||
bool get isOpen => api.PurchaseOrderStatus.isNameIn(logicalStatus, [
|
||||
"PENDING",
|
||||
"PLACED",
|
||||
"ON_HOLD",
|
||||
]);
|
||||
|
||||
bool get isPending =>
|
||||
api.PurchaseOrderStatus.isNameIn(status, ["PENDING", "ON_HOLD"]);
|
||||
api.PurchaseOrderStatus.isNameIn(logicalStatus, ["PENDING", "ON_HOLD"]);
|
||||
|
||||
bool get isPlaced => api.PurchaseOrderStatus.isNameIn(status, ["PLACED"]);
|
||||
bool get isPlaced =>
|
||||
api.PurchaseOrderStatus.isNameIn(logicalStatus, ["PLACED"]);
|
||||
|
||||
bool get isFailed => api.PurchaseOrderStatus.isNameIn(status, [
|
||||
bool get isFailed => api.PurchaseOrderStatus.isNameIn(logicalStatus, [
|
||||
"CANCELLED",
|
||||
"LOST",
|
||||
"RETURNED",
|
||||
|
||||
@@ -109,19 +109,20 @@ class InvenTreeSalesOrder extends InvenTreeOrder {
|
||||
|
||||
String get customerReference => getString("customer_reference");
|
||||
|
||||
bool get isOpen => api.SalesOrderStatus.isNameIn(status, [
|
||||
bool get isOpen => api.SalesOrderStatus.isNameIn(logicalStatus, [
|
||||
"PENDING",
|
||||
"IN_PROGRESS",
|
||||
"ON_HOLD",
|
||||
]);
|
||||
|
||||
bool get isPending =>
|
||||
api.SalesOrderStatus.isNameIn(status, ["PENDING", "ON_HOLD"]);
|
||||
api.SalesOrderStatus.isNameIn(logicalStatus, ["PENDING", "ON_HOLD"]);
|
||||
|
||||
bool get isInProgress =>
|
||||
api.SalesOrderStatus.isNameIn(status, ["IN_PROGRESS"]);
|
||||
api.SalesOrderStatus.isNameIn(logicalStatus, ["IN_PROGRESS"]);
|
||||
|
||||
bool get isComplete => api.SalesOrderStatus.isNameIn(status, ["SHIPPED"]);
|
||||
bool get isComplete =>
|
||||
api.SalesOrderStatus.isNameIn(logicalStatus, ["SHIPPED"]);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -6,13 +6,11 @@ import "package:flutter/material.dart";
|
||||
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/app_colors.dart";
|
||||
import "package:inventree/helpers.dart";
|
||||
import "package:inventree/inventree/model.dart";
|
||||
import "package:inventree/inventree/orders.dart";
|
||||
import "package:inventree/inventree/part.dart";
|
||||
import "package:inventree/inventree/stock.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
import "package:inventree/widget/order/transfer_order_detail.dart";
|
||||
|
||||
/*
|
||||
@@ -84,15 +82,20 @@ class InvenTreeTransferOrder extends InvenTreeOrder {
|
||||
// Should allocated stock be "consumed" rather than transferred?
|
||||
bool get consume => getBool("consume");
|
||||
|
||||
bool get isPending => TransferOrderStatus.isNameIn(status, ["PENDING"]);
|
||||
bool get isPending =>
|
||||
api.TransferOrderStatus.isNameIn(logicalStatus, ["PENDING"]);
|
||||
|
||||
bool get isIssued => TransferOrderStatus.isNameIn(status, ["ISSUED"]);
|
||||
bool get isIssued =>
|
||||
api.TransferOrderStatus.isNameIn(logicalStatus, ["ISSUED"]);
|
||||
|
||||
bool get isOnHold => TransferOrderStatus.isNameIn(status, ["ON_HOLD"]);
|
||||
bool get isOnHold =>
|
||||
api.TransferOrderStatus.isNameIn(logicalStatus, ["ON_HOLD"]);
|
||||
|
||||
bool get isComplete => TransferOrderStatus.isNameIn(status, ["COMPLETE"]);
|
||||
bool get isComplete =>
|
||||
api.TransferOrderStatus.isNameIn(logicalStatus, ["COMPLETE"]);
|
||||
|
||||
bool get isCancelled => TransferOrderStatus.isNameIn(status, ["CANCELLED"]);
|
||||
bool get isCancelled =>
|
||||
api.TransferOrderStatus.isNameIn(logicalStatus, ["CANCELLED"]);
|
||||
|
||||
// An "open" order is one which is not complete or cancelled
|
||||
bool get isOpen => !isComplete && !isCancelled;
|
||||
@@ -325,68 +328,3 @@ class InvenTreeTransferOrderAllocation extends InvenTreeModel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Transfer Order Status Codes
|
||||
* Ref: https://github.com/inventree/InvenTree/blob/master/src/backend/InvenTree/order/status_codes.py
|
||||
*/
|
||||
class TransferOrderStatus {
|
||||
// Status codes as defined in backend status_codes.py
|
||||
static const int PENDING = 10; // Transfer order is pending / inactive
|
||||
static const int ISSUED = 20; // Transfer order has been issued
|
||||
static const int ON_HOLD = 25; // Transfer order is on hold
|
||||
static const int COMPLETE = 30; // Transfer order is complete
|
||||
static const int CANCELLED = 40; // Transfer order was cancelled
|
||||
|
||||
static const Map<int, String> _names = {
|
||||
PENDING: "PENDING",
|
||||
ISSUED: "ISSUED",
|
||||
ON_HOLD: "ON_HOLD",
|
||||
COMPLETE: "COMPLETE",
|
||||
CANCELLED: "CANCELLED",
|
||||
};
|
||||
|
||||
// Return the (untranslated) name associated with a given status code
|
||||
static String name(int status) => _names[status] ?? "";
|
||||
|
||||
// Test if the name associated with the given code is in the provided list
|
||||
static bool isNameIn(int status, List<String> names) {
|
||||
return names.contains(name(status));
|
||||
}
|
||||
|
||||
// Return a color based on the transfer order status
|
||||
static Color getStatusColor(int status) {
|
||||
switch (status) {
|
||||
case PENDING:
|
||||
return COLOR_GRAY_LIGHT;
|
||||
case ISSUED:
|
||||
return COLOR_PROGRESS;
|
||||
case COMPLETE:
|
||||
return COLOR_SUCCESS;
|
||||
case CANCELLED:
|
||||
return COLOR_DANGER;
|
||||
case ON_HOLD:
|
||||
return COLOR_WARNING;
|
||||
default:
|
||||
return COLOR_GRAY_LIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
// Return a (translated) string based on the transfer order status
|
||||
static String getStatusText(int status) {
|
||||
switch (status) {
|
||||
case PENDING:
|
||||
return L10().pending;
|
||||
case ISSUED:
|
||||
return L10().issued;
|
||||
case COMPLETE:
|
||||
return L10().complete;
|
||||
case CANCELLED:
|
||||
return L10().cancelled;
|
||||
case ON_HOLD:
|
||||
return L10().onHold;
|
||||
default:
|
||||
return L10().unknown;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1178,15 +1178,6 @@
|
||||
"pending": "Pending",
|
||||
"@pending": {},
|
||||
|
||||
"issued": "Issued",
|
||||
"@issued": {},
|
||||
|
||||
"cancelled": "Cancelled",
|
||||
"@cancelled": {},
|
||||
|
||||
"onHold": "On Hold",
|
||||
"@onHold": {},
|
||||
|
||||
"permissionAccountDenied": "Your account does not have the required permissions to perform this action",
|
||||
"@permissionAccountDenied": {},
|
||||
|
||||
@@ -1966,9 +1957,6 @@
|
||||
"units": "Units",
|
||||
"@units": {},
|
||||
|
||||
"unknown": "Unknown",
|
||||
"@unknown": {},
|
||||
|
||||
"unknownResponse": "Unknown Response",
|
||||
"@unknownResponse": {},
|
||||
|
||||
|
||||
@@ -203,6 +203,7 @@ class _TransferOrderDetailState
|
||||
|
||||
@override
|
||||
Future<void> request(BuildContext context) async {
|
||||
await api.TransferOrderStatus.load();
|
||||
await widget.order.reload();
|
||||
|
||||
List<InvenTreeTransferOrderLineItem> lines = await widget.order
|
||||
@@ -270,8 +271,8 @@ class _TransferOrderDetailState
|
||||
title: Text(widget.order.reference),
|
||||
subtitle: Text(widget.order.description),
|
||||
trailing: LargeText(
|
||||
TransferOrderStatus.getStatusText(widget.order.status),
|
||||
color: TransferOrderStatus.getStatusColor(widget.order.status),
|
||||
api.TransferOrderStatus.label(widget.order.status),
|
||||
color: api.TransferOrderStatus.color(widget.order.status),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@ import "package:flutter/material.dart";
|
||||
import "package:flutter_speed_dial/flutter_speed_dial.dart";
|
||||
import "package:flutter_tabler_icons/flutter_tabler_icons.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
|
||||
import "package:inventree/inventree/model.dart";
|
||||
@@ -134,6 +135,7 @@ class _PaginatedTransferOrderListState
|
||||
int offset,
|
||||
Map<String, String> params,
|
||||
) async {
|
||||
await InvenTreeAPI().TransferOrderStatus.load();
|
||||
final page = await InvenTreeTransferOrder().listPaginated(
|
||||
limit,
|
||||
offset,
|
||||
@@ -151,8 +153,8 @@ class _PaginatedTransferOrderListState
|
||||
title: Text(order.reference),
|
||||
subtitle: Text(order.description),
|
||||
trailing: LargeText(
|
||||
TransferOrderStatus.getStatusText(order.status),
|
||||
color: TransferOrderStatus.getStatusColor(order.status),
|
||||
InvenTreeAPI().TransferOrderStatus.label(order.status),
|
||||
color: InvenTreeAPI().TransferOrderStatus.color(order.status),
|
||||
),
|
||||
onTap: () async {
|
||||
order.goToDetailPage(context);
|
||||
|
||||
+124
-18
@@ -10,8 +10,8 @@ import "package:inventree/inventree/build.dart";
|
||||
import "package:inventree/inventree/company.dart";
|
||||
import "package:inventree/inventree/purchase_order.dart";
|
||||
import "package:inventree/inventree/sales_order.dart";
|
||||
import "package:inventree/inventree/status_codes.dart";
|
||||
import "package:inventree/inventree/transfer_order.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
|
||||
// Construct status code data in the format provided by the server
|
||||
Map<String, dynamic> statusData(Map<String, int> codes) {
|
||||
@@ -21,6 +21,20 @@ Map<String, dynamic> statusData(Map<String, int> codes) {
|
||||
);
|
||||
}
|
||||
|
||||
// Add a user-defined custom state to the provided status codes,
|
||||
// in the format provided by the server.
|
||||
// Custom states have their own key and name, and map to a logical key
|
||||
void addCustomState(InvenTreeStatusCode codes, int key, int logicalKey) {
|
||||
codes.data["CUSTOM_${key}"] = {
|
||||
"key": key,
|
||||
"logical_key": logicalKey,
|
||||
"name": "CUSTOM_${key}",
|
||||
"label": "Custom ${key}",
|
||||
"color": "info",
|
||||
"custom": true,
|
||||
};
|
||||
}
|
||||
|
||||
void main() {
|
||||
setUp(() {
|
||||
InvenTreeAPI().BuildOrderStatus.data = statusData({
|
||||
@@ -49,6 +63,14 @@ void main() {
|
||||
"COMPLETE": 30,
|
||||
"CANCELLED": 40,
|
||||
});
|
||||
|
||||
InvenTreeAPI().TransferOrderStatus.data = statusData({
|
||||
"PENDING": 10,
|
||||
"ISSUED": 20,
|
||||
"ON_HOLD": 25,
|
||||
"COMPLETE": 30,
|
||||
"CANCELLED": 40,
|
||||
});
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
@@ -138,6 +160,27 @@ void main() {
|
||||
expect(cancelled.canIssue, isFalse);
|
||||
});
|
||||
|
||||
// Regression: a custom state must not hide the actions for its logical state
|
||||
test("Custom Status", () async {
|
||||
addCustomState(InvenTreeAPI().BuildOrderStatus, 101, 20);
|
||||
|
||||
final build = InvenTreeBuildOrder.fromJson({
|
||||
"status": 20,
|
||||
"status_custom_key": 101,
|
||||
"output_count": 1,
|
||||
});
|
||||
|
||||
expect(build.status, equals(101));
|
||||
expect(build.logicalStatus, equals(20));
|
||||
expect(build.isInProgress, isTrue);
|
||||
expect(build.canHold, isTrue);
|
||||
expect(build.canCancel, isTrue);
|
||||
expect(build.canCompleteOrder, isTrue);
|
||||
|
||||
// Display uses the custom state
|
||||
expect(InvenTreeAPI().BuildOrderStatus.label(build.status), "Custom 101");
|
||||
});
|
||||
|
||||
test("Build Line", () async {
|
||||
var line = InvenTreeBuildLine.fromJson({
|
||||
"quantity": 10,
|
||||
@@ -204,6 +247,29 @@ void main() {
|
||||
}
|
||||
});
|
||||
|
||||
// Regression: a custom state must not hide the actions for its logical state
|
||||
test("Custom Status", () async {
|
||||
addCustomState(InvenTreeAPI().PurchaseOrderStatus, 101, 10);
|
||||
addCustomState(InvenTreeAPI().PurchaseOrderStatus, 102, 20);
|
||||
|
||||
final pending = InvenTreePurchaseOrder.fromJson({
|
||||
"status": 10,
|
||||
"status_custom_key": 101,
|
||||
});
|
||||
|
||||
expect(pending.isOpen, isTrue);
|
||||
expect(pending.isPending, isTrue);
|
||||
|
||||
final placed = InvenTreePurchaseOrder.fromJson({
|
||||
"status": 20,
|
||||
"status_custom_key": 102,
|
||||
});
|
||||
|
||||
expect(placed.isOpen, isTrue);
|
||||
expect(placed.isPlaced, isTrue);
|
||||
expect(placed.isPending, isFalse);
|
||||
});
|
||||
|
||||
test("Supplier", () async {
|
||||
var order = InvenTreePurchaseOrder.fromJson({
|
||||
"supplier": 2,
|
||||
@@ -266,6 +332,20 @@ void main() {
|
||||
expect(order(40).isOpen, isFalse);
|
||||
});
|
||||
|
||||
// Regression: a custom state must not hide the actions for its logical state
|
||||
test("Custom Status", () async {
|
||||
addCustomState(InvenTreeAPI().SalesOrderStatus, 101, 15);
|
||||
|
||||
final order = InvenTreeSalesOrder.fromJson({
|
||||
"status": 15,
|
||||
"status_custom_key": 101,
|
||||
});
|
||||
|
||||
expect(order.isOpen, isTrue);
|
||||
expect(order.isInProgress, isTrue);
|
||||
expect(order.isComplete, isFalse);
|
||||
});
|
||||
|
||||
test("Customer", () async {
|
||||
var order = InvenTreeSalesOrder.fromJson({
|
||||
"customer": 4,
|
||||
@@ -373,50 +453,76 @@ void main() {
|
||||
|
||||
group("Transfer Order:", () {
|
||||
test("Status Codes", () async {
|
||||
expect(TransferOrderStatus.name(10), equals("PENDING"));
|
||||
expect(TransferOrderStatus.name(99), equals(""));
|
||||
expect(TransferOrderStatus.isNameIn(30, ["COMPLETE"]), isTrue);
|
||||
expect(TransferOrderStatus.isNameIn(99, ["COMPLETE"]), isFalse);
|
||||
final codes = InvenTreeAPI().TransferOrderStatus;
|
||||
|
||||
expect(TransferOrderStatus.getStatusText(10), equals(L10().pending));
|
||||
expect(TransferOrderStatus.getStatusText(20), equals(L10().issued));
|
||||
expect(TransferOrderStatus.getStatusText(25), equals(L10().onHold));
|
||||
expect(TransferOrderStatus.getStatusText(30), equals(L10().complete));
|
||||
expect(TransferOrderStatus.getStatusText(40), equals(L10().cancelled));
|
||||
expect(TransferOrderStatus.getStatusText(99), equals(L10().unknown));
|
||||
// Status codes are loaded from the generic server endpoint
|
||||
expect(codes.URL, equals("generic/status/TransferOrderStatus/"));
|
||||
|
||||
expect(codes.name(10), equals("PENDING"));
|
||||
expect(codes.name(99), equals(""));
|
||||
expect(codes.isNameIn(30, ["COMPLETE"]), isTrue);
|
||||
expect(codes.isNameIn(99, ["COMPLETE"]), isFalse);
|
||||
|
||||
// Labels are provided by the server
|
||||
expect(codes.label(20), equals("issued"));
|
||||
expect(codes.label(99), equals("99"));
|
||||
|
||||
// Status data is cleared along with other status codes
|
||||
InvenTreeAPI().clearStatusCodeData();
|
||||
expect(codes.data, isEmpty);
|
||||
});
|
||||
|
||||
test("Status", () async {
|
||||
InvenTreeTransferOrder order(int status) =>
|
||||
InvenTreeTransferOrder.fromJson({"status": status});
|
||||
|
||||
final pending = order(TransferOrderStatus.PENDING);
|
||||
final pending = order(10);
|
||||
expect(pending.isOpen, isTrue);
|
||||
expect(pending.canIssue, isTrue);
|
||||
expect(pending.canHold, isTrue);
|
||||
expect(pending.canCompleteOrder, isFalse);
|
||||
expect(pending.canCancel, isTrue);
|
||||
|
||||
final issued = order(TransferOrderStatus.ISSUED);
|
||||
final issued = order(20);
|
||||
expect(issued.canIssue, isFalse);
|
||||
expect(issued.canHold, isTrue);
|
||||
expect(issued.canCompleteOrder, isTrue);
|
||||
|
||||
final onHold = order(TransferOrderStatus.ON_HOLD);
|
||||
final onHold = order(25);
|
||||
expect(onHold.canIssue, isTrue);
|
||||
expect(onHold.canHold, isFalse);
|
||||
expect(onHold.canCompleteOrder, isTrue);
|
||||
|
||||
for (final status in [
|
||||
TransferOrderStatus.COMPLETE,
|
||||
TransferOrderStatus.CANCELLED,
|
||||
]) {
|
||||
for (final status in [30, 40]) {
|
||||
expect(order(status).isOpen, isFalse);
|
||||
expect(order(status).canCancel, isFalse);
|
||||
expect(order(status).canCompleteOrder, isFalse);
|
||||
}
|
||||
});
|
||||
|
||||
// Regression: a custom state must not hide the actions for its logical state
|
||||
test("Custom Status", () async {
|
||||
addCustomState(InvenTreeAPI().TransferOrderStatus, 101, 20);
|
||||
|
||||
final order = InvenTreeTransferOrder.fromJson({
|
||||
"status": 20,
|
||||
"status_custom_key": 101,
|
||||
});
|
||||
|
||||
expect(order.status, equals(101));
|
||||
expect(order.isIssued, isTrue);
|
||||
expect(order.isOpen, isTrue);
|
||||
expect(order.canHold, isTrue);
|
||||
expect(order.canCompleteOrder, isTrue);
|
||||
expect(order.canCancel, isTrue);
|
||||
|
||||
// Display uses the custom state
|
||||
expect(
|
||||
InvenTreeAPI().TransferOrderStatus.label(order.status),
|
||||
equals("Custom 101"),
|
||||
);
|
||||
});
|
||||
|
||||
test("Locations", () async {
|
||||
var order = InvenTreeTransferOrder.fromJson({
|
||||
"take_from": 2,
|
||||
|
||||
Reference in new issue
Block a user