finishCartV1 function

Future<String?> finishCartV1(
  1. ProductData currentService,
  2. bool temporary,
  3. String paymentMethodId,
  4. String stringFromUser,
  5. String stringNewBooking,
)

Implementation

Future<String?> finishCartV1(ProductData currentService, bool temporary, String paymentMethodId,
    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 "finishCartV1 user == null";

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

  try{
    var _provider = ProviderData.createEmpty();
    for (var item in providers)
      if (item.id == currentService.providers[0])
        _provider = item;
    if (_provider.id.isEmpty)
      return "Provider not found. _provider.id.isEmpty";

    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: userAccountData.userName,
      customerAvatar: userAccountData.userAvatar,
      providerId: _provider.id,
      provider: _provider.name,
      providerPhone: _provider.phone,
      providerAvatar: _provider.imageUpperServerPath,
      serviceId: currentService.id,
      service: currentService.name,
      // price
      price: price.price,
      discPrice: price.discPrice,
      priceUnit: price.priceUnit,
      count: countProduct,
      priceName: price.name,
      // coupon
      couponId: couponId,
      couponName: couponId.isEmpty ? "" : couponCode,
      discount: couponId.isEmpty ? 0 : discount,
      discountType: couponId.isEmpty ? "fixed" : discountType,
      //
      tax: currentService.tax,
      taxAdmin: currentService.taxAdmin,
      total: getTotal(),
      paymentMethod: paymentMethodId,
      //
      comment: cartHint,
      address: getCurrentAddress().address,
      anyTime: cartAnyTime,
      selectTime: cartSelectTime,
      //
      time: DateTime.now(),
      timeModify: DateTime.now(),
      //
      viewByAdmin: false,
      viewByProvider: false,
      //
      addon: _addon,
      products: [],
      customerPhone: getCurrentAddress().phone,
      customerEmail: userAccountData.userEmail,
    );
    await cartFinish(_data);
    // var retBooking = await FirebaseFirestore.instance.collection("booking").add(_data);
    if (!temporary){
      // await FirebaseFirestore.instance.collection("settings").doc("main")
      //     .set({"booking_count": FieldValue.increment(1)}, SetOptions(merge:true));
      // await FirebaseFirestore.instance.collection("settings").doc("main")
      //     .set({"booking_count_unread": FieldValue.increment(1)}, SetOptions(merge:true));
      // var ret = await loadUsers(this);
      // if (ret != null)
      //   return ret;
      // var _provider2 = getProviderById(_provider.id);
      // if (_provider2 != 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);
        }
      // sendMessage("${strings.get(117)} $userName",  /// "From user:",
      //     strings.get(118),  /// "New Booking was arrived",
      //     getProviderId(_provider.login));
    // }
  }catch(ex){
    return "finish " + ex.toString();
  }
}