2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-05-01 06:56:50 +00:00

Add function to remove stock

This commit is contained in:
Oliver Walters 2020-04-09 23:49:39 +10:00
parent f5b8311428
commit b10931f3b6
2 changed files with 82 additions and 16 deletions

View File

@ -179,15 +179,7 @@ class InvenTreeStockItem extends InvenTreeModel {
Future<http.Response> addStock(double quan) async { Future<http.Response> addStock(double quan) async {
// Cannot add stock to a serialized StockItem if (isSerialized() || quan <= 0) return null;
if (isSerialized()) {
return null;
}
// Cannot add negative stock
if (quan <= 0) {
return null;
}
return api.post("/stock/add/", body: { return api.post("/stock/add/", body: {
"item": { "item": {
@ -197,6 +189,18 @@ class InvenTreeStockItem extends InvenTreeModel {
}); });
} }
Future<http.Response> removeStock(double quan) async {
if (isSerialized() || quan <= 0) return null;
return api.post("/stock/remove/", body: {
"item": {
"pk": "${pk}",
"quantity": "${quan}",
}
});
}
} }

View File

@ -28,7 +28,7 @@ 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 _removeStockKey = GlobalKey<FormState>();
final _countStockKey = GlobalKey<FormState>(); final _countStockKey = GlobalKey<FormState>();
final _moveStockKey = GlobalKey<FormState>(); final _moveStockKey = GlobalKey<FormState>();
@ -82,7 +82,7 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
decoration: InputDecoration( decoration: InputDecoration(
labelText: "Add stock", labelText: "Add stock",
), ),
keyboardType: TextInputType.numberWithOptions(signed:false, decimal:true), keyboardType: TextInputType.numberWithOptions(signed: false, decimal: true),
validator: (value) { validator: (value) {
if (value.isEmpty) return "Value cannot be empty"; if (value.isEmpty) return "Value cannot be empty";
@ -104,8 +104,65 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
// TODO - Form for adding stock // TODO - Form for adding stock
} }
void _removeStock() { void _removeStock(double quantity) async {
// TODO - Form for removing stock Navigator.of(context).pop();
var response = await item.removeStock(quantity);
// TODO - Handle error cases
await item.reload();
setState(() {});
}
void _removeStockDialog() {
showDialog(context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Remove Stock"),
actions: <Widget>[
FlatButton(
child: Text("Remove"),
onPressed: () {
_removeStockKey.currentState.validate();
},
)
],
content: Form(
key: _removeStockKey,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Current quantity: ${item.quantity}"),
TextFormField(
decoration: InputDecoration(
labelText: "Remove stock",
),
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";
if (quantity > item.quantity) return "Cannot take more than current quantity";
_removeStock(quantity);
return null;
},
)
],
)
),
);
}
);
} }
void _countStock(double quantity) async { void _countStock(double quantity) async {
@ -169,7 +226,12 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
); );
} }
void _transferStock() {
void _transferStock(int location) {
// TODO
}
void _transferStockDialog() {
// TODO - Form for transferring stock // TODO - Form for transferring stock
} }
@ -326,7 +388,7 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
buttons.add(SpeedDialChild( buttons.add(SpeedDialChild(
child: Icon(FontAwesomeIcons.minusCircle), child: Icon(FontAwesomeIcons.minusCircle),
label: "Remove Stock", label: "Remove Stock",
onTap: _removeStock, onTap: _removeStockDialog,
), ),
); );
@ -340,7 +402,7 @@ class _StockItemDisplayState extends State<StockDetailWidget> {
buttons.add(SpeedDialChild( buttons.add(SpeedDialChild(
child: Icon(FontAwesomeIcons.exchangeAlt), child: Icon(FontAwesomeIcons.exchangeAlt),
label: "Transfer Stock", label: "Transfer Stock",
onTap: _transferStock, onTap: _transferStockDialog,
)); ));
return buttons; return buttons;