Author: jacopoc
Date: Sun Aug 12 13:12:36 2007 New Revision: 565129 URL: http://svn.apache.org/viewvc?view=rev&rev=565129 Log: Implemented service to create several test sales orders with random products in them. Modified: ofbiz/trunk/applications/order/servicedef/services.xml ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java Modified: ofbiz/trunk/applications/order/servicedef/services.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/servicedef/services.xml?view=diff&rev=565129&r1=565128&r2=565129 ============================================================================== --- ofbiz/trunk/applications/order/servicedef/services.xml (original) +++ ofbiz/trunk/applications/order/servicedef/services.xml Sun Aug 12 13:12:36 2007 @@ -813,4 +813,16 @@ <attribute name="orderId" type="String" mode="IN" optional="false"/> <attribute name="isBackOrder" type="Boolean" mode="OUT" optional="false"/> </service> + + <service name="createTestSalesOrders" engine="java" auth="true" transaction-timeout="300" + location="org.ofbiz.order.order.OrderServices" invoke="createTestSalesOrders"> + <description>Bulk create test sales orders.</description> + <attribute name="productCategoryId" type="String" mode="IN" optional="false"/> + <attribute name="productStoreId" type="String" mode="IN" optional="false"/> + <attribute name="currencyUomId" type="String" mode="IN" optional="false"/> + <attribute name="partyId" type="String" mode="IN" optional="false"/> + <attribute name="numberOfOrders" type="Integer" mode="IN" optional="false"/> + <attribute name="numberOfProductsPerOrder" type="Integer" mode="IN" optional="false"/> + <attribute name="salesChannel" type="String" mode="IN" optional="true"/> + </service> </services> Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java?view=diff&rev=565129&r1=565128&r2=565129 ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderServices.java Sun Aug 12 13:12:36 2007 @@ -4351,7 +4351,7 @@ if (orderItemSeqId.equals(orderItem.get("orderItemSeqId"))) { invoicedQuantity = invoicedQuantity.add(quantity); invoicedTotal = invoicedTotal.add(quantity.multiply(amount)); - } + } } // Retrieve the adjustments for this item @@ -4427,5 +4427,89 @@ result.put("invoicedAmount", orderItemTotalValue.setScale(orderDecimals, orderRounding)); result.put("invoicedQuantity", invoicedQuantity.setScale(orderDecimals, orderRounding)); return result; + } + + public static Map createTestSalesOrders(DispatchContext dctx, Map context) { + LocalDispatcher dispatcher = dctx.getDispatcher(); + GenericDelegator delegator = dctx.getDelegator(); + Locale locale = (Locale) context.get("locale"); + GenericValue userLogin = (GenericValue) context.get("userLogin"); + + String productCategoryId = (String) context.get("productCategoryId"); + String productStoreId = (String) context.get("productStoreId"); + String currencyUomId = (String) context.get("currencyUomId"); + String partyId = (String) context.get("partyId"); + Integer numberOfOrders = (Integer) context.get("numberOfOrders"); + Integer numberOfProductsPerOrder = (Integer) context.get("numberOfProductsPerOrder"); + String salesChannel = (String) context.get("salesChannel"); + if (UtilValidate.isEmpty(salesChannel)) { + salesChannel = "WEB_SALES_CHANNEL"; + } + + List productsList = FastList.newInstance(); + try { + Map result = dispatcher.runSync("getProductCategoryMembers", UtilMisc.toMap("categoryId", productCategoryId)); + if (result.get("categoryMembers") != null) { + List productCategoryMembers = (List)result.get("categoryMembers"); + if (productCategoryMembers != null) { + Iterator i = productCategoryMembers.iterator(); + while (i.hasNext()) { + GenericValue prodCatMemb = (GenericValue) i.next(); + if (prodCatMemb != null) { + productsList.add(prodCatMemb.getString("productId")); + } + } + } + } + } catch (Exception e) { + return ServiceUtil.returnError("The following error occurred: " + e.getMessage()); + } + if (productsList.size() == 0) { + return ServiceUtil.returnError("No products found in category [" + productCategoryId + "]; no orders will be created"); + } + + + Random r = new Random(); + int numberOfOrdersInt = numberOfOrders.intValue(); + int numberOfProductsPerOrderInt = numberOfProductsPerOrder.intValue(); + for (int i = 1; i <= numberOfOrdersInt; i++) { + ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, currencyUomId); + cart.setOrderType("SALES_ORDER"); + cart.setChannelType(salesChannel); + cart.setProductStoreId(productStoreId); + + cart.setBillToCustomerPartyId(partyId); + cart.setPlacingCustomerPartyId(partyId); + cart.setShipToCustomerPartyId(partyId); + cart.setEndUserCustomerPartyId(partyId); + try { + cart.setUserLogin(userLogin, dispatcher); + } catch (Exception exc) { + Debug.logWarning("Error setting userLogin in the cart: " + exc.getMessage(), module); + } + for (int j = 1; j <= numberOfProductsPerOrderInt; j++) { + // get a product + int k = r.nextInt(productsList.size()); + try { + cart.addOrIncreaseItem((String)productsList.get(k), null, 1, null, null, null, + null, null, null, null, + null /*catalogId*/, null, null/*itemType*/, null/*itemGroupNumber*/, null, dispatcher); + } catch (Exception exc) { + Debug.logWarning("Error adding product with id " + (String)productsList.get(k) + " to the cart: " + exc.getMessage(), module); + } + } + cart.setDefaultCheckoutOptions(dispatcher); + CheckOutHelper checkout = new CheckOutHelper(dispatcher, delegator, cart); + Map orderCreate = checkout.createOrder(userLogin); + String orderId = (String)orderCreate.get("orderId"); + + // approve the order + if (UtilValidate.isNotEmpty(orderId)) { + Debug.logInfo("Created test order with id: " + orderId, module); + boolean approved = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId); + Debug.logInfo("Test order with id: " + orderId + " has been approved: " + approved, module); + } + } + return ServiceUtil.returnSuccess(); } } |
Free forum by Nabble | Edit this page |