Search in sources :

Example 66 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class PromotableCandidateItemOfferImpl method calculateSavingsForOrderItem.

@Override
public Money calculateSavingsForOrderItem(PromotableOrderItem orderItem, int qtyToReceiveSavings) {
    Money savings = new Money(promotableOrder.getOrderCurrency());
    originalPrice = orderItem.getPriceBeforeAdjustments(getOffer().getApplyDiscountToSalePrice());
    BigDecimal offerUnitValue = PromotableOfferUtility.determineOfferUnitValue(offer, this);
    savings = PromotableOfferUtility.computeAdjustmentValue(originalPrice, offerUnitValue, this, this);
    return savings.multiply(qtyToReceiveSavings);
}
Also used : Money(org.broadleafcommerce.common.money.Money) BigDecimal(java.math.BigDecimal)

Example 67 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class PromotableCandidateOrderOfferImpl method calculatePotentialSavings.

protected void calculatePotentialSavings() {
    Money amountBeforeAdjustments = promotableOrder.calculateSubtotalWithoutAdjustments();
    potentialSavings = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, getCurrency());
    if (getOffer().getDiscountType().equals(OfferDiscountType.AMOUNT_OFF)) {
        potentialSavings = BroadleafCurrencyUtils.getMoney(getOffer().getValue(), getCurrency());
    } else if (getOffer().getDiscountType().equals(OfferDiscountType.FIX_PRICE)) {
        potentialSavings = amountBeforeAdjustments.subtract(BroadleafCurrencyUtils.getMoney(getOffer().getValue(), getCurrency()));
    } else if (getOffer().getDiscountType().equals(OfferDiscountType.PERCENT_OFF)) {
        potentialSavings = amountBeforeAdjustments.multiply(getOffer().getValue().divide(new BigDecimal("100")));
    }
    if (potentialSavings.greaterThan(amountBeforeAdjustments)) {
        potentialSavings = amountBeforeAdjustments;
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) BigDecimal(java.math.BigDecimal)

Example 68 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class ProductLinkedDataGeneratorImpl method addSkus.

