Skip to content

Commit b5051f2

Browse files
author
WooSignal
committed
refactor, pubspec.yaml update
1 parent 83f6b60 commit b5051f2

File tree

7 files changed

+205
-14
lines changed

7 files changed

+205
-14
lines changed

LabelStoreMax/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [2.0.0] - 2020-03-29
2+
3+
* Bug fixes, refactor, pubspec.yaml update
4+
15
## [1.0.1] - 2020-02-12
26

37
* Bug fixes, pubspec.yaml update, rm podfile

LabelStoreMax/lib/helpers/tools.dart

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import 'package:label_storemax/labelconfig.dart';
1616
import 'package:edge_alert/edge_alert.dart';
1717
import 'package:label_storemax/models/billing_details.dart';
1818
import 'package:label_storemax/models/cart.dart';
19+
import 'package:label_storemax/models/cart_line_item.dart';
1920
import 'package:label_storemax/models/checkout_session.dart';
2021
import 'package:label_storemax/models/payment_type.dart';
2122
import 'package:html/parser.dart';
2223
import 'package:flutter_web_browser/flutter_web_browser.dart';
2324
import 'package:flutter_money_formatter/flutter_money_formatter.dart';
25+
import 'package:math_expressions/math_expressions.dart';
2426
import 'package:status_alert/status_alert.dart';
2527
import 'package:woosignal/models/response/tax_rate.dart';
2628
import 'package:woosignal/woosignal.dart';
@@ -202,3 +204,153 @@ checkout(
202204
Cart cart = Cart.getInstance;
203205
return await completeCheckout(cartTotal, billingDetails, cart);
204206
}
207+
208+
double strCal({@required String sum}) {
209+
Parser p = Parser();
210+
Expression exp = p.parse(sum);
211+
ContextModel cm = ContextModel();
212+
return exp.evaluate(EvaluationType.REAL, cm);
213+
}
214+
215+
Future<double> workoutShippingCostWC({@required String sum}) async {
216+
List<CartLineItem> cartLineItem = await Cart.getInstance.getCart();
217+
sum = sum.replaceAllMapped(defaultRegex(r'\[qty\]', strict: true), (replace) {
218+
return cartLineItem.length.toString();
219+
});
220+
221+
String orderTotal = await Cart.getInstance.getSubtotal();
222+
223+
sum = sum.replaceAllMapped(defaultRegex(r'\[fee(.*)]'), (replace) {
224+
if (replace.groupCount < 1) {
225+
return "()";
226+
}
227+
String newSum = replace.group(1);
228+
229+
// PERCENT
230+
String percentVal = newSum.replaceAllMapped(
231+
defaultRegex(r'percent="([0-9\.]+)"'), (replacePercent) {
232+
if (replacePercent != null && replacePercent.groupCount >= 1) {
233+
String strPercentage = "( (" +
234+
orderTotal.toString() +
235+
" * " +
236+
replacePercent.group(1).toString() +
237+
") / 100 )";
238+
double calPercentage = strCal(sum: strPercentage);
239+
240+
// MIN
241+
String strRegexMinFee = r'min_fee="([0-9\.]+)"';
242+
if (defaultRegex(strRegexMinFee).hasMatch(newSum)) {
243+
String strMinFee =
244+
defaultRegex(strRegexMinFee).firstMatch(newSum).group(1) ?? "0";
245+
double doubleMinFee = double.parse(strMinFee);
246+
247+
if (calPercentage < doubleMinFee) {
248+
return "(" + doubleMinFee.toString() + ")";
249+
}
250+
newSum = newSum.replaceAll(defaultRegex(strRegexMinFee), "");
251+
}
252+
253+
// MAX
254+
String strRegexMaxFee = r'max_fee="([0-9\.]+)"';
255+
if (defaultRegex(strRegexMaxFee).hasMatch(newSum)) {
256+
String strMaxFee =
257+
defaultRegex(strRegexMaxFee).firstMatch(newSum).group(1) ?? "0";
258+
double doubleMaxFee = double.parse(strMaxFee);
259+
260+
if (calPercentage > doubleMaxFee) {
261+
return "(" + doubleMaxFee.toString() + ")";
262+
}
263+
newSum = newSum.replaceAll(defaultRegex(strRegexMaxFee), "");
264+
}
265+
return "(" + calPercentage.toString() + ")";
266+
}
267+
return "";
268+
});
269+
270+
percentVal = percentVal
271+
.replaceAll(
272+
defaultRegex(r'(min_fee=\"([0-9\.]+)\"|max_fee=\"([0-9\.]+)\")'),
273+
"")
274+
.trim();
275+
return percentVal;
276+
});
277+
278+
return strCal(sum: sum);
279+
}
280+
281+
Future<double> workoutShippingClassCostWC(
282+
{@required String sum, List<CartLineItem> cartLineItem}) async {
283+
sum = sum.replaceAllMapped(defaultRegex(r'\[qty\]', strict: true), (replace) {
284+
return cartLineItem.length.toString();
285+
});
286+
287+
String orderTotal = await Cart.getInstance.getSubtotal();
288+
289+
sum = sum.replaceAllMapped(defaultRegex(r'\[fee(.*)]'), (replace) {
290+
if (replace.groupCount < 1) {
291+
return "()";
292+
}
293+
String newSum = replace.group(1);
294+
295+
// PERCENT
296+
String percentVal = newSum.replaceAllMapped(
297+
defaultRegex(r'percent="([0-9\.]+)"'), (replacePercent) {
298+
if (replacePercent != null && replacePercent.groupCount >= 1) {
299+
String strPercentage = "( (" +
300+
orderTotal.toString() +
301+
" * " +
302+
replacePercent.group(1).toString() +
303+
") / 100 )";
304+
double calPercentage = strCal(sum: strPercentage);
305+
306+
// MIN
307+
String strRegexMinFee = r'min_fee="([0-9\.]+)"';
308+
if (defaultRegex(strRegexMinFee).hasMatch(newSum)) {
309+
String strMinFee =
310+
defaultRegex(strRegexMinFee).firstMatch(newSum).group(1) ?? "0";
311+
double doubleMinFee = double.parse(strMinFee);
312+
313+
if (calPercentage < doubleMinFee) {
314+
return "(" + doubleMinFee.toString() + ")";
315+
}
316+
newSum = newSum.replaceAll(defaultRegex(strRegexMinFee), "");
317+
}
318+
319+
// MAX
320+
String strRegexMaxFee = r'max_fee="([0-9\.]+)"';
321+
if (defaultRegex(strRegexMaxFee).hasMatch(newSum)) {
322+
String strMaxFee =
323+
defaultRegex(strRegexMaxFee).firstMatch(newSum).group(1) ?? "0";
324+
double doubleMaxFee = double.parse(strMaxFee);
325+
326+
if (calPercentage > doubleMaxFee) {
327+
return "(" + doubleMaxFee.toString() + ")";
328+
}
329+
newSum = newSum.replaceAll(defaultRegex(strRegexMaxFee), "");
330+
}
331+
return "(" + calPercentage.toString() + ")";
332+
}
333+
return "";
334+
});
335+
336+
percentVal = percentVal
337+
.replaceAll(
338+
defaultRegex(r'(min_fee=\"([0-9\.]+)\"|max_fee=\"([0-9\.]+)\")'),
339+
"")
340+
.trim();
341+
return percentVal;
342+
});
343+
344+
return strCal(sum: sum);
345+
}
346+
347+
RegExp defaultRegex(
348+
String pattern, {
349+
bool strict,
350+
}) {
351+
return new RegExp(
352+
pattern,
353+
caseSensitive: strict ?? false,
354+
multiLine: false,
355+
);
356+
}

