mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-11-03 23:05:44 +00:00 
			
		
		
		
	Merge pull request #82 from SchrodingersGat/stock-transfer-fix
Stock transfer fix
This commit is contained in:
		@@ -267,7 +267,7 @@ class StockItemScanIntoLocationHandler extends BarcodeHandler {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Transfer stock to specified location
 | 
					      // Transfer stock to specified location
 | 
				
			||||||
      final result = await item.transferStock(location);
 | 
					      final result = await item.transferStock(context, location);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (result) {
 | 
					      if (result) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -339,7 +339,7 @@ class StockLocationScanInItemsHandler extends BarcodeHandler {
 | 
				
			|||||||
            success: true
 | 
					            success: true
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        final result = await item.transferStock(location.pk);
 | 
					        final result = await item.transferStock(context, location.pk);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (result) {
 | 
					        if (result) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -455,10 +455,11 @@ class InvenTreeStockItem extends InvenTreeModel {
 | 
				
			|||||||
   * - Remove
 | 
					   * - Remove
 | 
				
			||||||
   * - Count
 | 
					   * - Count
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  Future<bool> adjustStock(BuildContext context, String endpoint, double q, {String? notes}) async {
 | 
					  // TODO: Remove this function when we deprecate support for the old API
 | 
				
			||||||
 | 
					  Future<bool> adjustStock(BuildContext context, String endpoint, double q, {String? notes, int? location}) async {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Serialized stock cannot be adjusted
 | 
					    // Serialized stock cannot be adjusted (unless it is a "transfer")
 | 
				
			||||||
    if (isSerialized()) {
 | 
					    if (isSerialized() && location == null) {
 | 
				
			||||||
      return false;
 | 
					      return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -467,18 +468,42 @@ class InvenTreeStockItem extends InvenTreeModel {
 | 
				
			|||||||
      return false;
 | 
					      return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print("Adjust stock: ${endpoint}");
 | 
					    Map<String, dynamic> data = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Note: Format of adjustment API was updated in API v14
 | 
				
			||||||
 | 
					    if (InvenTreeAPI().supportModernStockTransactions()) {
 | 
				
			||||||
 | 
					      // Modern (> 14) API
 | 
				
			||||||
 | 
					      data = {
 | 
				
			||||||
 | 
					        "items": [
 | 
				
			||||||
 | 
					          {
 | 
				
			||||||
 | 
					            "pk": "${pk}",
 | 
				
			||||||
 | 
					            "quantity": "${quantity}",
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      // Legacy (<= 14) API
 | 
				
			||||||
 | 
					      data = {
 | 
				
			||||||
 | 
					        "item": {
 | 
				
			||||||
 | 
					          "pk": "${pk}",
 | 
				
			||||||
 | 
					          "quantity": "${quantity}",
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    data["notes"] = notes ?? "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (location != null) {
 | 
				
			||||||
 | 
					      data["location"] = location;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Expected API return code depends on server API version
 | 
				
			||||||
 | 
					    final int expected_response = InvenTreeAPI().supportModernStockTransactions() ? 201 : 200;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var response = await api.post(
 | 
					    var response = await api.post(
 | 
				
			||||||
      endpoint,
 | 
					      endpoint,
 | 
				
			||||||
      body: {
 | 
					      body: data,
 | 
				
			||||||
        "item": {
 | 
					      expectedStatusCode: expected_response,
 | 
				
			||||||
        "pk": "${pk}",
 | 
					 | 
				
			||||||
        "quantity": "${q}",
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        "notes": notes ?? "",
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      expectedStatusCode: 200
 | 
					 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return response.isValid();
 | 
					    return response.isValid();
 | 
				
			||||||
@@ -509,25 +534,23 @@ class InvenTreeStockItem extends InvenTreeModel {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // TODO: Remove this function when we deprecate support for the old API
 | 
					  // TODO: Remove this function when we deprecate support for the old API
 | 
				
			||||||
  Future<bool> transferStock(int location, {double? quantity, String? notes}) async {
 | 
					  Future<bool> transferStock(BuildContext context, int location, {double? quantity, String? notes}) async {
 | 
				
			||||||
    if ((quantity == null) || (quantity < 0) || (quantity > this.quantity)) {
 | 
					
 | 
				
			||||||
      quantity = this.quantity;
 | 
					    double q = this.quantity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (quantity != null) {
 | 
				
			||||||
 | 
					      q = quantity;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    final response = await api.post(
 | 
					    final bool result = await adjustStock(
 | 
				
			||||||
 | 
					      context,
 | 
				
			||||||
      "/stock/transfer/",
 | 
					      "/stock/transfer/",
 | 
				
			||||||
      body: {
 | 
					      q,
 | 
				
			||||||
        "item": {
 | 
					      notes: notes,
 | 
				
			||||||
          "pk": "${pk}",
 | 
					      location: location,
 | 
				
			||||||
          "quantity": "${quantity}",
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
        "location": "${location}",
 | 
					 | 
				
			||||||
        "notes": notes ?? "",
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      expectedStatusCode: 200
 | 
					 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return response.isValid() && response.statusCode == 200;
 | 
					    return result;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -383,7 +383,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
 | 
				
			|||||||
    _quantityController.clear();
 | 
					    _quantityController.clear();
 | 
				
			||||||
    _notesController.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();
 | 
					    refresh();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user