finishCartV4 function

Future<String?> finishCartV4(
  1. bool temporary,
  2. String paymentMethodId,
  3. bool cachePayment,
  4. String stringFromUser,
  5. String stringNewBooking,
)

Implementation

Future<String?> finishCartV4(bool temporary, String paymentMethodId,
    bool cachePayment,
    String stringFromUser, /// strings.get(160) /// "From user:",
    String stringNewBooking, /// strings.get(157) /// "New Booking was arrived",
    ) async{
  User? user = FirebaseAuth.instance.currentUser;
  if (user == null)
    return "finishCartV4 user == null";
  if (cartCurrentProvider == null)
    return "finish cartCurrentProvider = null";

  try{
    var _products = cartGetProductsForBooking();
    for (var item in _products){
      List<AddonData> _addon = [];
      for (var item in item.addon)
        if (item.selected)
          _addon.add(item);
      // if (item.addon.isNotEmpty)
      //   for (var item in currentService.addon) {
      //     if (!item.selected)
      //       continue;
      //     _addon.add(item);
      //   }
      item.addon = _addon;
    }

    var _totalPrice = cartGetTotalForAllServices();
    var _data = OrderData(
      status: temporary ? "temp" : appSettings.statuses[0].id,
      history: [StatusHistory(
          statusId: appSettings.statuses[0].id,
          time: DateTime.now().toUtc(),
          byCustomer: true,
          activateUserId : user.uid
      )],
      customerId: user.uid,
      customer: getCurrentAddress().name,
      customerAvatar: userAccountData.userAvatar,
      providerId: cartCurrentProvider!.id,
      provider: cartCurrentProvider!.name,
      providerPhone: cartCurrentProvider!.phone,
      providerAvatar: cartCurrentProvider!.imageUpperServerPath,
      serviceId: "",
      service: [],
      serviceImage: cartGetImage(),
      // price
      price: 0,
      discPrice: 0,
      priceUnit: "",
      count: 0,
      priceName: [],
      // coupon
      couponId: couponId,
      couponName: couponId.isEmpty ? "" : couponCode,
      discount: couponId.isEmpty ? 0 : discount,
      discountType: couponId.isEmpty ? "fixed" : discountType,
      //
      tax: 0,
      taxAdmin: 0,
      total: _totalPrice.total,
      paymentMethod: paymentMethodId,
      paymentMethodCache: cachePayment,
      //
      comment: cartHint,
      address: getCurrentAddress().address,
      anyTime: cartAnyTime,
      selectTime: cartSelectTime,
      //
      time: DateTime.now(),
      timeModify: DateTime.now(),
      viewByAdmin: false,
      viewByProvider: false,
      //
      addon: [],
      products: _products,
      ver4: true,
      customerPhone: getCurrentAddress().phone,
      customerEmail: userAccountData.userEmail,
    );
    await cartFinish(_data);
    if (!temporary){
      var _provider = getProviderById(cartCurrentProvider!.id);
      if (_provider != null){
        UserData? _user = await getProviderUserByEmail(_provider.login);
        if (_user != null)
          sendMessage("$stringFromUser ${userAccountData.userName}",  /// "From user:",
            stringNewBooking,  /// "New Booking was arrived",
            _user.id, true, appSettings.cloudKey);
        clearCart();
      }
    }
  }catch(ex){
    return "finish:ver4 " + ex.toString();
  }
}