Search in sources :

Example 16 with FulfillmentGroupItem

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

the class GoogleUniversalAnalyticsProcessor method getItemJs.

protected String getItemJs(Order order, String trackerPrefix) {
    StringBuffer sb = new StringBuffer();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            OrderItem orderItem = fulfillmentGroupItem.getOrderItem();
            Sku sku = ((SkuAccessor) orderItem).getSku();
            sb.append("ga('" + trackerPrefix + "ecommerce:addItem', {");
            sb.append("'id': '" + order.getOrderNumber() + "'");
            sb.append(",'name': '" + sku.getName() + "'");
            sb.append(",'sku': '" + sku.getId() + "'");
            sb.append(",'category': '" + getVariation(orderItem) + "'");
            sb.append(",'price': '" + orderItem.getAveragePrice() + "'");
            sb.append(",'quantity': '" + orderItem.getQuantity() + "'");
            sb.append("});");
        }
    }
    return sb.toString();
}
Also used : FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Sku(org.broadleafcommerce.core.catalog.domain.Sku) SkuAccessor(org.broadleafcommerce.core.order.domain.SkuAccessor)

Example 17 with FulfillmentGroupItem

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

the class FulfillmentGroupServiceImpl method createFulfillmentGroupItemFromOrderItem.

protected FulfillmentGroupItem createFulfillmentGroupItemFromOrderItem(OrderItem orderItem, FulfillmentGroup fulfillmentGroup, int quantity) {
    FulfillmentGroupItem fgi = fulfillmentGroupItemDao.create();
    fgi.setFulfillmentGroup(fulfillmentGroup);
    fgi.setOrderItem(orderItem);
    fgi.setQuantity(quantity);
    return fgi;
}
Also used : FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)

Example 18 with FulfillmentGroupItem

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

the class BandedShippingModule method calculateShipping.

private void calculateShipping(FulfillmentGroup fulfillmentGroup) {
    if (!isValidModuleForService(fulfillmentGroup.getService()) && !isDefaultModule()) {
        LOG.info("fulfillment group (" + fulfillmentGroup.getId() + ") with a service type of (" + fulfillmentGroup.getService() + ") is not valid for this module service type (" + getServiceName() + ")");
        return;
    }
    if (fulfillmentGroup.getFulfillmentGroupItems().size() == 0) {
        LOG.warn("fulfillment group (" + fulfillmentGroup.getId() + ") does not contain any fulfillment group items. Unable to price banded shipping");
        fulfillmentGroup.setShippingPrice(BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, fulfillmentGroup.getOrder().getCurrency()));
        fulfillmentGroup.setSaleShippingPrice(BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, fulfillmentGroup.getOrder().getCurrency()));
        fulfillmentGroup.setRetailShippingPrice(BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, fulfillmentGroup.getOrder().getCurrency()));
        return;
    }
    Address address = fulfillmentGroup.getAddress();
    String state = null;
    if (StringUtils.isNotBlank(address.getStateProvinceRegion())) {
        state = address.getStateProvinceRegion();
    } else if (address.getState() != null) {
        state = address.getState().getAbbreviation();
    }
    BigDecimal retailTotal = new BigDecimal(0);
    String feeType = feeTypeMapping.get(fulfillmentGroup.getMethod());
    String feeSubType = ((feeSubTypeMapping.get(state) == null) ? feeSubTypeMapping.get("ALL") : feeSubTypeMapping.get(state));
    for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
        BigDecimal price = (fulfillmentGroupItem.getRetailPrice() != null) ? fulfillmentGroupItem.getRetailPrice().getAmount().multiply(BigDecimal.valueOf(fulfillmentGroupItem.getQuantity())) : null;
        if (price == null) {
            price = fulfillmentGroupItem.getOrderItem().getRetailPrice().getAmount().multiply(BigDecimal.valueOf(fulfillmentGroupItem.getQuantity()));
        }
        retailTotal = retailTotal.add(price);
    }
    ShippingRate sr = shippingRateService.readShippingRateByFeeTypesUnityQty(feeType, feeSubType, retailTotal);
    if (sr == null) {
        throw new NotImplementedException("Shipping rate " + fulfillmentGroup.getMethod() + " not supported");
    }
    BigDecimal shippingPrice = new BigDecimal(0);
    if (sr.getBandResultPercent().compareTo(0) > 0) {
        BigDecimal percent = new BigDecimal(sr.getBandResultPercent() / 100);
        shippingPrice = retailTotal.multiply(percent);
    } else {
        shippingPrice = sr.getBandResultQuantity();
    }
    fulfillmentGroup.setShippingPrice(BroadleafCurrencyUtils.getMoney(shippingPrice, fulfillmentGroup.getOrder().getCurrency()));
    fulfillmentGroup.setSaleShippingPrice(fulfillmentGroup.getShippingPrice());
    fulfillmentGroup.setRetailShippingPrice(fulfillmentGroup.getSaleShippingPrice());
}
Also used : Address(org.broadleafcommerce.profile.core.domain.Address) ShippingRate(org.broadleafcommerce.core.pricing.domain.ShippingRate) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) NotImplementedException(org.apache.commons.lang.NotImplementedException) BigDecimal(java.math.BigDecimal)

