}
public static Map<String, Object> EbayStoreCreateOrderShoppingCart(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
Map <String, Object> result = FastMap.newInstance();
String productStoreId = context.get("productStoreId").toString();
String defaultCurrencyUomId = null;
String payToPartyId = null;
String facilityId = null;
try {
if (productStoreId == null) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "ordersImportFromEbay.productStoreIdIsMandatory", locale));
} else {
GenericValue productStore = delegator.findByPrimaryKey("ProductStore", UtilMisc.toMap("productStoreId", productStoreId));
if (productStore != null) {
defaultCurrencyUomId = productStore.getString("defaultCurrencyUomId");
payToPartyId = productStore.getString("payToPartyId");
facilityId = productStore.getString("inventoryFacilityId");
} else {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "ordersImportFromEbay.productStoreIdIsMandatory", locale));
}
}
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, defaultCurrencyUomId);
// set the external id with the eBay Item Id
String externalId = (String) context.get("externalId");
cart.setOrderType("SALES_ORDER");
cart.setChannelType("EBAY_SALES_CHANNEL");
cart.setUserLogin(userLogin, dispatcher);
cart.setProductStoreId(productStoreId);
if (UtilValidate.isNotEmpty(facilityId)) {
cart.setFacilityId(facilityId);
}
String amountStr = (String) context.get("amountPaid");
BigDecimal amountPaid = BigDecimal.ZERO;
if (UtilValidate.isNotEmpty(amountStr)) {
amountPaid = new BigDecimal(amountStr);
}
cart.addPaymentAmount("EXT_EBAY", amountPaid, externalId, null, true, false, false);
Timestamp orderDate = UtilDateTime.nowTimestamp();
if (UtilValidate.isNotEmpty(context.get("createdDate"))) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date createdDate = dateFormat.parse((String) context.get("createdDate"));
orderDate = new Timestamp(createdDate.getTime());
}
cart.setOrderDate(orderDate);
// Before import the order from eBay to OFBiz is mandatory that the payment has be received
String paidTime = (String) context.get("paidTime");
if (UtilValidate.isEmpty(paidTime)) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "ordersImportFromEbay.paymentIsStillNotReceived", locale));
}
List<Map<String, Object>> orderItemList = UtilGenerics.checkList(context.get("orderItemList"));
Iterator<Map<String, Object>> orderItemIter = orderItemList.iterator();
while (orderItemIter.hasNext()) {
Map<String, Object> orderItem = orderItemIter.next();
addItem(cart, orderItem, dispatcher, delegator, 0);
}
// set partyId from
if (UtilValidate.isNotEmpty(payToPartyId)) {
cart.setBillFromVendorPartyId(payToPartyId);
}
Map<String, Object> shippingServiceSelectedCtx = UtilGenerics.checkMap(context.get("shippingServiceSelectedCtx"));
if (UtilValidate.isNotEmpty(shippingServiceSelectedCtx.get("shippingServiceCost"))) {
BigDecimal shippingAmount = new BigDecimal(shippingServiceSelectedCtx.get("shippingServiceCost").toString());
if (shippingAmount.doubleValue() > 0) {
GenericValue shippingAdjustment = EbayHelper.makeOrderAdjustment(delegator, "SHIPPING_CHARGES", cart.getOrderId(), null, null, shippingAmount.doubleValue(), 0.0);
if (shippingAdjustment != null) {
cart.addAdjustment(shippingAdjustment);
}
}
}
// Apply additional shipping costs as order adjustment
if (UtilValidate.isNotEmpty(shippingServiceSelectedCtx.get("shippingTotalAdditionalCost"))) {
BigDecimal shippingAdditionalCost = new BigDecimal(shippingServiceSelectedCtx.get("shippingTotalAdditionalCost").toString());
if (shippingAdditionalCost.doubleValue() > 0) {
GenericValue shippingAdjustment = EbayHelper.makeOrderAdjustment(delegator, "MISCELLANEOUS_CHARGE", cart.getOrderId(), null, null, shippingAdditionalCost.doubleValue(), 0.0);
if (shippingAdjustment != null) {
cart.addAdjustment(shippingAdjustment);
}
}
}
// Apply sales tax as order adjustment
Map<String, Object> shippingDetailsCtx = UtilGenerics.checkMap(context.get("shippingDetailsCtx"));
if (UtilValidate.isNotEmpty(shippingDetailsCtx.get("salesTaxAmount"))) {
BigDecimal salesTaxAmount = new BigDecimal(shippingDetailsCtx.get("salesTaxAmount").toString());
if (salesTaxAmount.doubleValue() > 0) {
double salesPercent = 0.0;
if (UtilValidate.isNotEmpty(shippingDetailsCtx.get("salesTaxPercent"))) {
salesPercent = new Double(shippingDetailsCtx.get("salesTaxPercent").toString()).doubleValue();
}
GenericValue salesTaxAdjustment = EbayHelper.makeOrderAdjustment(delegator, "SALES_TAX", cart.getOrderId(), null, null, salesTaxAmount.doubleValue(), salesPercent);
if (salesTaxAdjustment != null) {
cart.addAdjustment(salesTaxAdjustment);
}
}
}
Debug.logInfo("Importing new order from eBay", module);
// set partyId to
String partyId = null;
String contactMechId = null;
Map<String, Object> shippingAddressCtx = UtilGenerics.checkMap(context.get("shippingAddressCtx"));
if (UtilValidate.isNotEmpty(shippingAddressCtx)) {
String buyerName = (String) shippingAddressCtx.get("buyerName");
String firstName = buyerName.substring(0, buyerName.indexOf(" "));
String lastName = buyerName.substring(buyerName.indexOf(" ")+1);
String country = (String) shippingAddressCtx.get("shippingAddressCountry");
String state = (String) shippingAddressCtx.get("shippingAddressStateOrProvince");
String city = (String) shippingAddressCtx.get("shippingAddressCityName");
EbayHelper.correctCityStateCountry(dispatcher, shippingAddressCtx, city, state, country);
String shippingAddressStreet = null;
if (UtilValidate.isEmpty(shippingAddressCtx.get("shippingAddressStreet1"))) {
shippingAddressStreet = shippingAddressCtx.get("shippingAddressStreet").toString();
shippingAddressCtx.put("shippingAddressStreet1", shippingAddressStreet);
} else {
shippingAddressStreet = shippingAddressCtx.get("shippingAddressStreet1").toString();
}
List<GenericValue> shipInfo = PartyWorker.findMatchingPersonPostalAddresses(delegator, shippingAddressStreet,
(UtilValidate.isEmpty(shippingAddressCtx.get("shippingAddressStreet2")) ? null : shippingAddressCtx.get("shippingAddressStreet2").toString()), shippingAddressCtx.get("city").toString(), shippingAddressCtx.get("stateProvinceGeoId").toString(),
shippingAddressCtx.get("shippingAddressPostalCode").toString(), null, shippingAddressCtx.get("countryGeoId").toString(), firstName, null, lastName);
if (UtilValidate.isNotEmpty(shipInfo)) {
GenericValue first = EntityUtil.getFirst(shipInfo);
partyId = first.getString("partyId");
Debug.logInfo("Existing shipping address found for : (party: " + partyId + ")", module);
}
}
// If matching party not found then try to find partyId from PartyAttribute entity.
GenericValue partyAttribute = null;
if (UtilValidate.isNotEmpty(context.get("eiasTokenBuyer"))) {
partyAttribute = EntityUtil.getFirst(delegator.findByAnd("PartyAttribute", UtilMisc.toMap("attrValue", (String) context.get("eiasTokenBuyer"))));
if (UtilValidate.isNotEmpty(partyAttribute)) {
partyId = (String) partyAttribute.get("partyId");
}
}
// if we get a party, check its contact information.
if (UtilValidate.isNotEmpty(partyId)) {
Debug.logInfo("Found existing party associated to the eBay buyer: " + partyId, module);
GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
contactMechId = EbayHelper.setShippingAddressContactMech(dispatcher, delegator, party, userLogin, shippingAddressCtx);
String emailBuyer = (String) context.get("emailBuyer");
if (!(emailBuyer.equals("") || emailBuyer.equalsIgnoreCase("Invalid Request"))) {
EbayHelper.setEmailContactMech(dispatcher, delegator, party, userLogin, context);