mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 05:26:47 +00:00
Perform "addstock" action
This commit is contained in:
parent
1ec0b13479
commit
4cafa668a5
21
lib/api.dart
21
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<http.Response> post(String url, {Map<String, String> body}) async {
|
||||
Future<http.Response> post(String url, {Map<String, dynamic> body}) async {
|
||||
|
||||
var _url = makeApiUrl(url);
|
||||
var _headers = defaultHeaders();
|
||||
var _body = Map<String, String>();
|
||||
|
||||
// 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<String, String> jsonHeaders() {
|
||||
|
||||
var headers = defaultHeaders();
|
||||
headers['Content-Type'] = 'application/json';
|
||||
return headers;
|
||||
}
|
||||
|
||||
String _authorizationHeader () {
|
||||
if (_token.isNotEmpty) {
|
||||
return "Token $_token";
|
||||
|
@ -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<InvenTreeModel> results = new List<InvenTreeModel>();
|
||||
|
@ -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<http.Response> 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<String, dynamic> data = {
|
||||
"item": {
|
||||
"pk": "${pk}",
|
||||
"quantity": "${quan}",
|
||||
}
|
||||
};
|
||||
|
||||
return api.post("/stock/add/", body: data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -196,5 +220,4 @@ class InvenTreeStockLocation extends InvenTreeModel {
|
||||
|
||||
return loc;
|
||||
}
|
||||
|
||||
}
|
@ -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<StockDetailWidget> {
|
||||
|
||||
final _addStockKey = GlobalKey<FormState>();
|
||||
|
||||
_StockItemDisplayState(this.item) {
|
||||
// TODO
|
||||
}
|
||||
@ -37,6 +40,54 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
|
||||
}
|
||||
|
||||
void _addStock() {
|
||||
showDialog(context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text("Add Stock"),
|
||||
actions: <Widget>[
|
||||
FlatButton(
|
||||
child: Text("Add"),
|
||||
onPressed: () {
|
||||
_addStockKey.currentState.validate();
|
||||
},
|
||||
)
|
||||
],
|
||||
content: Form(
|
||||
key: _addStockKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user