Example 19 with FulfillmentGroupItem

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

the class SimpleTaxProvider method calculateTaxForOrder.

@Override
public Order calculateTaxForOrder(Order order, ModuleConfiguration config) throws TaxException {
    if (!order.getCustomer().isTaxExempt()) {
        for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
            // Set taxes on the fulfillment group items
            for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
                if (isItemTaxable(fgItem)) {
                    BigDecimal factor = determineItemTaxRate(fulfillmentGroup.getAddress());
                    if (factor != null && factor.compareTo(BigDecimal.ZERO) != 0) {
                        TaxDetail tax;
                        checkDetail: {
                            for (TaxDetail detail : fgItem.getTaxes()) {
                                if (detail.getType().equals(TaxType.COMBINED)) {
                                    tax = detail;
                                    break checkDetail;
                                }
                            }
                            tax = entityConfig.createEntityInstance(TaxDetail.class.getName(), TaxDetail.class);
                            tax.setType(TaxType.COMBINED);
                            fgItem.getTaxes().add(tax);
                        }
                        tax.setRate(factor);
                        tax.setAmount(fgItem.getTotalItemTaxableAmount().multiply(factor));
                    }
                }
            }
            for (FulfillmentGroupFee fgFee : fulfillmentGroup.getFulfillmentGroupFees()) {
                if (isFeeTaxable(fgFee)) {
                    BigDecimal factor = determineItemTaxRate(fulfillmentGroup.getAddress());
                    if (factor != null && factor.compareTo(BigDecimal.ZERO) != 0) {
                        TaxDetail tax;
                        checkDetail: {
                            for (TaxDetail detail : fgFee.getTaxes()) {
                                if (detail.getType().equals(TaxType.COMBINED)) {
                                    tax = detail;
                                    break checkDetail;
                                }
                            }
                            tax = entityConfig.createEntityInstance(TaxDetail.class.getName(), TaxDetail.class);
                            tax.setType(TaxType.COMBINED);
                            fgFee.getTaxes().add(tax);
                        }
                        tax.setRate(factor);
                        tax.setAmount(fgFee.getAmount().multiply(factor));
                    }
                }
            }
            BigDecimal factor = determineTaxRateForFulfillmentGroup(fulfillmentGroup);
            if (factor != null && factor.compareTo(BigDecimal.ZERO) != 0) {
                TaxDetail tax;
                checkDetail: {
                    for (TaxDetail detail : fulfillmentGroup.getTaxes()) {
                        if (detail.getType().equals(TaxType.COMBINED)) {
                            tax = detail;
                            break checkDetail;
                        }
                    }
                    tax = entityConfig.createEntityInstance(TaxDetail.class.getName(), TaxDetail.class);
                    tax.setType(TaxType.COMBINED);
                    fulfillmentGroup.getTaxes().add(tax);
                }
                tax.setRate(factor);
                tax.setAmount(fulfillmentGroup.getFulfillmentPrice().multiply(factor));
            }
        }
    }
    return order;
}
Also used : FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) TaxDetail(org.broadleafcommerce.core.order.domain.TaxDetail) FulfillmentGroupFee(org.broadleafcommerce.core.order.domain.FulfillmentGroupFee) BigDecimal(java.math.BigDecimal)

Example 20 with FulfillmentGroupItem

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

the class AutoBundleActivity method bundleItems.

/**
 * Builds a BundleOrderItem based on the passed in productBundle.    Creates new DiscreteOrderItems.
 * Removes the existing matching DiscreteOrderItems or modifies the quantity if needed.
 *
 * @param order
 * @param productBundle
 * @param numApplications
 * @throws PricingException
 * @throws ItemNotFoundException
 */
