use of org.broadleafcommerce.core.order.domain.FulfillmentGroup in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentGroupItemStrategyImpl method verify.
@Override
public CartOperationRequest verify(CartOperationRequest request) throws PricingException {
Order order = request.getOrder();
if (isRemoveEmptyFulfillmentGroups() && order.getFulfillmentGroups() != null) {
ListIterator<FulfillmentGroup> fgIter = order.getFulfillmentGroups().listIterator();
while (fgIter.hasNext()) {
FulfillmentGroup fg = fgIter.next();
if (fg.getFulfillmentGroupItems() == null || fg.getFulfillmentGroupItems().size() == 0) {
fgIter.remove();
fulfillmentGroupService.delete(fg);
}
}
}
Map<Long, Integer> oiQuantityMap = new HashMap<Long, Integer>();
List<OrderItem> expandedOrderItems = new ArrayList<OrderItem>();
for (OrderItem oi : order.getOrderItems()) {
if (oi instanceof BundleOrderItem) {
for (DiscreteOrderItem doi : ((BundleOrderItem) oi).getDiscreteOrderItems()) {
expandedOrderItems.add(doi);
}
} else if (oi instanceof DiscreteOrderItem) {
expandedOrderItems.add(oi);
} else {
expandedOrderItems.add(oi);
}
}
for (OrderItem oi : expandedOrderItems) {
Integer oiQuantity = oiQuantityMap.get(oi.getId());
if (oiQuantity == null) {
oiQuantity = 0;
}
if (oi instanceof DiscreteOrderItem && ((DiscreteOrderItem) oi).getBundleOrderItem() != null) {
oiQuantity += ((DiscreteOrderItem) oi).getBundleOrderItem().getQuantity() * oi.getQuantity();
} else {
oiQuantity += oi.getQuantity();
}
oiQuantityMap.put(oi.getId(), oiQuantity);
}
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
Long oiId = fgi.getOrderItem().getId();
Integer oiQuantity = oiQuantityMap.get(oiId);
if (oiQuantity == null) {
throw new IllegalStateException("Fulfillment group items and discrete order items are not in sync. DiscreteOrderItem id: " + oiId);
}
oiQuantity -= fgi.getQuantity();
oiQuantityMap.put(oiId, oiQuantity);
}
}
for (Entry<Long, Integer> entry : oiQuantityMap.entrySet()) {
if (!entry.getValue().equals(0)) {
throw new IllegalStateException("Not enough fulfillment group items found for DiscreteOrderItem id: " + entry.getKey());
}
}
return request;
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroup in project BroadleafCommerce by BroadleafCommerce.
the class FulfillmentGroupItemStrategyImpl method updateItemQuantity.
protected List<FulfillmentGroupItem> updateItemQuantity(Order order, OrderItem orderItem, Integer orderItemQuantityDelta) throws PricingException {
List<FulfillmentGroupItem> fgisToDelete = new ArrayList<FulfillmentGroupItem>();
boolean done = false;
if (orderItemQuantityDelta > 0) {
// fulfillment group we find that has that order item in it.
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
for (FulfillmentGroupItem fgItem : fg.getFulfillmentGroupItems()) {
if (!done && fgItem.getOrderItem().equals(orderItem)) {
fgItem.setQuantity(fgItem.getQuantity() + orderItemQuantityDelta);
done = true;
}
}
}
} else {
// The quantity has been decremented. We must ensure that the appropriate number
// of fulfillment group items are decremented as well.
int remainingToDecrement = -1 * orderItemQuantityDelta;
for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
for (FulfillmentGroupItem fgItem : fg.getFulfillmentGroupItems()) {
if (fgItem.getOrderItem().equals(orderItem)) {
if (!done && fgItem.getQuantity() == remainingToDecrement) {
// Quantity matches exactly. Simply remove the item.
fgisToDelete.add(fgItem);
done = true;
} else if (!done && fgItem.getQuantity() > remainingToDecrement) {
// We have enough quantity in this fg item to facilitate the entire requsted update
fgItem.setQuantity(fgItem.getQuantity() - remainingToDecrement);
done = true;
} else if (!done) {
// We do not have enough quantity. We'll remove this item and continue searching
// for the remainder.
remainingToDecrement = remainingToDecrement - fgItem.getQuantity();
fgisToDelete.add(fgItem);
}
}
}
}
}
if (!done) {
throw new IllegalStateException("Could not find matching fulfillment group item for the given order item");
}
return fgisToDelete;
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroup in project BroadleafCommerce by BroadleafCommerce.
the class OrderToPaymentRequestDTOServiceImpl method populateShipTo.
/**
* Uses the first shippable fulfillment group to populate the {@link PaymentRequestDTO#shipTo()} object
* @param order the {@link Order} to get data from
* @param requestDTO the {@link PaymentRequestDTO} that should be populated
* @see {@link FulfillmentGroupService#getFirstShippableFulfillmentGroup(Order)}
*/
@Override
public void populateShipTo(Order order, PaymentRequestDTO requestDTO) {
List<FulfillmentGroup> fgs = order.getFulfillmentGroups();
if (fgs != null && fgs.size() > 0) {
FulfillmentGroup defaultFg = fgService.getFirstShippableFulfillmentGroup(order);
if (defaultFg != null && defaultFg.getAddress() != null) {
Address fgAddress = defaultFg.getAddress();
String stateAbbr = null;
String countryAbbr = null;
String phone = null;
if (StringUtils.isNotBlank(fgAddress.getStateProvinceRegion())) {
stateAbbr = fgAddress.getStateProvinceRegion();
} else if (fgAddress.getState() != null) {
// support legacy
stateAbbr = fgAddress.getState().getAbbreviation();
}
if (fgAddress.getIsoCountryAlpha2() != null) {
countryAbbr = fgAddress.getIsoCountryAlpha2().getAlpha2();
} else if (fgAddress.getCountry() != null) {
// support legacy
countryAbbr = fgAddress.getCountry().getAbbreviation();
}
if (fgAddress.getPhonePrimary() != null) {
phone = fgAddress.getPhonePrimary().getPhoneNumber();
}
NameResponse name = getName(fgAddress);
requestDTO.shipTo().addressFirstName(name.firstName).addressLastName(name.lastName).addressCompanyName(fgAddress.getCompanyName()).addressLine1(fgAddress.getAddressLine1()).addressLine2(fgAddress.getAddressLine2()).addressCityLocality(fgAddress.getCity()).addressStateRegion(stateAbbr).addressPostalCode(fgAddress.getPostalCode()).addressCountryCode(countryAbbr).addressPhone(phone).addressEmail(fgAddress.getEmailAddress());
}
}
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroup in project BroadleafCommerce by BroadleafCommerce.
the class UpdateOrderMultishipOptionActivity method execute.
@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
CartOperationRequest request = context.getSeedData();
Long orderItemId = request.getItemRequest().getOrderItemId();
Integer orderItemQuantityDelta = request.getOrderItemQuantityDelta();
if (orderItemQuantityDelta < 0) {
int numToDelete = -1 * orderItemQuantityDelta;
// find the qty in the default fg
OrderItem orderItem = request.getOrderItem();
int qty = 0;
if (!CollectionUtils.isEmpty(orderItem.getOrder().getFulfillmentGroups())) {
FulfillmentGroup fg = orderItem.getOrder().getFulfillmentGroups().get(0);
if (fg.getAddress() == null && fg.getFulfillmentOption() == null) {
for (FulfillmentGroupItem fgItem : fg.getFulfillmentGroupItems()) {
if (fgItem.getOrderItem().getId().equals(orderItemId)) {
qty += fgItem.getQuantity();
}
}
}
}
if (numToDelete >= qty) {
request.getMultishipOptionsToDelete().add(new Long[] { orderItemId, (long) (numToDelete - qty) });
}
}
return context;
}
use of org.broadleafcommerce.core.order.domain.FulfillmentGroup in project BroadleafCommerce by BroadleafCommerce.
the class PricingTest method testPricing.
@Test(groups = { "testPricing" }, dependsOnGroups = { "testShippingInsert", "createCustomerIdGeneration" })
@Transactional
public void testPricing() throws Exception {
Order order = orderService.createNewCartForCustomer(createCustomer());
customerService.saveCustomer(order.getCustomer());
Country country = new CountryImpl();
country.setAbbreviation("US");
country.setName("United States");
country = countryService.save(country);
ISOCountry isoCountry = new ISOCountryImpl();
isoCountry.setAlpha2("US");
isoCountry.setName("UNITED STATES");
isoCountry = isoService.save(isoCountry);
State state = new StateImpl();
state.setAbbreviation("TX");
state.setName("Texas");
state.setCountry(country);
state = stateService.save(state);
Address address = new AddressImpl();
address.setAddressLine1("123 Test Rd");
address.setCity("Dallas");
address.setFirstName("Jeff");
address.setLastName("Fischer");
address.setPostalCode("75240");
address.setPrimaryPhone("972-978-9067");
address.setState(state);
address.setCountry(country);
address.setIsoCountrySubdivision("US-TX");
address.setIsoCountryAlpha2(isoCountry);
FulfillmentGroup group = new FulfillmentGroupImpl();
group.setAddress(address);
List<FulfillmentGroup> groups = new ArrayList<>();
group.setMethod("standard");
group.setService(ShippingServiceType.BANDED_SHIPPING.getType());
group.setOrder(order);
groups.add(group);
order.setFulfillmentGroups(groups);
Money total = new Money(8.5D);
group.setShippingPrice(total);
{
DiscreteOrderItem item = new DiscreteOrderItemImpl();
Sku sku = new SkuImpl();
sku.setName("Test Sku");
sku.setRetailPrice(new Money(10D));
sku.setDiscountable(true);
SkuFee fee = new SkuFeeImpl();
fee.setFeeType(SkuFeeType.FULFILLMENT);
fee.setName("fee test");
fee.setAmount(new Money(10D));
fee = catalogService.saveSkuFee(fee);
List<SkuFee> fees = new ArrayList<>();
fees.add(fee);
sku.setFees(fees);
sku = catalogService.saveSku(sku);
item.setSku(sku);
item.setQuantity(2);
item.setOrder(order);
item = (DiscreteOrderItem) orderItemService.saveOrderItem(item);
order.addOrderItem(item);
FulfillmentGroupItem fgItem = new FulfillmentGroupItemImpl();
fgItem.setFulfillmentGroup(group);
fgItem.setOrderItem(item);
fgItem.setQuantity(2);
// fgItem.setPrice(new Money(0D));
group.addFulfillmentGroupItem(fgItem);
}
{
DiscreteOrderItem item = new DiscreteOrderItemImpl();
Sku sku = new SkuImpl();
sku.setName("Test Product 2");
sku.setRetailPrice(new Money(20D));
sku.setDiscountable(true);
sku = catalogService.saveSku(sku);
item.setSku(sku);
item.setQuantity(1);
item.setOrder(order);
item = (DiscreteOrderItem) orderItemService.saveOrderItem(item);
order.addOrderItem(item);
FulfillmentGroupItem fgItem = new FulfillmentGroupItemImpl();
fgItem.setFulfillmentGroup(group);
fgItem.setOrderItem(item);
fgItem.setQuantity(1);
// fgItem.setPrice(new Money(0D));
group.addFulfillmentGroupItem(fgItem);
}
order.addOfferCode(createOfferCode("20 Percent Off Item Offer", OfferType.ORDER_ITEM, OfferDiscountType.PERCENT_OFF, 20, null, "discreteOrderItem.sku.name==\"Test Sku\""));
order.addOfferCode(createOfferCode("3 Dollars Off Item Offer", OfferType.ORDER_ITEM, OfferDiscountType.AMOUNT_OFF, 3, null, "discreteOrderItem.sku.name!=\"Test Sku\""));
order.addOfferCode(createOfferCode("1.20 Dollars Off Order Offer", OfferType.ORDER, OfferDiscountType.AMOUNT_OFF, 1.20, null, null));
order.setTotalShipping(new Money(0D));
orderService.save(order, true);
assert order.getSubTotal().subtract(order.getOrderAdjustmentsValue()).equals(new Money(31.80D));
assert (order.getTotal().greaterThan(order.getSubTotal()));
// Shipping is not taxable
assert (order.getTotalTax().equals(order.getSubTotal().subtract(order.getOrderAdjustmentsValue()).multiply(0.05D)));
// determine the total cost of the fulfillment group fees
Money fulfillmentGroupFeeTotal = getFulfillmentGroupFeeTotal(order);
assert (order.getTotal().equals(order.getSubTotal().add(order.getTotalTax()).add(order.getTotalShipping()).add(fulfillmentGroupFeeTotal).subtract(order.getOrderAdjustmentsValue())));
}
Aggregations