Search in sources :

Example 16 with OrderImpl

use of org.broadleafcommerce.core.order.domain.OrderImpl 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;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) FulfillmentGroupImpl(org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl) DiscreteOrderItemImpl(org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl) ArrayList(java.util.ArrayList) BandedPriceFulfillmentOption(org.broadleafcommerce.core.order.fulfillment.domain.BandedPriceFulfillmentOption) BigDecimal(java.math.BigDecimal) Weight(org.broadleafcommerce.core.catalog.domain.Weight) Money(org.broadleafcommerce.common.money.Money) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) OrderImpl(org.broadleafcommerce.core.order.domain.OrderImpl) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Sku(org.broadleafcommerce.core.catalog.domain.Sku) BandedWeightFulfillmentOption(org.broadleafcommerce.core.order.fulfillment.domain.BandedWeightFulfillmentOption) FulfillmentGroupItemImpl(org.broadleafcommerce.core.order.domain.FulfillmentGroupItemImpl)

Example 17 with OrderImpl

use of org.broadleafcommerce.core.order.domain.OrderImpl in project BroadleafCommerce by BroadleafCommerce.

the class ResourcePurgeDaoImpl method buildCartQuery.

protected <T> TypedQuery<T> buildCartQuery(String[] names, OrderStatus[] statuses, Date dateCreatedMinThreshold, Boolean isPreview, Class<T> returnType, List<Long> excludedIds) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<T> criteria = builder.createQuery(returnType);
    Root<OrderImpl> root = criteria.from(OrderImpl.class);
    if (Long.class.equals(returnType)) {
        criteria.select((Selection<? extends T>) builder.count(root));
    } else {
        criteria.select((Selection<? extends T>) root);
    }
    List<Predicate> restrictions = new ArrayList<Predicate>();
    List<String> statusList = new ArrayList<String>();
    if (statuses != null) {
        for (OrderStatus status : statuses) {
            statusList.add(status.getType());
        }
    } else {
        statusList.add("IN_PROCESS");
    }
    restrictions.add(root.get("status").in(statusList));
    if (names != null) {
        restrictions.add(root.get("name").in(Arrays.asList(names)));
    }
    if (dateCreatedMinThreshold != null) {
        restrictions.add(builder.lessThan(root.get("auditable").get("dateCreated").as(Date.class), dateCreatedMinThreshold));
    }
    if (isPreview != null) {
        if (isPreview) {
            restrictions.add(builder.isTrue(root.get("previewable").get("isPreview").as(Boolean.class)));
        } else {
            restrictions.add(builder.or(builder.isNull(root.get("previewable").get("isPreview")), builder.isFalse(root.get("previewable").get("isPreview").as(Boolean.class))));
        }
    }
    if (excludedIds != null && excludedIds.size() > 0) {
        applyLimitedInClause(excludedIds, builder, root, restrictions);
    }
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    return em.createQuery(criteria);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) OrderStatus(org.broadleafcommerce.core.order.service.type.OrderStatus) OrderImpl(org.broadleafcommerce.core.order.domain.OrderImpl) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Aggregations

OrderImpl (org.broadleafcommerce.core.order.domain.OrderImpl)17 Order (org.broadleafcommerce.core.order.domain.Order)12 ArrayList (java.util.ArrayList)9 Money (org.broadleafcommerce.common.money.Money)9 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)6 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)5 Predicate (javax.persistence.criteria.Predicate)4 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)4 CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)4 Product (org.broadleafcommerce.core.catalog.domain.Product)4 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)4 Sku (org.broadleafcommerce.core.catalog.domain.Sku)4 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)4 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)4 NullOrderImpl (org.broadleafcommerce.core.order.domain.NullOrderImpl)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3 PromotableOrder (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder)3