0001: /*
0002: * $Id: OrderReadHelper.java,v 1.21 2004/02/24 10:09:02 jonesde Exp $
0003: *
0004: * Copyright (c) 2002 The Open For Business Project - www.ofbiz.org
0005: *
0006: * Permission is hereby granted, free of charge, to any person obtaining a
0007: * copy of this software and associated documentation files (the "Software"),
0008: * to deal in the Software without restriction, including without limitation
0009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0010: * and/or sell copies of the Software, and to permit persons to whom the
0011: * Software is furnished to do so, subject to the following conditions:
0012: *
0013: * The above copyright notice and this permission notice shall be included
0014: * in all copies or substantial portions of the Software.
0015: *
0016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
0021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
0022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023: */
0024: package org.ofbiz.order.order;
0025:
0026: import java.sql.Timestamp;
0027: import java.util.*;
0028:
0029: import org.ofbiz.base.util.Debug;
0030: import org.ofbiz.base.util.UtilFormatOut;
0031: import org.ofbiz.base.util.UtilMisc;
0032: import org.ofbiz.base.util.UtilValidate;
0033: import org.ofbiz.common.DataModelConstants;
0034: import org.ofbiz.entity.GenericDelegator;
0035: import org.ofbiz.entity.GenericEntity;
0036: import org.ofbiz.entity.GenericEntityException;
0037: import org.ofbiz.entity.GenericValue;
0038: import org.ofbiz.entity.condition.EntityExpr;
0039: import org.ofbiz.entity.condition.EntityOperator;
0040: import org.ofbiz.entity.util.EntityUtil;
0041: import org.ofbiz.product.product.ProductWorker;
0042: import org.ofbiz.security.Security;
0043:
0044: /**
0045: * Utility class for easily extracting important information from orders
0046: *
0047: * <p>NOTE: in the current scheme order adjustments are never included in tax or shipping,
0048: * but order item adjustments ARE included in tax and shipping calcs unless they are
0049: * tax or shipping adjustments or the includeInTax or includeInShipping are set to N.</p>
0050: *
0051: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
0052: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
0053: * @author Eric Pabst
0054: * @author <a href="mailto:ray.barlow@whatsthe-point.com">Ray Barlow</a>
0055: * @version $Revision: 1.21 $
0056: * @since 2.0
0057: */
0058: public class OrderReadHelper {
0059:
0060: public static final String module = OrderReadHelper.class.getName();
0061:
0062: protected GenericValue orderHeader = null;
0063: protected List orderItems = null;
0064: protected List adjustments = null;
0065: protected List paymentPrefs = null;
0066: protected List orderStatuses = null;
0067: protected List orderItemPriceInfos = null;
0068: protected List orderItemInventoryReses = null;
0069: protected List orderItemIssuances = null;
0070: protected Double totalPrice = null;
0071:
0072: protected OrderReadHelper() {
0073: }
0074:
0075: public OrderReadHelper(GenericValue orderHeader, List adjustments,
0076: List orderItems) {
0077: this .orderHeader = orderHeader;
0078: this .adjustments = adjustments;
0079: this .orderItems = orderItems;
0080: }
0081:
0082: public OrderReadHelper(GenericValue orderHeader) {
0083: this .orderHeader = orderHeader;
0084: }
0085:
0086: // ==========================================
0087: // ========== Order Header Methods ==========
0088: // ==========================================
0089:
0090: public String getOrderId() {
0091: return orderHeader.getString("orderId");
0092: }
0093:
0094: public String getWebSiteId() {
0095: return orderHeader.getString("webSiteId");
0096: }
0097:
0098: public String getProductStoreId() {
0099: return orderHeader.getString("productStoreId");
0100: }
0101:
0102: public String getOrderTypeId() {
0103: return orderHeader.getString("orderTypeId");
0104: }
0105:
0106: public String getCurrency() {
0107: return orderHeader.getString("currencyUom");
0108: }
0109:
0110: public List getAdjustments() {
0111: if (adjustments == null) {
0112: try {
0113: adjustments = orderHeader.getRelated("OrderAdjustment");
0114: } catch (GenericEntityException e) {
0115: Debug.logError(e, module);
0116: }
0117: if (adjustments == null)
0118: adjustments = new ArrayList();
0119: }
0120: return (List) adjustments;
0121: }
0122:
0123: public List getPaymentPreferences() {
0124: if (paymentPrefs == null) {
0125: try {
0126: paymentPrefs = orderHeader
0127: .getRelated("OrderPaymentPreference");
0128: } catch (GenericEntityException e) {
0129: Debug.logError(e, module);
0130: }
0131: }
0132: return paymentPrefs;
0133: }
0134:
0135: public List getOrderPayments() {
0136: List orderPayments = new ArrayList();
0137: List prefs = getPaymentPreferences();
0138: if (prefs != null) {
0139: Iterator i = prefs.iterator();
0140: while (i.hasNext()) {
0141: GenericValue payPref = (GenericValue) i.next();
0142: try {
0143: orderPayments.addAll(payPref.getRelated("Payment"));
0144: } catch (GenericEntityException e) {
0145: Debug.logError(e, module);
0146: return null;
0147: }
0148: }
0149: }
0150: return orderPayments;
0151: }
0152:
0153: public List getOrderStatuses() {
0154: if (orderStatuses == null) {
0155: try {
0156: orderStatuses = orderHeader.getRelated("OrderStatus");
0157: } catch (GenericEntityException e) {
0158: Debug.logError(e, module);
0159: }
0160: }
0161: return (List) orderStatuses;
0162: }
0163:
0164: public String getShippingMethod() {
0165: try {
0166: GenericValue shipmentPreference = null;
0167: Iterator tempIter = UtilMisc.toIterator(orderHeader
0168: .getRelated("OrderShipmentPreference"));
0169:
0170: if (tempIter != null && tempIter.hasNext()) {
0171: shipmentPreference = (GenericValue) tempIter.next();
0172: }
0173: if (shipmentPreference != null) {
0174: GenericValue carrierShipmentMethod = shipmentPreference
0175: .getRelatedOne("CarrierShipmentMethod");
0176:
0177: if (carrierShipmentMethod != null) {
0178: GenericValue shipmentMethodType = carrierShipmentMethod
0179: .getRelatedOne("ShipmentMethodType");
0180:
0181: if (shipmentMethodType != null) {
0182: return UtilFormatOut
0183: .checkNull(shipmentPreference
0184: .getString("carrierPartyId"))
0185: + " "
0186: + UtilFormatOut
0187: .checkNull(shipmentMethodType
0188: .getString("description"));
0189: }
0190: }
0191: return UtilFormatOut.checkNull(shipmentPreference
0192: .getString("carrierPartyId"));
0193: }
0194: return "";
0195: } catch (GenericEntityException e) {
0196: Debug.logWarning(e, module);
0197: }
0198: return "";
0199: }
0200:
0201: public String getShippingMethodCode() {
0202: try {
0203: GenericValue shipmentPreference = null;
0204: Iterator tempIter = UtilMisc.toIterator(orderHeader
0205: .getRelated("OrderShipmentPreference"));
0206:
0207: if (tempIter != null && tempIter.hasNext()) {
0208: shipmentPreference = (GenericValue) tempIter.next();
0209: }
0210: if (shipmentPreference != null) {
0211: GenericValue carrierShipmentMethod = shipmentPreference
0212: .getRelatedOne("CarrierShipmentMethod");
0213:
0214: if (carrierShipmentMethod != null) {
0215: GenericValue shipmentMethodType = carrierShipmentMethod
0216: .getRelatedOne("ShipmentMethodType");
0217:
0218: if (shipmentMethodType != null) {
0219: return UtilFormatOut
0220: .checkNull(shipmentMethodType
0221: .getString("shipmentMethodTypeId"))
0222: + "@"
0223: + UtilFormatOut
0224: .checkNull(shipmentPreference
0225: .getString("carrierPartyId"));
0226: }
0227: }
0228: return UtilFormatOut.checkNull(shipmentPreference
0229: .getString("carrierPartyId"));
0230: }
0231: return "";
0232: } catch (GenericEntityException e) {
0233: Debug.logWarning(e, module);
0234: }
0235: return "";
0236: }
0237:
0238: public GenericValue getShippingAddress() {
0239: GenericDelegator delegator = orderHeader.getDelegator();
0240:
0241: try {
0242: GenericValue orderContactMech = EntityUtil
0243: .getFirst(orderHeader.getRelatedByAnd(
0244: "OrderContactMech", UtilMisc.toMap(
0245: "contactMechPurposeTypeId",
0246: "SHIPPING_LOCATION")));
0247:
0248: if (orderContactMech != null) {
0249: GenericValue contactMech = orderContactMech
0250: .getRelatedOne("ContactMech");
0251:
0252: if (contactMech != null) {
0253: return contactMech.getRelatedOne("PostalAddress");
0254: }
0255: }
0256: } catch (GenericEntityException e) {
0257: Debug.logWarning(e, module);
0258: }
0259: return null;
0260: }
0261:
0262: public GenericValue getBillingAddress() {
0263: GenericDelegator delegator = orderHeader.getDelegator();
0264: GenericValue billingAddress = null;
0265: try {
0266: GenericValue orderContactMech = EntityUtil
0267: .getFirst(orderHeader.getRelatedByAnd(
0268: "OrderContactMech", UtilMisc.toMap(
0269: "contactMechPurposeTypeId",
0270: "BILLING_LOCATION")));
0271:
0272: if (orderContactMech != null) {
0273: GenericValue contactMech = orderContactMech
0274: .getRelatedOne("ContactMech");
0275:
0276: if (contactMech != null) {
0277: billingAddress = contactMech
0278: .getRelatedOne("PostalAddress");
0279: }
0280: }
0281: } catch (GenericEntityException e) {
0282: Debug.logWarning(e, module);
0283: }
0284:
0285: if (billingAddress == null) {
0286: // get the address from the billing account
0287: GenericValue billingAccount = getBillingAccount();
0288: if (billingAccount != null) {
0289: try {
0290: billingAddress = billingAccount
0291: .getRelatedOne("PostalAddress");
0292: } catch (GenericEntityException e) {
0293: Debug.logWarning(e, module);
0294: }
0295: } else {
0296: // get the address from the first payment method
0297: GenericValue paymentPreference = EntityUtil
0298: .getFirst(getPaymentPreferences());
0299: if (paymentPreference != null) {
0300: try {
0301: GenericValue paymentMethod = paymentPreference
0302: .getRelatedOne("PaymentMethod");
0303: if (paymentMethod != null) {
0304: GenericValue creditCard = paymentMethod
0305: .getRelatedOne("CreditCard");
0306: if (creditCard != null) {
0307: billingAddress = creditCard
0308: .getRelatedOne("PostalAddress");
0309: } else {
0310: GenericValue eftAccount = paymentMethod
0311: .getRelatedOne("EftAccount");
0312: if (eftAccount != null) {
0313: billingAddress = eftAccount
0314: .getRelatedOne("PostalAddress");
0315: }
0316: }
0317: }
0318: } catch (GenericEntityException e) {
0319: Debug.logWarning(e, module);
0320: }
0321: }
0322: }
0323: }
0324: return billingAddress;
0325: }
0326:
0327: public String getCurrentStatusString() {
0328: GenericValue statusItem = null;
0329: try {
0330: statusItem = orderHeader.getRelatedOneCache("StatusItem");
0331: } catch (GenericEntityException e) {
0332: Debug.logError(e, module);
0333: }
0334: if (statusItem != null) {
0335: return statusItem.getString("description");
0336: } else {
0337: return orderHeader.getString("statusId");
0338: }
0339: }
0340:
0341: public String getStatusString() {
0342: List orderStatusList = this .getOrderHeaderStatuses();
0343:
0344: if (orderStatusList == null)
0345: return "";
0346:
0347: Iterator orderStatusIter = orderStatusList.iterator();
0348: StringBuffer orderStatusIds = new StringBuffer(50);
0349: boolean statusSet = false;
0350:
0351: while (orderStatusIter.hasNext()) {
0352: try {
0353: GenericValue orderStatus = (GenericValue) orderStatusIter
0354: .next();
0355: GenericValue statusItem = orderStatus
0356: .getRelatedOneCache("StatusItem");
0357:
0358: if (false == statusSet) {
0359: statusSet = true;
0360: } else {
0361: orderStatusIds.append("/");
0362: }
0363:
0364: if (statusItem != null) {
0365: orderStatusIds.append(statusItem
0366: .getString("description"));
0367: } else {
0368: orderStatusIds.append(orderStatus
0369: .getString("statusId"));
0370: }
0371: } catch (GenericEntityException gee) {
0372: Debug.logWarning(gee, module);
0373: }
0374: }
0375:
0376: if (false == statusSet) {
0377: orderStatusIds.append("(unspecified)");
0378: }
0379: return orderStatusIds.toString();
0380: }
0381:
0382: public GenericValue getBillingAccount() {
0383: GenericValue billingAccount = null;
0384: try {
0385: billingAccount = orderHeader
0386: .getRelatedOne("BillingAccount");
0387: } catch (GenericEntityException e) {
0388: Debug.logWarning(e, module);
0389: }
0390: return billingAccount;
0391: }
0392:
0393: public GenericValue getBillToPerson() {
0394: GenericDelegator delegator = orderHeader.getDelegator();
0395:
0396: try {
0397: GenericEntity billToRole = EntityUtil.getFirst(orderHeader
0398: .getRelatedByAnd("OrderRole", UtilMisc.toMap(
0399: "roleTypeId", "BILL_TO_CUSTOMER")));
0400:
0401: if (billToRole != null) {
0402: return delegator.findByPrimaryKey("Person", UtilMisc
0403: .toMap("partyId", billToRole
0404: .getString("partyId")));
0405: } else {
0406: return null;
0407: }
0408: } catch (GenericEntityException e) {
0409: Debug.logWarning(e, module);
0410: }
0411: return null;
0412: }
0413:
0414: public GenericValue getPlacingParty() {
0415: return this .getPartyFromRole("PLACING_CUSTOMER");
0416: }
0417:
0418: public GenericValue getSupplierAgent() {
0419: return this .getPartyFromRole("SUPPLIER_AGENT");
0420: }
0421:
0422: public GenericValue getPartyFromRole(String roleTypeId) {
0423: GenericDelegator delegator = orderHeader.getDelegator();
0424: GenericValue partyObject = null;
0425: try {
0426: GenericValue orderRole = EntityUtil.getFirst(orderHeader
0427: .getRelatedByAnd("OrderRole", UtilMisc.toMap(
0428: "roleTypeId", roleTypeId)));
0429:
0430: if (orderRole != null) {
0431: partyObject = delegator.findByPrimaryKey("Person",
0432: UtilMisc.toMap("partyId", orderRole
0433: .getString("partyId")));
0434:
0435: if (partyObject == null) {
0436: partyObject = delegator.findByPrimaryKey(
0437: "PartyGroup", UtilMisc.toMap("partyId",
0438: orderRole.getString("partyId")));
0439: }
0440: }
0441: } catch (GenericEntityException e) {
0442: Debug.logError(e, module);
0443: }
0444: return partyObject;
0445: }
0446:
0447: public String getDistributorId() {
0448: GenericDelegator delegator = orderHeader.getDelegator();
0449:
0450: try {
0451: GenericEntity distributorRole = EntityUtil
0452: .getFirst(orderHeader
0453: .getRelatedByAnd("OrderRole", UtilMisc
0454: .toMap("roleTypeId", "DISTRIBUTOR")));
0455:
0456: return distributorRole == null ? null : distributorRole
0457: .getString("partyId");
0458: } catch (GenericEntityException e) {
0459: Debug.logWarning(e, module);
0460: }
0461: return null;
0462: }
0463:
0464: public String getAffiliateId() {
0465: GenericDelegator delegator = orderHeader.getDelegator();
0466:
0467: try {
0468: GenericEntity distributorRole = EntityUtil
0469: .getFirst(orderHeader.getRelatedByAnd("OrderRole",
0470: UtilMisc.toMap("roleTypeId", "AFFILIATE")));
0471:
0472: return distributorRole == null ? null : distributorRole
0473: .getString("partyId");
0474: } catch (GenericEntityException e) {
0475: Debug.logWarning(e, module);
0476: }
0477: return null;
0478: }
0479:
0480: public double getShippingTotal() {
0481: return OrderReadHelper.calcOrderAdjustments(
0482: getOrderHeaderAdjustments(), getOrderItemsSubTotal(),
0483: false, false, true);
0484: }
0485:
0486: public Map getFeatureIdQtyMap() {
0487: Map featureMap = new HashMap();
0488: List validItems = getValidOrderItems();
0489: if (validItems != null) {
0490: Iterator i = validItems.iterator();
0491: while (i.hasNext()) {
0492: GenericValue item = (GenericValue) i.next();
0493: List featureAppls = null;
0494: if (item.get("productId") != null) {
0495: try {
0496: featureAppls = item
0497: .getDelegator()
0498: .findByAndCache(
0499: "ProductFeatureAppl",
0500: UtilMisc
0501: .toMap(
0502: "productId",
0503: item
0504: .getString("productId")));
0505: List filterExprs = UtilMisc
0506: .toList(new EntityExpr(
0507: "productFeatureApplTypeId",
0508: EntityOperator.EQUALS,
0509: "STANDARD_FEATURE"));
0510: filterExprs.add(new EntityExpr(
0511: "productFeatureApplTypeId",
0512: EntityOperator.EQUALS,
0513: "REQUIRED_FEATURE"));
0514: featureAppls = EntityUtil.filterByOr(
0515: featureAppls, filterExprs);
0516: } catch (GenericEntityException e) {
0517: Debug.logError(e,
0518: "Unable to get ProductFeatureAppl for item : "
0519: + item, module);
0520: }
0521: if (featureAppls != null) {
0522: Iterator fai = featureAppls.iterator();
0523: while (fai.hasNext()) {
0524: GenericValue appl = (GenericValue) fai
0525: .next();
0526: Double lastQuantity = (Double) featureMap
0527: .get(appl
0528: .getString("productFeatureId"));
0529: if (lastQuantity == null) {
0530: lastQuantity = new Double(0);
0531: }
0532: Double newQuantity = new Double(
0533: lastQuantity.doubleValue()
0534: + getOrderItemQuantity(item)
0535: .doubleValue());
0536: featureMap.put(appl
0537: .getString("productFeatureId"),
0538: newQuantity);
0539: }
0540: }
0541: }
0542:
0543: // get the ADDITIONAL_FEATURE adjustments
0544: List additionalFeatures = null;
0545: try {
0546: additionalFeatures = item.getRelatedByAnd(
0547: "OrderAdjustment", UtilMisc.toMap(
0548: "orderAdjustmentTypeId",
0549: "ADDITIONAL_FEATURE"));
0550: } catch (GenericEntityException e) {
0551: Debug.logError(e,
0552: "Unable to get OrderAdjustment from item : "
0553: + item, module);
0554: }
0555: if (additionalFeatures != null) {
0556: Iterator afi = additionalFeatures.iterator();
0557: while (afi.hasNext()) {
0558: GenericValue adj = (GenericValue) afi.next();
0559: String featureId = adj
0560: .getString("productFeatureId");
0561: if (featureId != null) {
0562: Double lastQuantity = (Double) featureMap
0563: .get(featureId);
0564: if (lastQuantity == null) {
0565: lastQuantity = new Double(0);
0566: }
0567: Double newQuantity = new Double(
0568: lastQuantity.doubleValue()
0569: + getOrderItemQuantity(item)
0570: .doubleValue());
0571: featureMap.put(featureId, newQuantity);
0572: }
0573: }
0574: }
0575: }
0576: }
0577:
0578: return featureMap;
0579: }
0580:
0581: public double getShippableTotal() {
0582: double shippableTotal = 0.00;
0583: List validItems = getValidOrderItems();
0584: if (validItems != null) {
0585: Iterator i = validItems.iterator();
0586: while (i.hasNext()) {
0587: GenericValue item = (GenericValue) i.next();
0588: GenericValue product = null;
0589: try {
0590: product = item.getRelatedOne("Product");
0591: } catch (GenericEntityException e) {
0592: Debug
0593: .logError(
0594: e,
0595: "Problem getting Product from OrderItem; returning 0",
0596: module);
0597: return 0.00;
0598: }
0599: if (product != null) {
0600: if (ProductWorker.shippingApplies(product)) {
0601: shippableTotal += OrderReadHelper
0602: .getOrderItemSubTotal(item,
0603: getAdjustments(), false, true);
0604: }
0605: }
0606: }
0607: }
0608: return shippableTotal;
0609: }
0610:
0611: public double getShippableQuantity() {
0612: double shippableQuantity = 0.00;
0613: List validItems = getValidOrderItems();
0614: if (validItems != null) {
0615: Iterator i = validItems.iterator();
0616: while (i.hasNext()) {
0617: GenericValue item = (GenericValue) i.next();
0618: GenericValue product = null;
0619: try {
0620: product = item.getRelatedOne("Product");
0621: } catch (GenericEntityException e) {
0622: Debug
0623: .logError(
0624: e,
0625: "Problem getting Product from OrderItem; returning 0",
0626: module);
0627: return 0.00;
0628: }
0629: if (product != null) {
0630: if (ProductWorker.shippingApplies(product)) {
0631: shippableQuantity += getOrderItemQuantity(item)
0632: .doubleValue();
0633: }
0634: }
0635: }
0636: }
0637: return shippableQuantity;
0638: }
0639:
0640: public double getShippableWeight() {
0641: GenericDelegator delegator = orderHeader.getDelegator();
0642: double shippableWeight = 0.00;
0643: List validItems = getValidOrderItems();
0644: if (validItems != null) {
0645: Iterator i = validItems.iterator();
0646: while (i.hasNext()) {
0647: GenericValue item = (GenericValue) i.next();
0648: GenericValue product = null;
0649: try {
0650: product = item.getRelatedOne("Product");
0651: } catch (GenericEntityException e) {
0652: Debug
0653: .logError(
0654: e,
0655: "Problem getting Product from OrderItem; returning 0",
0656: module);
0657: return 0.00;
0658: }
0659: if (product != null) {
0660: if (ProductWorker.shippingApplies(product)) {
0661: Double weight = product.getDouble("weight");
0662: String isVariant = product
0663: .getString("isVariant");
0664: if (weight == null && isVariant != null
0665: && "Y".equals(isVariant)) {
0666: // get the virtual product and check its weight
0667: GenericValue virtual = null;
0668: try {
0669: List virtuals = delegator
0670: .findByAnd(
0671: "ProductAssoc",
0672: UtilMisc
0673: .toMap(
0674: "productIdTo",
0675: product
0676: .getString("productId"),
0677: "productAssocTypeId",
0678: "PRODUCT_VARIENT"),
0679: UtilMisc
0680: .toList("-fromDate"));
0681: if (virtuals != null) {
0682: virtuals = EntityUtil
0683: .filterByDate(virtuals);
0684: }
0685: virtual = EntityUtil.getFirst(virtuals);
0686: } catch (GenericEntityException e) {
0687: Debug
0688: .logError(e,
0689: "Problem getting virtual product");
0690: }
0691: if (virtual != null) {
0692: weight = virtual.getDouble("weight");
0693: }
0694: }
0695:
0696: if (weight != null) {
0697: shippableWeight += weight.doubleValue();
0698: }
0699: }
0700: }
0701: }
0702: }
0703: return shippableWeight;
0704: }
0705:
0706: public List getShippableSizes() {
0707: GenericDelegator delegator = orderHeader.getDelegator();
0708: List shippableSizes = new LinkedList();
0709:
0710: List validItems = getValidOrderItems();
0711: if (validItems != null) {
0712: Iterator i = validItems.iterator();
0713: while (i.hasNext()) {
0714: GenericValue item = (GenericValue) i.next();
0715: GenericValue product = null;
0716: try {
0717: product = item.getRelatedOne("Product");
0718: } catch (GenericEntityException e) {
0719: Debug.logError(e,
0720: "Problem getting Product from OrderItem",
0721: module);
0722: return shippableSizes;
0723: }
0724: if (product != null) {
0725: if (ProductWorker.shippingApplies(product)) {
0726: Double height = product
0727: .getDouble("productHeight");
0728: Double width = product
0729: .getDouble("productWidth");
0730: Double depth = product
0731: .getDouble("productDepth");
0732: String isVariant = product
0733: .getString("isVariant");
0734: if (height == null && width == null
0735: && depth == null && isVariant != null
0736: && "Y".equals(isVariant)) {
0737: // get the virtual product and check its values
0738: GenericValue virtual = null;
0739: try {
0740: List virtuals = delegator
0741: .findByAnd(
0742: "ProductAssoc",
0743: UtilMisc
0744: .toMap(
0745: "productIdTo",
0746: product
0747: .getString("productId"),
0748: "productAssocTypeId",
0749: "PRODUCT_VARIENT"),
0750: UtilMisc
0751: .toList("-fromDate"));
0752: if (virtuals != null) {
0753: virtuals = EntityUtil
0754: .filterByDate(virtuals);
0755: }
0756: virtual = EntityUtil.getFirst(virtuals);
0757: } catch (GenericEntityException e) {
0758: Debug
0759: .logError(e,
0760: "Problem getting virtual product");
0761: }
0762: if (virtual != null) {
0763: height = virtual
0764: .getDouble("productHeight");
0765: width = virtual
0766: .getDouble("productWidth");
0767: depth = virtual
0768: .getDouble("productDepth");
0769: }
0770: }
0771:
0772: if (height == null)
0773: height = new Double(0);
0774: if (width == null)
0775: width = new Double(0);
0776: if (depth == null)
0777: depth = new Double(0);
0778: shippableSizes.add(new Double(height
0779: .doubleValue()
0780: * width.doubleValue()
0781: * depth.doubleValue()));
0782: }
0783: }
0784: }
0785: }
0786: return shippableSizes;
0787: }
0788:
0789: public String getOrderEmailString() {
0790: GenericDelegator delegator = orderHeader.getDelegator();
0791: // get the email addresses from the order contact mech(s)
0792: List orderContactMechs = null;
0793: try {
0794: Map ocFields = UtilMisc.toMap("orderId", orderHeader
0795: .get("orderId"), "contactMechPurposeTypeId",
0796: "ORDER_EMAIL");
0797: orderContactMechs = delegator.findByAnd("OrderContactMech",
0798: ocFields);
0799: } catch (GenericEntityException e) {
0800: Debug.logWarning(e, "Problems getting order contact mechs",
0801: module);
0802: }
0803:
0804: StringBuffer emails = new StringBuffer();
0805: if (orderContactMechs != null) {
0806: Iterator oci = orderContactMechs.iterator();
0807: while (oci.hasNext()) {
0808: try {
0809: GenericValue orderContactMech = (GenericValue) oci
0810: .next();
0811: GenericValue contactMech = orderContactMech
0812: .getRelatedOne("ContactMech");
0813: emails
0814: .append(emails.length() > 0 ? "," : "")
0815: .append(contactMech.getString("infoString"));
0816: } catch (GenericEntityException e) {
0817: Debug
0818: .logWarning(
0819: e,
0820: "Problems getting contact mech from order contact mech",
0821: module);
0822: }
0823: }
0824: }
0825: return emails.toString();
0826: }
0827:
0828: public double getOrderGrandTotal() {
0829: if (totalPrice == null) {
0830: totalPrice = new Double(getOrderGrandTotal(
0831: getValidOrderItems(), getAdjustments()));
0832: }// else already set
0833: return totalPrice.doubleValue();
0834: }
0835:
0836: public List getOrderHeaderAdjustments() {
0837: return getOrderHeaderAdjustments(getAdjustments());
0838: }
0839:
0840: public List getOrderHeaderAdjustmentsToShow() {
0841: return filterOrderAdjustments(getOrderHeaderAdjustments(),
0842: true, false, false, false, false);
0843: }
0844:
0845: public List getOrderHeaderStatuses() {
0846: return getOrderHeaderStatuses(getOrderStatuses());
0847: }
0848:
0849: public double getOrderAdjustmentsTotal() {
0850: return getOrderAdjustmentsTotal(getValidOrderItems(),
0851: getAdjustments());
0852: }
0853:
0854: public double getOrderAdjustmentTotal(GenericValue adjustment) {
0855: return calcOrderAdjustment(adjustment, getOrderItemsSubTotal());
0856: }
0857:
0858: // ========================================
0859: // ========== Order Item Methods ==========
0860: // ========================================
0861:
0862: public List getOrderItems() {
0863: if (orderItems == null) {
0864: try {
0865: orderItems = orderHeader.getRelated("OrderItem");
0866: } catch (GenericEntityException e) {
0867: Debug.logWarning(e, module);
0868: }
0869: }
0870: return (List) orderItems;
0871: }
0872:
0873: public List getValidOrderItems() {
0874: List exprs = UtilMisc.toList(new EntityExpr("statusId",
0875: EntityOperator.NOT_EQUAL, "ITEM_CANCELLED"),
0876: new EntityExpr("statusId", EntityOperator.NOT_EQUAL,
0877: "ITEM_REJECTED"));
0878: return EntityUtil.filterByAnd(getOrderItems(), exprs);
0879: }
0880:
0881: public GenericValue getOrderItem(String orderItemSeqId) {
0882: List exprs = UtilMisc.toList(new EntityExpr("orderItemSeqId",
0883: EntityOperator.EQUALS, orderItemSeqId));
0884: return EntityUtil.getFirst(EntityUtil.filterByAnd(
0885: getOrderItems(), exprs));
0886: }
0887:
0888: public List getValidDigitalItems() {
0889: List digitalItems = new ArrayList();
0890: // only approved or complete items apply
0891: List exprs = UtilMisc.toList(new EntityExpr("statusId",
0892: EntityOperator.EQUALS, "ITEM_APPROVED"),
0893: new EntityExpr("statusId", EntityOperator.EQUALS,
0894: "ITEM_COMPLETED"));
0895: List items = EntityUtil.filterByOr(getOrderItems(), exprs);
0896: Iterator i = items.iterator();
0897: while (i.hasNext()) {
0898: GenericValue item = (GenericValue) i.next();
0899: if (item.get("productId") != null) {
0900: GenericValue product = null;
0901: try {
0902: product = item.getRelatedOne("Product");
0903: } catch (GenericEntityException e) {
0904: Debug.logError(e,
0905: "Unable to get Product from OrderItem",
0906: module);
0907: }
0908: if (product != null) {
0909: String productType = product
0910: .getString("productTypeId");
0911: if ("DIGITAL_GOOD".equals(productType)
0912: || "FINDIG_GOOD".equals(productType)) {
0913: // make sure we have an OrderItemBilling record
0914: List orderItemBillings = null;
0915: try {
0916: orderItemBillings = item
0917: .getRelated("OrderItemBilling");
0918: } catch (GenericEntityException e) {
0919: Debug
0920: .logError(e,
0921: "Unable to get OrderItemBilling from OrderItem");
0922: }
0923:
0924: if (orderItemBillings != null
0925: && orderItemBillings.size() > 0) {
0926: // get the ProductContent records
0927: List productContents = null;
0928: try {
0929: productContents = product
0930: .getRelated("ProductContent");
0931: } catch (GenericEntityException e) {
0932: Debug
0933: .logError(
0934: "Unable to get ProductContent from Product",
0935: module);
0936: }
0937: List cExprs = UtilMisc.toList(
0938: new EntityExpr(
0939: "productContentTypeId",
0940: EntityOperator.EQUALS,
0941: "DIGITAL_DOWNLOAD"),
0942: new EntityExpr(
0943: "productContentTypeId",
0944: EntityOperator.EQUALS,
0945: "FULFILLMENT_EMAIL"),
0946: new EntityExpr(
0947: "productContentTypeId",
0948: EntityOperator.EQUALS,
0949: "FULFILLMENT_EXTERNAL"));
0950: // add more as needed
0951: productContents = EntityUtil
0952: .filterByDate(productContents);
0953: productContents = EntityUtil.filterByOr(
0954: productContents, cExprs);
0955:
0956: if (productContents != null
0957: && productContents.size() > 0) {
0958: // make sure we are still within the allowed timeframe and use limits
0959: Iterator pci = productContents
0960: .iterator();
0961: while (pci.hasNext()) {
0962: GenericValue productContent = (GenericValue) pci
0963: .next();
0964: Timestamp fromDate = productContent
0965: .getTimestamp("purchaseFromDate");
0966: Timestamp thruDate = productContent
0967: .getTimestamp("purchaseThruDate");
0968: if (fromDate == null
0969: || item.getTimestamp(
0970: "orderDate").after(
0971: fromDate)) {
0972: if (thruDate == null
0973: || item
0974: .getTimestamp(
0975: "orderDate")
0976: .before(
0977: thruDate)) {
0978: // TODO: Implement use count and days
0979: digitalItems.add(item);
0980: }
0981: }
0982: }
0983: }
0984: }
0985: }
0986: }
0987: }
0988: }
0989: return digitalItems;
0990: }
0991:
0992: public List getOrderItemAdjustments(GenericValue orderItem) {
0993: return getOrderItemAdjustmentList(orderItem, getAdjustments());
0994: }
0995:
0996: public String getCurrentItemStatus(GenericValue orderItem) {
0997: GenericValue statusItem = null;
0998: try {
0999: statusItem = orderItem.getRelatedOne("StatusItem");
1000: } catch (GenericEntityException e) {
1001: Debug.logError(e, "Trouble getting StatusItem : "
1002: + orderItem, module);
1003: }
1004: if (statusItem == null || statusItem.get("description") == null) {
1005: return "Not Available";
1006: } else {
1007: return statusItem.getString("description");
1008: }
1009: }
1010:
1011: public List getOrderItemPriceInfos(GenericValue orderItem) {
1012: if (orderItem == null)
1013: return null;
1014: if (this .orderItemPriceInfos == null) {
1015: GenericDelegator delegator = orderHeader.getDelegator();
1016:
1017: try {
1018: orderItemPriceInfos = delegator.findByAnd(
1019: "OrderItemPriceInfo", UtilMisc.toMap("orderId",
1020: orderHeader.get("orderId")));
1021: } catch (GenericEntityException e) {
1022: Debug.logWarning(e, module);
1023: }
1024: }
1025: String orderItemSeqId = (String) orderItem
1026: .get("orderItemSeqId");
1027:
1028: return EntityUtil.filterByAnd(this .orderItemPriceInfos,
1029: UtilMisc.toMap("orderItemSeqId", orderItemSeqId));
1030: }
1031:
1032: public List getOrderItemInventoryReses(GenericValue orderItem) {
1033: if (orderItem == null)
1034: return null;
1035: if (this .orderItemInventoryReses == null) {
1036: GenericDelegator delegator = orderItem.getDelegator();
1037:
1038: try {
1039: orderItemInventoryReses = delegator.findByAnd(
1040: "OrderItemInventoryRes", UtilMisc.toMap(
1041: "orderId", orderItem.get("orderId")));
1042: } catch (GenericEntityException e) {
1043: Debug.logWarning(e,
1044: "Trouble getting OrderItemInventoryRes(s)",
1045: module);
1046: }
1047: }
1048: return EntityUtil.filterByAnd(orderItemInventoryReses, UtilMisc
1049: .toMap("orderItemSeqId", orderItem
1050: .getString("orderItemSeqId")));
1051: }
1052:
1053: public static List getOrderItemInventoryResFacilityIds(
1054: GenericValue orderHeader) {
1055: GenericDelegator delegator = orderHeader.getDelegator();
1056: List orderItems = null;
1057: List orderItemInventoryRes = new ArrayList();
1058: List result = new ArrayList();
1059:
1060: // filter for approved items only
1061: try {
1062: orderItems = delegator.findByAnd("OrderItem", UtilMisc
1063: .toMap("orderId", orderHeader.getString("orderId"),
1064: "statusId", "ITEM_APPROVED"));
1065: } catch (GenericEntityException e) {
1066: Debug.logError(e,
1067: "Cannot locate OrderItems from OrderHeader "
1068: + orderHeader.getString("orderId"), module);
1069: }
1070: if (UtilValidate.isNotEmpty(orderItems)) {
1071: Iterator oiIter = orderItems.iterator();
1072: GenericValue orderItem = null;
1073: List oiInventoryRes = null;
1074:
1075: while (oiIter.hasNext()) {
1076: orderItem = (GenericValue) oiIter.next();
1077:
1078: try {
1079: oiInventoryRes = orderItem
1080: .getRelated("OrderItemInventoryRes");
1081: } catch (GenericEntityException e) {
1082: Debug
1083: .logError(
1084: e,
1085: "Cannot locate OrderItemInventoryRes from OrderItem "
1086: + orderItem
1087: .getString("orderId")
1088: + " sequenceNum "
1089: + orderItem
1090: .getString("orderItemSeqId"),
1091: module);
1092: }
1093:
1094: if (UtilValidate.isNotEmpty(oiInventoryRes)) {
1095: orderItemInventoryRes.addAll(oiInventoryRes);
1096: }
1097: if (oiInventoryRes.size() > 1) {
1098: Debug
1099: .logWarning(
1100: "Warning - Should not use quickShip with more than one orderItemInventoryRes for order "
1101: + orderHeader
1102: .getString("orderId")
1103: + " item sequenceNum "
1104: + orderItem
1105: .get("orderItemsSeqId"),
1106: module);
1107: }
1108: }
1109: if (UtilValidate.isNotEmpty(orderItemInventoryRes)) {
1110: Iterator orderItemInventoryResIter = orderItemInventoryRes
1111: .iterator();
1112: GenericValue anInventoryRes = null;
1113:
1114: while (orderItemInventoryResIter.hasNext()) {
1115: anInventoryRes = (GenericValue) orderItemInventoryResIter
1116: .next();
1117: GenericValue inventoryItem = null;
1118:
1119: try {
1120: inventoryItem = delegator
1121: .findByPrimaryKey(
1122: "InventoryItem",
1123: UtilMisc
1124: .toMap(
1125: "inventoryItemId",
1126: anInventoryRes
1127: .getString("inventoryItemId")));
1128: } catch (GenericEntityException e) {
1129: Debug
1130: .logError(
1131: e,
1132: "Cannot locate InventoryItem for ID "
1133: + anInventoryRes
1134: .getString("inventoryItemId"),
1135: module);
1136: }
1137: result.add(inventoryItem.getString("facilityId"));
1138: }
1139: }
1140: }
1141: return result;
1142: }
1143:
1144: public List getOrderItemIssuances(GenericValue orderItem) {
1145: if (orderItem == null)
1146: return null;
1147: if (this .orderItemIssuances == null) {
1148: GenericDelegator delegator = orderItem.getDelegator();
1149:
1150: try {
1151: orderItemIssuances = delegator.findByAnd(
1152: "ItemIssuance", UtilMisc.toMap("orderId",
1153: orderItem.get("orderId")));
1154: } catch (GenericEntityException e) {
1155: Debug.logWarning(e, "Trouble getting ItemIssuance(s)",
1156: module);
1157: }
1158: }
1159: return EntityUtil.filterByAnd(orderItemIssuances, UtilMisc
1160: .toMap("orderItemSeqId", orderItem
1161: .getString("orderItemSeqId")));
1162: }
1163:
1164: public double getOrderReturnedQuantity() {
1165: GenericDelegator delegator = orderHeader.getDelegator();
1166: List returnedItems = null;
1167: try {
1168: returnedItems = delegator
1169: .findByAnd("ReturnItem", UtilMisc.toMap("orderId",
1170: orderHeader.getString("orderId")));
1171: } catch (GenericEntityException e) {
1172: Debug.logError(e, "Problem getting ReturnItem from order",
1173: module);
1174: return -1;
1175: }
1176:
1177: double returnedQuantity = 0.00;
1178: if (returnedItems != null) {
1179: Iterator i = returnedItems.iterator();
1180: while (i.hasNext()) {
1181: GenericValue returnedItem = (GenericValue) i.next();
1182: if (returnedItem.get("returnQuantity") != null) {
1183: returnedQuantity += returnedItem.getDouble(
1184: "returnQuantity").doubleValue();
1185: }
1186: }
1187: }
1188: return returnedQuantity;
1189: }
1190:
1191: public double getItemShippedQuantity(GenericValue orderItem) {
1192: double quantityShipped = 0.00;
1193: List issuance = getOrderItemIssuances(orderItem);
1194: if (issuance != null) {
1195: Iterator i = issuance.iterator();
1196: while (i.hasNext()) {
1197: GenericValue issue = (GenericValue) i.next();
1198: Double issueQty = issue.getDouble("quantity");
1199: if (issueQty != null) {
1200: quantityShipped += issueQty.doubleValue();
1201: }
1202: }
1203: }
1204: return quantityShipped;
1205: }
1206:
1207: public double getItemReservedQuantity(GenericValue orderItem) {
1208: double reserved = 0.00;
1209:
1210: List reses = getOrderItemInventoryReses(orderItem);
1211: if (reses != null) {
1212: Iterator i = reses.iterator();
1213: while (i.hasNext()) {
1214: GenericValue res = (GenericValue) i.next();
1215: Double quantity = res.getDouble("quantity");
1216: if (quantity != null) {
1217: reserved += quantity.doubleValue();
1218: }
1219: }
1220: }
1221: return reserved;
1222: }
1223:
1224: public double getItemBackorderedQuantity(GenericValue orderItem) {
1225: double backOrdered = 0.00;
1226:
1227: Timestamp shipDate = orderItem
1228: .getTimestamp("estimatedShipDate");
1229: Timestamp autoCancel = orderItem.getTimestamp("autoCancelDate");
1230:
1231: List reses = getOrderItemInventoryReses(orderItem);
1232: if (reses != null) {
1233: Iterator i = reses.iterator();
1234: while (i.hasNext()) {
1235: GenericValue res = (GenericValue) i.next();
1236: Timestamp promised = res
1237: .getTimestamp("currentPromisedDate");
1238: if (promised == null) {
1239: promised = res.getTimestamp("promisedDatetime");
1240: }
1241: if (autoCancel != null
1242: || (shipDate != null && shipDate
1243: .after(promised))) {
1244: Double resQty = res.getDouble("quantity");
1245: if (resQty != null) {
1246: backOrdered += resQty.doubleValue();
1247: }
1248: }
1249: }
1250: }
1251: return backOrdered;
1252: }
1253:
1254: public double getItemPendingShipmentQuantity(GenericValue orderItem) {
1255: double reservedQty = getItemReservedQuantity(orderItem);
1256: double backordered = getItemBackorderedQuantity(orderItem);
1257: return (reservedQty - backordered);
1258: }
1259:
1260: public double getItemCanceledQuantity(GenericValue orderItem) {
1261: Double cancelQty = orderItem.getDouble("cancelQuantity");
1262: if (cancelQty == null)
1263: cancelQty = new Double(0.0);
1264: return cancelQty.doubleValue();
1265: }
1266:
1267: public double getTotalOrderItemsQuantity() {
1268: List orderItems = getValidOrderItems();
1269: double totalItems = 0;
1270:
1271: for (int i = 0; i < orderItems.size(); i++) {
1272: GenericValue oi = (GenericValue) orderItems.get(i);
1273:
1274: totalItems += getOrderItemQuantity(oi).doubleValue();
1275: }
1276: return totalItems;
1277: }
1278:
1279: public double getTotalOrderItemsOrderedQuantity() {
1280: List orderItems = getValidOrderItems();
1281: double totalItems = 0;
1282:
1283: for (int i = 0; i < orderItems.size(); i++) {
1284: GenericValue oi = (GenericValue) orderItems.get(i);
1285:
1286: totalItems += oi.getDouble("quantity").doubleValue();
1287: }
1288: return totalItems;
1289: }
1290:
1291: public double getOrderItemsSubTotal() {
1292: return getOrderItemsSubTotal(getValidOrderItems(),
1293: getAdjustments());
1294: }
1295:
1296: public double getOrderItemSubTotal(GenericValue orderItem) {
1297: return getOrderItemSubTotal(orderItem, getAdjustments());
1298: }
1299:
1300: public double getOrderItemsTotal() {
1301: return getOrderItemsTotal(getValidOrderItems(),
1302: getAdjustments());
1303: }
1304:
1305: public double getOrderItemTotal(GenericValue orderItem) {
1306: return getOrderItemTotal(orderItem, getAdjustments());
1307: }
1308:
1309: public double getOrderItemTax(GenericValue orderItem) {
1310: return getOrderItemAdjustmentsTotal(orderItem, false, true,
1311: false);
1312: }
1313:
1314: public double getOrderItemShipping(GenericValue orderItem) {
1315: return getOrderItemAdjustmentsTotal(orderItem, false, false,
1316: true);
1317: }
1318:
1319: public double getOrderItemAdjustmentsTotal(GenericValue orderItem,
1320: boolean includeOther, boolean includeTax,
1321: boolean includeShipping) {
1322: return getOrderItemAdjustmentsTotal(orderItem,
1323: getAdjustments(), includeOther, includeTax,
1324: includeShipping);
1325: }
1326:
1327: public double getOrderItemAdjustmentsTotal(GenericValue orderItem) {
1328: return getOrderItemAdjustmentsTotal(orderItem, true, false,
1329: false);
1330: }
1331:
1332: public double getOrderItemAdjustmentTotal(GenericValue orderItem,
1333: GenericValue adjustment) {
1334: return calcItemAdjustment(adjustment, orderItem);
1335: }
1336:
1337: public String getAdjustmentType(GenericValue adjustment) {
1338: GenericValue adjustmentType = null;
1339: try {
1340: adjustmentType = adjustment
1341: .getRelatedOne("OrderAdjustmentType");
1342: } catch (GenericEntityException e) {
1343: Debug.logError(e, "Problems with order adjustment", module);
1344: }
1345: if (adjustmentType == null
1346: || adjustmentType.get("description") == null) {
1347: return "";
1348: } else {
1349: return adjustmentType.getString("description");
1350: }
1351: }
1352:
1353: public List getOrderItemStatuses(GenericValue orderItem) {
1354: return getOrderItemStatuses(orderItem, getOrderStatuses());
1355: }
1356:
1357: public String getCurrentItemStatusString(GenericValue orderItem) {
1358: GenericValue statusItem = null;
1359: try {
1360: statusItem = orderItem.getRelatedOneCache("StatusItem");
1361: } catch (GenericEntityException e) {
1362: Debug.logError(e, module);
1363: }
1364: if (statusItem != null) {
1365: return statusItem.getString("description");
1366: } else {
1367: return orderHeader.getString("statusId");
1368: }
1369: }
1370:
1371: /**
1372: * Checks to see if this user has read permission on this order
1373: * @param userLogin The UserLogin value object to check
1374: * @return boolean True if we have read permission
1375: */
1376: public boolean hasPermission(Security security,
1377: GenericValue userLogin) {
1378: return OrderReadHelper.hasPermission(security, userLogin,
1379: orderHeader);
1380: }
1381:
1382: /**
1383: * Getter for property orderHeader.
1384: * @return Value of property orderHeader.
1385: */
1386: public GenericValue getOrderHeader() {
1387: return orderHeader;
1388: }
1389:
1390: // ======================================================
1391: // =================== Static Methods ===================
1392: // ======================================================
1393:
1394: public static GenericValue getOrderHeader(
1395: GenericDelegator delegator, String orderId) {
1396: GenericValue orderHeader = null;
1397: if (orderId != null && delegator != null) {
1398: try {
1399: orderHeader = delegator.findByPrimaryKey("OrderHeader",
1400: UtilMisc.toMap("orderId", orderId));
1401: } catch (GenericEntityException e) {
1402: Debug.logError(e, "Cannot get order header", module);
1403: }
1404: }
1405: return orderHeader;
1406: }
1407:
1408: public static Double getOrderItemQuantity(GenericValue orderItem) {
1409: Double cancelQty = orderItem.getDouble("cancelQuantity");
1410: Double orderQty = orderItem.getDouble("quantity");
1411:
1412: if (cancelQty == null)
1413: cancelQty = new Double(0.0);
1414: if (orderQty == null)
1415: orderQty = new Double(0.0);
1416: return new Double(orderQty.doubleValue()
1417: - cancelQty.doubleValue());
1418: }
1419:
1420: public static GenericValue getProductStoreFromOrder(
1421: GenericDelegator delegator, String orderId) {
1422: return getProductStoreFromOrder(getOrderHeader(delegator,
1423: orderId));
1424: }
1425:
1426: public static GenericValue getProductStoreFromOrder(
1427: GenericValue orderHeader) {
1428: GenericDelegator delegator = orderHeader.getDelegator();
1429: GenericValue productStore = null;
1430: if (orderHeader != null
1431: && orderHeader.get("productStoreId") != null) {
1432: try {
1433: productStore = delegator.findByPrimaryKeyCache(
1434: "ProductStore", UtilMisc.toMap(
1435: "productStoreId", orderHeader
1436: .getString("productStoreId")));
1437: } catch (GenericEntityException e) {
1438: Debug.logError(e,
1439: "Cannot locate ProductStore from OrderHeader",
1440: module);
1441: }
1442: } else {
1443: Debug.logError("Null header or productStoreId", module);
1444: }
1445: return productStore;
1446: }
1447:
1448: public static double getOrderGrandTotal(List orderItems,
1449: List adjustments) {
1450: double total = getOrderItemsTotal(orderItems, adjustments);
1451: double adj = getOrderAdjustmentsTotal(orderItems, adjustments);
1452:
1453: return total + adj;
1454: }
1455:
1456: public static List getOrderHeaderAdjustments(List adjustments) {
1457: List contraints1 = UtilMisc.toList(new EntityExpr(
1458: "orderItemSeqId", EntityOperator.EQUALS, null));
1459: List contraints2 = UtilMisc.toList(new EntityExpr(
1460: "orderItemSeqId", EntityOperator.EQUALS,
1461: DataModelConstants.SEQ_ID_NA));
1462: List contraints3 = UtilMisc.toList(new EntityExpr(
1463: "orderItemSeqId", EntityOperator.EQUALS, ""));
1464: List adj = new LinkedList();
1465:
1466: adj.addAll(EntityUtil.filterByAnd(adjustments, contraints1));
1467: adj.addAll(EntityUtil.filterByAnd(adjustments, contraints2));
1468: adj.addAll(EntityUtil.filterByAnd(adjustments, contraints3));
1469: return adj;
1470: }
1471:
1472: public static List getOrderHeaderStatuses(List orderStatuses) {
1473: List contraints1 = UtilMisc.toList(new EntityExpr(
1474: "orderItemSeqId", EntityOperator.EQUALS, null));
1475: List contraints2 = UtilMisc.toList(new EntityExpr(
1476: "orderItemSeqId", EntityOperator.EQUALS,
1477: DataModelConstants.SEQ_ID_NA));
1478: List contraints3 = UtilMisc.toList(new EntityExpr(
1479: "orderItemSeqId", EntityOperator.EQUALS, ""));
1480: List newOrderStatuses = new LinkedList();
1481:
1482: newOrderStatuses.addAll(EntityUtil.filterByAnd(orderStatuses,
1483: contraints1));
1484: newOrderStatuses.addAll(EntityUtil.filterByAnd(orderStatuses,
1485: contraints2));
1486: newOrderStatuses.addAll(EntityUtil.filterByAnd(orderStatuses,
1487: contraints3));
1488: newOrderStatuses = EntityUtil.orderBy(newOrderStatuses,
1489: UtilMisc.toList("statusDatetime"));
1490: return newOrderStatuses;
1491: }
1492:
1493: public static double getOrderAdjustmentsTotal(List orderItems,
1494: List adjustments) {
1495: return calcOrderAdjustments(
1496: getOrderHeaderAdjustments(adjustments),
1497: getOrderItemsSubTotal(orderItems, adjustments), true,
1498: true, true);
1499: }
1500:
1501: // ================= Order Adjustments =================
1502:
1503: public static double calcOrderAdjustments(
1504: List orderHeaderAdjustments, double subTotal,
1505: boolean includeOther, boolean includeTax,
1506: boolean includeShipping) {
1507: double adjTotal = 0.0;
1508:
1509: if (orderHeaderAdjustments != null
1510: && orderHeaderAdjustments.size() > 0) {
1511: List filteredAdjs = filterOrderAdjustments(
1512: orderHeaderAdjustments, includeOther, includeTax,
1513: includeShipping, false, false);
1514: Iterator adjIt = filteredAdjs.iterator();
1515:
1516: while (adjIt.hasNext()) {
1517: GenericValue orderAdjustment = (GenericValue) adjIt
1518: .next();
1519:
1520: adjTotal += OrderReadHelper.calcOrderAdjustment(
1521: orderAdjustment, subTotal);
1522: }
1523: }
1524: return adjTotal;
1525: }
1526:
1527: public static double calcOrderAdjustment(
1528: GenericValue orderAdjustment, double orderSubTotal) {
1529: double adjustment = 0.0;
1530:
1531: if (orderAdjustment.get("amount") != null) {
1532: adjustment += orderAdjustment.getDouble("amount")
1533: .doubleValue();
1534: }
1535: if (orderAdjustment.get("percentage") != null) {
1536: adjustment += (orderAdjustment.getDouble("percentage")
1537: .doubleValue() * orderSubTotal);
1538: }
1539: return adjustment;
1540: }
1541:
1542: // ================= Order Item Adjustments =================
1543:
1544: public static double getOrderItemsSubTotal(List orderItems,
1545: List adjustments) {
1546: double result = 0.0;
1547: Iterator itemIter = UtilMisc.toIterator(orderItems);
1548:
1549: while (itemIter != null && itemIter.hasNext()) {
1550: GenericValue orderItem = (GenericValue) itemIter.next();
1551: double itemTotal = getOrderItemSubTotal(orderItem,
1552: adjustments);
1553: //Debug.log("Item : " + orderItem.getString("orderId") + " / " + orderItem.getString("orderItemSeqId") + " = " + itemTotal, module);
1554: result += itemTotal;
1555: }
1556: return result;
1557: }
1558:
1559: /** The passed adjustments can be all adjustments for the order, ie for all line items */
1560: public static double getOrderItemSubTotal(GenericValue orderItem,
1561: List adjustments) {
1562: return getOrderItemSubTotal(orderItem, adjustments, false,
1563: false);
1564: }
1565:
1566: /** The passed adjustments can be all adjustments for the order, ie for all line items */
1567: public static double getOrderItemSubTotal(GenericValue orderItem,
1568: List adjustments, boolean forTax, boolean forShipping) {
1569: Double unitPrice = orderItem.getDouble("unitPrice");
1570: Double quantity = getOrderItemQuantity(orderItem);
1571: double result = 0.0;
1572:
1573: if (unitPrice == null || quantity == null) {
1574: Debug
1575: .logWarning(
1576: "[getOrderItemTotal] unitPrice or quantity are null, using 0 for the item base price",
1577: module);
1578: } else {
1579: if (Debug.verboseOn())
1580: Debug.logVerbose("Unit Price : "
1581: + unitPrice.doubleValue() + " / "
1582: + "Quantity : " + quantity.doubleValue(),
1583: module);
1584: result = unitPrice.doubleValue() * quantity.doubleValue();
1585: }
1586:
1587: // subtotal also includes non tax and shipping adjustments; tax and shipping will be calculated using this adjusted value
1588: result += getOrderItemAdjustmentsTotal(orderItem, adjustments,
1589: true, false, false, forTax, forShipping);
1590:
1591: return result;
1592: }
1593:
1594: public static double getOrderItemsTotal(List orderItems,
1595: List adjustments) {
1596: double result = 0.0;
1597: Iterator itemIter = UtilMisc.toIterator(orderItems);
1598:
1599: while (itemIter != null && itemIter.hasNext()) {
1600: result += getOrderItemTotal((GenericValue) itemIter.next(),
1601: adjustments);
1602: }
1603: return result;
1604: }
1605:
1606: public static double getOrderItemTotal(GenericValue orderItem,
1607: List adjustments) {
1608: // add tax and shipping to subtotal
1609: return (getOrderItemSubTotal(orderItem, adjustments) + getOrderItemAdjustmentsTotal(
1610: orderItem, adjustments, false, true, true));
1611: }
1612:
1613: public static double getAllOrderItemsAdjustmentsTotal(
1614: List orderItems, List adjustments, boolean includeOther,
1615: boolean includeTax, boolean includeShipping) {
1616: double result = 0.0;
1617: Iterator itemIter = UtilMisc.toIterator(orderItems);
1618:
1619: while (itemIter != null && itemIter.hasNext()) {
1620: result += getOrderItemAdjustmentsTotal(
1621: (GenericValue) itemIter.next(), adjustments,
1622: includeOther, includeTax, includeShipping);
1623: }
1624: return result;
1625: }
1626:
1627: /** The passed adjustments can be all adjustments for the order, ie for all line items */
1628: public static double getOrderItemAdjustmentsTotal(
1629: GenericValue orderItem, List adjustments,
1630: boolean includeOther, boolean includeTax,
1631: boolean includeShipping) {
1632: return getOrderItemAdjustmentsTotal(orderItem, adjustments,
1633: includeOther, includeTax, includeShipping, false, false);
1634: }
1635:
1636: /** The passed adjustments can be all adjustments for the order, ie for all line items */
1637: public static double getOrderItemAdjustmentsTotal(
1638: GenericValue orderItem, List adjustments,
1639: boolean includeOther, boolean includeTax,
1640: boolean includeShipping, boolean forTax, boolean forShipping) {
1641: return calcItemAdjustments(getOrderItemQuantity(orderItem),
1642: orderItem.getDouble("unitPrice"),
1643: getOrderItemAdjustmentList(orderItem, adjustments),
1644: includeOther, includeTax, includeShipping, false, false);
1645: }
1646:
1647: public static List getOrderItemAdjustmentList(
1648: GenericValue orderItem, List adjustments) {
1649: return EntityUtil.filterByAnd(adjustments, UtilMisc.toMap(
1650: "orderItemSeqId", orderItem.get("orderItemSeqId")));
1651: }
1652:
1653: public static List getOrderItemStatuses(GenericValue orderItem,
1654: List orderStatuses) {
1655: return EntityUtil.filterByAnd(orderStatuses, UtilMisc.toMap(
1656: "orderItemSeqId", orderItem.get("orderItemSeqId")));
1657: }
1658:
1659: // Order Item Adjs Utility Methods
1660:
1661: public static double calcItemAdjustments(Double quantity,
1662: Double unitPrice, List adjustments, boolean includeOther,
1663: boolean includeTax, boolean includeShipping,
1664: boolean forTax, boolean forShipping) {
1665: double adjTotal = 0.0;
1666:
1667: if (adjustments != null && adjustments.size() > 0) {
1668: List filteredAdjs = filterOrderAdjustments(adjustments,
1669: includeOther, includeTax, includeShipping, forTax,
1670: forShipping);
1671: Iterator adjIt = filteredAdjs.iterator();
1672:
1673: while (adjIt.hasNext()) {
1674: GenericValue orderAdjustment = (GenericValue) adjIt
1675: .next();
1676:
1677: adjTotal += OrderReadHelper.calcItemAdjustment(
1678: orderAdjustment, quantity, unitPrice);
1679: }
1680: }
1681: return adjTotal;
1682: }
1683:
1684: public static double calcItemAdjustment(
1685: GenericValue itemAdjustment, GenericValue item) {
1686: return calcItemAdjustment(itemAdjustment,
1687: getOrderItemQuantity(item), item.getDouble("unitPrice"));
1688: }
1689:
1690: public static double calcItemAdjustment(
1691: GenericValue itemAdjustment, Double quantity,
1692: Double unitPrice) {
1693: double adjustment = 0.0;
1694:
1695: if (itemAdjustment.get("amount") != null) {
1696: adjustment += itemAdjustment.getDouble("amount")
1697: .doubleValue();
1698: }
1699: if (itemAdjustment.get("amountPerQuantity") != null
1700: && quantity != null) {
1701: adjustment += itemAdjustment.getDouble("amountPerQuantity")
1702: .doubleValue()
1703: * quantity.doubleValue();
1704: }
1705: if (itemAdjustment.get("percentage") != null
1706: && unitPrice != null) {
1707: adjustment += (itemAdjustment.getDouble("percentage")
1708: .doubleValue() * unitPrice.doubleValue());
1709: }
1710: if (Debug.verboseOn())
1711: Debug.logVerbose("calcItemAdjustment: " + itemAdjustment
1712: + ", quantity=" + quantity + ", unitPrice="
1713: + unitPrice + ", adjustment=" + adjustment, module);
1714: return adjustment;
1715: }
1716:
1717: public static List filterOrderAdjustments(List adjustments,
1718: boolean includeOther, boolean includeTax,
1719: boolean includeShipping, boolean forTax, boolean forShipping) {
1720: List newOrderAdjustmentsList = new LinkedList();
1721:
1722: if (adjustments != null && adjustments.size() > 0) {
1723: Iterator adjIt = adjustments.iterator();
1724:
1725: while (adjIt.hasNext()) {
1726: GenericValue orderAdjustment = (GenericValue) adjIt
1727: .next();
1728:
1729: boolean includeAdjustment = false;
1730:
1731: if ("SALES_TAX".equals(orderAdjustment
1732: .getString("orderAdjustmentTypeId"))) {
1733: if (includeTax)
1734: includeAdjustment = true;
1735: } else if ("SHIPPING_CHARGES".equals(orderAdjustment
1736: .getString("orderAdjustmentTypeId"))) {
1737: if (includeShipping)
1738: includeAdjustment = true;
1739: } else {
1740: if (includeOther)
1741: includeAdjustment = true;
1742: }
1743:
1744: // default to yes, include for shipping; so only exclude if includeInShipping is N, or false; if Y or null or anything else it will be included
1745: if (forTax
1746: && "N".equals(orderAdjustment
1747: .getString("includeInTax"))) {
1748: includeAdjustment = false;
1749: }
1750:
1751: // default to yes, include for shipping; so only exclude if includeInShipping is N, or false; if Y or null or anything else it will be included
1752: if (forShipping
1753: && "N".equals(orderAdjustment
1754: .getString("includeInShipping"))) {
1755: includeAdjustment = false;
1756: }
1757:
1758: if (includeAdjustment) {
1759: newOrderAdjustmentsList.add(orderAdjustment);
1760: }
1761: }
1762: }
1763: return newOrderAdjustmentsList;
1764: }
1765:
1766: /**
1767: * Checks to see if this user has read permission on the specified order
1768: * @param userLogin The UserLogin value object to check
1769: * @param orderHeader The OrderHeader for the specified order
1770: * @return boolean True if we have read permission
1771: */
1772: public static boolean hasPermission(Security security,
1773: GenericValue userLogin, GenericValue orderHeader) {
1774: if (userLogin == null || orderHeader == null)
1775: return false;
1776:
1777: if (security
1778: .hasEntityPermission("ORDERMGR", "_VIEW", userLogin)) {
1779: return true;
1780: } else if (security.hasEntityPermission("ORDERMGR",
1781: "_ROLEVIEW", userLogin)) {
1782: List orderRoles = null;
1783: try {
1784: orderRoles = orderHeader.getRelatedByAnd("OrderRole",
1785: UtilMisc.toMap("partyId", userLogin
1786: .getString("partyId")));
1787: } catch (GenericEntityException e) {
1788: Debug
1789: .logError(
1790: e,
1791: "Cannot get OrderRole from OrderHeader",
1792: module);
1793: }
1794:
1795: if (orderRoles.size() > 0) {
1796: // we are in at least one role
1797: return true;
1798: }
1799: }
1800:
1801: return false;
1802: }
1803:
1804: public static OrderReadHelper getHelper(GenericValue orderHeader) {
1805: return new OrderReadHelper(orderHeader);
1806: }
1807: }
|