package org.openedit.store; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import org.openedit.Data; import org.openedit.WebPageRequest; import org.openedit.money.Money; import org.openedit.store.adjustments.CouponAdjustment; import org.openedit.store.adjustments.DiscountAdjustment; import org.openedit.store.adjustments.FixedPriceAdjustment; import org.openedit.store.adjustments.SaleAdjustment; import org.openedit.store.orders.Order; import org.openedit.store.orders.OrderId; import org.openedit.util.DateStorageUtil; public class Coupon { protected Data fieldInventoryItem; public Coupon(Data inItem) { setInventoryItem(inItem); } public static boolean isCoupon(CartItem inCartItem) { if (inCartItem == null) return false; return isCoupon(inCartItem.getInventoryItem()); } public static boolean isCoupon(InventoryItem inItem) { if (inItem == null) return false; return isCoupon(inItem.getProduct()); } public static boolean isCoupon(Product inProduct) { if (inProduct == null) return false; return (inProduct.get("producttype")!=null && inProduct.get("producttype").equals("coupon")); } public boolean isSingleValueCoupon() { if (getPercentage() > 0) { return false; } if (getDiscount() > 0) { return false; } return true; } public void setInventoryItem(Data inItem) { fieldInventoryItem = inItem; } public Data getCouponData() { return fieldInventoryItem; } public double getPercentage() { double percentage = 0.0d; if (getCouponData().get("percentage")!=null && !getCouponData().get("percentage").isEmpty()) { percentage = Double.parseDouble(getCouponData().get("percentage")); } return percentage; } public Money getFixedPrice() { Money fixedprice = new Money(); if (getCouponData().get("couponvalue")!=null && !getCouponData().get("couponvalue").isEmpty()) { fixedprice = fixedprice.add(getCouponData().get("couponvalue")); } return fixedprice; } public double getDiscount() { double discount = 0.0d; if (getCouponData().get("dollardiscount")!=null && !getCouponData().get("dollardiscount").isEmpty()) { discount = Double.parseDouble(getCouponData().get("dollardiscount")); } return discount; } public String getProductId() { String productid = getCouponData().get("product"); return productid; } public List getProducts(){ List list = new ArrayList(); String productid = getProductId(); if (productid!=null && !productid.isEmpty()){ StringTokenizer tok = new StringTokenizer(productid,"|"); while(tok.hasMoreTokens()) { list.add(tok.nextToken().trim()); } } return list; } public boolean hasProductRestrictions(){ return getProductId() != null && !getProductId().isEmpty(); } public boolean hasMultipleProducts(){ return (getProducts().size() > 1); } public int getMininumProductQuantity() { int minquantity = -1; if (getCouponData().get("minquantity")!=null && !getCouponData().get("minquantity").isEmpty()) { minquantity = Integer.parseInt(getCouponData().get("minquantity")); } return minquantity; } public double getMinimumSubtotal() { double minsubtotal = 0.0d; if (getCouponData().get("minsubtotal") !=null && !getCouponData().get("minsubtotal").isEmpty()) { minsubtotal = Double.parseDouble(getCouponData().get("minsubtotal")); } return minsubtotal; } public Date getCouponExpiry() { Date expiry = DateStorageUtil.getStorageUtil().parseFromStorage(getCouponData().get("expirydate")); return expiry; } public boolean isAcceptsMultiple() { boolean multiple = Boolean.parseBoolean(getCouponData().get("acceptmultiple")); return multiple; } public boolean isOnePerUser() { boolean oneperuser = Boolean.parseBoolean(getCouponData().get("oneperuser")); return oneperuser; } public String getUserRestriction() { String user = getCouponData().get("restricttouser"); return user; } public boolean hasExpired() { Date expiry = getCouponExpiry(); return (expiry != null && (new Date()).after(expiry)); } public boolean isCustomerOk(Cart inCart) { String user = getUserRestriction(); return ( user == null || user.isEmpty() || (inCart.getCustomer()!=null && inCart.getCustomer().getId().equals(user)) ); } public boolean hasCustomerUsedCoupon(WebPageRequest inReq, Cart inCart) { if (!isOnePerUser()) { return false; } String inventoryItemSku = this.getCouponData().get("code"); if (inventoryItemSku == null) { return false; } if (inCart.getCustomer()==null) { return false; } String user = inCart.getCustomer().getId(); Store store = (Store) inReq.getPageValue("store"); List ids = store.getOrderArchive().listAllOrderIds(store); for(Object id:ids){ Order order = (Order) store.getOrderSearcher().searchById(((OrderId)id).getOrderId()); if (order == null || order.getItems() == null) { continue; } List cartItems = order.getItems(); for(Object item:cartItems){ CartItem cartItem = (CartItem) item; if (isCoupon(cartItem) && cartItem.getInventoryItem()!=null) { String sku = cartItem.getInventoryItem().getSku(); if (sku!=null && sku.equals(inventoryItemSku)) { if (order.getCustomer().getId().equals(user)) { return true; } } } } } return false; } public boolean isCartSubtotalOk(Cart inCart) { double minsubtotal = getMinimumSubtotal(); Money minmoney = new Money(minsubtotal); Money subtotal = inCart.getSubtotalWithoutCoupons(); return (subtotal.doubleValue() >= minmoney.doubleValue()); } public boolean isCartItemsOk(Cart inCart) { int minquantity = getMininumProductQuantity(); if ( hasProductRestrictions() || minquantity > 0 ) { List products = getProducts(); boolean isproductpresent = false; boolean isminimumok = (minquantity > 0) ? false : true; for (Iterator iter = inCart.getItems().iterator(); iter.hasNext();) { CartItem item = (CartItem) iter.next(); if (isCoupon(item) || item.getProduct() == null){ continue; } String productid = item.getProduct().getId(); if (products.contains(productid)){ isproductpresent = true; if (minquantity > 0 && item.getQuantity() < minquantity){ continue; } isminimumok = true; } } if (!isproductpresent) { return false; } else { if (!isminimumok){ return false; } } } return true; } private Money getValue() { return new Money(getCouponData().get("couponvalue")); } public List getAllAffectedCartItems(Cart inCart){ List list = new ArrayList(); List productIds = getProducts(); Iterator itr = inCart.getItems().iterator(); while (itr.hasNext()){ CartItem item = (CartItem) itr.next(); if (Coupon.isCoupon(item) || item.getProduct() == null){ continue; } if (productIds.contains(item.getProduct().getId()) || productIds.size() == 0){ int minquantity = getMininumProductQuantity(); if (minquantity > 0 && item.getQuantity() < minquantity){ continue; } list.add(item); } } return list; } public List getAllCoupons(Cart inCart){ List list = new ArrayList(); Iterator itr = inCart.getItems().iterator(); while (itr.hasNext()){ Coupon item = (Coupon) itr.next(); list.add(item); } return list; } public boolean isAvailable() { if(getCouponData().getValue("couponavailable") == null) { return false; } return (boolean) getCouponData().getValue("couponavailable"); } }