From b5051f2d902200d45c22acff3cfe0d547495a5bb Mon Sep 17 00:00:00 2001 From: WooSignal Date: Sun, 29 Mar 2020 16:30:11 +0100 Subject: [PATCH] refactor, pubspec.yaml update --- LabelStoreMax/CHANGELOG.md | 4 + LabelStoreMax/lib/helpers/tools.dart | 152 ++++++++++++++++++ LabelStoreMax/lib/labelconfig.dart | 3 +- .../lib/pages/checkout_confirmation.dart | 4 +- .../lib/pages/checkout_shipping_type.dart | 48 ++++-- LabelStoreMax/pubspec.lock | 7 + LabelStoreMax/pubspec.yaml | 1 + 7 files changed, 205 insertions(+), 14 deletions(-) diff --git a/LabelStoreMax/CHANGELOG.md b/LabelStoreMax/CHANGELOG.md index f223484..3940d54 100644 --- a/LabelStoreMax/CHANGELOG.md +++ b/LabelStoreMax/CHANGELOG.md @@ -1,3 +1,7 @@ +## [2.0.0] - 2020-03-29 + +* Bug fixes, refactor, pubspec.yaml update + ## [1.0.1] - 2020-02-12 * Bug fixes, pubspec.yaml update, rm podfile diff --git a/LabelStoreMax/lib/helpers/tools.dart b/LabelStoreMax/lib/helpers/tools.dart index 44d050d..69d42a6 100644 --- a/LabelStoreMax/lib/helpers/tools.dart +++ b/LabelStoreMax/lib/helpers/tools.dart @@ -16,11 +16,13 @@ import 'package:label_storemax/labelconfig.dart'; import 'package:edge_alert/edge_alert.dart'; import 'package:label_storemax/models/billing_details.dart'; import 'package:label_storemax/models/cart.dart'; +import 'package:label_storemax/models/cart_line_item.dart'; import 'package:label_storemax/models/checkout_session.dart'; import 'package:label_storemax/models/payment_type.dart'; import 'package:html/parser.dart'; import 'package:flutter_web_browser/flutter_web_browser.dart'; import 'package:flutter_money_formatter/flutter_money_formatter.dart'; +import 'package:math_expressions/math_expressions.dart'; import 'package:status_alert/status_alert.dart'; import 'package:woosignal/models/response/tax_rate.dart'; import 'package:woosignal/woosignal.dart'; @@ -202,3 +204,153 @@ checkout( Cart cart = Cart.getInstance; return await completeCheckout(cartTotal, billingDetails, cart); } + +double strCal({@required String sum}) { + Parser p = Parser(); + Expression exp = p.parse(sum); + ContextModel cm = ContextModel(); + return exp.evaluate(EvaluationType.REAL, cm); +} + +Future workoutShippingCostWC({@required String sum}) async { + List cartLineItem = await Cart.getInstance.getCart(); + sum = sum.replaceAllMapped(defaultRegex(r'\[qty\]', strict: true), (replace) { + return cartLineItem.length.toString(); + }); + + String orderTotal = await Cart.getInstance.getSubtotal(); + + sum = sum.replaceAllMapped(defaultRegex(r'\[fee(.*)]'), (replace) { + if (replace.groupCount < 1) { + return "()"; + } + String newSum = replace.group(1); + + // PERCENT + String percentVal = newSum.replaceAllMapped( + defaultRegex(r'percent="([0-9\.]+)"'), (replacePercent) { + if (replacePercent != null && replacePercent.groupCount >= 1) { + String strPercentage = "( (" + + orderTotal.toString() + + " * " + + replacePercent.group(1).toString() + + ") / 100 )"; + double calPercentage = strCal(sum: strPercentage); + + // MIN + String strRegexMinFee = r'min_fee="([0-9\.]+)"'; + if (defaultRegex(strRegexMinFee).hasMatch(newSum)) { + String strMinFee = + defaultRegex(strRegexMinFee).firstMatch(newSum).group(1) ?? "0"; + double doubleMinFee = double.parse(strMinFee); + + if (calPercentage < doubleMinFee) { + return "(" + doubleMinFee.toString() + ")"; + } + newSum = newSum.replaceAll(defaultRegex(strRegexMinFee), ""); + } + + // MAX + String strRegexMaxFee = r'max_fee="([0-9\.]+)"'; + if (defaultRegex(strRegexMaxFee).hasMatch(newSum)) { + String strMaxFee = + defaultRegex(strRegexMaxFee).firstMatch(newSum).group(1) ?? "0"; + double doubleMaxFee = double.parse(strMaxFee); + + if (calPercentage > doubleMaxFee) { + return "(" + doubleMaxFee.toString() + ")"; + } + newSum = newSum.replaceAll(defaultRegex(strRegexMaxFee), ""); + } + return "(" + calPercentage.toString() + ")"; + } + return ""; + }); + + percentVal = percentVal + .replaceAll( + defaultRegex(r'(min_fee=\"([0-9\.]+)\"|max_fee=\"([0-9\.]+)\")'), + "") + .trim(); + return percentVal; + }); + + return strCal(sum: sum); +} + +Future workoutShippingClassCostWC( + {@required String sum, List cartLineItem}) async { + sum = sum.replaceAllMapped(defaultRegex(r'\[qty\]', strict: true), (replace) { + return cartLineItem.length.toString(); + }); + + String orderTotal = await Cart.getInstance.getSubtotal(); + + sum = sum.replaceAllMapped(defaultRegex(r'\[fee(.*)]'), (replace) { + if (replace.groupCount < 1) { + return "()"; + } + String newSum = replace.group(1); + + // PERCENT + String percentVal = newSum.replaceAllMapped( + defaultRegex(r'percent="([0-9\.]+)"'), (replacePercent) { + if (replacePercent != null && replacePercent.groupCount >= 1) { + String strPercentage = "( (" + + orderTotal.toString() + + " * " + + replacePercent.group(1).toString() + + ") / 100 )"; + double calPercentage = strCal(sum: strPercentage); + + // MIN + String strRegexMinFee = r'min_fee="([0-9\.]+)"'; + if (defaultRegex(strRegexMinFee).hasMatch(newSum)) { + String strMinFee = + defaultRegex(strRegexMinFee).firstMatch(newSum).group(1) ?? "0"; + double doubleMinFee = double.parse(strMinFee); + + if (calPercentage < doubleMinFee) { + return "(" + doubleMinFee.toString() + ")"; + } + newSum = newSum.replaceAll(defaultRegex(strRegexMinFee), ""); + } + + // MAX + String strRegexMaxFee = r'max_fee="([0-9\.]+)"'; + if (defaultRegex(strRegexMaxFee).hasMatch(newSum)) { + String strMaxFee = + defaultRegex(strRegexMaxFee).firstMatch(newSum).group(1) ?? "0"; + double doubleMaxFee = double.parse(strMaxFee); + + if (calPercentage > doubleMaxFee) { + return "(" + doubleMaxFee.toString() + ")"; + } + newSum = newSum.replaceAll(defaultRegex(strRegexMaxFee), ""); + } + return "(" + calPercentage.toString() + ")"; + } + return ""; + }); + + percentVal = percentVal + .replaceAll( + defaultRegex(r'(min_fee=\"([0-9\.]+)\"|max_fee=\"([0-9\.]+)\")'), + "") + .trim(); + return percentVal; + }); + + return strCal(sum: sum); +} + +RegExp defaultRegex( + String pattern, { + bool strict, +}) { + return new RegExp( + pattern, + caseSensitive: strict ?? false, + multiLine: false, + ); +} diff --git a/LabelStoreMax/lib/labelconfig.dart b/LabelStoreMax/lib/labelconfig.dart index fce6a98..f8b30ee 100644 --- a/LabelStoreMax/lib/labelconfig.dart +++ b/LabelStoreMax/lib/labelconfig.dart @@ -22,7 +22,8 @@ const app_name = "MyApp"; -const app_key = "My app key"; +const app_key = + "My App Key"; const app_logo_url = "https://woosignal.com/images/120x120_woosignal.png"; diff --git a/LabelStoreMax/lib/pages/checkout_confirmation.dart b/LabelStoreMax/lib/pages/checkout_confirmation.dart index c9be328..9e07503 100644 --- a/LabelStoreMax/lib/pages/checkout_confirmation.dart +++ b/LabelStoreMax/lib/pages/checkout_confirmation.dart @@ -217,6 +217,8 @@ class CheckoutConfirmationPageState extends State { color: Colors.black12, thickness: 1, ), + wsCheckoutSubtotalWidgetFB( + title: trans(context, "Subtotal")), widgetCheckoutMeta(context, title: trans(context, "Shipping fee"), amount: @@ -227,8 +229,6 @@ class CheckoutConfirmationPageState extends State { (_taxRate != null ? wsCheckoutTaxAmountWidgetFB(taxRate: _taxRate) : Container()), - wsCheckoutSubtotalWidgetFB( - title: trans(context, "Subtotal")), wsCheckoutTotalWidgetFB( title: trans(context, "Total"), taxRate: _taxRate), Divider( diff --git a/LabelStoreMax/lib/pages/checkout_shipping_type.dart b/LabelStoreMax/lib/pages/checkout_shipping_type.dart index 3c65db6..3b20394 100644 --- a/LabelStoreMax/lib/pages/checkout_shipping_type.dart +++ b/LabelStoreMax/lib/pages/checkout_shipping_type.dart @@ -17,6 +17,7 @@ import 'package:label_storemax/models/customer_address.dart'; import 'package:label_storemax/models/shipping_type.dart'; import 'package:label_storemax/widgets/app_loader.dart'; import 'package:label_storemax/widgets/buttons.dart'; +import 'package:math_expressions/math_expressions.dart'; import 'package:woosignal/models/response/shipping_method.dart'; import 'package:label_storemax/app_country_options.dart'; @@ -59,11 +60,9 @@ class _CheckoutShippingTypePageState extends State { .firstWhere((c) => c['name'] == country, orElse: () => null)["code"]; for (final shipping in wsShipping) { - Locations location = shipping.locations - .firstWhere((ws) => (ws.code == postalCode || ws.code == countryCode), - orElse: () { - return null; - }); + Locations location = shipping.locations.firstWhere( + (ws) => (ws.code == postalCode || ws.code == countryCode), + orElse: () => null); if (location != null) { _shipping = shipping; @@ -129,21 +128,48 @@ class _CheckoutShippingTypePageState extends State { Future _getShippingPrice(int index) async { double total = 0; List cartLineItem = await Cart.getInstance.getCart(); + + total += + await workoutShippingCostWC(sum: _wsShippingOptions[index]['cost']); + switch (_wsShippingOptions[index]['method_id']) { case "flat_rate": FlatRate flatRate = (_wsShippingOptions[index]['object'] as FlatRate); - cartLineItem.forEach((c) { - ShippingClasses shippingClasses = flatRate.shippingClasses - .firstWhere((s) => s.id == c.shippingClassId, orElse: () => null); + + if (cartLineItem.firstWhere( + (t) => t.shippingClassId == null || t.shippingClassId == "0", + orElse: () => null) != + null) { + total += await workoutShippingClassCostWC( + sum: flatRate.classCost, + cartLineItem: cartLineItem + .where((t) => + t.shippingClassId == null || t.shippingClassId == "0") + .toList()); + } + + List cItemsWithShippingClasses = cartLineItem + .where((t) => t.shippingClassId != null && t.shippingClassId != "0") + .toList(); + for (int i = 0; i < cItemsWithShippingClasses.length; i++) { + ShippingClasses shippingClasses = flatRate.shippingClasses.firstWhere( + (d) => d.id == cItemsWithShippingClasses[i].shippingClassId, + orElse: () => null); if (shippingClasses != null) { - total = total + double.parse(shippingClasses.cost); + double classTotal = await workoutShippingClassCostWC( + sum: shippingClasses.cost, + cartLineItem: cartLineItem + .where((g) => g.shippingClassId == shippingClasses.id) + .toList()); + total += classTotal; } - }); + } + break; default: break; } - return (total + double.parse(_wsShippingOptions[index]['cost'])).toString(); + return (total).toString(); } @override diff --git a/LabelStoreMax/pubspec.lock b/LabelStoreMax/pubspec.lock index 98a1261..1c8e64d 100644 --- a/LabelStoreMax/pubspec.lock +++ b/LabelStoreMax/pubspec.lock @@ -231,6 +231,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.12.6" + math_expressions: + dependency: "direct main" + description: + name: math_expressions + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" meta: dependency: transitive description: diff --git a/LabelStoreMax/pubspec.yaml b/LabelStoreMax/pubspec.yaml index 9528d07..91c856a 100644 --- a/LabelStoreMax/pubspec.yaml +++ b/LabelStoreMax/pubspec.yaml @@ -36,6 +36,7 @@ dependencies: flutter_swiper: ^1.1.6 edge_alert: ^0.0.1 status_alert: ^0.1.1 + math_expressions: ^2.0.0 fluttertoast: ^4.0.1 flutter_spinkit: ^4.1.2+1 flutter_launcher_icons: ^0.7.4