productAddAddon function

Future<String?> productAddAddon(
  1. String name,
  2. double price,
  3. String locale,
  4. String stringEnterName,
  5. String stringEnterPrice,
)

Implementation

Future<String?> productAddAddon(String name, double price, String locale,
    String stringEnterName,  // strings.get(115); /// "Please Enter Name",
    String stringEnterPrice,  // strings.get(170); /// "Please enter price",
    ) async {
  if (name.isEmpty)
    return stringEnterName; /// "Please Enter Name",
  if (price == 0)
    return stringEnterPrice; /// "Please enter price",
  late AddonData _addon;
  if (editAddon == null){
    _addon = AddonData(Uuid().v4(), [StringData(code: locale, text: name)], price);
    currentProduct.addon.add(_addon);
  }else {
    editAddon!.price = price;
    var _found = false;
    for (var item in editAddon!.name)
      if (item.code == locale) {
        item.text = name;
        _found = true;
      }
    if (!_found)
      editAddon!.name.add(StringData(code: locale, text: name));
    _addon = editAddon!;
  }

  if (currentProduct.providers.isNotEmpty){
    for (var item in providers)
      if (item.id == currentProduct.providers[0]){
        if (editAddon == null)
          item.addon.add(_addon);
        else{
          for (var _add in item.addon)
            if (_add.id == _addon.id) {
              _add.price = _addon.price;
              _add.name = _addon.name;
            }
        }
        try{
          var _data = item.toJson();
          await FirebaseFirestore.instance.collection("provider").doc(item.id).set(_data, SetOptions(merge:true));
        }catch(ex){
          return "MainDataService addAddon " + ex.toString();
        }
      }
  }
  editAddon = null;
  return null;
}