Search in sources :

Example 11 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class OfferCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    addIsActiveFiltering(cto);
    DynamicResultSet resultSet = helper.getCompatibleModule(OperationType.BASIC).fetch(persistencePackage, cto);
    String customCriteria = "";
    if (persistencePackage.getCustomCriteria().length > 0) {
        customCriteria = persistencePackage.getCustomCriteria()[0];
    }
    for (Entity entity : resultSet.getRecords()) {
        Property discountType = entity.findProperty("discountType");
        Property discountValue = entity.findProperty("value");
        String value = discountValue.getValue();
        if (discountType == null || StringUtils.isBlank(discountType.getValue())) {
            discountValue.setValue("");
        } else if (discountType.getValue().equals("PERCENT_OFF")) {
            value = !value.contains(".") ? value : value.replaceAll("0*$", "").replaceAll("\\.$", "");
            discountValue.setValue(value + "%");
        } else if (discountType.getValue().equals("AMOUNT_OFF")) {
            Locale locale = BroadleafRequestContext.getBroadleafRequestContext().getLocale();
            BroadleafCurrency currency = BroadleafRequestContext.getBroadleafRequestContext().getBroadleafCurrency();
            NumberFormat nf = BroadleafCurrencyUtils.getNumberFormatFromCache(locale.getJavaLocale(), currency.getJavaCurrency());
            discountValue.setValue(nf.format(new BigDecimal(value)));
        }
        Property timeRule = entity.findProperty("offerMatchRules---TIME");
        entity.addProperty(buildAdvancedVisibilityOptionsProperty(timeRule));
        Property offerItemQualifierRuleType = entity.findProperty(OFFER_ITEM_QUALIFIER_RULE_TYPE);
        entity.addProperty(buildQualifiersCanBeQualifiersProperty(offerItemQualifierRuleType));
        entity.addProperty(buildQualifiersCanBeTargetsProperty(offerItemQualifierRuleType));
        Property offerItemTargetRuleType = entity.findProperty(OFFER_ITEM_TARGET_RULE_TYPE);
        entity.addProperty(buildStackableProperty(offerItemTargetRuleType));
        if (!"listGridView".equals(customCriteria)) {
            String setValue = discountValue.getValue();
            setValue = setValue.replaceAll("\\%", "").replaceAll("\\$", "");
            discountValue.setValue(setValue);
        }
        addIsActiveStatus(helper, entity);
    }
    return resultSet;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) Entity(org.broadleafcommerce.openadmin.dto.Entity) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) Property(org.broadleafcommerce.openadmin.dto.Property) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat)

Example 12 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class SkuPricingPersistenceProvider method getCurrency.

@Override
protected Currency getCurrency(ExtractValueRequest extractValueRequest, Property property) {
    BroadleafCurrency bc = null;
    if (extractValueRequest.getEntity() instanceof Product && isDefaultSkuProperty(extractValueRequest, property)) {
        Product p = (Product) extractValueRequest.getEntity();
        bc = p.getDefaultSku().getCurrency();
    } else if (extractValueRequest.getEntity() instanceof Sku) {
        Sku s = (Sku) extractValueRequest.getEntity();
        bc = s.getCurrency();
    }
    if (bc == null) {
        BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
        return brc.getJavaCurrency();
    } else {
        return Currency.getInstance(bc.getCurrencyCode());
    }
}
Also used : BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 13 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class ValidateAddRequestActivity method hasSameCurrency.

protected boolean hasSameCurrency(OrderItemRequestDTO orderItemRequestDTO, CartOperationRequest request, Sku sku) {
    if (orderItemRequestDTO instanceof NonDiscreteOrderItemRequestDTO || sku == null || sku.getCurrency() == null) {
        return true;
    } else {
        BroadleafCurrency orderCurrency = request.getOrder().getCurrency();
        BroadleafCurrency skuCurrency = sku.getCurrency();
        return orderCurrency.equals(skuCurrency);
    }
}
Also used : BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) NonDiscreteOrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.NonDiscreteOrderItemRequestDTO)

Example 14 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCurrencyUtils method getUnitAmount.

/**
 * Returns the unit amount (e.g. .01 for US and all other 2 decimal currencies)
 *
 * @param blCurrency
 * @return
 */
public static Money getUnitAmount(BroadleafCurrency blCurrency) {
    Currency currency = getCurrency(blCurrency);
    BigDecimal divisor = new BigDecimal(Math.pow(10, currency.getDefaultFractionDigits()));
    BigDecimal unitAmount = new BigDecimal("1").divide(divisor);
    return new Money(unitAmount, currency);
}
Also used : Money(org.broadleafcommerce.common.money.Money) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) Currency(java.util.Currency) BigDecimal(java.math.BigDecimal)

