in applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartServices.java [698:996]
public static Map<String, Object> loadCartFromQuote(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String quoteId = (String) context.get("quoteId");
String applyQuoteAdjustmentsString = (String) context.get("applyQuoteAdjustments");
Locale locale = (Locale) context.get("locale");
boolean applyQuoteAdjustments = applyQuoteAdjustmentsString == null || "true".equals(applyQuoteAdjustmentsString);
// get the quote header
GenericValue quote = null;
try {
quote = EntityQuery.use(delegator).from("Quote").where("quoteId", quoteId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
// initial require cart info
String productStoreId = quote.getString("productStoreId");
String currency = quote.getString("currencyUomId");
// create the cart
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, currency);
// set shopping cart type
if ("PURCHASE_QUOTE".equals(quote.getString("quoteTypeId"))) {
cart.setOrderType("PURCHASE_ORDER");
cart.setBillFromVendorPartyId(quote.getString("partyId"));
}
try {
cart.setUserLogin(userLogin, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
cart.setQuoteId(quoteId);
cart.setOrderName(quote.getString("quoteName"));
cart.setChannelType(quote.getString("salesChannelEnumId"));
List<GenericValue> quoteItems = null;
List<GenericValue> quoteAdjs = null;
List<GenericValue> quoteRoles = null;
List<GenericValue> quoteAttributes = null;
List<GenericValue> quoteTerms = null;
try {
quoteItems = quote.getRelated("QuoteItem", null, UtilMisc.toList("quoteItemSeqId"), false);
quoteAdjs = quote.getRelated("QuoteAdjustment", null, null, false);
quoteRoles = quote.getRelated("QuoteRole", null, null, false);
quoteAttributes = quote.getRelated("QuoteAttribute", null, null, false);
quoteTerms = quote.getRelated("QuoteTerm", null, null, false);
} catch (GenericEntityException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
// set the role information
cart.setOrderPartyId(quote.getString("partyId"));
if (UtilValidate.isNotEmpty(quoteRoles)) {
for (GenericValue quoteRole : quoteRoles) {
String quoteRoleTypeId = quoteRole.getString("roleTypeId");
String quoteRolePartyId = quoteRole.getString("partyId");
if ("PLACING_CUSTOMER".equals(quoteRoleTypeId)) {
cart.setPlacingCustomerPartyId(quoteRolePartyId);
} else if ("BILL_TO_CUSTOMER".equals(quoteRoleTypeId)) {
cart.setBillToCustomerPartyId(quoteRolePartyId);
} else if ("SHIP_TO_CUSTOMER".equals(quoteRoleTypeId)) {
cart.setShipToCustomerPartyId(quoteRolePartyId);
} else if ("END_USER_CUSTOMER".equals(quoteRoleTypeId)) {
cart.setEndUserCustomerPartyId(quoteRolePartyId);
} else if ("BILL_FROM_VENDOR".equals(quoteRoleTypeId)) {
cart.setBillFromVendorPartyId(quoteRolePartyId);
} else {
cart.addAdditionalPartyRole(quoteRolePartyId, quoteRoleTypeId);
}
}
}
// set the order term
if (UtilValidate.isNotEmpty(quoteTerms)) {
// create order term from quote term
for (GenericValue quoteTerm : quoteTerms) {
BigDecimal termValue = BigDecimal.ZERO;
if (UtilValidate.isNotEmpty(quoteTerm.getString("termValue"))) {
termValue = new BigDecimal(quoteTerm.getString("termValue"));
}
long termDays = 0;
if (UtilValidate.isNotEmpty(quoteTerm.getString("termDays"))) {
termDays = Long.parseLong(quoteTerm.getString("termDays").trim());
}
String orderItemSeqId = quoteTerm.getString("quoteItemSeqId");
cart.addOrderTerm(quoteTerm.getString("termTypeId"), orderItemSeqId, termValue, termDays, quoteTerm.getString("textValue"),
quoteTerm.getString("description"));
}
}
// set the attribute information
if (UtilValidate.isNotEmpty(quoteAttributes)) {
for (GenericValue quoteAttribute : quoteAttributes) {
cart.setOrderAttribute(quoteAttribute.getString("attrName"), quoteAttribute.getString("attrValue"));
}
}
// Convert the quote adjustment to order header adjustments and
// put them in a map: the key/values pairs are quoteItemSeqId/List of adjs
Map<String, List<GenericValue>> orderAdjsMap = new HashMap<>();
for (GenericValue quoteAdj : quoteAdjs) {
List<GenericValue> orderAdjs = orderAdjsMap.get(UtilValidate.isNotEmpty(quoteAdj.getString("quoteItemSeqId")) ? quoteAdj.getString(
"quoteItemSeqId") : quoteId);
if (orderAdjs == null) {
orderAdjs = new LinkedList<>();
orderAdjsMap.put(UtilValidate.isNotEmpty(quoteAdj.getString("quoteItemSeqId")) ? quoteAdj.getString("quoteItemSeqId") : quoteId,
orderAdjs);
}
// convert quote adjustments to order adjustments
GenericValue orderAdj = delegator.makeValue("OrderAdjustment");
orderAdj.put("orderAdjustmentId", quoteAdj.get("quoteAdjustmentId"));
orderAdj.put("orderAdjustmentTypeId", quoteAdj.get("quoteAdjustmentTypeId"));
orderAdj.put("orderItemSeqId", quoteAdj.get("quoteItemSeqId"));
orderAdj.put("comments", quoteAdj.get("comments"));
orderAdj.put("description", quoteAdj.get("description"));
orderAdj.put("amount", quoteAdj.get("amount"));
orderAdj.put("productPromoId", quoteAdj.get("productPromoId"));
orderAdj.put("productPromoRuleId", quoteAdj.get("productPromoRuleId"));
orderAdj.put("productPromoActionSeqId", quoteAdj.get("productPromoActionSeqId"));
orderAdj.put("productFeatureId", quoteAdj.get("productFeatureId"));
orderAdj.put("correspondingProductId", quoteAdj.get("correspondingProductId"));
orderAdj.put("sourceReferenceId", quoteAdj.get("sourceReferenceId"));
orderAdj.put("sourcePercentage", quoteAdj.get("sourcePercentage"));
orderAdj.put("customerReferenceId", quoteAdj.get("customerReferenceId"));
orderAdj.put("primaryGeoId", quoteAdj.get("primaryGeoId"));
orderAdj.put("secondaryGeoId", quoteAdj.get("secondaryGeoId"));
orderAdj.put("exemptAmount", quoteAdj.get("exemptAmount"));
orderAdj.put("taxAuthGeoId", quoteAdj.get("taxAuthGeoId"));
orderAdj.put("taxAuthPartyId", quoteAdj.get("taxAuthPartyId"));
orderAdj.put("overrideGlAccountId", quoteAdj.get("overrideGlAccountId"));
orderAdj.put("includeInTax", quoteAdj.get("includeInTax"));
orderAdj.put("includeInShipping", quoteAdj.get("includeInShipping"));
orderAdj.put("createdDate", quoteAdj.get("createdDate"));
orderAdj.put("createdByUserLogin", quoteAdj.get("createdByUserLogin"));
orderAdjs.add(orderAdj);
}
long nextItemSeq = 0;
if (UtilValidate.isNotEmpty(quoteItems)) {
Pattern pattern = Pattern.compile("\\P{Digit}");
for (GenericValue quoteItem : quoteItems) {
// get the next item sequence id
String orderItemSeqId = quoteItem.getString("quoteItemSeqId");
Matcher pmatcher = pattern.matcher(orderItemSeqId);
orderItemSeqId = pmatcher.replaceAll("");
try {
long seq = Long.parseLong(orderItemSeqId);
if (seq > nextItemSeq) {
nextItemSeq = seq;
}
} catch (NumberFormatException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
boolean isPromo = quoteItem.get("isPromo") != null && "Y".equals(quoteItem.getString("isPromo"));
if (isPromo && !applyQuoteAdjustments) {
// do not include PROMO items
continue;
}
// not a promo item; go ahead and add it in
BigDecimal amount = quoteItem.getBigDecimal("selectedAmount");
if (amount == null) {
amount = BigDecimal.ZERO;
}
BigDecimal quantity = quoteItem.getBigDecimal("quantity");
if (quantity == null) {
quantity = BigDecimal.ZERO;
}
BigDecimal quoteUnitPrice = quoteItem.getBigDecimal("quoteUnitPrice");
if (quoteUnitPrice == null) {
quoteUnitPrice = BigDecimal.ZERO;
}
if (amount.compareTo(BigDecimal.ZERO) > 0) {
// If, in the quote, an amount is set, we need to
// pass to the cart the quoteUnitPrice/amount value.
quoteUnitPrice = quoteUnitPrice.divide(amount, GEN_ROUNDING);
}
//rental product data
Timestamp reservStart = quoteItem.getTimestamp("reservStart");
BigDecimal reservLength = quoteItem.getBigDecimal("reservLength");
BigDecimal reservPersons = quoteItem.getBigDecimal("reservPersons");
int itemIndex = -1;
if (quoteItem.get("productId") == null) {
// non-product item
String desc = quoteItem.getString("comments");
try {
// note that passing in null for itemGroupNumber as there is no real grouping concept in the quotes right now
itemIndex = cart.addNonProductItem(null, desc, null, null, quantity, null, null, null, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
} else {
// product item
String productId = quoteItem.getString("productId");
ProductConfigWrapper configWrapper = null;
if (UtilValidate.isNotEmpty(quoteItem.getString("configId"))) {
configWrapper = ProductConfigWorker.loadProductConfigWrapper(delegator, dispatcher, quoteItem.getString("configId"),
productId, productStoreId, null, null, currency, locale, userLogin);
}
try {
itemIndex = cart.addItemToEnd(productId, amount, quantity, quoteUnitPrice, reservStart, reservLength, reservPersons, null,
null, null, null, null, configWrapper, null, dispatcher, !applyQuoteAdjustments,
quoteUnitPrice.compareTo(BigDecimal.ZERO) == 0, Boolean.FALSE, Boolean.FALSE);
} catch (ItemNotFoundException | CartItemModifyException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
}
// flag the item w/ the orderItemSeqId so we can reference it
ShoppingCartItem cartItem = cart.findCartItem(itemIndex);
cartItem.setOrderItemSeqId(orderItemSeqId);
// attach additional item information
cartItem.setItemComment(quoteItem.getString("comments"));
cartItem.setQuoteId(quoteItem.getString("quoteId"));
cartItem.setQuoteItemSeqId(quoteItem.getString("quoteItemSeqId"));
cartItem.setIsPromo(isPromo);
}
}
// If applyQuoteAdjustments is set to false then standard cart adjustments are used.
if (applyQuoteAdjustments) {
// The cart adjustments, derived from quote adjustments, are added to the cart
// Tax adjustments should be added to the shipping group and shipping group item info
// Other adjustments like promotional price should be added to the cart independent of
// the ship group.
// We're creating the cart right now using data from the quote, so there cannot yet be more than one ship group.
List<GenericValue> cartAdjs = cart.getAdjustments();
CartShipInfo shipInfo = cart.getShipInfo(0);
List<GenericValue> adjs = orderAdjsMap.get(quoteId);
if (adjs != null) {
for (GenericValue adj : adjs) {
if (isTaxAdjustment(adj)) {
shipInfo.addShipTaxAdj(adj);
} else {
cartAdjs.add(adj);
}
}
}
// The cart item adjustments, derived from quote item adjustments, are added to the cart
if (quoteItems != null) {
for (ShoppingCartItem item : cart) {
String orderItemSeqId = item.getOrderItemSeqId();
if (orderItemSeqId != null) {
adjs = orderAdjsMap.get(orderItemSeqId);
} else {
adjs = null;
}
if (adjs != null) {
for (GenericValue adj : adjs) {
if (isTaxAdjustment(adj)) {
CartShipItemInfo csii = shipInfo.getShipItemInfo(item);
if (csii.getItemTaxAdj() == null) {
shipInfo.setItemInfo(item, UtilMisc.toList(adj));
} else {
csii.getItemTaxAdj().add(adj);
}
} else {
item.addAdjustment(adj);
}
}
}
}
}
}
// set the item seq in the cart
if (nextItemSeq > 0) {
try {
cart.setNextItemSeq(nextItemSeq + 1);
} catch (GeneralException e) {
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("shoppingCart", cart);
return result;
}