diff --git a/lib/api.dart b/lib/api.dart index 885fd145..c2872b6b 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -59,7 +59,7 @@ class InvenTreeAPI { String makeApiUrl(String endpoint) { - return apiUrl + endpoint; + return _makeUrl("/api/" + endpoint); } String makeUrl(String endpoint) { @@ -272,20 +272,18 @@ class InvenTreeAPI { } // Perform a POST request - Future post(String url, {Map body}) async { + Future post(String url, {Map body}) async { var _url = makeApiUrl(url); - var _headers = defaultHeaders(); - var _body = Map(); - - // Copy across provided data - body.forEach((K, V) => _body[K] = V); + var _headers = jsonHeaders(); print("POST: " + _url); + var data = jsonEncode(body); + return http.post(_url, headers: _headers, - body: _body, + body: data, ); } @@ -324,6 +322,13 @@ class InvenTreeAPI { return headers; } + Map jsonHeaders() { + + var headers = defaultHeaders(); + headers['Content-Type'] = 'application/json'; + return headers; + } + String _authorizationHeader () { if (_token.isNotEmpty) { return "Token $_token"; diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 7b62ac0e..d929df88 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -102,7 +102,7 @@ class InvenTreeModel { print("GET: $addr ${params.toString()}"); - var response = await InvenTreeAPI().get(addr, params: params); + var response = await api.get(addr, params: params); if (response.statusCode != 200) { print("Error retrieving data"); @@ -134,7 +134,7 @@ class InvenTreeModel { // TODO - Add "timeout" // TODO - Add error catching - var response = await InvenTreeAPI().get(URL, params:params); + var response = await api.get(URL, params:params); // A list of "InvenTreeModel" items List results = new List(); diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index 5336ff33..9679ab6d 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:http/http.dart' as http; import 'model.dart'; import 'package:InvenTree/api.dart'; @@ -155,6 +156,29 @@ class InvenTreeStockItem extends InvenTreeModel { return item; } + + Future addStock(double quan) async { + + // Cannot add stock to a serialized StockItem + if (isSerialized()) { + return null; + } + + // Cannot add negative stock + if (quan <= 0) { + return null; + } + + Map data = { + "item": { + "pk": "${pk}", + "quantity": "${quan}", + } + }; + + return api.post("/stock/add/", body: data); + } + } @@ -196,5 +220,4 @@ class InvenTreeStockLocation extends InvenTreeModel { return loc; } - } \ No newline at end of file diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index 3f3787ba..cc5f782a 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -10,6 +10,7 @@ import 'package:flutter/material.dart'; import 'package:InvenTree/api.dart'; import 'package:InvenTree/widget/drawer.dart'; +import 'package:flutter/services.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:flutter_speed_dial/flutter_speed_dial.dart'; @@ -26,6 +27,8 @@ class StockDetailWidget extends StatefulWidget { class _StockItemDisplayState extends State { + final _addStockKey = GlobalKey(); + _StockItemDisplayState(this.item) { // TODO } @@ -37,6 +40,54 @@ class _StockItemDisplayState extends State { } void _addStock() { + showDialog(context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text("Add Stock"), + actions: [ + FlatButton( + child: Text("Add"), + onPressed: () { + _addStockKey.currentState.validate(); + }, + ) + ], + content: Form( + key: _addStockKey, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + TextFormField( + decoration: InputDecoration(labelText: "Stock Quantity"), + keyboardType: TextInputType.numberWithOptions(signed:false, decimal:true), + validator: (value) { + if (value.isEmpty) { + return "Value cannot be empty"; + } + + double quantity = double.tryParse(value); + + if (quantity == null) { + return "Value cannot be converted to a number"; + } + + if (quantity <= 0) { + return "Value must be positive"; + } + + print("Adding stock!"); + + item.addStock(quantity).then((var response) { + print("added stock"); + }); + }, + ), + ], + ) + ), + ); + } + ); // TODO - Form for adding stock }