Example 15 with BroadleafCurrency

use of org.broadleafcommerce.common.currency.domain.BroadleafCurrency in project BroadleafCommerce by BroadleafCommerce.

the class UpdateCartServiceImpl method copyCartToCurrentContext.

@Override
public UpdateCartResponse copyCartToCurrentContext(Order currentCart) {
    if (currentCart.getOrderItems() == null) {
        return null;
    }
    BroadleafCurrency currency = findActiveCurrency();
    if (currency == null) {
        return null;
    }
    // Reprice order logic
    List<OrderItemRequestDTO> itemsToReprice = new ArrayList<>();
    List<OrderItem> itemsToRemove = new ArrayList<>();
    List<OrderItem> itemsToReset = new ArrayList<>();
    boolean repriceOrder = true;
    for (OrderItem orderItem : currentCart.getOrderItems()) {
        // Lookup price in price list, if null, then add to itemsToRemove
        if (orderItem instanceof DiscreteOrderItem) {
            DiscreteOrderItem doi = (DiscreteOrderItem) orderItem;
            if (checkAvailabilityInLocale(doi, currency)) {
                OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                itemRequest.setProductId(doi.getProduct().getId());
                itemRequest.setQuantity(doi.getQuantity());
                itemsToReprice.add(itemRequest);
                itemsToReset.add(orderItem);
            } else {
                itemsToRemove.add(orderItem);
            }
        } else if (orderItem instanceof BundleOrderItem) {
            BundleOrderItem boi = (BundleOrderItem) orderItem;
            for (DiscreteOrderItem doi : boi.getDiscreteOrderItems()) {
                if (checkAvailabilityInLocale(doi, currency)) {
                    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO();
                    itemRequest.setProductId(doi.getProduct().getId());
                    itemRequest.setQuantity(doi.getQuantity());
                    itemsToReprice.add(itemRequest);
                    itemsToReset.add(orderItem);
                } else {
                    itemsToRemove.add(orderItem);
                }
            }
        }
    }
    for (OrderItem orderItem : itemsToReset) {
        try {
            currentCart = orderService.removeItem(currentCart.getId(), orderItem.getId(), false);
        } catch (RemoveFromCartException e) {
            LOG.error("Could not remove from cart.", e);
        }
    }
    for (OrderItemRequestDTO itemRequest : itemsToReprice) {
        try {
            currentCart = orderService.addItem(currentCart.getId(), itemRequest, false);
        } catch (AddToCartException e) {
            LOG.error("Could not add to cart.", e);
        }
    }
    // Reprice and save the cart
    try {
        currentCart = orderService.save(currentCart, repriceOrder);
    } catch (PricingException e) {
        LOG.error("Could not save cart.", e);
    }
    setSavedCurrency(currency);
    UpdateCartResponse updateCartResponse = new UpdateCartResponse();
    updateCartResponse.setRemovedItems(itemsToRemove);
    updateCartResponse.setOrder(currentCart);
    return updateCartResponse;
}
Also used : DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) ArrayList(java.util.ArrayList) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) UpdateCartResponse(org.broadleafcommerce.core.order.service.call.UpdateCartResponse) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) RemoveFromCartException(org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)

Aggregations

BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)15 Money (org.broadleafcommerce.common.money.Money)5 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)2 Currency (java.util.Currency)2 HashMap (java.util.HashMap)2 Query (javax.persistence.Query)2 BroadleafCurrencyImpl (org.broadleafcommerce.common.currency.domain.BroadleafCurrencyImpl)2 Locale (org.broadleafcommerce.common.locale.domain.Locale)2 Offer (org.broadleafcommerce.core.offer.domain.Offer)2 DataProvider (org.testng.annotations.DataProvider)2 NumberFormat (java.text.NumberFormat)1 List (java.util.List)1 BeanComparator (org.apache.commons.beanutils.BeanComparator)1 NullComparator (org.apache.commons.collections.comparators.NullComparator)1 ReverseComparator (org.apache.commons.collections.comparators.ReverseComparator)1 BroadleafRequestedCurrencyDto (org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)1 ClonePolicyCollectionOverride (org.broadleafcommerce.common.extensibility.jpa.clone.ClonePolicyCollectionOverride)1 CurrencyConversionService (org.broadleafcommerce.common.money.CurrencyConversionService)1 PaymentRequestDTO (org.broadleafcommerce.common.payment.dto.PaymentRequestDTO)1