protected void addSkus(final HttpServletRequest request, final Product product, final JSONObject productData, final String url) throws JSONException {
    final JSONArray offers = new JSONArray();
    final String currency = product.getRetailPrice().getCurrency().getCurrencyCode();
    BigDecimal highPrice = BigDecimal.ZERO;
    BigDecimal lowPrice = null;
    for (final Sku sku : product.getAllSellableSkus()) {
        final JSONObject offer = new JSONObject();
        offer.put("@type", "Offer");
        offer.put("sku", sku.getId());
        offer.put("priceCurrency", currency);
        offer.put("availability", determineAvailability(sku));
        offer.put("url", url);
        offer.put("category", product.getCategory().getName());
        if (sku.getActiveEndDate() != null) {
            offer.put("priceValidUntil", ISO_8601_FORMAT.format(sku.getActiveEndDate()));
        }
        final Money price = sku.getPriceData().getPrice();
        offer.put("price", price.getAmount());
        if (price.greaterThan(highPrice)) {
            highPrice = price.getAmount();
        }
        if (lowPrice == null || price.lessThan(lowPrice)) {
            lowPrice = price.getAmount();
        }
        extensionManager.getProxy().addSkuData(request, product, offer);
        offers.put(offer);
    }
    // use aggregateOffer to handle multiple sellable SKUs for a single product
    if (offers.length() > 1) {
        final JSONObject aggregateOffer = new JSONObject();
        aggregateOffer.put("@type", "AggregateOffer");
        aggregateOffer.put("highPrice", highPrice.doubleValue());
        aggregateOffer.put("lowPrice", lowPrice.doubleValue());
        aggregateOffer.put("priceCurrency", currency);
        aggregateOffer.put("offerCount", offers.length());
        aggregateOffer.put("offers", offers);
        extensionManager.getProxy().addAggregateSkuData(request, product, aggregateOffer);
        productData.put("offers", aggregateOffer);
    } else {
        productData.put("offers", offers);
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) Sku(org.broadleafcommerce.core.catalog.domain.Sku) BigDecimal(java.math.BigDecimal)

Example 69 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class CheckoutFormVariableExpression method shouldShowAllPaymentMethods.

/**
 * Toggle the Payment Info Section based on what payments were applied to the order
 *  (e.g. Third Party Account (i.e. PayPal Express) or Gift Cards/Customer Credit)
 */
public boolean shouldShowAllPaymentMethods() {
    Money orderTotalAfterAppliedPayments = CartState.getCart().getTotalAfterAppliedPayments();
    boolean totalCoveredByAppliedPayments = (orderTotalAfterAppliedPayments != null && orderTotalAfterAppliedPayments.isZero());
    return !cartStateService.cartHasThirdPartyPayment() && !totalCoveredByAppliedPayments;
}
Also used : Money(org.broadleafcommerce.common.money.Money)

Example 70 with Money

use of org.broadleafcommerce.common.money.Money in project BroadleafCommerce by BroadleafCommerce.

the class OnePageCheckoutProcessor method populateSectionViewStates.

/**
 * This method is responsible of populating the variables necessary to draw the checkout page.
 * This logic is highly dependent on your layout. If your layout does not follow the same flow
 * as the HeatClinic demo, you will need to override with your own custom layout implementation
 *
 * @param localVars
 */
protected void populateSectionViewStates(Map<String, Object> localVars) {
    boolean orderInfoPopulated = hasPopulatedOrderInfo(CartState.getCart());
    boolean billingPopulated = hasPopulatedBillingAddress(CartState.getCart());
    boolean shippingPopulated = hasPopulatedShippingAddress(CartState.getCart());
    localVars.put("orderInfoPopulated", orderInfoPopulated);
    localVars.put("billingPopulated", billingPopulated);
    localVars.put("shippingPopulated", shippingPopulated);
    // Logic to show/hide sections based on state of the order
    // show all sections including header unless specifically hidden
    // (e.g. hide shipping if no shippable items in order or hide billing section if the order payment doesn't need
    // an address i.e. PayPal Express)
    boolean showBillingInfoSection = true;
    boolean showShippingInfoSection = true;
    boolean showAllPaymentMethods = true;
    boolean showPaymentMethodSection = true;
    int numShippableFulfillmentGroups = calculateNumShippableFulfillmentGroups();
    if (numShippableFulfillmentGroups == 0) {
        showShippingInfoSection = false;
    }
    boolean orderContainsThirdPartyPayment = false;
    boolean orderContainsUnconfirmedCreditCard = false;
    OrderPayment unconfirmedCC = null;
    if (CartState.getCart().getPayments() != null) {
        for (OrderPayment payment : CartState.getCart().getPayments()) {
            if (payment.isActive() && PaymentType.THIRD_PARTY_ACCOUNT.equals(payment.getType())) {
                orderContainsThirdPartyPayment = true;
            }
            if (payment.isActive() && (PaymentType.CREDIT_CARD.equals(payment.getType()) && !PaymentGatewayType.TEMPORARY.equals(payment.getGatewayType()))) {
                orderContainsUnconfirmedCreditCard = true;
                unconfirmedCC = payment;
            }
        }
    }
    // Toggle the Payment Info Section based on what payments were applied to the order
    // (e.g. Third Party Account (i.e. PayPal Express) or Gift Cards/Customer Credit)
    Money orderTotalAfterAppliedPayments = CartState.getCart().getTotalAfterAppliedPayments();
    if (orderContainsThirdPartyPayment || orderContainsUnconfirmedCreditCard) {
        showBillingInfoSection = false;
        showAllPaymentMethods = false;
    } else if (orderTotalAfterAppliedPayments != null && orderTotalAfterAppliedPayments.isZero()) {
        // If all the applied payments (e.g. gift cards) cover the entire amount
        // we don't need to show all payment method options.
        showAllPaymentMethods = false;
    }
    localVars.put("showBillingInfoSection", showBillingInfoSection);
    localVars.put("showAllPaymentMethods", showAllPaymentMethods);
    localVars.put("showPaymentMethodSection", showPaymentMethodSection);
    localVars.put("orderContainsThirdPartyPayment", orderContainsThirdPartyPayment);
    localVars.put("orderContainsUnconfirmedCreditCard", orderContainsUnconfirmedCreditCard);
    localVars.put("unconfirmedCC", unconfirmedCC);
    // The Sections are all initialized to INACTIVE view
    List<CheckoutSectionDTO> drawnSections = new LinkedList<>();
    CheckoutSectionDTO orderInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.ORDER_INFO, orderInfoPopulated);
    CheckoutSectionDTO billingInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.BILLING_INFO, billingPopulated);
    CheckoutSectionDTO shippingInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.SHIPPING_INFO, shippingPopulated);
    CheckoutSectionDTO paymentInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.PAYMENT_INFO, false);
    String orderInfoHelpMessage = (String) localVars.get("orderInfoHelpMessage");
    String billingInfoHelpMessage = (String) localVars.get("billingInfoHelpMessage");
    String shippingInfoHelpMessage = (String) localVars.get("shippingInfoHelpMessage");
    // Add the Order Info Section
    drawnSections.add(orderInfoSection);
    // Add the Billing Section
    if (showBillingInfoSection) {
        billingInfoSection.setHelpMessage(orderInfoHelpMessage);
        drawnSections.add(billingInfoSection);
    }
    // Add the Shipping Section
    if (showShippingInfoSection) {
        if (showBillingInfoSection) {
            shippingInfoSection.setHelpMessage(billingInfoHelpMessage);
        } else {
            shippingInfoSection.setHelpMessage(orderInfoHelpMessage);
        }
        drawnSections.add(shippingInfoSection);
    }
    // Add the Payment Section
    if (showShippingInfoSection) {
        paymentInfoSection.setHelpMessage(shippingInfoHelpMessage);
    } else if (showBillingInfoSection) {
        paymentInfoSection.setHelpMessage(billingInfoHelpMessage);
    } else {
        paymentInfoSection.setHelpMessage(orderInfoHelpMessage);
    }
    drawnSections.add(paymentInfoSection);
    // Logic to toggle state between form view, saved view, and inactive view
    // This is dependent on the layout of your checkout form. Override this if layout is different.
    // initialize first view to always be a FORM view
    CheckoutSectionDTO firstSection = drawnSections.get(0);
    firstSection.setState(CheckoutSectionStateType.FORM);
    for (ListIterator<CheckoutSectionDTO> itr = drawnSections.listIterator(); itr.hasNext(); ) {
        CheckoutSectionDTO previousSection = null;
        if (itr.hasPrevious()) {
            previousSection = drawnSections.get(itr.previousIndex());
        }
        CheckoutSectionDTO section = itr.next();
        // if the previous section is populated, set this section to a Form View
        if (previousSection != null && previousSection.isPopulated()) {
            section.setState(CheckoutSectionStateType.FORM);
        }
        // If this sections is populated then set this section to the Saved View
        if (section.isPopulated()) {
            section.setState(CheckoutSectionStateType.SAVED);
        }
        // {@see DefaultPaymentGatewayCheckoutService where payments are invalidated on an unsuccessful transaction}
        if (CheckoutSectionViewType.PAYMENT_INFO.equals(section.getView())) {
            if (showBillingInfoSection && !billingPopulated) {
                section.setState(CheckoutSectionStateType.INACTIVE);
                section.setHelpMessage(billingInfoHelpMessage);
            }
        }
        // Finally, if the edit button is explicitly clicked, set the section to Form View
        BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
        HttpServletRequest request = blcContext.getRequest();
        boolean editOrderInfo = BooleanUtils.toBoolean(request.getParameter("edit-order-info"));
        boolean editBillingInfo = BooleanUtils.toBoolean(request.getParameter("edit-billing"));
        boolean editShippingInfo = BooleanUtils.toBoolean(request.getParameter("edit-shipping"));
        if (CheckoutSectionViewType.ORDER_INFO.equals(section.getView()) && editOrderInfo) {
            section.setState(CheckoutSectionStateType.FORM);
        } else if (CheckoutSectionViewType.BILLING_INFO.equals(section.getView()) && editBillingInfo) {
            section.setState(CheckoutSectionStateType.FORM);
        } else if (CheckoutSectionViewType.SHIPPING_INFO.equals(section.getView()) && editShippingInfo) {
            section.setState(CheckoutSectionStateType.FORM);
        }
    }
    localVars.put("checkoutSectionDTOs", drawnSections);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Money(org.broadleafcommerce.common.money.Money) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment) CheckoutSectionDTO(org.broadleafcommerce.core.web.checkout.section.CheckoutSectionDTO) LinkedList(java.util.LinkedList)

Aggregations

Money (org.broadleafcommerce.common.money.Money)158 Order (org.broadleafcommerce.core.order.domain.Order)43 BigDecimal (java.math.BigDecimal)38 Offer (org.broadleafcommerce.core.offer.domain.Offer)29 Test (org.testng.annotations.Test)29 Transactional (org.springframework.transaction.annotation.Transactional)24 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)22 ArrayList (java.util.ArrayList)21 CommonSetupBaseTest (org.broadleafcommerce.test.CommonSetupBaseTest)21 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)19 CustomerOffer (org.broadleafcommerce.core.offer.domain.CustomerOffer)18 Sku (org.broadleafcommerce.core.catalog.domain.Sku)16 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)14 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)13 HashMap (java.util.HashMap)12 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)12 FixedPriceFulfillmentOption (org.broadleafcommerce.core.order.fulfillment.domain.FixedPriceFulfillmentOption)12 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)11 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)11 FixedPriceFulfillmentOptionImpl (org.broadleafcommerce.core.order.fulfillment.domain.FixedPriceFulfillmentOptionImpl)10