private Order bundleItems(Order order, ProductBundle productBundle, Integer numApplications, List<DiscreteOrderItem> unbundledItems) throws PricingException, RemoveFromCartException {
    BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemDao.create(OrderItemType.BUNDLE);
    bundleOrderItem.setQuantity(numApplications);
    bundleOrderItem.setCategory(productBundle.getDefaultCategory());
    bundleOrderItem.setSku(productBundle.getDefaultSku());
    bundleOrderItem.setName(productBundle.getName());
    bundleOrderItem.setProductBundle(productBundle);
    bundleOrderItem.setOrder(order);
    // make a copy of the fulfillment group items since they will be deleted when the order items are removed
    Map<Long, FulfillmentGroupItem> skuIdFulfillmentGroupMap = new HashMap<Long, FulfillmentGroupItem>();
    for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
        for (FulfillmentGroupItem fulfillmentGroupItem : fulfillmentGroup.getFulfillmentGroupItems()) {
            if (fulfillmentGroupItem.getOrderItem() instanceof DiscreteOrderItem) {
                DiscreteOrderItem discreteOrderItem = (DiscreteOrderItem) fulfillmentGroupItem.getOrderItem();
                skuIdFulfillmentGroupMap.put(discreteOrderItem.getSku().getId(), fulfillmentGroupItem);
            }
        }
    }
    for (SkuBundleItem skuBundleItem : productBundle.getSkuBundleItems()) {
        List<DiscreteOrderItem> itemMatches = new ArrayList<DiscreteOrderItem>();
        int skuMatches = populateItemMatchesForSku(itemMatches, order, unbundledItems, skuBundleItem.getSku().getId());
        int skusRequired = skuBundleItem.getQuantity() * numApplications;
        if (skuMatches < skusRequired) {
            throw new IllegalArgumentException("Something went wrong creating automatic bundles.  Not enough skus to fulfill bundle requirements for sku id: " + skuBundleItem.getSku().getId());
        }
        // this call also deletes any fulfillment group items that are associated with that order item
        for (DiscreteOrderItem item : itemMatches) {
            order = orderService.removeItem(order.getId(), item.getId(), false);
        }
        DiscreteOrderItem baseItem = null;
        if (itemMatches.size() > 0) {
            baseItem = itemMatches.get(0);
        } else {
            for (DiscreteOrderItem discreteOrderItem : unbundledItems) {
                if (discreteOrderItem.getSku().getId().equals(skuBundleItem.getSku().getId())) {
                    baseItem = discreteOrderItem;
                }
            }
        }
        // Add item to the skuBundle
        DiscreteOrderItem newSkuBundleItem = (DiscreteOrderItem) baseItem.clone();
        newSkuBundleItem.setSkuBundleItem(skuBundleItem);
        newSkuBundleItem.setBundleOrderItem(bundleOrderItem);
        newSkuBundleItem.setQuantity(skuBundleItem.getQuantity());
        newSkuBundleItem.setOrder(null);
        bundleOrderItem.getDiscreteOrderItems().add(newSkuBundleItem);
        if (skuMatches > skusRequired) {
            // Add a non-bundled item to the order with the remaining sku count.
            DiscreteOrderItem newOrderItem = (DiscreteOrderItem) baseItem.clone();
            newOrderItem.setBundleOrderItem(null);
            newOrderItem.setSkuBundleItem(null);
            newOrderItem.setQuantity(skuMatches - skusRequired);
            newOrderItem = (DiscreteOrderItem) orderItemDao.save(newOrderItem);
            newOrderItem.setOrder(order);
            newOrderItem.updateSaleAndRetailPrices();
            // Re-associate fulfillment group item to newOrderItem
            FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(newSkuBundleItem.getSku().getId());
            if (fulfillmentGroupItem != null) {
                FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
                newFulfillmentGroupItem.setOrderItem(newOrderItem);
                newFulfillmentGroupItem.setQuantity(newOrderItem.getQuantity());
                newFulfillmentGroupItem = fulfillmentGroupItemDao.save(newFulfillmentGroupItem);
                // these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
                for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                    if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
                        fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
                    }
                }
            }
            order.getOrderItems().add(newOrderItem);
        }
    }
    bundleOrderItem.updateSaleAndRetailPrices();
    order.getOrderItems().add(bundleOrderItem);
    order = orderService.save(order, false);
    for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem bundleItem = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem discreteOrderItem : bundleItem.getDiscreteOrderItems()) {
                // Re-associate fulfillment group item to newly created skuBundles
                FulfillmentGroupItem fulfillmentGroupItem = skuIdFulfillmentGroupMap.get(discreteOrderItem.getSku().getId());
                if (fulfillmentGroupItem != null) {
                    FulfillmentGroupItem newFulfillmentGroupItem = fulfillmentGroupItem.clone();
                    newFulfillmentGroupItem.setOrderItem(discreteOrderItem);
                    newFulfillmentGroupItem.setQuantity(discreteOrderItem.getQuantity());
                    // these associations may have not been committed yet. This order is used in other activities and will not be reloaded if in a transaction.
                    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
                        if (newFulfillmentGroupItem.getFulfillmentGroup() != null && fg.getId().equals(newFulfillmentGroupItem.getFulfillmentGroup().getId())) {
                            fg.addFulfillmentGroupItem(newFulfillmentGroupItem);
                        }
                    }
                }
            }
        }
    }
    // reload order with new fulfillment group items
    order = orderService.save(order, false);
    return order;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) SkuBundleItem(org.broadleafcommerce.core.catalog.domain.SkuBundleItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup)

Aggregations

FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)48 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)39 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)25 Order (org.broadleafcommerce.core.order.domain.Order)21 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)20 Money (org.broadleafcommerce.common.money.Money)18 ArrayList (java.util.ArrayList)16 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)15 Sku (org.broadleafcommerce.core.catalog.domain.Sku)12 Transactional (org.springframework.transaction.annotation.Transactional)11 FulfillmentGroupItemImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupItemImpl)9 Address (org.broadleafcommerce.profile.core.domain.Address)9 BigDecimal (java.math.BigDecimal)8 Test (org.testng.annotations.Test)8 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)7 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)7 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)7 HashMap (java.util.HashMap)6 AddressImpl (org.broadleafcommerce.profile.core.domain.AddressImpl)6 ISOCountry (org.broadleafcommerce.common.i18n.domain.ISOCountry)5