LabelStoreMax/lib/labelconfig.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
const app_name = "MyApp";
2424

25-
const app_key = "My app key";
25+
const app_key =
26+
"My App Key";
2627

2728
const app_logo_url = "https://woosignal.com/images/120x120_woosignal.png";
2829

LabelStoreMax/lib/pages/checkout_confirmation.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ class CheckoutConfirmationPageState extends State<CheckoutConfirmationPage> {
217217
color: Colors.black12,
218218
thickness: 1,
219219
),
220+
wsCheckoutSubtotalWidgetFB(
221+
title: trans(context, "Subtotal")),
220222
widgetCheckoutMeta(context,
221223
title: trans(context, "Shipping fee"),
222224
amount:
@@ -227,8 +229,6 @@ class CheckoutConfirmationPageState extends State<CheckoutConfirmationPage> {
227229
(_taxRate != null
228230
? wsCheckoutTaxAmountWidgetFB(taxRate: _taxRate)
229231
: Container()),
230-
wsCheckoutSubtotalWidgetFB(
231-
title: trans(context, "Subtotal")),
232232
wsCheckoutTotalWidgetFB(
233233
title: trans(context, "Total"), taxRate: _taxRate),
234234
Divider(

LabelStoreMax/lib/pages/checkout_shipping_type.dart

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:label_storemax/models/customer_address.dart';
1717
import 'package:label_storemax/models/shipping_type.dart';
1818
import 'package:label_storemax/widgets/app_loader.dart';
1919
import 'package:label_storemax/widgets/buttons.dart';
20+
import 'package:math_expressions/math_expressions.dart';
2021
import 'package:woosignal/models/response/shipping_method.dart';
2122
import 'package:label_storemax/app_country_options.dart';
2223

@@ -59,11 +60,9 @@ class _CheckoutShippingTypePageState extends State<CheckoutShippingTypePage> {
5960
.firstWhere((c) => c['name'] == country, orElse: () => null)["code"];
6061

6162
for (final shipping in wsShipping) {
62-
Locations location = shipping.locations
63-
.firstWhere((ws) => (ws.code == postalCode || ws.code == countryCode),
64-
orElse: () {
65-
return null;
66-
});
63+
Locations location = shipping.locations.firstWhere(
64+
(ws) => (ws.code == postalCode || ws.code == countryCode),
65+
orElse: () => null);
6766

6867
if (location != null) {
6968
_shipping = shipping;
@@ -129,21 +128,48 @@ class _CheckoutShippingTypePageState extends State<CheckoutShippingTypePage> {
129128
Future<String> _getShippingPrice(int index) async {
130129
double total = 0;
131130
List<CartLineItem> cartLineItem = await Cart.getInstance.getCart();
131+
132+
total +=
133+
await workoutShippingCostWC(sum: _wsShippingOptions[index]['cost']);
134+
132135
switch (_wsShippingOptions[index]['method_id']) {
133136
case "flat_rate":
134137
FlatRate flatRate = (_wsShippingOptions[index]['object'] as FlatRate);
135-
cartLineItem.forEach((c) {
136-
ShippingClasses shippingClasses = flatRate.shippingClasses
137-
.firstWhere((s) => s.id == c.shippingClassId, orElse: () => null);
138+
139+
if (cartLineItem.firstWhere(
140+
(t) => t.shippingClassId == null || t.shippingClassId == "0",
141+
orElse: () => null) !=
142+
null) {
143+
total += await workoutShippingClassCostWC(
144+
sum: flatRate.classCost,
145+
cartLineItem: cartLineItem
146+
.where((t) =>
147+
t.shippingClassId == null || t.shippingClassId == "0")
148+
.toList());
149+
}
150+
151+
List<CartLineItem> cItemsWithShippingClasses = cartLineItem
152+
.where((t) => t.shippingClassId != null && t.shippingClassId != "0")
153+
.toList();
154+
for (int i = 0; i < cItemsWithShippingClasses.length; i++) {
155+
ShippingClasses shippingClasses = flatRate.shippingClasses.firstWhere(
156+
(d) => d.id == cItemsWithShippingClasses[i].shippingClassId,
157+
orElse: () => null);
138158
if (shippingClasses != null) {
139-
total = total + double.parse(shippingClasses.cost);
159+
double classTotal = await workoutShippingClassCostWC(
160+
sum: shippingClasses.cost,
161+
cartLineItem: cartLineItem
162+
.where((g) => g.shippingClassId == shippingClasses.id)
163+
.toList());
164+
total += classTotal;
140165
}
141-
});
166+
}
167+
142168
break;
143169
default:
144170
break;
145171
}
146-
return (total + double.parse(_wsShippingOptions[index]['cost'])).toString();
172+
return (total).toString();
147173
}
148174

149175
@override

LabelStoreMax/pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ packages:
231231
url: "https://pub.dartlang.org"
232232
source: hosted
233233
version: "0.12.6"
234+
math_expressions:
235+
dependency: "direct main"
236+
description:
237+
name: math_expressions
238+
url: "https://pub.dartlang.org"
239+
source: hosted
240+
version: "2.0.0"
234241
meta:
235242
dependency: transitive
236243
description:

LabelStoreMax/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dependencies:
3636
flutter_swiper: ^1.1.6
3737
edge_alert: ^0.0.1
3838
status_alert: ^0.1.1
39+
math_expressions: ^2.0.0
3940
fluttertoast: ^4.0.1
4041
flutter_spinkit: ^4.1.2+1
4142
flutter_launcher_icons: ^0.7.4

0 commit comments

Comments
 (0)