2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-29 05:56:47 +00:00

Action to count stock items now works

This commit is contained in:
Oliver Walters 2020-04-09 23:41:16 +10:00
parent 80f7694abb
commit f5b8311428
2 changed files with 96 additions and 24 deletions

View File

@ -83,20 +83,22 @@ class InvenTreeModel {
/* /*
* Reload this object, by requesting data from the server * Reload this object, by requesting data from the server
*/ */
void reload() async { Future<bool> reload() async {
print("Reloading data from $url"); print("Reloading data from $url");
var response = await api.get(url); var response = await api.get(url, params: defaultGetFilters());
if (response.statusCode != 200) { if (response.statusCode != 200) {
print("Error retrieving data"); print("Error retrieving data");
return; return false;
} }
final Map<String, dynamic> data = json.decode(response.body); final Map<String, dynamic> data = json.decode(response.body);
jsondata = data; jsondata = data;
return true;
} }
// Return the detail view for the associated pk // Return the detail view for the associated pk

View File

@ -28,6 +28,9 @@ class StockDetailWidget extends StatefulWidget {
class _StockItemDisplayState extends State<StockDetailWidget> { class _StockItemDisplayState extends State<StockDetailWidget> {
final _addStockKey = GlobalKey<FormState>(); final _addStockKey = GlobalKey<FormState>();
final _takeStockKey = GlobalKey<FormState>();
final _countStockKey = GlobalKey<FormState>();
final _moveStockKey = GlobalKey<FormState>();
_StockItemDisplayState(this.item) { _StockItemDisplayState(this.item) {
// TODO // TODO
@ -39,7 +42,22 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
// TODO - Form for editing stock item // TODO - Form for editing stock item
} }
void _addStock() { void _addStock(double quantity) async {
Navigator.of(context).pop();
// Await response to prevent the button from being pressed multiple times
var response = await item.addStock(quantity);
// TODO - Handle error cases
await item.reload();
setState(() {});
}
void _addStockDialog() async {
showDialog(context: context, showDialog(context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return AlertDialog( return AlertDialog(
@ -55,31 +73,26 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
content: Form( content: Form(
key: _addStockKey, key: _addStockKey,
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
Text("Current Quantity: ${item.quantity}"),
TextFormField( TextFormField(
decoration: InputDecoration(labelText: "Stock Quantity"), decoration: InputDecoration(
labelText: "Add stock",
),
keyboardType: TextInputType.numberWithOptions(signed:false, decimal:true), keyboardType: TextInputType.numberWithOptions(signed:false, decimal:true),
validator: (value) { validator: (value) {
if (value.isEmpty) { if (value.isEmpty) return "Value cannot be empty";
return "Value cannot be empty";
}
double quantity = double.tryParse(value); double quantity = double.tryParse(value);
if (quantity == null) return "Value cannot be converted to a number";
if (quantity <= 0) return "Value must be positive";
if (quantity == null) { _addStock(quantity);
return "Value cannot be converted to a number";
}
if (quantity <= 0) { return null;
return "Value must be positive";
}
print("Adding stock!");
item.addStock(quantity).then((var response) {
print("added stock");
});
}, },
), ),
], ],
@ -95,8 +108,65 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
// TODO - Form for removing stock // TODO - Form for removing stock
} }
void _countStock() { void _countStock(double quantity) async {
// TODO - Form for counting stock
Navigator.of(context).pop();
var response = await item.countStock(quantity);
// TODO - Handle error cases
await item.reload();
setState(() {});
}
void _countStockDialog() async {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Count Stock"),
actions: <Widget>[
FlatButton(
child: Text("Count"),
onPressed: () {
_countStockKey.currentState.validate();
},
)
],
content: Form(
key: _countStockKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: "Count stock",
hintText: "${item.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 cannot be negative";
_countStock(quantity);
return null;
},
)
],
)
)
);
}
);
} }
void _transferStock() { void _transferStock() {
@ -249,7 +319,7 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
buttons.add(SpeedDialChild( buttons.add(SpeedDialChild(
child: Icon(FontAwesomeIcons.plusCircle), child: Icon(FontAwesomeIcons.plusCircle),
label: "Add Stock", label: "Add Stock",
onTap: _addStock, onTap: _addStockDialog,
) )
); );
@ -263,7 +333,7 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
buttons.add(SpeedDialChild( buttons.add(SpeedDialChild(
child: Icon(FontAwesomeIcons.checkCircle), child: Icon(FontAwesomeIcons.checkCircle),
label: "Count Stock", label: "Count Stock",
onTap: _countStock, onTap: _countStockDialog,
)); ));
} }