Search in sources :

Example 16 with Address

use of org.broadleafcommerce.profile.core.domain.Address in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafBillingInfoController method saveBillingAddress.

/**
 * Processes the request to save a billing address.
 *
 * Note: this default Broadleaf implementation will create an OrderPayment of
 * type CREDIT_CARD if it doesn't exist and save the passed in billing address
 *
 * @param request
 * @param response
 * @param model
 * @param billingForm
 * @return the return path
 * @throws org.broadleafcommerce.common.exception.ServiceException
 */
public String saveBillingAddress(HttpServletRequest request, HttpServletResponse response, Model model, BillingInfoForm billingForm, BindingResult result) throws PricingException, ServiceException {
    Order cart = CartState.getCart();
    CustomerPayment customerPayment = null;
    if (billingForm.isUseShippingAddress()) {
        copyShippingAddressToBillingAddress(cart, billingForm);
    }
    Boolean useCustomerPayment = billingForm.getUseCustomerPayment();
    if (useCustomerPayment && billingForm.getCustomerPaymentId() != null) {
        customerPayment = customerPaymentService.readCustomerPaymentById(billingForm.getCustomerPaymentId());
        if (customerPayment != null) {
            Address address = customerPayment.getBillingAddress();
            if (address != null) {
                billingForm.setAddress(addressService.copyAddress(address));
            }
        }
    }
    addressService.populateAddressISOCountrySub(billingForm.getAddress());
    billingInfoFormValidator.validate(billingForm, result);
    if (result.hasErrors()) {
        return getCheckoutView();
    }
    if ((billingForm.getAddress().getPhonePrimary() != null) && (StringUtils.isEmpty(billingForm.getAddress().getPhonePrimary().getPhoneNumber()))) {
        billingForm.getAddress().setPhonePrimary(null);
    }
    if ((billingForm.getAddress().getPhoneSecondary() != null) && (StringUtils.isEmpty(billingForm.getAddress().getPhoneSecondary().getPhoneNumber()))) {
        billingForm.getAddress().setPhoneSecondary(null);
    }
    if ((billingForm.getAddress().getPhoneFax() != null) && (StringUtils.isEmpty(billingForm.getAddress().getPhoneFax().getPhoneNumber()))) {
        billingForm.getAddress().setPhoneFax(null);
    }
    boolean found = false;
    String paymentName = billingForm.getPaymentName();
    Boolean saveNewPayment = billingForm.getSaveNewPayment();
    for (OrderPayment p : cart.getPayments()) {
        if (PaymentType.CREDIT_CARD.equals(p.getType()) && p.isActive()) {
            if (p.getBillingAddress() == null) {
                p.setBillingAddress(billingForm.getAddress());
            } else {
                Address updatedAddress = addressService.copyAddress(p.getBillingAddress(), billingForm.getAddress());
                p.setBillingAddress(updatedAddress);
            }
            found = true;
        }
    }
    if (!found) {
        // A Temporary Order Payment will be created to hold the billing address.
        // The Payment Gateway will send back any validated address and
        // the PaymentGatewayCheckoutService will persist a new payment of type CREDIT_CARD when it applies it to the Order
        OrderPayment tempOrderPayment = orderPaymentService.create();
        tempOrderPayment.setType(PaymentType.CREDIT_CARD);
        tempOrderPayment.setPaymentGatewayType(PaymentGatewayType.TEMPORARY);
        tempOrderPayment.setBillingAddress(billingForm.getAddress());
        tempOrderPayment.setOrder(cart);
        cart.getPayments().add(tempOrderPayment);
    }
    orderService.save(cart, true);
    if (isAjaxRequest(request)) {
        // Add module specific model variables
        checkoutControllerExtensionManager.getProxy().addAdditionalModelVariables(model);
        return getCheckoutView();
    } else {
        return getCheckoutPageRedirect();
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) Address(org.broadleafcommerce.profile.core.domain.Address) CustomerPayment(org.broadleafcommerce.profile.core.domain.CustomerPayment) OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment)

Example 17 with Address

use of org.broadleafcommerce.profile.core.domain.Address in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafManageCustomerAddressesController method addCustomerAddress.

