001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.opportunity;
018:
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Vector;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntityException;
026: import org.ofbiz.entity.GenericValue;
027:
028: import com.sourcetap.sfa.event.EventUtility;
029: import com.sourcetap.sfa.ui.UICache;
030: import com.sourcetap.sfa.ui.UIFieldInfo;
031: import com.sourcetap.sfa.ui.UIWebScreenSection;
032: import com.sourcetap.sfa.util.UserInfo;
033:
034: /**
035: * DOCUMENT ME!
036: *
037: */
038: public class UIWebScreenSectionOpportunity extends UIWebScreenSection {
039: public static final String module = UIWebScreenSectionOpportunity.class
040: .getName();
041:
042: public UIWebScreenSectionOpportunity(UserInfo userInfo,
043: String screenName, String sectionName,
044: GenericDelegator delegator, UICache uiCache)
045: throws GenericEntityException {
046: super (userInfo, screenName, sectionName, delegator, uiCache);
047: }
048:
049: /**
050: * DOCUMENT ME!
051: *
052: * @param fieldInfo
053: * @param entityDetailsVector
054: * @param action
055: * @param row
056: * @param isSubsection
057: * @param tabOffset
058: *
059: * @return
060: */
061: public String displayField(UIFieldInfo fieldInfo,
062: Vector entityDetailsVector, String action, int row,
063: boolean isSubsection, int tabOffset) {
064: if (fieldInfo.getUiAttribute().getAttributeName().trim()
065: .equals("amount")) {
066: GenericValue fieldEntityDetails = (GenericValue) entityDetailsVector
067: .get(0);
068:
069: String useProductsForAmount = (fieldEntityDetails
070: .get("useProductsForAmount") == null) ? "N"
071: : fieldEntityDetails
072: .getString("useProductsForAmount");
073:
074: if (useProductsForAmount.equalsIgnoreCase("Y")) {
075: // Get Opportunity Products for the opportunity and add up their amounts.
076: double amount = 0.0;
077:
078: try {
079: HashMap dealMap = new HashMap();
080: dealMap.put("dealId", fieldEntityDetails
081: .getString("dealId"));
082:
083: List opportunityProductList = delegator.findByAnd(
084: "OpportunityProduct", dealMap, null);
085: GenericValue[] oppProds = (GenericValue[]) opportunityProductList
086: .toArray(new GenericValue[0]);
087: GenericValue oppProd = null;
088:
089: long quantity = 0;
090:
091: for (int i = 0; i < oppProds.length; i++) {
092: oppProd = oppProds[i];
093: quantity = (((oppProd.getString("quantity") == null) || oppProd
094: .getString("quantity")
095: .equalsIgnoreCase("null")) ? 1
096: : oppProd.getLong("quantity")
097: .longValue());
098:
099: // Get the quantity * price
100: if ((oppProd.getString("quantity") == null)
101: || (oppProd.getString("defaultPrice") == null)) {
102: amount = amount
103: + (oppProd
104: .getDouble("defaultPrice")
105: .doubleValue() * quantity);
106: } else if (oppProd.getString("amount") != null) {
107: amount = amount
108: + oppProd.getDouble("amount")
109: .doubleValue();
110: } else {
111: amount = amount + 0.0;
112: }
113: }
114: } catch (GenericEntityException e) {
115: Debug
116: .logError(
117: "[UIWebScreenSectionOpportunity.displayField]: Error occurred while searching for opportunity products: ",
118: module);
119: Debug.logError(" " + e.getLocalizedMessage(),
120: module);
121: }
122:
123: // Store the new amount in the generic value so the super can display the new amount.
124:
125: fieldEntityDetails.set("amount", EventUtility
126: .parseNumber(String.valueOf(amount)));
127: }
128: }
129:
130: return super.displayField(fieldInfo, entityDetailsVector,
131: action, row, isSubsection, tabOffset);
132: }
133: }
|