From 5d0ffa059df19f346d119ae0809d0485f7259ebc Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 8 Jan 2022 23:05:45 +1100 Subject: [PATCH] Refactor stock adjustment functions - Provide compatibility with "modern" and "legacy" API versions --- lib/barcode.dart | 4 +-- lib/inventree/stock.dart | 60 +++++++++++++++++++++++++++--------- lib/widget/stock_detail.dart | 2 +- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/lib/barcode.dart b/lib/barcode.dart index a173c41f..c3cca44b 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -267,7 +267,7 @@ class StockItemScanIntoLocationHandler extends BarcodeHandler { } // Transfer stock to specified location - final result = await item.transferStock(location); + final result = await item.transferStock(context, location); if (result) { @@ -339,7 +339,7 @@ class StockLocationScanInItemsHandler extends BarcodeHandler { success: true ); } else { - final result = await item.transferStock(location.pk); + final result = await item.transferStock(context, location.pk); if (result) { diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index 8ce457f5..70375a9b 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -455,7 +455,8 @@ class InvenTreeStockItem extends InvenTreeModel { * - Remove * - Count */ - Future adjustStock(BuildContext context, String endpoint, double q, {String? notes}) async { + // TODO: Remove this function when we deprecate support for the old API + Future adjustStock(BuildContext context, String endpoint, double q, {String? notes, int? location}) async { // Serialized stock cannot be adjusted if (isSerialized()) { @@ -469,6 +470,35 @@ class InvenTreeStockItem extends InvenTreeModel { print("Adjust stock: ${endpoint}"); + Map data = {}; + + // Note: Format of adjustment API was updated in API v14 + if (InvenTreeAPI().supportModernStockTransactions()) { + // Modern (> 14) API + data = { + "items": [ + { + "pk": "${pk}", + "quantity": "${quantity}", + } + ], + "notes": notes ?? "" + }; + } else { + // Legacy (<= 14) API + data = { + "item": { + "pk": "${pk}", + "quantity": "${quantity}", + }, + "notes": notes ?? "", + }; + } + + if (location != null) { + data["location"] = location; + } + var response = await api.post( endpoint, body: { @@ -509,25 +539,25 @@ class InvenTreeStockItem extends InvenTreeModel { } // TODO: Remove this function when we deprecate support for the old API - Future transferStock(int location, {double? quantity, String? notes}) async { - if ((quantity == null) || (quantity < 0) || (quantity > this.quantity)) { - quantity = this.quantity; + Future transferStock(BuildContext context, int location, {double? quantity, String? notes}) async { + + print("transferStock()"); + + double q = this.quantity; + + if (quantity != null) { + q = quantity; } - final response = await api.post( + final bool result = await adjustStock( + context, "/stock/transfer/", - body: { - "item": { - "pk": "${pk}", - "quantity": "${quantity}", - }, - "location": "${location}", - "notes": notes ?? "", - }, - expectedStatusCode: 200 + q, + notes: notes, + location: location, ); - return response.isValid() && response.statusCode == 200; + return result; } } diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index 8ff4a11d..2d39f308 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -383,7 +383,7 @@ class _StockItemDisplayState extends RefreshableState { _quantityController.clear(); _notesController.clear(); - var result = await item.transferStock(locationId, quantity: quantity, notes: notes); + var result = await item.transferStock(context, locationId, quantity: quantity, notes: notes); refresh();