use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem in project BroadleafCommerce by BroadleafCommerce.
the class BandedPriceFulfillmentTest method createCandidateOrder.
/**
* @param total - this number divided by the number of items to create is the value of either the weight or the price
* (depending on which <b>option</b> is being passed in) for a single order item. Note that the final price of each item
* will be: (<b>total</b> / <b>orderItemsToCreate</b>) * <b>quantity</b>
* @param orderItemsToCreate - the number of order items to split the retail total across
* @param flatRates - the flat rates to assign to the OrderItems that are created. To have an Order that is mixed between OrderItems and
* DiscreteOrderItems (which are created for flat rates) ensure that the size of this array is less than <b>orderItemsToCreate</b>
* @param quantities - the quantities to assign to each OrderItem. If specified, this should be equal to the number of
* items to create
* @param option - the option to associate with the flat rates
* @return
*/
protected Order createCandidateOrder(BigDecimal total, int orderItemsToCreate, String[] flatRates, int[] quantities, FulfillmentOption option) {
if (flatRates != null && flatRates.length > orderItemsToCreate) {
throw new IllegalStateException("Flat rates for Skus should be less than or equal to the number of order items being created");
}
if (quantities != null && quantities.length != orderItemsToCreate) {
throw new IllegalStateException("Quantities for Skus should be less than or equal to the number of order items being created");
}
Order result = new OrderImpl();
List<FulfillmentGroupItem> fulfillmentItems = new ArrayList<FulfillmentGroupItem>();
for (int i = 0; i < orderItemsToCreate; i++) {
DiscreteOrderItem orderItem = new DiscreteOrderItemImpl();
Sku sku = new SkuImpl();
// set the sku price to some arbitrary amount - won't matter because the test is based on order item price
sku.setRetailPrice(new Money("1"));
orderItem.setSku(sku);
if (flatRates != null && i < flatRates.length) {
sku.getFulfillmentFlatRates().put(option, new BigDecimal(flatRates[i]));
}
if (option instanceof BandedPriceFulfillmentOption) {
orderItem.setPrice(new Money(total.divide(new BigDecimal(orderItemsToCreate))));
} else if (option instanceof BandedWeightFulfillmentOption) {
Weight weight = new Weight();
weight.setWeight(total.divide(new BigDecimal(orderItemsToCreate)));
weight.setWeightUnitOfMeasure(WeightUnitOfMeasureType.POUNDS);
orderItem.getSku().setWeight(weight);
orderItem.setPrice(new Money(BigDecimal.ZERO));
}
orderItem.setOrder(result);
FulfillmentGroupItem fulfillmentItem = new FulfillmentGroupItemImpl();
fulfillmentItem.setOrderItem(orderItem);
if (quantities == null) {
fulfillmentItem.setQuantity(1);
} else {
fulfillmentItem.setQuantity(quantities[i]);
}
fulfillmentItems.add(fulfillmentItem);
}
FulfillmentGroup group = new FulfillmentGroupImpl();
group.setOrder(result);
group.setFulfillmentGroupItems(fulfillmentItems);
List<FulfillmentGroup> groups = new ArrayList<FulfillmentGroup>();
groups.add(group);
result.setFulfillmentGroups(groups);
return result;
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentItemPricingActivityTest method testDistributeOneDollarAcrossFiveItems.
public void testDistributeOneDollarAcrossFiveItems() throws Exception {
Order order = dataProvider.createBasicOrder();
OrderAdjustment adjustment = new OrderAdjustmentImpl();
adjustment.setValue(new Money(new BigDecimal("1"), order.getCurrency()));
adjustment.setOrder(order);
order.getOrderAdjustments().add(adjustment);
ProcessContext<Order> context = new DefaultProcessContextImpl<Order>();
context.setSeedData(order);
fulfillmentItemPricingActivity.execute(context);
Money adj1 = new Money(".31");
Money adj2 = new Money(".69");
for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
if (fulfillmentGroupItem.getSalePrice().equals(new Money("19.99"))) {
assertTrue(fulfillmentGroupItem.getProratedOrderAdjustmentAmount().equals(adj1));
} else {
assertTrue(fulfillmentGroupItem.getProratedOrderAdjustmentAmount().equals(adj2));
}
}
}
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentGroupServiceImpl method addItemToFulfillmentGroup.
@Override
public // @Transactional("blTransactionManager")
FulfillmentGroup addItemToFulfillmentGroup(FulfillmentGroupItemRequest fulfillmentGroupItemRequest, boolean priceOrder, boolean save) throws PricingException {
if (priceOrder && !save) {
throw new IllegalArgumentException("Pricing requires a save");
}
Order order = fulfillmentGroupItemRequest.getOrder();
OrderItem item = fulfillmentGroupItemRequest.getOrderItem();
FulfillmentGroup fulfillmentGroup = fulfillmentGroupItemRequest.getFulfillmentGroup();
if (order == null) {
if (item.getOrder() != null) {
order = item.getOrder();
} else {
throw new IllegalArgumentException("Order must not be null");
}
}
// 1) Find the order item's existing fulfillment group, if any
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
Iterator<FulfillmentGroupItem> itr = fg.getFulfillmentGroupItems().iterator();
while (itr.hasNext()) {
FulfillmentGroupItem fgItem = itr.next();
if (fgItem.getOrderItem().equals(item)) {
// 2) remove item from it's existing fulfillment group
itr.remove();
fulfillmentGroupItemDao.delete(fgItem);
}
}
}
if (fulfillmentGroup == null) {
// API user is trying to add an item to a fulfillment group not created
fulfillmentGroup = fulfillmentGroupDao.create();
FulfillmentGroupRequest fgRequest = new FulfillmentGroupRequest();
fgRequest.setOrder(order);
fulfillmentGroup = addFulfillmentGroupToOrder(fgRequest, false);
fulfillmentGroup = save(fulfillmentGroup);
order.getFulfillmentGroups().add(fulfillmentGroup);
}
FulfillmentGroupItem fgi = createFulfillmentGroupItemFromOrderItem(item, fulfillmentGroup, fulfillmentGroupItemRequest.getQuantity());
if (save) {
fgi = fulfillmentGroupItemDao.save(fgi);
}
// 3) add the item to the new fulfillment group
fulfillmentGroup.addFulfillmentGroupItem(fgi);
if (save) {
order = orderService.save(order, priceOrder);
}
return fulfillmentGroup;
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentGroupServiceImpl method removeOrderItemFromFullfillmentGroups.
@Override
@Transactional("blTransactionManager")
public void removeOrderItemFromFullfillmentGroups(Order order, OrderItem orderItem) {
List<FulfillmentGroup> fulfillmentGroups = order.getFulfillmentGroups();
for (FulfillmentGroup fulfillmentGroup : fulfillmentGroups) {
Iterator<FulfillmentGroupItem> itr = fulfillmentGroup.getFulfillmentGroupItems().iterator();
while (itr.hasNext()) {
FulfillmentGroupItem fulfillmentGroupItem = itr.next();
if (fulfillmentGroupItem.getOrderItem().equals(orderItem)) {
itr.remove();
fulfillmentGroupItemDao.delete(fulfillmentGroupItem);
} else if (orderItem instanceof BundleOrderItem) {
BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
for (DiscreteOrderItem discreteOrderItem : bundleOrderItem.getDiscreteOrderItems()) {
if (fulfillmentGroupItem.getOrderItem().equals(discreteOrderItem)) {
itr.remove();
fulfillmentGroupItemDao.delete(fulfillmentGroupItem);
break;
}
}
}
}
}
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroupItem in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentGroupServiceImpl method getFulfillmentGroupItemsForOrderItem.
@Override
public List<FulfillmentGroupItem> getFulfillmentGroupItemsForOrderItem(Order order, OrderItem orderItem) {
List<FulfillmentGroupItem> fgis = new ArrayList<FulfillmentGroupItem>();
List<FulfillmentGroup> fulfillmentGroups = order.getFulfillmentGroups();
for (FulfillmentGroup fulfillmentGroup : fulfillmentGroups) {
for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
if (fulfillmentGroupItem.getOrderItem().equals(orderItem)) {
fgis.add(fulfillmentGroupItem);
} else if (orderItem instanceof BundleOrderItem) {
BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItem;
for (DiscreteOrderItem discreteOrderItem : bundleOrderItem.getDiscreteOrderItems()) {
if (fulfillmentGroupItem.getOrderItem().equals(discreteOrderItem)) {
fgis.add(fulfillmentGroupItem);
break;
}
}
}
}
}
return fgis;
}
Aggregations