fix return item with consumed points
This commit is contained in:
@@ -134,14 +134,28 @@ class _ReturnScreenState<T> extends BaseState<ReturnScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
num extractReturnPoints(Map purchase) {
|
||||
final returnPoints = purchase['sum_bonus'];
|
||||
if(returnPoints is num) {
|
||||
return returnPoints;
|
||||
} else if(returnPoints is String) {
|
||||
final RegExp returnPattern = RegExp(r"(\d+)");
|
||||
final List<Match> digits = returnPattern.allMatches(returnPoints).toList();
|
||||
return int.parse(digits[0].group(0)) - int.parse(digits[1].group(1));
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void _confirmReturnPurchase(Map purchase, String formattedDate) {
|
||||
final num totalReturn = extractReturnPoints(purchase);
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) =>
|
||||
AlertDialog(
|
||||
title: Text(StringsLocalization.confirmation()),
|
||||
content: Text(StringsLocalization.returnConfirmation(
|
||||
purchase['sum_total'], formattedDate, _bonus - purchase['sum_bonus'])),
|
||||
purchase['sum_total'], formattedDate, _bonus - totalReturn)),
|
||||
actions: <Widget>[
|
||||
FlatButton(
|
||||
child: Text(StringsLocalization.no()),
|
||||
@@ -151,7 +165,7 @@ class _ReturnScreenState<T> extends BaseState<ReturnScreen> {
|
||||
child: Text(StringsLocalization.yes()),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
_returnPurchase(purchase, formattedDate);
|
||||
_returnPurchase(purchase, formattedDate, totalReturn);
|
||||
}
|
||||
)
|
||||
],
|
||||
@@ -159,11 +173,11 @@ class _ReturnScreenState<T> extends BaseState<ReturnScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
void _returnPurchase(Map purchase, String formattedDate) async {
|
||||
void _returnPurchase(Map purchase, String formattedDate, num totalReturn) async {
|
||||
String token = await helper.getToken();
|
||||
final Response response = await returnPurchase(token, widget.userId, purchase['id']);
|
||||
if(response.statusCode == 204) {
|
||||
_bonus += purchase['sum_bonus'];
|
||||
_bonus += totalReturn;
|
||||
setState(() {
|
||||
_purchases.removeWhere((p) => p['id'] == purchase['id']);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user