public String addCustomerAddress(HttpServletRequest request, Model model, CustomerAddressForm form, BindingResult result, RedirectAttributes redirectAttributes) throws ServiceException {
    addressService.populateAddressISOCountrySub(form.getAddress());
    customerAddressValidator.validate(form, result);
    if (result.hasErrors()) {
        return getCustomerAddressesView();
    }
    removeUnusedPhones(form);
    Address address = addressService.saveAddress(form.getAddress());
    CustomerAddress customerAddress = customerAddressService.create();
    customerAddress.setAddress(address);
    customerAddress.setAddressName(form.getAddressName());
    customerAddress.setCustomer(CustomerState.getCustomer());
    customerAddress = customerAddressService.saveCustomerAddress(customerAddress);
    if (form.getAddress().isDefault()) {
        customerAddressService.makeCustomerAddressDefault(customerAddress.getId(), customerAddress.getCustomer().getId());
    }
    form.setCustomerAddressId(customerAddress.getId());
    if (!isAjaxRequest(request)) {
        List<CustomerAddress> addresses = customerAddressService.readActiveCustomerAddressesByCustomerId(CustomerState.getCustomer().getId());
        model.addAttribute("addresses", addresses);
    }
    redirectAttributes.addFlashAttribute("successMessage", getAddressAddedMessage());
    return getCustomerAddressesRedirect();
}
Also used : Address(org.broadleafcommerce.profile.core.domain.Address) CustomerAddress(org.broadleafcommerce.profile.core.domain.CustomerAddress) CustomerAddress(org.broadleafcommerce.profile.core.domain.CustomerAddress)

Example 18 with Address

use of org.broadleafcommerce.profile.core.domain.Address in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafPaymentInfoController method copyShippingAddressToBillingAddress.

/**
 * This method will copy the shipping address of the first fulfillment group on the order
 * to the billing address on the PaymentInfoForm that is passed in.
 */
protected void copyShippingAddressToBillingAddress(Order order, PaymentInfoForm paymentInfoForm) {
    if (order.getFulfillmentGroups().get(0) != null) {
        Address shipping = order.getFulfillmentGroups().get(0).getAddress();
        if (shipping != null) {
            Address billing = addressService.copyAddress(shipping);
            paymentInfoForm.setAddress(billing);
        }
    }
}
Also used : Address(org.broadleafcommerce.profile.core.domain.Address)

Example 19 with Address

use of org.broadleafcommerce.profile.core.domain.Address in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafShippingInfoController method removeUnusedPhones.

public void removeUnusedPhones(ShippingInfoForm form) {
    Address address = form.getAddress();
    Phone primaryPhone = address.getPhonePrimary();
    Phone secondaryPhone = address.getPhoneSecondary();
    Phone faxPhone = address.getPhoneFax();
    if ((primaryPhone != null) && (StringUtils.isEmpty(primaryPhone.getPhoneNumber()))) {
        address.setPhonePrimary(null);
    }
    if ((secondaryPhone != null) && (StringUtils.isEmpty(secondaryPhone.getPhoneNumber()))) {
        address.setPhoneSecondary(null);
    }
    if ((faxPhone != null) && (StringUtils.isEmpty(faxPhone.getPhoneNumber()))) {
        address.setPhoneFax(null);
    }
}
Also used : Address(org.broadleafcommerce.profile.core.domain.Address) CustomerAddress(org.broadleafcommerce.profile.core.domain.CustomerAddress) Phone(org.broadleafcommerce.profile.core.domain.Phone)

Example 20 with Address

use of org.broadleafcommerce.profile.core.domain.Address 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)

Aggregations

Address (org.broadleafcommerce.profile.core.domain.Address)37 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)15 Order (org.broadleafcommerce.core.order.domain.Order)15 Customer (org.broadleafcommerce.profile.core.domain.Customer)14 AddressImpl (org.broadleafcommerce.profile.core.domain.AddressImpl)11 CustomerAddress (org.broadleafcommerce.profile.core.domain.CustomerAddress)11 Transactional (org.springframework.transaction.annotation.Transactional)10 Test (org.testng.annotations.Test)10 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)9 Money (org.broadleafcommerce.common.money.Money)8 ArrayList (java.util.ArrayList)7 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)7 State (org.broadleafcommerce.profile.core.domain.State)7 StateImpl (org.broadleafcommerce.profile.core.domain.StateImpl)7 ISOCountry (org.broadleafcommerce.common.i18n.domain.ISOCountry)6 ISOCountryImpl (org.broadleafcommerce.common.i18n.domain.ISOCountryImpl)6 FulfillmentGroupImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupImpl)6 FulfillmentGroupItemImpl (org.broadleafcommerce.core.order.domain.FulfillmentGroupItemImpl)6 Country (org.broadleafcommerce.profile.core.domain.Country)6 CountryImpl (org.broadleafcommerce.profile.core.domain.CountryImpl)6