prepareSellInCartRequest method

dynamic prepareSellInCartRequest()

Implementation

prepareSellInCartRequest() async {
  // var transactionType = "SELL_IN";
  // int buyerType = storage.read("LEVEL_ID") ?? 0;
  // int sellerType = storage.read("DISTRIBUTER_LEVEL_ID") ?? 0;
  // String regionName = storage.read("REGION_NAME") ?? "";
  // String levelNameLogin = SecureStorageService.readSecureData(
  //       SecureStorageService.levelNameLogin,
  //     ) ??
  //     "";
  addToCartSellIn.AddToCartSellInRequest request =
      addToCartSellIn.AddToCartSellInRequest();

  // // if (isPaymentSelected.value = true) {
  // //   paymentDetails.add(addToCartSellIn.Payment(
  // //       id: "", name: isPaymentSelectName.value, href: transactionType));
  // // }
  // List<addToCartSellIn.RelatedParty> relatedPart = []; //1
  // relatedPart.add(
  //   addToCartSellIn.RelatedParty(
  //     name: SecureStorageService.readSecureData(
  //       SecureStorageService.userId,
  //     ),
  //     role: "CreatedBy",
  //   ),
  // );
  // relatedPart.add(addToCartSellIn.RelatedParty(
  //   name: levelNameLogin,
  //   role: "DistributorType",
  // ));
  // relatedPart.add(
  //   addToCartSellIn.RelatedParty(
  //     name: SecureStorageService.readSecureData(
  //       SecureStorageService.userId,
  //     ),
  //     role: "agentId",
  //   ),
  // );
  // List<addToCartSellIn.SelectedCartoffers> selectedCartOffers = []; //2
  // for (var product in selectedProduct) {
  //   selectedCartOffers.add(addToCartSellIn.SelectedCartoffers(
  //       id: product.productId,
  //       autoRefill: false,
  //       isAttached: 'false',
  //       pog: product.productId,
  //       quantity: product.scannedQty.toString(),
  //       recommended: false,
  //       assetId: "",
  //       startRange: "",
  //       endRange: ""));
  // }

  // addToCartSellIn.ContextParameters contextParameters =
  //     addToCartSellIn.ContextParameters(
  //   currencyType: "USD",
  //   buyerType: buyerType.toString(),
  //   buyerTypeEntityId: partnerId,
  //   sellerTypeEntityId: parentId,
  //   sellerType: sellerType.toString(),
  //   region: regionName,
  //   transactionType: 'SELL_IN',
  //   creditLimitBalance: "",
  // );

  List<addToCartSellIn.ShippingMethod> shippingMethod = [];
  List<addToCartSellIn.AddressDetails> addressDetails = [];

  String locationCode = "";
  String entityId = "";
  String entityName = "";
  String latitude = "";
  String longitude = "";
  String building = "";
  String buildingNe = "";
  String streetName = "";
  String streetNameNe = "";
  String postalCode = "";
  String locationName1 = "";
  String locationName2 = "";
  String locationName3 = "";
  String locationName4 = "";
  String locationName1Ne = "";
  String locationName2Ne = "";
  String locationName3Ne = "";
  String locationName4Ne = "";
  String formattedAddress = "";
  String formattedAddressNe = "";
  String firstName = "";
  String lastName = "";
  String email = "";
  String phone = "";
  String invoiceLocationCode = "";
  if (selectedDeliveryName.value == "pickup") {
    entityId = pickupSelectedAddress.value.partnerId ?? "";
    entityName = pickupSelectedAddress.value.partnerName ?? "";
    locationCode = pickupSelectedAddress.value.address?.locationCode ?? "";
    building = pickupSelectedAddress.value.address?.building ?? "";
    buildingNe = pickupSelectedAddress.value.address?.buildingNe ?? "";
    latitude = pickupSelectedAddress.value.address?.lattitude ?? "0.0";
    longitude = pickupSelectedAddress.value.address?.longitude ?? "0.0";
    streetName = pickupSelectedAddress.value.address?.streetName ?? "";
    postalCode =
        pickupSelectedAddress.value.address?.postalCode?.toString() ?? "";
    locationName1 = pickupSelectedAddress.value.address?.locationName1 ?? "";
    locationName2 = pickupSelectedAddress.value.address?.locationName2 ?? "";
    locationName3 = pickupSelectedAddress.value.address?.locationName3 ?? "";
    locationName4 = pickupSelectedAddress.value.address?.locationName4 ?? "";
    firstName = pickupSelectedAddress.value.partnerName ?? "";
    lastName = pickupSelectedAddress.value.lastName ?? "";
    email = pickupSelectedAddress.value.emailId ?? "";
    phone = pickupSelectedAddress.value.phone ?? "";

    for (var company
        in pickupSelectedAddress.value.profileGroup?.company ?? []) {
      if (company.field == "invoiceLocationCode") {
        invoiceLocationCode = company.value;
        break; // Stop the loop once we find the matching field
      }
    }

    // if (companyList != null) {
    //   var matchingField = companyList.firstWhere(
    //     (element) =>
    //         element.field ==
    //         "invoiceLocationCode", // Return an empty map instead of null
    //   );

    //   invoiceLocationCode = matchingField.value;
    // }
    final parts = <String>[
      building,
      streetName,
      postalCode,
      locationName1,
      locationName2,
      locationName3,
      locationName4,
    ];

    formattedAddress = parts.where((part) => part.isNotEmpty).join(',');
    addressDetails.add(addToCartSellIn.AddressDetails(
      type: selectedDeliveryName.value == "pickup"
          ? "PICK_UP_ADDRESS"
          : "DELIVERY_ADDRESS",
      locationCode: locationCode,
      invoiceLocationCode: invoiceLocationCode,
      entityId: entityId,
      entityName: entityName,
      building: building,
      buildingNe: buildingNe,
      lattitude: latitude,
      longitude: longitude,
      streetName: streetName,
      postalCode: postalCode,
      locationName1: locationName1,
      locationName2: locationName2,
      locationName3: locationName3,
      locationName4: locationName4,
      streetNameNe: "",
      formattedAddress: formattedAddress,
      firstName: firstName,
      lastName: lastName,
      emailId: email,
      phone: phone,
    ));
  } else {
    if (selectedLocationType.value == "Dealer") {
      entityId = "";
      entityName = "";
      building = currentAddress.value.building ?? "";
      buildingNe = currentAddress.value.buildingNe ?? "";
      latitude = currentAddress.value.lattitude ?? "0.0";
      longitude = currentAddress.value.longitude ?? "0.0";
      streetName = currentAddress.value.streetName ?? "";
      streetNameNe = currentAddress.value.streetNameNe ?? "";
      postalCode = currentAddress.value.postalCode?.toString() ?? "";
      locationName1 = currentAddress.value.locationName1 ?? "";
      locationName2 = currentAddress.value.locationName2 ?? "";
      locationName3 = currentAddress.value.locationName3 ?? "";
      locationName4 = currentAddress.value.locationName4 ?? "";
      locationName1Ne = currentAddress.value.locationName1Ne ?? "";
      locationName2Ne = currentAddress.value.locationName2Ne ?? "";
      locationName3Ne = currentAddress.value.locationName3Ne ?? "";
      locationName4Ne = currentAddress.value.locationName4Ne ?? "";

      final parts = <String>[
        building,
        streetName,
        postalCode,
        locationName1,
        locationName2,
        locationName3,
        locationName4,
      ];
      formattedAddress = parts.where((part) => part.isNotEmpty).join(',');

      final partsNe = <String>[
        buildingNe,
        streetNameNe,
        postalCode,
        locationName1Ne,
        locationName2Ne,
        locationName3Ne,
        locationName4Ne,
      ];
      formattedAddressNe = partsNe.where((part) => part.isNotEmpty).join(',');
    } else {
      for (var component in currentSeletedAddress) {
        var types = component['types'];

        if (types.contains('street_number')) {
          building = builderController.text.isEmpty
              ? component['long_name'] ?? ''
              : builderController.text.trim();
        } else if (types.contains('route')) {
          streetName = component['long_name'] ?? '';
        } else if (types.contains('postal_code')) {
          postalCode = component['long_name'] ?? "";
        } else if (types.contains('administrative_area_level_1')) {
          locationName1 = component['long_name'] ?? '';
        } else if (types.contains('administrative_area_level_2')) {
          locationName2 = component['long_name'] ?? '';
        } else if (types.contains('administrative_area_level_3')) {
          locationName3 = component['long_name'] ?? '';
        } else if (types.contains('locality')) {
          locationName4 = component['long_name'] ?? '';
        }
      }
      if (building.isEmpty) {
        if (builderController.text.isNotEmpty) {
          building = builderController.text.trim();
        }
      }
      entityId = "";
      entityName = "";
      buildingNe = "";
      latitude = selectedDeliverylattitude.value.toString();
      longitude = selectedDeliverylongitude.value.toString();
      streetName = building == streetName ? "" : streetName;
      final parts = <String>[
        building,
        streetName,
        postalCode,
        locationName1,
        locationName2,
        locationName3,
        locationName4,
      ];

      formattedAddress = parts.where((part) => part.isNotEmpty).join(',');
    }
    addressDetails.add(addToCartSellIn.AddressDetails(
      type: "DELIVERY_ADDRESS",
      locationCode: "",
      entityId: entityId,
      entityName: entityName,
      building: building.toString(),
      buildingNe: buildingNe,
      lattitude: latitude,
      longitude: longitude,
      streetName: streetName.toString(),
      postalCode: postalCode,
      locationName1: locationName1.toString(),
      locationName2: locationName2.toString(),
      locationName3: locationName3.toString(),
      locationName4: locationName4.toString(),
      streetNameNe: "",
      formattedAddress: formattedAddress,
      formattedAddressNe:
          selectedLocationType.value == "Dealer" ? formattedAddressNe : null,
    ));
    entityId = pickupSelectedAddress.value.partnerId ?? "";
    entityName = pickupSelectedAddress.value.partnerName ?? "";
    locationCode = pickupSelectedAddress.value.address?.locationCode ?? "";
    building = pickupSelectedAddress.value.address?.building ?? "";
    buildingNe = pickupSelectedAddress.value.address?.buildingNe ?? "";
    latitude = pickupSelectedAddress.value.address?.lattitude ?? "0.0";
    longitude = pickupSelectedAddress.value.address?.longitude ?? "0.0";
    streetName = pickupSelectedAddress.value.address?.streetName ?? "";
    postalCode =
        pickupSelectedAddress.value.address?.postalCode?.toString() ?? "";
    locationName1 = pickupSelectedAddress.value.address?.locationName1 ?? "";
    locationName2 = pickupSelectedAddress.value.address?.locationName2 ?? "";
    locationName3 = pickupSelectedAddress.value.address?.locationName3 ?? "";
    locationName4 = pickupSelectedAddress.value.address?.locationName4 ?? "";
    firstName = pickupSelectedAddress.value.partnerName ?? "";
    lastName = pickupSelectedAddress.value.lastName ?? "";
    email = pickupSelectedAddress.value.emailId ?? "";
    phone = pickupSelectedAddress.value.phone ?? "";
    ConditionalLogs().customLog(
        "objectData  ${pickupSelectedAddress.value.profileGroup?.company}");
    for (var company
        in pickupSelectedAddress.value.profileGroup?.company ?? []) {
      if (company.field == "invoiceLocationCode") {
        invoiceLocationCode = company.value;
        break; // Stop the loop once we find the matching field
      }
    }
    final parts = <String>[
      building,
      streetName,
      postalCode,
      locationName1,
      locationName2,
      locationName3,
      locationName4,
    ];

    formattedAddress = parts.where((part) => part.isNotEmpty).join(',');
    addressDetails.add(addToCartSellIn.AddressDetails(
      type: "PICK_UP_ADDRESS",
      locationCode: locationCode,
      invoiceLocationCode: invoiceLocationCode,
      entityId: entityId,
      entityName: entityName,
      building: building,
      buildingNe: buildingNe,
      lattitude: latitude,
      longitude: longitude,
      streetName: streetName,
      postalCode: postalCode,
      locationName1: locationName1,
      locationName2: locationName2,
      locationName3: locationName3,
      locationName4: locationName4,
      streetNameNe: "",
      formattedAddress: formattedAddress,
      firstName: firstName,
      lastName: lastName,
      emailId: email,
      phone: phone,
    ));
  }

  shippingMethod.add(
    selectedDeliveryName.value == "pickup"
        ? addToCartSellIn.ShippingMethod(
            type: selectedDeliveryName.value == "pickup"
                ? "PICK_UP"
                : "DELIVERY",
            entityId: "",
            entityName: "",
            addressDetails: addressDetails,
          )
        : addToCartSellIn.ShippingMethod(
            type: selectedDeliveryName.value == "pickup"
                ? "PICK_UP"
                : "DELIVERY",
            deliveryType: deliveryShippingSelected.value,
            entityId: "",
            entityName: "",
            addressDetails: addressDetails,
          ),
  );

  // request.category = transactionType;
  // request.contextParameters = contextParameters;
  // request.relatedParty = relatedPart;
  // request.selectedCartoffers = selectedCartOffers;
  request.shippingMethod = shippingMethod;
  return request;
}