@@ -16,11 +16,13 @@ import 'package:label_storemax/labelconfig.dart';
1616import 'package:edge_alert/edge_alert.dart' ;
1717import 'package:label_storemax/models/billing_details.dart' ;
1818import 'package:label_storemax/models/cart.dart' ;
19+ import 'package:label_storemax/models/cart_line_item.dart' ;
1920import 'package:label_storemax/models/checkout_session.dart' ;
2021import 'package:label_storemax/models/payment_type.dart' ;
2122import 'package:html/parser.dart' ;
2223import 'package:flutter_web_browser/flutter_web_browser.dart' ;
2324import 'package:flutter_money_formatter/flutter_money_formatter.dart' ;
25+ import 'package:math_expressions/math_expressions.dart' ;
2426import 'package:status_alert/status_alert.dart' ;
2527import 'package:woosignal/models/response/tax_rate.dart' ;
2628import '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+ }
0 commit comments