1 16 17 18 39 package com.opensourcestrategies.crmsfa.opportunities; 40 41 import java.util.Map ; 42 import java.util.List ; 43 import java.util.Iterator ; 44 import java.util.ArrayList ; 45 import java.util.Locale ; 46 47 import org.ofbiz.base.util.Debug; 48 import org.ofbiz.base.util.UtilMisc; 49 import org.ofbiz.base.util.UtilDateTime; 50 import org.ofbiz.common.period.PeriodWorker; 51 import org.ofbiz.entity.*; 52 import org.ofbiz.entity.condition.*; 53 import org.ofbiz.entity.util.*; 54 55 60 61 public class UtilOpportunity { 62 63 public static final String module = UtilOpportunity.class.getName(); 64 65 70 public static String getOpportunityAccountPartyId(GenericValue opportunity) throws GenericEntityException { 71 List candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "ACCOUNT")); 72 if (candidates.size() == 0) return null; 73 GenericValue salesOpportunityRole = (GenericValue) candidates.get(0); 75 return salesOpportunityRole.getString("partyId"); 76 } 77 78 83 public static String getOpportunityLeadPartyId(GenericValue opportunity) throws GenericEntityException { 84 List candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "PROSPECT")); 85 if (candidates.size() == 0) return null; 86 GenericValue salesOpportunityRole = (GenericValue) candidates.get(0); 88 return salesOpportunityRole.getString("partyId"); 89 } 90 91 92 public static String getOpportunityAccountOrLeadPartyId(GenericValue opportunity) throws GenericEntityException { 93 List candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "ACCOUNT")); 94 if (candidates.size() > 0) return ((GenericValue) candidates.get(0)).getString("partyId"); 95 candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "PROSPECT")); 96 if (candidates.size() > 0) return ((GenericValue) candidates.get(0)).getString("partyId"); 97 return null; 98 } 99 100 104 public static List getOpportunityAccountPartyIds(GenericDelegator delegator, String salesOpportunityId) throws GenericEntityException { 105 return getOpportunityPartiesByRole(delegator, salesOpportunityId, "ACCOUNT"); 106 } 107 108 109 public static List getOpportunityLeadPartyIds(GenericDelegator delegator, String salesOpportunityId) throws GenericEntityException { 110 return getOpportunityPartiesByRole(delegator, salesOpportunityId, "PROSPECT"); 111 } 112 113 114 public static List getOpportunityContactPartyIds(GenericDelegator delegator, String salesOpportunityId) throws GenericEntityException { 115 return getOpportunityPartiesByRole(delegator, salesOpportunityId, "CONTACT"); 116 } 117 118 119 public static List getOpportunityPartiesByRole(GenericDelegator delegator, String salesOpportunityId, String roleTypeId) throws GenericEntityException { 120 List maps = delegator.findByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", roleTypeId, "salesOpportunityId", salesOpportunityId)); 121 List results = new ArrayList (); 122 for (Iterator iter = maps.iterator(); iter.hasNext(); ) { 123 GenericValue map = (GenericValue) iter.next(); 124 results.add(map.getString("partyId")); 125 } 126 return results; 127 } 128 129 133 public static GenericValue createSalesOpportunityHistory(GenericValue opportunity, GenericDelegator delegator, Map context) throws GenericEntityException { 134 GenericValue userLogin = (GenericValue) context.get("userLogin"); 135 String historyId = delegator.getNextSeqId("SalesOpportunityHistory"); 136 GenericValue history = delegator.makeValue("SalesOpportunityHistory", UtilMisc.toMap("salesOpportunityHistoryId", historyId)); 137 138 history.setNonPKFields(opportunity.getAllFields()); 140 history.set("changeNote", context.get("changeNote")); 141 history.set("modifiedByUserLogin", userLogin.getString("userLoginId")); 142 history.set("modifiedTimestamp", UtilDateTime.nowTimestamp()); 143 history.create(); 144 return history; 145 } 146 147 160 public static EntityListIterator getOpportunitiesForMyAccounts(String organizationPartyId, String internalPartyId, String customTimePeriodId, 161 EntityConditionList additionalConditions, List orderBy, GenericDelegator delegator) 162 throws GenericEntityException { 163 164 List combinedConditions = UtilMisc.toList( 166 new EntityExpr("partyIdTo", EntityOperator.EQUALS, internalPartyId), 167 new EntityExpr("partyRelationshipTypeId", EntityOperator.EQUALS, "RESPONSIBLE_FOR"), 168 new EntityConditionList( UtilMisc.toList( 169 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "PROSPECT"), 170 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "ACCOUNT") 171 ), EntityOperator.OR), 172 EntityUtil.getFilterByDateExpr()); 174 return getOpportunitiesForPartyHelper(customTimePeriodId, combinedConditions, additionalConditions, orderBy, delegator); 175 } 176 177 181 public static EntityListIterator getOpportunitiesForMyTeams(String organizationPartyId, String internalPartyId, String customTimePeriodId, 182 EntityConditionList additionalConditions, List orderBy, GenericDelegator delegator) throws GenericEntityException { 183 184 EntityConditionList conditions = new EntityConditionList( UtilMisc.toList( 186 new EntityExpr("partyIdTo", EntityOperator.EQUALS, internalPartyId), 187 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "ACCOUNT"), 188 new EntityConditionList( UtilMisc.toList( 189 new EntityExpr("partyRelationshipTypeId", EntityOperator.EQUALS, "RESPONSIBLE_FOR"), 190 new EntityExpr("partyRelationshipTypeId", EntityOperator.EQUALS, "ASSIGNED_TO") 191 ), EntityOperator.OR), 192 EntityUtil.getFilterByDateExpr() 193 ), EntityOperator.AND); 194 List accounts = delegator.findByCondition("PartyRelationship", conditions, null, null); 195 ArrayList accountIds = new ArrayList (); 196 for (Iterator iter = accounts.iterator(); iter.hasNext(); ) { 197 GenericValue account = (GenericValue) iter.next(); 198 accountIds.add(account.get("partyIdFrom")); 199 } 200 201 if (accountIds.size() < 1) { 203 return null; 204 } 205 206 List combinedConditions = UtilMisc.toList( 208 new EntityExpr("partyIdFrom", EntityOperator.IN, accountIds), 209 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "ACCOUNT") 210 ); 211 212 return getOpportunitiesForPartyHelper(customTimePeriodId, combinedConditions, additionalConditions, orderBy, delegator); 213 } 214 215 220 public static EntityListIterator getOpportunitiesForInternalParty(String organizationPartyId, String internalPartyId, String customTimePeriodId, 221 EntityConditionList additionalConditions, List orderBy, GenericDelegator delegator) 222 throws GenericEntityException { 223 224 List combinedConditions = UtilMisc.toList( 226 new EntityExpr("partyIdTo", EntityOperator.EQUALS, internalPartyId), 227 new EntityConditionList( UtilMisc.toList( 228 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "ACCOUNT"), 229 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "PROSPECT") 230 ), EntityOperator.OR), 231 EntityUtil.getFilterByDateExpr()); 233 return getOpportunitiesForPartyHelper(customTimePeriodId, combinedConditions, additionalConditions, orderBy, delegator); 234 } 235 236 237 private static EntityListIterator getOpportunitiesForPartyHelper(String customTimePeriodId, List combinedConditions, 238 EntityConditionList additionalConditions, List orderBy, GenericDelegator delegator) throws GenericEntityException { 239 if ((customTimePeriodId != null)) { 241 GenericValue timePeriod = delegator.findByPrimaryKeyCache("CustomTimePeriod", UtilMisc.toMap("customTimePeriodId", customTimePeriodId)); 242 if (timePeriod != null) { 243 combinedConditions.add(PeriodWorker.getFilterByPeriodExpr("estimatedCloseDate", timePeriod)); 244 } 245 } 246 247 if (additionalConditions != null) { 249 combinedConditions.add(additionalConditions); 250 } 251 EntityConditionList conditionList = new EntityConditionList(combinedConditions, EntityOperator.AND); 252 253 List fields = UtilMisc.toList("salesOpportunityId", "partyIdFrom", "opportunityName", "opportunityStageId", "estimatedAmount", "estimatedCloseDate"); 255 fields.add("estimatedProbability"); 256 fields.add("currencyUomId"); 257 258 EntityListIterator opportunities = delegator.findListIteratorByCondition("PartyRelationshipAndSalesOpportunity", conditionList, null, 260 fields, 261 orderBy, new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true)); 265 266 return opportunities; 267 } 268 269 273 public static List getOpportunityTeamMembers(String salesOpportunityId, GenericDelegator delegator) throws GenericEntityException { 274 GenericValue opportunity = delegator.findByPrimaryKey("SalesOpportunity", UtilMisc.toMap("salesOpportunityId", salesOpportunityId)); 277 String accountPartyId = getOpportunityAccountPartyId(opportunity); 278 279 EntityConditionList conditions = new EntityConditionList(UtilMisc.toList( 280 new EntityExpr("partyIdFrom", EntityOperator.EQUALS, accountPartyId), 281 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "ACCOUNT"), 282 new EntityExpr("partyRelationshipTypeId", EntityOperator.EQUALS, "ASSIGNED_TO"), 283 EntityUtil.getFilterByDateExpr() 284 ), EntityOperator.AND); 285 return delegator.findByConditionCache("PartyToSummaryByRelationship", conditions, null, null); 286 } 287 288 289 } 290 | Popular Tags |