Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************
- Description : CPQ_PRD04_SoldServiceMappingHlpr class
- Created by : Bartosz Walczak (bartosz.walczak@enxoo.com)
- Date created : 2016-03-23
- Revision :
- 2016-03-23 Bartosz Walczak (bartosz.walczak@enxoo.com) Class created
- 2017-02-06 Maciej Simm (maciej.simm@enxoo.com) Adding logic for custom Colocation Reference handling (locations)
- 2017-03-07 Mateusz Osiak (mateusz.osiak@enxoo.com) Adding logic for automation of delivery comm templates.
- 2017-03-20 Tomasz Bogdanski (tomasz.bogdanski@enxoo.com) SUN-1191 Adding batch support for createSoldServices method for huge opportunities.
- 2017-03-22 Maciej Simm (maciej.simm@enxoo.com) SUN-726 Professional Solutions - removing Colo Ref logic & adding A/B End Handling
- ************************************************************************/
- public without sharing class CPQ_PRD04_SoldServiceMappingHlpr {
- public static final String RT_QUITEM_PRODUCT = 'Product';
- public static final String RT_QUITEM_COMPONENT = 'Component';
- public static final String RT_SSERVICE_COMMERCIAL = 'TC CommercialProduct v1';
- public static final String RT_SSERVICE_OPERATIONAL_DI = 'TC OperationalProduct D&I v1';
- public static final String RT_SSERVICE_OPERATIONAL_VM = 'TC OperationalProduct V&M v1';
- public static final String RT_SSERVICE_INTERNAL = 'TC InternalProduct v1';
- public static final String PROD_TYPE_COMMERCIAL = 'CommercialProduct';
- public static final String PROD_TYPE_OPERATIONAL = 'OperationalProduct';
- public static final String PROD_TYPE_INTERNAL = 'InternalProduct';
- public static final String ACTION_ADD = 'Add';
- public static final String ACTION_CHANGE = 'Change';
- public static final String ACTION_DISMANTLE = 'Dismantle';
- public static final String ACTION_RENEWAL = 'Renewal';
- public static final String ACTION_CEASE = 'Cease & Reprovide';
- public static final String ACTION_NO_ACTION = 'No Action';
- public static final String STAGE_NEW = '10. Review & Plan';
- public static final String STAGE_RENEWAL = '29. Renewal in Waiting';
- public static final String STAGE_DISMANTLE = '95. Pending Dismantle';
- public static final String STAGE_REPLACEMENT = '80. Pending Replacement';
- public static final String TPS_STATUS_REPLACED = '29 Renewal in Waiting';
- public static final String TPS_STATUS_DISMANTLE = '90 Pending Dismantle';
- public static final String TPI_STATUS_QUOTE_REQ = '2. Quote Required';
- public static final String TPS_PRIMARY_PROD_SITE = 'Site & Facility';
- public static final String ATTRIBUTE_PORTS_GRID = 'ATR_IPPORTS_';
- public static final String ATTRIBUTE_DATE_OF_CHANGE = 'ATR_DATEOFCHANGE_';
- public static final String ATTRIBUTE_BILLING_COMM = 'ATR_BILLINGCOMENCEMENTDATE_';
- public static final String ATTRIBUTE_CONTRACT_LENGTH = 'ATR_LENGTHOFCONTRACTMONTHS_';
- public static final String ATTRIBUTE_DUALLINK_PREFIX = 'ATR_DUALLINK';
- public static final String PORT_PREFIX = 'requested';
- public static final String DISMANTLE_TXT = 'Dismantle as part of renewal';
- public static final String CEASEANDREPROVIDE_TXT = 'From Cease & Reprovide';
- public static final String CEASE_HOT_CUT = 'Hot Cut';
- public static final String CEASE_PARALLEL_BUILD = 'Parallel Build';
- public static final String TPI_ORDER_TYPE_DISMANTLE = 'Dismantle';
- public static final Integer QUANTITY_SUM_TRESHOLD = Integer.valueOf(System.Label.CPQ_Opportunity_Quote_Quantity_Batch_Treshold);
- public static final Integer BATCH_SIZE = Integer.valueOf(System.Label.CPQ_Opportunity_Close_Batch_Size);
- public static Set<Id> batchOpportunities = new Set<Id>();
- public static final String createQuoteItemsQuery = 'select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('CPQ_QuoteItem__c', null, null) + ', RecordType.Name, Product_Definition__r.Product_Group__c, Product_Definition__r.Product_Type__c, Opportunity__r.CloseDate, Opportunity__r.Commercial_Contact__c, Opportunity__r.Commercial_Contact__r.Name, ' +
- 'Product_Definition__r.Name, Component_Definition__r.Name, Existing_Object_ID__r.Name, Existing_Object_ID__r.RecordTypeId, Existing_Object_ID__r.Item_Type__c, Existing_Object_ID__r.Renewed_To__c, Existing_Object_ID__r.Renewed_To__r.Status__c, ' +
- 'A_Port__r.Port_Type__c, A_Port__r.Homing_Gateway__c, A_Port__r.Homing_Gateway__r.Continent__c, B_Port__r.Port_Type__c, B_Port__r.Homing_Gateway__c, B_Port__r.Homing_Gateway__r.Continent__c, A_Site__r.Name, B_Site__r.Name, ' +
- '(select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('CPQ_QuoteItemAttribute__c', null, null) + ', Value_Attribute__r.Name, Value_Attribute__r.TECH_Value_ID__c from QuoteItemAttributes__r), ' +
- '(select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('Port_Change_Request__c', null, null) + ' from Port_Change_Requests__r), ' +
- '(select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('X3rd_Party_Item__c', null, null) + ', Parent__r.Upsert_Key__c from X3rd_Party_Items__r) ' +
- 'from CPQ_QuoteItem__c where Opportunity__c = :opportunityId and Primary__c = true order by Order__c';
- private static Map<String,Id> contactIdByRole = new Map<String,Id>();
- public static void createSoldServices(Id opportunityId) {
- System.debug('@@ START CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: createSoldServices');
- /* loading selected Quote Items with Attributes to create Sold Services */
- List<CPQ_QuoteItem__c> quoteItemsList = Database.query(createQuoteItemsQuery);
- Decimal itemsQuantitySum = 0;
- for(CPQ_QuoteItem__c qi : quoteItemsList) {
- if(qi.Quantity__c != null) {
- itemsQuantitySum+=qi.Quantity__c;
- }
- }
- if(itemsQuantitySum < 100) {
- createSoldServices(quoteItemsList, opportunityId, new Map<String,Id>());
- } else {
- List<CPQ_OpportunityQuoteItem__c> quantityHelperList = new List<CPQ_OpportunityQuoteItem__c>();
- for(CPQ_QuoteItem__c qi : quoteItemsList) {
- if(qi.Quantity__c != null) {
- for(Integer i=1; i<=qi.Quantity__c; i++) {
- CPQ_OpportunityQuoteItem__c newQuantityHelper = new CPQ_OpportunityQuoteItem__c(Opportunity__c = opportunityId, Quote_Item__c = qi.Id);
- quantityHelperList.add(newQuantityHelper);
- }
- }
- }
- insert quantityHelperList;
- CPQ_PRD08_CreateSoldServiceBatchHlpr batch = new CPQ_PRD08_CreateSoldServiceBatchHlpr(opportunityId);
- Database.executeBatch(batch, BATCH_SIZE);
- batchOpportunities.add(opportunityId);
- }
- }
- public static createSoldServicesResult createSoldServices(List<CPQ_QuoteItem__c> quoteItemsList, Id opportunityId, Map<String, Id> quoteItemToSoldService) {
- createSoldServicesResult result = new createSoldServicesResult();
- Map<Id, OpportunityLineItem> oppLineItems = new Map<Id, OpportunityLineItem>([select Id, Product2.Name from OpportunityLineItem where OpportunityId = :opportunityId]);
- List<CPQ_QuoteItem__c> products = new List<CPQ_QuoteItem__c>();
- Map<Id, List<CPQ_QuoteItem__c>> components = new Map<Id, List<CPQ_QuoteItem__c>>();
- Set<Id> productIds = new Set<Id>();
- Set<Id> soldServiceIds = new Set<Id>();
- Set<Id> portsToCloneIds = new Set<Id>();
- for(CPQ_QuoteItem__c qItem : quoteItemsList) {
- if(qItem.RecordType.Name == RT_QUITEM_PRODUCT) {
- products.add(qItem);
- productIds.add(qItem.Product_Definition__c);
- } else if(qItem.RecordType.Name == RT_QUITEM_COMPONENT) {
- List<CPQ_QuoteItem__c> componentsList = components.get(qItem.Parent_Item__c);
- if(componentsList == null) {
- componentsList = new List<CPQ_QuoteItem__c>();
- components.put(qItem.Parent_Item__c, componentsList);
- }
- componentsList.add(qItem);
- }
- if(qItem.Existing_Object_ID__c != null) {
- soldServiceIds.add(qItem.Existing_Object_ID__c);
- }
- if(qItem.Quantity__c != null && qItem.Quantity__c > 1) {
- if(qItem.A_Port__c != null && qItem.A_Port_Option__c == 'New Port') {
- portsToCloneIds.add(qItem.A_Port__c);
- }
- if(qItem.B_Port__c != null && qItem.B_Port_Option__c == 'New Port') {
- portsToCloneIds.add(qItem.B_Port__c);
- }
- }
- }
- Map<Id, Port__c> portsToClone = new Map<Id, Port__c>();
- if(!portsToCloneIds.isEmpty()) {
- String portsToCloneQuery = 'select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('Port__c', null, null) + ' from Port__c where Id in :portsToCloneIds';
- portsToClone = new Map<Id, Port__c>((List<Port__c>)Database.query(portsToCloneQuery));
- }
- /* loading mapping configuration */
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Attributes = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Fields = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Constants = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Numbers = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- Map<String, String> typeOfServiceToproductCode = new Map<String, String>();
- /* loading product codes from Custom Setting */
- for(CPQ_Mapping__c m : CPQ_Mapping__c.getall().values()) {
- if(m.Setting__c == CPQ_Utils.CPQ_MAPPING_PRODUCT_CODE) {
- typeOfServiceToproductCode.put(m.Col1__c, m.Col2__c);
- }
- }
- for(CPQ_Sold_Service_Mapping__c mapping : [select Id, Attribute__c, Attribute__r.TECH_Definition_ID__c, Attribute__r.Grid_Layout__c, Attribute__r.Type__c, Component__c, Data_Type__c, Generate_ICB_Number__c, Generate_ICC_Number__c, Generate_IC_Number__c, Product__c, Update_on_Change__c,
- Source_Constant_Value__c, Source_Field_Name__c, Target_Field_OLI__c, Target_Field_Sold_Service__c from CPQ_Sold_Service_Mapping__c where Product__c in :productIds]){
- Map<Id, List<CPQ_Sold_Service_Mapping__c>> componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_ATTRIBUTE_MAPPING) {
- componentMappings = soldServiceMappings_Attributes.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Attributes.put(mapping.Product__c, componentMappings);
- }
- } else if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_FIELD_MAPPING) {
- componentMappings = soldServiceMappings_Fields.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Fields.put(mapping.Product__c, componentMappings);
- }
- } else if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_CONSTANT_MAPPING) {
- componentMappings = soldServiceMappings_Constants.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Constants.put(mapping.Product__c, componentMappings);
- }
- } else if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_NUMBER_MAPPING) {
- componentMappings = soldServiceMappings_Numbers.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Numbers.put(mapping.Product__c, componentMappings);
- }
- }
- List<CPQ_Sold_Service_Mapping__c> mappings = componentMappings.get(mapping.Component__c);
- if(mappings == null) {
- mappings = new List<CPQ_Sold_Service_Mapping__c>();
- componentMappings.put(mapping.Component__c, mappings);
- }
- mappings.add(mapping);
- }
- /* initializing codes generator */
- DynamicValuesWrapper codes = new DynamicValuesWrapper(ApplicationConstant.IC_PREFIX);
- Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
- /* initializing fields for TPI -> TPS mapping */
- Map<String,String> tpsFieldMap = SoldServiceUtility.buildFieldMapping(ApplicationConstant.FIELD_MAPPING_THIRDPARTY);
- Id tpiReadOnlyRecordType = RecordTypeUtility.getRecordTypeId(X3rd_Party_Item__c.SObjectType, ApplicationConstant.RT_X3RDPARTYITEM_DEFAULT_READONLY_V1, 'X3rd_Party_Item__c');
- /* processing Quote Items */
- List<Sold_Service__c> soldServiceProductsToUpsert = new List<Sold_Service__c>();
- List<Sold_Service__c> soldServiceComponentsToUpsert = new List<Sold_Service__c>();
- List<X3rd_Party_Services__c> thirdPartyServicesToUpsert = new List<X3rd_Party_Services__c>();
- Map<Id, X3rd_Party_Services__c> thirdPartyServicesToUpsert2 = new Map<Id, X3rd_Party_Services__c>();
- List<X3rd_Party_Services__c> thirdPartyServicesToUpsertParent = new List<X3rd_Party_Services__c>();
- List<X3rd_Party_Item__c> thirdPartyItemsToUpdate = new List<X3rd_Party_Item__c>();
- List<CPQ_Charge__c> chargesToUpsert = new List<CPQ_Charge__c>();
- List<CPQ_Charge__c> chargesToDelete = new List<CPQ_Charge__c>();
- List<Port__c> portsToCreate = new List<Port__c>();
- Map<Id, Port_Change_Request__c> portCRsToUpsert = new Map<Id, Port_Change_Request__c>();
- Set<Id> tpisAlreadyAdded = new Set<Id>();
- Set<Id> portIds = new Set<Id>();
- Set<String> portExternalIds = new Set<String>();
- /* map of Quote Item ID to Upsert Key of created Sold Service */
- Map<Id, String> quoteItemId2SoldServiceExternalId = new Map<Id, String>();
- /* map of Sold Service Upsert Key to Quote Item ID corresponding to related Dual Link service */
- Map<String, Id> soldServiceDualLinkReference = new Map<String, Id>();
- /* map of Sold Service Upsert Key to Sold Service field where dual link relation should be stored */
- Map<String, String> soldServiceDualLinkField = new Map<String, String>();
- /* map of Sold Service Upsert Key to Quote Item ID corresponding to related A End and B End service */
- Map<String, String> soldServiceAEndReference = new Map<String, String>();
- Map<String, String> soldServiceBEndReference = new Map<String, String>();
- for(CPQ_QuoteItem__c product : products) {
- for(Integer i = 1; i <= product.Quantity__c; i++) {
- Sold_Service__c newSoldService;
- Boolean existingProductRecordUpdate = false;
- if(soldServiceMappings_Numbers.get(product.Product_Definition__c) == null || soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null) == null ||
- (soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null)[0].Generate_ICC_Number__c == false &&
- soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null)[0].Generate_IC_Number__c == false &&
- soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null)[0].Generate_ICB_Number__c == false)) {
- continue;
- } else if(soldServiceMappings_Numbers.get(product.Product_Definition__c) != null && soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null) != null &&
- soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null)[0].Update_on_Change__c && product.Existing_Object_ID__c != null) {
- newSoldService = new Sold_Service__c(Id = product.Existing_Object_ID__c);
- existingProductRecordUpdate = true;
- } else if(product.Item_Action__c == ACTION_NO_ACTION) {
- if(product.Existing_Object_ID__c != null && product.Existing_Object_ID__r.Renewed_To__c != null && product.Existing_Object_ID__r.Renewed_To__r.Status__c != null && product.Existing_Object_ID__r.Renewed_To__r.Status__c == 'Active') {
- newSoldService = new Sold_Service__c(Id = product.Existing_Object_ID__r.Renewed_To__c);
- existingProductRecordUpdate = true;
- } else {
- newSoldService = new Sold_Service__c(Id = product.Existing_Object_ID__c);
- existingProductRecordUpdate = true;
- }
- } else {
- newSoldService = new Sold_Service__c();
- }
- //newSoldService.RecordType = new RecordType(Name = RT_SSERVICE_PRODUCT);
- newSoldService.Item_Action__c = product.Item_Action__c;
- if(product.Item_Action__c != ACTION_NO_ACTION) {
- newSoldService.Service_Description__c = product.Description__c;
- newSoldService.Configuration_Description__c = product.Configuration_Description__c;
- if(!existingProductRecordUpdate) {
- newSoldService.Stage__c = STAGE_NEW;
- }
- }
- /* keeping sold service number for modifications */
- if(product.Item_Action__c == ACTION_ADD) {
- CPQ_Sold_Service_Mapping__c whatCode;
- if(soldServiceMappings_Numbers.get(product.Product_Definition__c) != null && soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null) != null) {
- whatCode = soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null)[0];
- }
- if(whatCode != null && whatCode.Generate_ICC_Number__c){
- newSoldService.Name = codes.generateCode(system.label.CRM_COM_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_IC_Number__c) {
- newSoldService.Name = codes.generateCode(system.label.CRM_OPR_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_ICB_Number__c) {
- newSoldService.Name = codes.generateCode(system.label.CRM_INTERNAL_PRODUCT);
- }
- /* assigning appropriate record type */
- if(product.Product_Definition__c != null && product.Product_Definition__r.Product_Type__c == PROD_TYPE_COMMERCIAL) {
- newSoldService.RecordType = new RecordType(Name = RT_SSERVICE_COMMERCIAL);
- newSoldService.Item_Type__c = system.label.CRM_COM_PRODUCT;
- } else if(product.Product_Definition__c != null && product.Product_Definition__r.Product_Type__c == PROD_TYPE_OPERATIONAL) {
- if(product.Product_Definition__r.Product_Group__c == 'Voice' || product.Product_Definition__r.Product_Group__c == 'Roaming & Messaging') {
- newSoldService.RecordType = new RecordType(Name = RT_SSERVICE_OPERATIONAL_VM);
- } else {
- newSoldService.RecordType = new RecordType(Name = RT_SSERVICE_OPERATIONAL_DI);
- }
- newSoldService.Item_Type__c = system.label.CRM_OPR_PRODUCT;
- } else if(product.Product_Definition__c != null && product.Product_Definition__r.Product_Type__c == PROD_TYPE_INTERNAL) {
- newSoldService.RecordType = new RecordType(Name = RT_SSERVICE_INTERNAL);
- newSoldService.Item_Type__c = system.label.CRM_INTERNAL_PRODUCT;
- }
- } else if(product.Item_Action__c == ACTION_CHANGE) {
- newSoldService.Name = product.Existing_Object_ID__r.Name;
- if(!existingProductRecordUpdate) {
- newSoldService.Renewed_From__c = product.Existing_Object_ID__c;
- newSoldService.RecordTypeId = product.Existing_Object_ID__r.RecordTypeId;
- newSoldService.Item_Type__c = product.Existing_Object_ID__r.Item_Type__c;
- Sold_Service__c changedService = new Sold_Service__c(Id = product.Existing_Object_ID__c);
- changedService.Stage__c = STAGE_REPLACEMENT;
- soldServiceProductsToUpsert.add(changedService);
- }
- } else if(product.Item_Action__c == ACTION_RENEWAL) {
- newSoldService.Name = product.Existing_Object_ID__r.Name;
- newSoldService.Estimated_Delivery_Date__c = null;
- if(!existingProductRecordUpdate) {
- newSoldService.Renewed_From__c = product.Existing_Object_ID__c;
- newSoldService.RecordTypeId = product.Existing_Object_ID__r.RecordTypeId;
- newSoldService.Item_Type__c = product.Existing_Object_ID__r.Item_Type__c;
- Sold_Service__c changedService = new Sold_Service__c(Id = product.Existing_Object_ID__c);
- changedService.Stage__c = STAGE_REPLACEMENT;
- soldServiceProductsToUpsert.add(changedService);
- }
- } else if(product.Item_Action__c == ACTION_CEASE) {
- newSoldService.Name = product.Existing_Object_ID__r.Name;
- if(!existingProductRecordUpdate) {
- newSoldService.Stage__c = STAGE_NEW;
- newSoldService.Renewed_From__c = product.Existing_Object_ID__c;
- newSoldService.RecordTypeId = product.Existing_Object_ID__r.RecordTypeId;
- newSoldService.Item_Type__c = product.Existing_Object_ID__r.Item_Type__c;
- Sold_Service__c changedService = new Sold_Service__c(Id = product.Existing_Object_ID__c);
- changedService.Stage__c = STAGE_REPLACEMENT;
- if(product.Cease_and_Reprovide_Type__c != null && product.Cease_and_Reprovide_Type__c == CEASE_PARALLEL_BUILD) {
- if(product.Opportunity__c != null) {
- changedService.Specification_Received_Date__c = product.Opportunity__r.CloseDate;
- if(product.Opportunity__r.Commercial_Contact__c != null) {
- changedService.Person_requesting_the_dismantle__c = product.Opportunity__r.Commercial_Contact__r.Name;
- }
- }
- changedService.Dismantle_Notes__c = CEASEANDREPROVIDE_TXT;
- }
- soldServiceProductsToUpsert.add(changedService);
- }
- } else if(product.Item_Action__c == ACTION_DISMANTLE) {
- Sold_Service__c dismantledService = new Sold_Service__c(Id = product.Existing_Object_ID__c);
- dismantledService.Previous_MRC__c = product.MRC_Previous__c;
- dismantledService.MRC__c = 0;
- dismantledService.Stage__c = STAGE_DISMANTLE;
- dismantledService.Date_for_Dismantle__c = product.Date_Expected_Delivery__c;
- dismantledService.Dismantle_Notes__c = DISMANTLE_TXT;
- if(product.Opportunity__c != null) {
- dismantledService.Specification_Received_Date__c = product.Opportunity__r.CloseDate;
- if(product.Opportunity__r.Commercial_Contact__c != null) {
- dismantledService.Person_requesting_the_dismantle__c = product.Opportunity__r.Commercial_Contact__r.Name;
- }
- }
- dismantledService.Dismantle_Related_Services__c = 'No';
- soldServiceProductsToUpsert.add(dismantledService);
- if(product.X3rd_Party_Items__r != null) {
- for(X3rd_Party_Item__c tpi : product.X3rd_Party_Items__r) {
- if(tpi.Existing_X3rd_Party_Service__c != null) {
- X3rd_Party_Services__c oldTPS = new X3rd_Party_Services__c(Id = tpi.Existing_X3rd_Party_Service__c);
- oldTPS.Status__c = '90 Pending Dismantle';
- thirdPartyServicesToUpsert2.put(oldTPS.id, oldTPS);
- }
- }
- }
- }
- if(product.Item_Action__c != ACTION_DISMANTLE && product.Item_Action__c != ACTION_NO_ACTION) {
- /* generate a new IC/ICC number if the option was selected */
- if(product.Assign_New_IC_ICC_Number__c && !existingProductRecordUpdate) {
- CPQ_Sold_Service_Mapping__c whatCode;
- if(soldServiceMappings_Numbers.get(product.Product_Definition__c) != null && soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null) != null) {
- whatCode = soldServiceMappings_Numbers.get(product.Product_Definition__c).get(null)[0];
- }
- if(whatCode != null && whatCode.Generate_ICC_Number__c){
- newSoldService.Name = codes.generateCode(system.label.CRM_COM_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_IC_Number__c) {
- newSoldService.Name = codes.generateCode(system.label.CRM_OPR_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_ICB_Number__c) {
- newSoldService.Name = codes.generateCode(system.label.CRM_INTERNAL_PRODUCT);
- }
- }
- if(product.Item_Action__c == ACTION_RENEWAL && !existingProductRecordUpdate) {
- newSoldService.Stage__c = STAGE_RENEWAL;
- }
- newSoldService.CurrencyIsoCode = product.CurrencyIsoCode;
- newSoldService.Opp_Link__c = product.Opportunity__c;
- newSoldService.CRM_Account__c = product.Account__c;
- newSoldService.Instance_ID__c = newSoldService.Name;
- newSoldService.CPQ_Product__c = product.Product_Definition__c;
- newSoldService.Item_Name__c = product.Product_Definition__r.Name;
- if(product.Product_Definition__c != null && product.Product_Definition__r.Name == 'Colocation' && product.A_Site__c != null){
- newSoldService.Site_ID__c = product.A_Site__r.Name;
- }
- newSoldService.OLI_Link_ID__c = product.OpportunityLineItem_ID__c;
- newSoldService.OLI_Link_Name__c = oppLineItems.get(product.OpportunityLineItem_ID__c) == null ? product.Name : oppLineItems.get(product.OpportunityLineItem_ID__c).Product2.Name;
- newSoldService.Item_Action_Description__c = product.Item_Action_Description__c;
- newSoldService.Days_of_Parallel_Running__c = product.Parallel_build_period__c;
- newSoldService.List_OTC__c = product.OTC_List__c;
- newSoldService.OTC__c = product.OTC__c;
- newSoldService.OTC_Rolled__c = product.OTC_Rolled__c == null ? 0 : product.OTC_Rolled__c;
- newSoldService.List_MRC__c = product.MRC_List__c;
- newSoldService.MRC__c = product.MRC__c;
- newSoldService.MRC_Rolled__c = product.MRC_Rolled__c == null ? 0 : product.MRC_Rolled__c;
- newSoldService.Previous_MRC__c = product.MRC_Previous__c;
- newSoldService.dMRC__c = (
- (newSoldService.MRC__c == null ? 0 : newSoldService.MRC__c) +
- (newSoldService.MRC_Rolled__c == null ? 0 : newSoldService.MRC_Rolled__c)
- ) - (product.MRC_Previous__c == null ? 0 : product.MRC_Previous__c);
- newSoldService.Recommended_Base_Unit_Price__c = product.Unit_Price__c;
- newSoldService.Recommended_Burst_Price__c = product.Unit_Burst_Price__c;
- newSoldService.Capex__c = product.TPI_Capex__c;
- newSoldService.OTE__c = product.OTE__c;
- newSoldService.Budget_OTE__c = product.OTE__c;
- newSoldService.MRE__c = product.MRE__c;
- newSoldService.Budget_MRE__c = product.MRE__c;
- newSoldService.Anticipated_month_one_impact__c = product.Anticipated_month_one_impact__c;
- newSoldService.A_Location__c = product.A_Location__c;
- if(i == 1 || product.A_Port_Option__c != 'New Port') {
- newSoldService.A_Port__c = product.A_Port__c;
- } else if(product.A_Port__c != null && portsToClone.get(product.A_Port__c) != null) {
- Port__c newPort = portsToClone.get(product.A_Port__c).clone(false, true);
- String p1 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String portUpsertKey = p1.substring(0,8)+ '-' + p1.substring(8,12) + '-' + p1.substring(12,16) + '-' + p1.substring(16,20) + '-' + p1.substring(20);
- newPort.TECH_External_ID__c = portUpsertKey;
- portsToCreate.add(newPort);
- newSoldService.A_Port__r = new Port__c(TECH_External_ID__c = portUpsertKey);
- portExternalIds.add(portUpsertKey);
- }
- newSoldService.A_Port_Option__c = product.A_Port_Option__c;
- newSoldService.A_Port_Type__c = product.A_Port_Type__c;
- newSoldService.A_Connector__c = product.A_Port_Connector__c;
- newSoldService.A_Interface__c = product.A_Port_Interface__c;
- newSoldService.A_Site__c = product.A_Site__c;
- newSoldService.A_Demarc_Site__c = product.A_Demarc_Site__c;
- newSoldService.A_Demarc_Type__c = product.A_Demarc_Type__c;
- newSoldService.A_Demarc_Comments__c = product.A_Demarc_Comments__c;
- newSoldService.A_Floor__c = product.A_Floor__c;
- newSoldService.A_Room__c = product.A_Room__c;
- newSoldService.A_Rack_Cabinet__c = product.A_Rack_Cabinet__c;
- newSoldService.A_Homing_Gateway_Location__c = product.A_Homing_Gateway_Location__c;
- newSoldService.B_Location__c = product.B_Location__c;
- if(i == 1 || product.B_Port_Option__c != 'New Port') {
- newSoldService.B_Port__c = product.B_Port__c;
- } else if(product.B_Port__c != null && portsToClone.get(product.B_Port__c) != null) {
- Port__c newPort = portsToClone.get(product.B_Port__c).clone(false, true);
- String p1 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String portUpsertKey = p1.substring(0,8)+ '-' + p1.substring(8,12) + '-' + p1.substring(12,16) + '-' + p1.substring(16,20) + '-' + p1.substring(20);
- newPort.TECH_External_ID__c = portUpsertKey;
- portsToCreate.add(newPort);
- newSoldService.B_Port__r = new Port__c(TECH_External_ID__c = portUpsertKey);
- portExternalIds.add(portUpsertKey);
- }
- newSoldService.B_Port_Option__c = product.B_Port_Option__c;
- newSoldService.B_Port_Type__c = product.B_Port_Type__c;
- newSoldService.B_Connector__c = product.B_Port_Connector__c;
- newSoldService.B_Interface__c = product.B_Port_Interface__c;
- newSoldService.B_Site__c = product.B_Site__c;
- newSoldService.B_Demarc_Site__c = product.B_Demarc_Site__c;
- newSoldService.B_Demarc_Type__c = product.B_Demarc_Type__c;
- newSoldService.B_Demarc_Comments__c = product.B_Demarc_Comments__c;
- newSoldService.B_Floor__c = product.B_Floor__c;
- newSoldService.B_Room__c = product.B_Room__c;
- newSoldService.B_Rack_Cabinet__c = product.B_Rack_Cabinet__c;
- newSoldService.B_Homing_Gateway_Location__c = product.B_Homing_Gateway_Location__c;
- newSoldService.Product_Type__c = 'Product';
- newSoldService.Product_Group__c = product.Product_Definition__c != null ? product.Product_Definition__r.Product_Group__c : '';
- newSoldService.Item_Main_Category__c = newSoldService.Product_Group__c;
- newSoldService.Product_Code__c = typeOfServiceToproductCode.get(product.Type_of_Service__c);
- /* assigning attribute values */
- Id dualLinkReference = null;
- String dualLinkField = null;
- Map<String, String> coloRefMap = new Map<String, String>();
- if(soldServiceMappings_Attributes.get(product.Product_Definition__c) != null && soldServiceMappings_Attributes.get(product.Product_Definition__c).get(null) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Attributes.get(product.Product_Definition__c).get(null)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- for(CPQ_QuoteItemAttribute__c attr : product.QuoteItemAttributes__r) {
- if(mapping.Attribute__c == attr.Attribute__c) {
- String attrValue = '';
- if(mapping.Attribute__r.Type__c == CPQ_ATR01_Manager.ATR_TYPE_SERVICE_REFERENCE && mapping.Attribute__r.TECH_Definition_ID__c.contains(ATTRIBUTE_DUALLINK_PREFIX)) {
- if(attr.Value_Quote_Item__c != null) {
- dualLinkReference = attr.Value_Quote_Item__c;
- dualLinkField = mapping.Target_Field_Sold_Service__c;
- break;
- } else {
- attrValue = attr.Value_Sold_Service__c;
- }
- } else if(mapping.Attribute__r.TECH_Definition_ID__c == ATTRIBUTE_PORTS_GRID) {
- attrValue = CalculateGridValueForPorts(mapping.Attribute__r.Grid_Layout__c, components.get(product.Id), null);
- } else {
- attrValue = CPQ_PRI01_Manager.getAttributeValueAsString(attr, true);
- }
- CPQ_PRI01_Manager.setFieldValueFromString(newSoldService, mapping.Target_Field_Sold_Service__c, attrValue, globalDescribe);
- break;
- }
- }
- }
- }
- }
- /* assigning field values */
- if(soldServiceMappings_Fields.get(product.Product_Definition__c) != null && soldServiceMappings_Fields.get(product.Product_Definition__c).get(null) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Fields.get(product.Product_Definition__c).get(null)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- String fieldValue = String.valueOf(product.get(mapping.Source_Field_Name__c));
- CPQ_PRI01_Manager.setFieldValueFromString(newSoldService, mapping.Target_Field_Sold_Service__c, fieldValue, globalDescribe);
- }
- }
- }
- /* assigning constant values */
- if(soldServiceMappings_Constants.get(product.Product_Definition__c) != null && soldServiceMappings_Constants.get(product.Product_Definition__c).get(null) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Constants.get(product.Product_Definition__c).get(null)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- CPQ_PRI01_Manager.setFieldValueFromString(newSoldService, mapping.Target_Field_Sold_Service__c, mapping.Source_Constant_Value__c, globalDescribe);
- }
- }
- }
- if(product.Opportunity__c != null && product.Opportunity__r.CloseDate != null && newSoldService.Estimated_Delivery_Date__c == null) {
- Decimal leadTime = product.Lead_Time__c == null ? 0 : product.Lead_Time__c;
- newSoldService.Estimated_Delivery_Date__c = product.Opportunity__r.CloseDate.addDays(leadTime.intValue());
- }
- if(product.Item_Action__c == ACTION_CHANGE) {
- newSoldService.Billing_Commence_Date__c = null;
- }
- /* generating upsert key */
- String h1 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String productUpsertKey = h1.substring(0,8)+ '-' + h1.substring(8,12) + '-' + h1.substring(12,16) + '-' + h1.substring(16,20) + '-' + h1.substring(20);
- newSoldService.Upsert_Key__c = productUpsertKey;
- quoteItemId2SoldServiceExternalId.put(product.Id, newSoldService.Upsert_Key__c);
- /* handling dual link data */
- if(String.isNotBlank(dualLinkReference) && String.isNotBlank(dualLinkField)) {
- soldServiceDualLinkReference.put(newSoldService.Upsert_Key__c, dualLinkReference);
- soldServiceDualLinkField.put(newSoldService.Upsert_Key__c, dualLinkField);
- }
- /* handling A / B end data */
- if (!String.isBlank(product.A_End_ID__c)) {
- soldServiceAEndReference.put(productUpsertKey, product.A_End_ID__c);
- }
- if (!String.isBlank(product.B_End_ID__c)) {
- soldServiceBEndReference.put(productUpsertKey, product.B_End_ID__c);
- }
- if(newSoldService.A_Port__c != null && product.A_Port_Option__c == 'New Port'){
- portIds.add(newSoldService.A_Port__c);
- }
- if(newSoldService.B_Port__c != null && product.B_Port_Option__c == 'New Port'){
- portIds.add(newSoldService.B_Port__c);
- }
- if(quoteItemToSoldService == null || !quoteItemToSoldService.keySet().contains(key(i,product.Id))) {
- soldServiceProductsToUpsert.add(newSoldService);
- result.upsertKeyToQuoteItem.put(newSoldService.Upsert_Key__c, key(i,product.Id));
- }
- Date maxTPSDate;
- if(product.X3rd_Party_Items__r != null) {
- Map<Id, Id> tpiToParent = new Map<Id, Id>();
- Map<Id, String> tpiIdToTPSExternalId = new Map<Id, String>();
- List<X3rd_Party_Services__c> tpsToParentUpdate = new List<X3rd_Party_Services__c>();
- for(X3rd_Party_Item__c tpi : product.X3rd_Party_Items__r){
- if(tpi.Chosen__c){
- X3rd_Party_Services__c tps;
- if(tpi.TPI_Order_Type__c == TPI_ORDER_TYPE_DISMANTLE && tpi.Existing_X3rd_Party_Service__c != null) {
- tps = new X3rd_Party_Services__c(Id = tpi.Existing_X3rd_Party_Service__c);
- tps.Status__c = TPS_STATUS_DISMANTLE;
- tps.Date_for_Dismantle__c = product.Date_Expected_Delivery__c;
- tps.Dismantle_Request_Received__c = product.Opportunity__r.CloseDate;
- thirdPartyServicesToUpsert2.put(tps.Id, tps);
- } else if(tpi.TPI_Order_Type__c != TPI_ORDER_TYPE_DISMANTLE) {
- tps = new X3rd_Party_Services__c();
- tps.Status__c = ApplicationConstant.TPS_STATUS_POREQUIRED;
- newSoldService.Minimum_Status_TPS__c = tps.Status__c;
- if(ApplicationConstant.TPI_PRIMARY_GENERIC_SUPPLIER_PRODUCT_TSIC_BACKHAUL.equalsIgnoreCase(tpi.Primary_Generic_Supplier_Product__c)
- && String.isNotBlank(tpi.Supplier_Account_Name__c) && tpi.Supplier_Account_Name__c.containsIgnoreCase(ApplicationConstant.COMPANY_NAME)){
- tps.Status__c = ApplicationConstant.TPS_STATUS_LIVE;
- tps.Supplier_Service_Reference__c = ApplicationConstant.COMPANY_NAME;
- }
- //tps.Name = 'TPS';
- tps.Name = codes.generateCode(system.label.CRM_THIRD_PARTY_SERVICE);
- tps.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldService.Upsert_Key__c);
- tps.Supporting_Sold_Service__c = tpi.Supporting_Sold_Service__c;
- tps.Source_TPI_No__c = tpi.Id;
- tps.Port__c = tpi.Port__c;
- for (String f : tpsFieldMap.keySet()) {
- tps.put(tpsFieldMap.get(f), tpi.get(f));
- }
- String h3 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String tpsUpsertKey = h3.substring(0,8)+ '-' + h3.substring(8,12) + '-' + h3.substring(12,16) + '-' + h3.substring(16,20) + '-' + h3.substring(20);
- tps.x3ParSerUID__c = tpsUpsertKey;
- if(tpi.Existing_X3rd_Party_Service__c != null) {
- tps.Replaced_TPS__c = tpi.Existing_X3rd_Party_Service__c;
- X3rd_Party_Services__c oldTPS = new X3rd_Party_Services__c(Id = tpi.Existing_X3rd_Party_Service__c);
- oldTPS.TPS_replaced_by__r = new X3rd_Party_Services__c(x3ParSerUID__c = tpsUpsertKey);
- oldTPS.Status__c = TPS_STATUS_REPLACED;
- if(product.Item_Action__c == ACTION_RENEWAL) {
- /* moving TOS under new sold service for renewal */
- oldTPS.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldService.Upsert_Key__c);
- }
- thirdPartyServicesToUpsert2.put(oldTPS.Id, oldTPS);
- }
- Date maxDate;
- if(tps.Forecasted_Delivery_Date__c != null){
- maxDate = tps.Forecasted_Delivery_Date__c.date();
- }
- if(tps.Committed_Delivery_Date__c != null){
- maxDate = tps.Committed_Delivery_Date__c.date();
- }
- if(tps.Revised_Delivery_Date__c != null){
- maxDate = tps.Revised_Delivery_Date__c.date();
- }
- if(maxDate != null) {
- if(maxTPSDate == null || maxTPSDate < maxDate) {
- maxTPSDate = maxDate;
- }
- }
- thirdPartyServicesToUpsert.add(tps);
- if(tpi.Parent__c != null && String.isNotEmpty(tpi.Parent__r.Upsert_Key__c)) {
- tpiToParent.put(tpi.Id, tpi.Parent__c);
- tpsToParentUpdate.add(tps);
- }
- tpiIdToTPSExternalId.put(tpi.Id, tps.x3ParSerUID__c);
- if(!tpisAlreadyAdded.contains(tpi.Id)) {
- tpi.RecordTypeId = tpiReadOnlyRecordType;
- thirdPartyItemsToUpdate.add(tpi);
- tpisAlreadyAdded.add(tpi.Id);
- }
- if (tpi.Secondary_Supplier_Product__c == 'NNI') {
- newSoldService.NNI_Used__c = true;
- }
- if (tpi.Secondary_Supplier_Product__c == 'CPE' || tpi.Secondary_Supplier_Product__c == 'NID') {
- newSoldService.Includes_CPE__c = true;
- }
- }
- }
- }
- for(X3rd_Party_Services__c tps : tpsToParentUpdate) {
- if(tpiToParent.keySet().contains(tps.Source_TPI_No__c)) {
- if(tpiIdToTPSExternalId.get(tpiToParent.get(tps.Source_TPI_No__c)) != null) {
- X3rd_Party_Services__c tpsParent = new X3rd_Party_Services__c(x3ParSerUID__c = tps.x3ParSerUID__c);
- tpsParent.Parent__r = new X3rd_Party_Services__c(x3ParSerUID__c = tpiIdToTPSExternalId.get(tpiToParent.get(tps.Source_TPI_No__c)));
- thirdPartyServicesToUpsertParent.add(tpsParent);
- }
- }
- }
- }
- if(maxTPSDate != null) newSoldService.Maximum_RFS_Date_TPS__c = maxTPSDate;
- /* adding charges if appropriate attribute type found */
- for(CPQ_QuoteItemAttribute__c attr : product.QuoteItemAttributes__r) {
- if(attr.Type__c == CPQ_ATR01_Manager.ATR_TYPE_CHARGE) {
- if(String.isNotEmpty(attr.Value_Text_Long__c)){
- List<CPQ_Charge__c> deserializedCharges = (List<CPQ_Charge__c>)JSON.deserialize(attr.Value_Text_Long__c, List<CPQ_Charge__c>.class);
- for(CPQ_Charge__c charge : deserializedCharges){
- CPQ_Charge__c newCharge;
- if(charge.Id != null) {
- newCharge = new CPQ_Charge__c(Id = charge.Id);
- } else {
- newCharge = new CPQ_Charge__c();
- }
- cloneObject(charge, newCharge, 'CPQ_Charge__c');
- newCharge.Attribute__c = attr.Attribute__c;
- newCharge.Quote__c = product.Quote__c;
- newCharge.Quote_Item__c = product.Id;
- newCharge.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldService.Upsert_Key__c);
- chargesToUpsert.add(newCharge);
- }
- }
- }
- }
- /* Handling port change requests if applicable */
- if(product.Port_Change_Requests__r != null) {
- for(Port_Change_Request__c pcr : product.Port_Change_Requests__r) {
- if(pcr.Port__c == product.A_Port__c && product.A_Port_Option__c == 'Existing Port') {
- newSoldService.A_Port_in_Change__c = true;
- }
- if(pcr.Port__c == product.B_Port__c && product.B_Port_Option__c == 'Existing Port') {
- newSoldService.B_Port_in_Change__c = true;
- }
- Port_Change_Request__c pcrToUpsert = new Port_Change_Request__c(Id = pcr.Id);
- pcrToUpsert.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldService.Upsert_Key__c);
- pcrToUpsert.Stage__c = STAGE_NEW;
- portCRsToUpsert.put(pcr.Id, pcrToUpsert);
- }
- }
- }
- if(components.get(product.Id) != null) {
- for(CPQ_QuoteItem__c component : components.get(product.Id)) {
- for(Integer j = 1; j <= component.Quantity__c; j++) {
- Sold_Service__c newSoldServiceComponent;
- Boolean existingComponentRecordUpdate = false;
- if(soldServiceMappings_Numbers.get(product.Product_Definition__c) == null || soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c) == null ||
- (soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c)[0].Generate_ICC_Number__c == false &&
- soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c)[0].Generate_IC_Number__c == false &&
- soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c)[0].Generate_ICB_Number__c == false)) {
- continue;
- } else if(soldServiceMappings_Numbers.get(product.Product_Definition__c) != null && soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c) != null &&
- soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c)[0].Update_on_Change__c && component.Existing_Object_ID__c != null) {
- newSoldServiceComponent = new Sold_Service__c(Id = component.Existing_Object_ID__c);
- existingComponentRecordUpdate = true;
- } else {
- newSoldServiceComponent = new Sold_Service__c();
- }
- newSoldServiceComponent.Item_Action__c = component.Item_Action__c;
- newSoldServiceComponent.Service_Description__c = component.Description__c;
- newSoldServiceComponent.Configuration_Description__c = component.Configuration_Description__c;
- if(quoteItemToSoldService.get(key(i,product.Id)) != null) {
- newSoldServiceComponent.CRM_Parent_Sold_Service__c = quoteItemToSoldService.get(key(i,product.Id));
- newSoldServiceComponent.CRM_Main_Sold_Service__c = quoteItemToSoldService.get(key(i,product.Id));
- }else if(newSoldService.Id != null) {
- newSoldServiceComponent.CRM_Parent_Sold_Service__c = newSoldService.Id;
- newSoldServiceComponent.CRM_Main_Sold_Service__c = newSoldService.Id;
- } else {
- newSoldServiceComponent.CRM_Parent_Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldService.Upsert_Key__c);
- newSoldServiceComponent.CRM_Main_Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldService.Upsert_Key__c);
- }
- if(!existingComponentRecordUpdate) {
- newSoldServiceComponent.Stage__c = STAGE_NEW;
- }
- if(product.Product_Definition__r.Product_Group__c == 'Voice' || product.Product_Definition__r.Product_Group__c == 'Roaming & Messaging') {
- newSoldServiceComponent.RecordType = new RecordType(Name = RT_SSERVICE_OPERATIONAL_VM);
- } else {
- newSoldServiceComponent.RecordType = new RecordType(Name = RT_SSERVICE_OPERATIONAL_DI);
- }
- /* keeping sold service number for modifications */
- if(component.Item_Action__c == ACTION_ADD) {
- CPQ_Sold_Service_Mapping__c whatCode;
- if(soldServiceMappings_Numbers.get(product.Product_Definition__c) != null && soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c) != null) {
- whatCode = soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c)[0];
- }
- if(whatCode != null && whatCode.Generate_ICC_Number__c){
- newSoldServiceComponent.Name = codes.generateCode(system.label.CRM_COM_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_IC_Number__c) {
- newSoldServiceComponent.Name = codes.generateCode(system.label.CRM_OPR_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_ICB_Number__c) {
- newSoldServiceComponent.Name = codes.generateCode(system.label.CRM_INTERNAL_PRODUCT);
- }
- } else if(component.Item_Action__c == ACTION_CHANGE) {
- newSoldServiceComponent.Name = component.Existing_Object_ID__r.Name;
- if(!existingComponentRecordUpdate) {
- newSoldServiceComponent.Renewed_From__c = component.Existing_Object_ID__c;
- Sold_Service__c changedService = new Sold_Service__c(Id = component.Existing_Object_ID__c);
- changedService.Stage__c = STAGE_REPLACEMENT;
- soldServiceComponentsToUpsert.add(changedService);
- }
- } else if(component.Item_Action__c == ACTION_RENEWAL) {
- newSoldServiceComponent.Name = component.Existing_Object_ID__r.Name;
- newSoldServiceComponent.Estimated_Delivery_Date__c = null;
- if(!existingComponentRecordUpdate) {
- newSoldServiceComponent.Renewed_From__c = component.Existing_Object_ID__c;
- Sold_Service__c changedService = new Sold_Service__c(Id = component.Existing_Object_ID__c);
- changedService.Stage__c = STAGE_REPLACEMENT;
- soldServiceComponentsToUpsert.add(changedService);
- }
- } else if(component.Item_Action__c == ACTION_CEASE) {
- newSoldServiceComponent.Name = component.Existing_Object_ID__r.Name;
- if(!existingComponentRecordUpdate) {
- newSoldServiceComponent.Renewed_From__c = component.Existing_Object_ID__c;
- Sold_Service__c changedService = new Sold_Service__c(Id = component.Existing_Object_ID__c);
- changedService.Stage__c = STAGE_REPLACEMENT;
- if(component.Cease_and_Reprovide_Type__c != null && component.Cease_and_Reprovide_Type__c == CEASE_PARALLEL_BUILD) {
- if(component.Opportunity__c != null) {
- changedService.Specification_Received_Date__c = component.Opportunity__r.CloseDate;
- if(component.Opportunity__r.Commercial_Contact__c != null) {
- changedService.Person_requesting_the_dismantle__c = component.Opportunity__r.Commercial_Contact__r.Name;
- }
- }
- changedService.Dismantle_Notes__c = CEASEANDREPROVIDE_TXT;
- }
- soldServiceComponentsToUpsert.add(changedService);
- }
- } else if(component.Item_Action__c == ACTION_DISMANTLE) {
- Sold_Service__c dismantledService = new Sold_Service__c(Id = component.Existing_Object_ID__c);
- dismantledService.Previous_MRC__c = component.MRC_Previous__c;
- dismantledService.MRC__c = 0;
- dismantledService.Stage__c = STAGE_DISMANTLE;
- dismantledService.Date_for_Dismantle__c = product.Date_Expected_Delivery__c == null ? component.Date_Expected_Delivery__c : product.Date_Expected_Delivery__c;
- dismantledService.Dismantle_Notes__c = DISMANTLE_TXT;
- if(component.Opportunity__c != null) {
- dismantledService.Specification_Received_Date__c = component.Opportunity__r.CloseDate;
- if(component.Opportunity__r.Commercial_Contact__c != null) {
- dismantledService.Person_requesting_the_dismantle__c = component.Opportunity__r.Commercial_Contact__r.Name;
- }
- }
- soldServiceComponentsToUpsert.add(dismantledService);
- if(component.X3rd_Party_Items__r != null) {
- for(X3rd_Party_Item__c tpi : component.X3rd_Party_Items__r) {
- if(tpi.Existing_X3rd_Party_Service__c != null) {
- X3rd_Party_Services__c oldTPS = new X3rd_Party_Services__c(Id = tpi.Existing_X3rd_Party_Service__c);
- oldTPS.Status__c = '90 Pending Dismantle';
- thirdPartyServicesToUpsert2.put(oldTPS.Id, oldTPS);
- }
- }
- }
- }
- if(component.Item_Action__c != ACTION_DISMANTLE) {
- /* generate a new IC/ICC number if the option was selected */
- if(component.Assign_New_IC_ICC_Number__c && !existingComponentRecordUpdate) {
- CPQ_Sold_Service_Mapping__c whatCode;
- if(soldServiceMappings_Numbers.get(product.Product_Definition__c) != null && soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c) != null) {
- whatCode = soldServiceMappings_Numbers.get(product.Product_Definition__c).get(component.Component_Definition__c)[0];
- }
- if(whatCode != null && whatCode.Generate_ICC_Number__c){
- newSoldServiceComponent.Name = codes.generateCode(system.label.CRM_COM_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_IC_Number__c) {
- newSoldServiceComponent.Name = codes.generateCode(system.label.CRM_OPR_PRODUCT);
- } else if(whatCode != null && whatCode.Generate_ICB_Number__c) {
- newSoldServiceComponent.Name = codes.generateCode(system.label.CRM_INTERNAL_PRODUCT);
- }
- }
- if(component.Item_Action__c == ACTION_RENEWAL && !existingComponentRecordUpdate) {
- newSoldServiceComponent.Stage__c = STAGE_RENEWAL;
- }
- newSoldServiceComponent.CurrencyIsoCode = component.CurrencyIsoCode;
- newSoldServiceComponent.Opp_Link__c = product.Opportunity__c;
- newSoldServiceComponent.CRM_Account__c = product.Account__c;
- newSoldServiceComponent.Instance_ID__c = newSoldServiceComponent.Name;
- newSoldServiceComponent.CPQ_Component__c = component.Component_Definition__c;
- newSoldServiceComponent.Item_Name__c = component.Component_Definition__r.Name;
- newSoldServiceComponent.Item_Type__c = system.label.CRM_OPR_PRODUCT;
- newSoldServiceComponent.OLI_Link_ID__c = component.OpportunityLineItem_ID__c;
- newSoldServiceComponent.OLI_Link_Name__c = oppLineItems.get(component.OpportunityLineItem_ID__c) == null ? component.Name : oppLineItems.get(component.OpportunityLineItem_ID__c).Product2.Name;
- newSoldServiceComponent.Item_Action_Description__c = component.Item_Action_Description__c;
- newSoldServiceComponent.Days_of_Parallel_Running__c = component.Parallel_build_period__c;
- newSoldServiceComponent.List_OTC__c = component.OTC_List__c;
- newSoldServiceComponent.OTC__c = component.OTC__c;
- newSoldServiceComponent.OTC_Rolled__c = component.OTC_Rolled__c == null ? 0 : component.OTC_Rolled__c;
- newSoldServiceComponent.List_MRC__c = component.MRC_List__c;
- newSoldServiceComponent.MRC__c = component.MRC__c;
- newSoldServiceComponent.MRC_Rolled__c = component.MRC_Rolled__c == null ? 0 : component.MRC_Rolled__c;
- newSoldServiceComponent.Previous_MRC__c = component.MRC_Previous__c;
- newSoldServiceComponent.dMRC__c = (
- (newSoldServiceComponent.MRC__c == null ? 0 : newSoldServiceComponent.MRC__c) +
- (newSoldServiceComponent.MRC_Rolled__c == null ? 0 : newSoldServiceComponent.MRC_Rolled__c)
- ) - (component.MRC_Previous__c == null ? 0 : component.MRC_Previous__c);
- newSoldServiceComponent.Recommended_Base_Unit_Price__c = component.Unit_Price__c;
- newSoldServiceComponent.Recommended_Burst_Price__c = component.Unit_Burst_Price__c;
- newSoldServiceComponent.Capex__c = component.TPI_Capex__c;
- newSoldServiceComponent.OTE__c = component.OTE__c;
- newSoldServiceComponent.Budget_OTE__c = component.OTE__c;
- newSoldServiceComponent.MRE__c = component.MRE__c;
- newSoldServiceComponent.Budget_MRE__c = component.MRE__c;
- newSoldServiceComponent.Anticipated_month_one_impact__c = component.Anticipated_month_one_impact__c;
- newSoldServiceComponent.A_Location__c = component.A_Location__c;
- if((i == 1 && j == 1) || component.A_Port_Option__c != 'New Port') {
- newSoldServiceComponent.A_Port__c = component.A_Port__c;
- } else if(component.A_Port__c != null && portsToClone.get(component.A_Port__c) != null) {
- Port__c newPort = portsToClone.get(component.A_Port__c).clone(false, true);
- String p1 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String portUpsertKey = p1.substring(0,8)+ '-' + p1.substring(8,12) + '-' + p1.substring(12,16) + '-' + p1.substring(16,20) + '-' + p1.substring(20);
- newPort.TECH_External_ID__c = portUpsertKey;
- portsToCreate.add(newPort);
- newSoldServiceComponent.A_Port__r = new Port__c(TECH_External_ID__c = portUpsertKey);
- portExternalIds.add(portUpsertKey);
- }
- newSoldServiceComponent.A_Port_Option__c = component.A_Port_Option__c;
- newSoldServiceComponent.A_Port_Type__c = component.A_Port_Type__c;
- newSoldServiceComponent.A_Connector__c = component.A_Port_Connector__c;
- newSoldServiceComponent.A_Interface__c = component.A_Port_Interface__c;
- newSoldServiceComponent.A_Site__c = component.A_Site__c;
- newSoldServiceComponent.A_Demarc_Site__c = component.A_Demarc_Site__c;
- newSoldServiceComponent.A_Demarc_Type__c = component.A_Demarc_Type__c;
- newSoldServiceComponent.A_Demarc_Comments__c = component.A_Demarc_Comments__c;
- newSoldServiceComponent.A_Floor__c = component.A_Floor__c;
- newSoldServiceComponent.A_Room__c = component.A_Room__c;
- newSoldServiceComponent.A_Rack_Cabinet__c = component.A_Rack_Cabinet__c;
- newSoldServiceComponent.A_Homing_Gateway_Location__c = component.A_Homing_Gateway_Location__c;
- newSoldServiceComponent.B_Location__c = component.B_Location__c;
- if((i == 1 && j == 1) || component.B_Port_Option__c != 'New Port') {
- newSoldServiceComponent.B_Port__c = component.B_Port__c;
- } else if(component.B_Port__c != null && portsToClone.get(component.B_Port__c) != null) {
- Port__c newPort = portsToClone.get(component.B_Port__c).clone(false, true);
- String p1 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String portUpsertKey = p1.substring(0,8)+ '-' + p1.substring(8,12) + '-' + p1.substring(12,16) + '-' + p1.substring(16,20) + '-' + p1.substring(20);
- newPort.TECH_External_ID__c = portUpsertKey;
- portsToCreate.add(newPort);
- newSoldServiceComponent.B_Port__r = new Port__c(TECH_External_ID__c = portUpsertKey);
- portExternalIds.add(portUpsertKey);
- }
- newSoldServiceComponent.B_Port_Option__c = component.B_Port_Option__c;
- newSoldServiceComponent.B_Port_Type__c = component.B_Port_Type__c;
- newSoldServiceComponent.B_Connector__c = component.B_Port_Connector__c;
- newSoldServiceComponent.B_Interface__c = component.B_Port_Interface__c;
- newSoldServiceComponent.B_Site__c = component.B_Site__c;
- newSoldServiceComponent.B_Demarc_Site__c = component.B_Demarc_Site__c;
- newSoldServiceComponent.B_Demarc_Type__c = component.B_Demarc_Type__c;
- newSoldServiceComponent.B_Demarc_Comments__c = component.B_Demarc_Comments__c;
- newSoldServiceComponent.B_Floor__c = component.B_Floor__c;
- newSoldServiceComponent.B_Room__c = component.B_Room__c;
- newSoldServiceComponent.B_Rack_Cabinet__c = component.B_Rack_Cabinet__c;
- newSoldServiceComponent.B_Homing_Gateway_Location__c = component.B_Homing_Gateway_Location__c;
- newSoldServiceComponent.Product_Type__c = 'Component';
- newSoldServiceComponent.Product_Group__c = product.Product_Definition__c != null ? product.Product_Definition__r.Product_Group__c : '';
- newSoldServiceComponent.Item_Main_Category__c = newSoldServiceComponent.Product_Group__c;
- newSoldServiceComponent.Product_Code__c = typeOfServiceToproductCode.get(component.Type_of_Service__c);
- /* assigning attribute values */
- Id dualLinkReferenceComponent = null;
- String dualLinkFieldComponent = null;
- Map<String, String> coloRefMapComponent = new Map<String, String>();
- if(soldServiceMappings_Attributes.get(product.Product_Definition__c) != null && soldServiceMappings_Attributes.get(product.Product_Definition__c).get(component.Component_Definition__c) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Attributes.get(product.Product_Definition__c).get(component.Component_Definition__c)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- for(CPQ_QuoteItemAttribute__c attr : component.QuoteItemAttributes__r) {
- if(mapping.Attribute__c == attr.Attribute__c) {
- String attrValue = '';
- if(mapping.Attribute__r.Type__c == CPQ_ATR01_Manager.ATR_TYPE_SERVICE_REFERENCE && mapping.Attribute__r.TECH_Definition_ID__c.contains(ATTRIBUTE_DUALLINK_PREFIX)) {
- if(attr.Value_Quote_Item__c != null) {
- dualLinkReferenceComponent = attr.Value_Quote_Item__c;
- dualLinkFieldComponent = mapping.Target_Field_Sold_Service__c;
- break;
- } else {
- attrValue = attr.Value_Sold_Service__c;
- }
- } else {
- attrValue = CPQ_PRI01_Manager.getAttributeValueAsString(attr, true);
- }
- CPQ_PRI01_Manager.setFieldValueFromString(newSoldServiceComponent, mapping.Target_Field_Sold_Service__c, attrValue, globalDescribe);
- break;
- }
- }
- }
- }
- }
- /* assigning field values */
- if(soldServiceMappings_Fields.get(product.Product_Definition__c) != null && soldServiceMappings_Fields.get(product.Product_Definition__c).get(component.Component_Definition__c) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Fields.get(product.Product_Definition__c).get(component.Component_Definition__c)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- String fieldValue = String.valueOf(component.get(mapping.Source_Field_Name__c));
- CPQ_PRI01_Manager.setFieldValueFromString(newSoldServiceComponent, mapping.Target_Field_Sold_Service__c, fieldValue, globalDescribe);
- }
- }
- }
- /* assigning constant values */
- if(soldServiceMappings_Constants.get(product.Product_Definition__c) != null && soldServiceMappings_Constants.get(product.Product_Definition__c).get(component.Component_Definition__c) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Constants.get(product.Product_Definition__c).get(component.Component_Definition__c)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- CPQ_PRI01_Manager.setFieldValueFromString(newSoldServiceComponent, mapping.Target_Field_Sold_Service__c, mapping.Source_Constant_Value__c, globalDescribe);
- }
- }
- }
- if(component.Opportunity__c != null && component.Opportunity__r.CloseDate != null && newSoldServiceComponent.Estimated_Delivery_Date__c == null) {
- Decimal leadTime = component.Lead_Time__c == null ? 0 : component.Lead_Time__c;
- newSoldServiceComponent.Estimated_Delivery_Date__c = component.Opportunity__r.CloseDate.addDays(leadTime.intValue());
- }
- if(component.Item_Action__c == ACTION_RENEWAL) {
- newSoldServiceComponent.Estimated_Delivery_Date__c = null;
- }
- if(component.Item_Action__c == ACTION_CHANGE) {
- newSoldServiceComponent.Billing_Commence_Date__c = null;
- }
- /* generating upsert key */
- String h2 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String componentUpsertKey = h2.substring(0,8)+ '-' + h2.substring(8,12) + '-' + h2.substring(12,16) + '-' + h2.substring(16,20) + '-' + h2.substring(20);
- newSoldServiceComponent.Upsert_Key__c = componentUpsertKey;
- quoteItemId2SoldServiceExternalId.put(component.Id, newSoldServiceComponent.Upsert_Key__c);
- /* handling dual link data */
- if(String.isNotBlank(dualLinkReferenceComponent) && String.isNotBlank(dualLinkFieldComponent)) {
- soldServiceDualLinkReference.put(newSoldServiceComponent.Upsert_Key__c, dualLinkReferenceComponent);
- soldServiceDualLinkField.put(newSoldServiceComponent.Upsert_Key__c, dualLinkFieldComponent);
- }
- /* handling A / B end data */
- if (!String.isBlank(component.A_End_ID__c)) {
- soldServiceAEndReference.put(componentUpsertKey, component.A_End_ID__c);
- }
- if (!String.isBlank(component.B_End_ID__c)) {
- soldServiceBEndReference.put(componentUpsertKey, component.B_End_ID__c);
- }
- if(newSoldServiceComponent.A_Port__c != null && component.A_Port_Option__c == 'New Port'){
- portIds.add(newSoldServiceComponent.A_Port__c);
- }
- if(newSoldServiceComponent.B_Port__c != null && component.B_Port_Option__c == 'New Port'){
- portIds.add(newSoldServiceComponent.B_Port__c);
- }
- Date maxTPSDateComponent;
- if(component.X3rd_Party_Items__r != null) {
- Map<Id, Id> tpiToParent = new Map<Id, Id>();
- Map<Id, String> tpiIdToTPSExternalId = new Map<Id, String>();
- List<X3rd_Party_Services__c> tpsToParentUpdate = new List<X3rd_Party_Services__c>();
- for(X3rd_Party_Item__c tpi : component.X3rd_Party_Items__r){
- if(tpi.Chosen__c){
- X3rd_Party_Services__c tps;
- if(tpi.TPI_Order_Type__c == TPI_ORDER_TYPE_DISMANTLE && tpi.Existing_X3rd_Party_Service__c != null) {
- tps = new X3rd_Party_Services__c(Id = tpi.Existing_X3rd_Party_Service__c);
- tps.Status__c = TPS_STATUS_DISMANTLE;
- tps.Date_for_Dismantle__c = product.Date_Expected_Delivery__c == null ? component.Date_Expected_Delivery__c : product.Date_Expected_Delivery__c;
- tps.Dismantle_Request_Received__c = component.Opportunity__r.CloseDate;
- thirdPartyServicesToUpsert2.put(tps.Id, tps);
- } else if(tpi.TPI_Order_Type__c != TPI_ORDER_TYPE_DISMANTLE) {
- tps = new X3rd_Party_Services__c();
- tps.Status__c = ApplicationConstant.TPS_STATUS_POREQUIRED;
- newSoldServiceComponent.Minimum_Status_TPS__c = tps.Status__c;
- if(ApplicationConstant.TPI_PRIMARY_GENERIC_SUPPLIER_PRODUCT_TSIC_BACKHAUL.equalsIgnoreCase(tpi.Primary_Generic_Supplier_Product__c)
- && String.isNotBlank(tpi.Supplier_Account_Name__c) && tpi.Supplier_Account_Name__c.containsIgnoreCase(ApplicationConstant.COMPANY_NAME)){
- tps.Status__c = ApplicationConstant.TPS_STATUS_LIVE;
- tps.Supplier_Service_Reference__c = ApplicationConstant.COMPANY_NAME;
- }
- //tps.Name = 'TPS';
- tps.Name = codes.generateCode(system.label.CRM_THIRD_PARTY_SERVICE);
- tps.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldServiceComponent.Upsert_Key__c);
- tps.Supporting_Sold_Service__c = tpi.Supporting_Sold_Service__c;
- tps.Port__c = tpi.Port__c;
- for (String f : tpsFieldMap.keySet()) {
- tps.put(tpsFieldMap.get(f), tpi.get(f));
- }
- String h4 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String tpsUpsertKey = h4.substring(0,8)+ '-' + h4.substring(8,12) + '-' + h4.substring(12,16) + '-' + h4.substring(16,20) + '-' + h4.substring(20);
- tps.x3ParSerUID__c = tpsUpsertKey;
- if(tpi.Existing_X3rd_Party_Service__c != null) {
- tps.Replaced_TPS__c = tpi.Existing_X3rd_Party_Service__c;
- X3rd_Party_Services__c oldTPS = new X3rd_Party_Services__c(Id = tpi.Existing_X3rd_Party_Service__c);
- oldTPS.TPS_replaced_by__r = new X3rd_Party_Services__c(x3ParSerUID__c = tpsUpsertKey);
- oldTPS.Status__c = TPS_STATUS_REPLACED;
- if(component.Item_Action__c == ACTION_RENEWAL) {
- /* moving TOS under new sold service for renewal */
- oldTPS.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldServiceComponent.Upsert_Key__c);
- }
- thirdPartyServicesToUpsert2.put(oldTPS.Id, oldTPS);
- }
- Date maxDate;
- if(tps.Forecasted_Delivery_Date__c != null){
- maxDate = tps.Forecasted_Delivery_Date__c.date();
- }
- if(tps.Committed_Delivery_Date__c != null){
- maxDate = tps.Committed_Delivery_Date__c.date();
- }
- if(tps.Revised_Delivery_Date__c != null){
- maxDate = tps.Revised_Delivery_Date__c.date();
- }
- if(maxDate != null) {
- if(maxTPSDateComponent == null || maxTPSDateComponent < maxDate) {
- maxTPSDateComponent = maxDate;
- }
- }
- thirdPartyServicesToUpsert.add(tps);
- if(tpi.Parent__c != null && String.isNotEmpty(tpi.Parent__r.Upsert_Key__c)) {
- tpiToParent.put(tpi.Id, tpi.Parent__c);
- tpsToParentUpdate.add(tps);
- }
- tpiIdToTPSExternalId.put(tpi.Id, tps.x3ParSerUID__c);
- if(!tpisAlreadyAdded.contains(tpi.Id)) {
- tpi.RecordTypeId = tpiReadOnlyRecordType;
- thirdPartyItemsToUpdate.add(tpi);
- tpisAlreadyAdded.add(tpi.Id);
- }
- if (tpi.Secondary_Supplier_Product__c == 'NNI') {
- newSoldServiceComponent.NNI_Used__c = true;
- }
- if (tpi.Secondary_Supplier_Product__c == 'CPE' || tpi.Secondary_Supplier_Product__c == 'NID') {
- newSoldServiceComponent.Includes_CPE__c = true;
- }
- }
- }
- }
- for(X3rd_Party_Services__c tps : tpsToParentUpdate) {
- if(tpiToParent.keySet().contains(tps.Source_TPI_No__c)) {
- if(tpiIdToTPSExternalId.get(tpiToParent.get(tps.Source_TPI_No__c)) != null) {
- X3rd_Party_Services__c tpsParent = new X3rd_Party_Services__c(x3ParSerUID__c = tps.x3ParSerUID__c);
- tpsParent.Parent__r = new X3rd_Party_Services__c(x3ParSerUID__c = tpiIdToTPSExternalId.get(tpiToParent.get(tps.Source_TPI_No__c)));
- thirdPartyServicesToUpsertParent.add(tpsParent);
- }
- }
- }
- }
- if(maxTPSDateComponent != null) newSoldServiceComponent.Maximum_RFS_Date_TPS__c = maxTPSDateComponent;
- soldServiceComponentsToUpsert.add(newSoldServiceComponent);
- /* adding charges if appropriate attribute type found */
- for(CPQ_QuoteItemAttribute__c attr : component.QuoteItemAttributes__r) {
- if(attr.Type__c == CPQ_ATR01_Manager.ATR_TYPE_CHARGE) {
- if(String.isNotEmpty(attr.Value_Text_Long__c)){
- List<CPQ_Charge__c> deserializedCharges = (List<CPQ_Charge__c>)JSON.deserialize(attr.Value_Text_Long__c, List<CPQ_Charge__c>.class);
- for(CPQ_Charge__c charge : deserializedCharges){
- CPQ_Charge__c newCharge;
- if(charge.Id != null) {
- newCharge = new CPQ_Charge__c(Id = charge.Id);
- } else {
- newCharge = new CPQ_Charge__c();
- }
- cloneObject(charge, newCharge, 'CPQ_Charge__c');
- newCharge.Attribute__c = attr.Attribute__c;
- newCharge.Quote__c = component.Quote__c;
- newCharge.Quote_Item__c = component.Id;
- newCharge.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldServiceComponent.Upsert_Key__c);
- chargesToUpsert.add(newCharge);
- }
- }
- }
- }
- /* Handling port change requests if applicable */
- if(component.Port_Change_Requests__r != null) {
- for(Port_Change_Request__c pcr : component.Port_Change_Requests__r) {
- if(pcr.Port__c == component.A_Port__c && component.A_Port_Option__c == 'Existing Port') {
- newSoldServiceComponent.A_Port_in_Change__c = true;
- }
- if(pcr.Port__c == component.B_Port__c && component.B_Port_Option__c == 'Existing Port') {
- newSoldServiceComponent.B_Port_in_Change__c = true;
- }
- Port_Change_Request__c pcrToUpsert = new Port_Change_Request__c(Id = pcr.Id);
- pcrToUpsert.Sold_Service__r = new Sold_Service__c(Upsert_Key__c = newSoldServiceComponent.Upsert_Key__c);
- pcrToUpsert.Stage__c = STAGE_NEW;
- portCRsToUpsert.put(pcr.Id, pcrToUpsert);
- }
- }
- }
- }
- }
- }
- }
- }
- upsert portsToCreate;
- upsert soldServiceProductsToUpsert;
- upsert soldServiceComponentsToUpsert;
- sendEmailNotyficationAfterCreatingSoldService(opportunityId);
- /* build map of Upsert Key to Id */
- Map<String, Id> sServicesIdMap = new Map<String, Id>();
- for (Sold_Service__c svc : soldServiceProductsToUpsert) {
- sServicesIdMap.put(svc.Upsert_Key__c, svc.Id);
- result.quoteItemToSoldService.put(result.upsertKeyToQuoteItem.get(svc.Upsert_Key__c), svc.Id);
- }
- for (Sold_Service__c svc : soldServiceComponentsToUpsert) {
- sServicesIdMap.put(svc.Upsert_Key__c, svc.Id);
- }
- /* replace port name with generated numbers */
- if(!portIds.isEmpty()) {
- List<Port__c> portsToUpdate = new List<Port__c>();
- for(Port__c p : [select Id, Name from Port__c where Id in :portIds or TECH_External_ID__c in :portExternalIds]){
- if(p.Name.startsWithIgnoreCase(PORT_PREFIX)) {
- p.Name = codes.generateCode(system.label.CRM_COMPONENT);
- p.Stage__c = STAGE_NEW;
- portsToUpdate.add(p);
- }
- }
- if(!portsToUpdate.isEmpty()) update portsToUpdate;
- }
- codes.updateValues();
- CPQ_Utils04_RequestStateCls.setOn('TPS_NUMBERS_ASSIGNED');
- upsert thirdPartyServicesToUpsert;
- upsert thirdPartyServicesToUpsert2.values();
- CPQ_Utils04_RequestStateCls.setOff('TPS_NUMBERS_ASSIGNED');
- /*
- List<X3rd_Party_Services__c> tpsesToUpsert = new List<X3rd_Party_Services__c>();
- tpsesToUpsert.addAll(thirdPartyServicesToUpsert);
- tpsesToUpsert.addAll(thirdPartyServicesToUpsert2);
- upsert tpsesToUpsert;
- */
- upsert thirdPartyServicesToUpsertParent x3ParSerUID__c;
- update thirdPartyItemsToUpdate;
- upsert chargesToUpsert;
- upsert portCRsToUpsert.values();
- List<Sold_Service__c> sServices = new List<Sold_Service__c>();
- sServices.addAll(soldServiceProductsToUpsert);
- sServices.addAll(soldServiceComponentsToUpsert);
- List<SoldService_Contact_Junction__c> soldServiceContacts = initContacts(sServices, opportunityId);
- if(soldServiceContacts != null && !soldServiceContacts.isEmpty()) insert soldServiceContacts;
- createChangeRequestAndLineItems(sServices, opportunityId);
- /* process dual link data */
- if(!soldServiceDualLinkReference.isEmpty()) {
- List<Sold_Service__c> dualLinkServices = new List<Sold_Service__c>();
- for(String upsertKey : soldServiceDualLinkReference.keySet()){
- if(quoteItemId2SoldServiceExternalId.get(soldServiceDualLinkReference.get(upsertKey)) != null){
- Sold_Service__c sserv = new Sold_Service__c(Upsert_Key__c = upsertKey);
- String fieldValue = '';
- for(Sold_Service__c s : sServices) {
- if(s.Upsert_Key__c == quoteItemId2SoldServiceExternalId.get(soldServiceDualLinkReference.get(upsertKey))) {
- fieldValue = s.Id;
- break;
- }
- }
- CPQ_PRI01_Manager.setFieldValueFromString(sserv, soldServiceDualLinkField.get(upsertKey), fieldValue, globalDescribe);
- dualLinkServices.add(sserv);
- }
- }
- upsert dualLinkServices Upsert_Key__c;
- }
- /* process a/b end data - reference to quote item / sold service should be put into A_End__c, B_End__c fields on quote item */
- Map<String, Sold_Service__c> soldServiceEndReferenceToUpdateMap = new Map<String, Sold_Service__c>();
- if (!soldServiceAEndReference.isEmpty()) {
- for(String upsertKey : soldServiceAEndReference.keySet()){
- Sold_Service__c sserv = new Sold_Service__c(Upsert_Key__c = upsertKey);
- if (soldServiceAEndReference.get(upsertKey).startsWith(CPQ_QuoteItem__c.sObjectType.getDescribe().getKeyPrefix())) {
- sserv.A_End__c = sServicesIdMap.get(quoteItemId2SoldServiceExternalId.get(soldServiceAEndReference.get(upsertKey)));
- } else if (soldServiceAEndReference.get(upsertKey).startsWith(Sold_Service__c.sObjectType.getDescribe().getKeyPrefix())) {
- sserv.A_End__c = soldServiceAEndReference.get(upsertKey);
- }
- soldServiceEndReferenceToUpdateMap.put(upsertKey, sserv);
- }
- }
- if (!soldServiceBEndReference.isEmpty()) {
- for(String upsertKey : soldServiceBEndReference.keySet()){
- Sold_Service__c sserv = (soldServiceEndReferenceToUpdateMap.get(upsertKey) == null ? new Sold_Service__c(Upsert_Key__c = upsertKey) : soldServiceEndReferenceToUpdateMap.get(upsertKey));
- if (soldServiceBEndReference.get(upsertKey).startsWith(CPQ_QuoteItem__c.sObjectType.getDescribe().getKeyPrefix())) {
- sserv.B_End__c = sServicesIdMap.get(quoteItemId2SoldServiceExternalId.get(soldServiceBEndReference.get(upsertKey)));
- } else if (soldServiceBEndReference.get(upsertKey).startsWith(Sold_Service__c.sObjectType.getDescribe().getKeyPrefix())) {
- sserv.B_End__c = soldServiceBEndReference.get(upsertKey);
- }
- soldServiceEndReferenceToUpdateMap.put(upsertKey, sserv);
- }
- }
- if (!soldServiceEndReferenceToUpdateMap.isEmpty()) {
- upsert soldServiceEndReferenceToUpdateMap.values() Upsert_Key__c;
- }
- System.debug('@@ END CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: createSoldServices');
- return result;
- }
- //Email Message Automation Step 1
- @future
- public static void sendEmailNotyficationAfterCreatingSoldService(Id opportunityId) {
- System.debug('@@ START CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: sendEmailNotyficationAfterCreatingSoldService');
- Opportunity opp = [Select id,
- OwnerId,
- Opportunity_Business_Line__c,
- Owner.Email,
- Owner.Name,
- Commercial_Contact__r.Email,
- Commercial_Contact__c,
- (SELECT Id,
- Item_Action__c
- from Sold_Services__r)
- from Opportunity where Id = :opportunityId
- ];
- Boolean actionDismante =false;
- for(Sold_Service__c ssList : opp.Sold_Services__r) {
- if(ssList.Item_Action__c == ACTION_DISMANTLE){
- actionDismante = true;
- }
- }
- if (opp.Opportunity_Business_Line__c !='Internal Orders' && actionDismante==false && !opp.Sold_Services__r.isEmpty()) {
- String FOLDER_AUTOMATION = 'D&I Delivery Automation';
- String mainString;
- String emailBody;
- String emailTable = '';
- Id contactId;
- Boolean isMoreThanOnePort = false;
- List<String> mailAdresses = new List<String>();
- List<Sold_Service__c> parentSoldServiceList = new List<Sold_Service__c>();
- List<Sold_Service__c> sortedSoldServiceList = new List<Sold_Service__c>();
- List<String> ccAddresses = new List<String>();
- Set<String> ssSet = new Set<String>();
- Set<String> ccAddressesSet = new Set<String>();
- EmailTemplate templateBody;
- EmailTemplate templateTable;
- DelComSettings__c temNames = DelComSettings__c.getInstance();
- System.debug('@@ SS1 Body Loading' + temNames);
- List<Sold_Service__c> soldServiceList = [SELECT Id,
- Opp_Link__c,
- A_Port__c,
- B_Location__c,
- Delivery_Coordinator__r.Email,
- (Select id,Contact__c,Contact__r.Email from SoldService_Contact_Junctions__r where Name = 'Delivery Contact'),
- B_Port__c,
- CRM_Parent_Sold_Service__c,
- Opp_Link__r.Commercial_Contact__c,
- Name, CRM_Account__c,
- Type_of_Service__c,
- RFS_Date__c,
- Item_Name__c,
- Stage__c,
- Status__c
- from Sold_Service__c where Opp_Link__c = :opportunityId
- ];
- for (Sold_Service__c ss : soldServiceList) {
- List<SoldService_Contact_Junction__c> ssJun = ss.SoldService_Contact_Junctions__r;
- for(SoldService_Contact_Junction__c ssJunCon : ssJun)
- {
- if (ssJunCon.Contact__r.Email != null && ssJunCon.Contact__c != contactId) {
- ccAddressesSet.add(ssJunCon.Contact__r.Email);
- }
- }
- if (ss.B_Port__c != null || ss.B_Location__c != null) {
- isMoreThanOnePort = true;
- }
- if(ss.CRM_Parent_Sold_Service__c == null){
- parentSoldServiceList.add(ss);
- }
- }
- mailAdresses.add(opp.Commercial_Contact__r.Email);
- ccAddresses.addAll(ccAddressesSet);
- List<EmailTemplate> templates = [select Id, Name, Subject, Folder.Name , HtmlValue from EmailTemplate where Folder.Name = : FOLDER_AUTOMATION];
- for (EmailTemplate et : templates) {
- if (isMoreThanOnePort) {
- if (et.Name.contains(temNames.Step_1_with_A_B_Table__c)) {
- templateTable = et;
- }
- if (et.Name.contains(temNames.Step_1_with_A_B__c)) {
- templateBody = et;
- }
- } else {
- if (et.Name.contains(temNames.Step_1_with_A_only_Table__c)) {
- templateTable = et;
- }
- if (et.Name.contains(temNames.Step_1_with_A_only__c)) {
- templateBody = et;
- }
- }
- }
- List<String> mailBodies = loadEmailTable(templateTable, soldServiceList);
- Integer cursor = 0;
- for (String mailBody : mailBodies) {
- String rowCss = '<tr height=\'30px\' style=\'background-color: #FFFFFF; margin: 0px; padding: 0px;\' id="templateBody">';
- if (Math.mod(cursor, 2) == 0) {
- rowCss = '<tr height=\'30px\' style=\'background-color: #f2f2f2; margin: 0px; padding: 0px;\'>';
- }
- emailTable += rowCss + mailBody + '</tr>';
- cursor++;
- }
- System.debug('@@ SS1 emailBody Loading' + emailTable);
- emailBody = loadEmailBody(templateBody, opportunityId , opp.Commercial_Contact__c);
- System.debug('@@ SS1 emailBody Loading ' + emailBody);
- mainString = emailBody.substring(0, emailBody.indexOf('<tr id="splitCursor"></tr>')) + emailTable + emailBody.substring(emailBody.indexOf('<tr id="splitCursor"></tr>'), emailBody.length());
- List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
- Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
- mail.setHtmlBody(mainString);
- mail.setWhatId(opportunityId);
- mail.setToAddresses(mailAdresses);
- if (!ccAddressesSet.isEmpty() && ccAddressesSet != null) {
- mail.setCcAddresses(ccAddresses);
- }
- mail.setTargetObjectId(opp.Commercial_Contact__c);
- mail.setSubject(templateBody.Subject);
- mail.setSaveAsActivity(true);
- //mail.setReplyTo(opp.Owner.Email);
- mails.add(mail);
- try {
- Messaging.sendEmail(mails);
- } catch (Exception e) {
- System.debug('Attachment insert failed. Details: ' + e);
- }
- System.debug('@@ END CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: sendEmailNotyficationAfterCreatingSoldService');
- }
- }
- //Email Template Body Loading
- public static String loadEmailBody(EmailTemplate emailTemplate , Id objectId , Id conId) {
- String result = '';
- Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
- mail.setWhatId(objectId);
- if (emailTemplate != null) {
- mail.setTemplateId(emailTemplate.Id);
- } else {
- mail.setHtmlBody('<p>test</p>');
- }
- mail.setUseSignature(false);
- mail.setTargetObjectId(conId);
- Savepoint sp = Database.setSavepoint();
- Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
- Database.rollback(sp);
- result = mail.getHtmlBody();
- return result;
- }
- //Email Template Table Loading
- public static List<String> loadEmailTable(EmailTemplate emailTemplate, List<Sold_Service__c> soldService) {
- List<String> result = new List<String>();
- Set<String> ssSet = new Set<String>();
- String valueOfField;
- Pattern pt = Pattern.compile('\\{([^}]*)\\}');
- Matcher m = pt.matcher(emailTemplate.htmlValue);
- for (Sold_Service__c listSs : soldService) {
- ssSet.add(listSs.Id);
- }
- Map<String, Schema.SObjectField> sObjMap = Schema.SObjectType.Sold_Service__c.fields.getMap();
- Schema.SObjectField sField;
- Schema.DisplayType sFldType;
- List<String> names = new List<String>();
- while (m.find()) {
- names.add(m.group().replace('{!Sold_Service__c.', '').replace('}', ''));
- }
- List<Sold_Service__c> sSoldServiceList = Database.query('SELECT ' + String.join(names, ',') + ' FROM Sold_Service__c WHERE Id IN :ssSet');
- for (Sold_Service__c ss : sSoldServiceList) {
- String email = '';
- email = emailTemplate.htmlValue;
- for (String field : names) {
- if (String.valueOf( ss.get(field)) != null) {
- if (String.valueOf( ss.get(field)) != null) {
- sFldType = sObjMap.get(field.toLowerCase()).getDescribe().getType();
- if (sFldType == Schema.DisplayType.DateTime) {
- valueOfField = String.valueOf(Datetime.valueOf(String.valueOf(ss.get(field))).date());
- } else {
- valueOfField = String.valueOf( ss.get(field));
- }
- } else {
- valueOfField = '';
- }
- } else {
- valueOfField = '';
- }
- email = email.replace('{!Sold_Service__c.' + field + '}', valueOfField);
- }
- result.add(email);
- }
- return result;
- }
- public static void updateQuoteItemFromSoldServiceMain(Set<Id> mergedQuoteItems) {
- if(mergedQuoteItems.size() <= QUANTITY_SUM_TRESHOLD) {
- updateQuoteItemFromSoldService(mergedQuoteItems);
- } else {
- CPQ_PRD09_UpdateFromSoldServiceBatchHlpr batch = new CPQ_PRD09_UpdateFromSoldServiceBatchHlpr(mergedQuoteItems);
- Database.executeBatch(batch, BATCH_SIZE);
- }
- }
- public static void updateQuoteItemFromSoldService(Set<Id> mergedQuoteItems) {
- System.debug('### updateQuoteItemFromSoldService [' + mergedQuoteItems + ']');
- String quoteItemsQuery = 'select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('CPQ_QuoteItem__c', null, null) + ', RecordType.Name, Product_Definition__r.Product_Group__c, Product_Definition__r.Name, Component_Definition__r.Name, Existing_Object_ID__r.Name ' +
- 'from CPQ_QuoteItem__c where Id in :mergedQuoteItems and Item_Action__c != \'Add\'';
- List<CPQ_QuoteItem__c> products = new List<CPQ_QuoteItem__c>();
- Map<Id, List<CPQ_QuoteItem__c>> components = new Map<Id, List<CPQ_QuoteItem__c>>();
- Set<Id> productIds = new Set<Id>();
- Set<Id> componentIds = new Set<Id>();
- Set<Id> soldServiceIds = new Set<Id>();
- Set<Id> opportunityLineItemIds = new Set<Id>();
- Map<Id, CPQ_QuoteItem__c> soldServiceToQuoteItemMap = new Map<Id, CPQ_QuoteItem__c>();
- List<CPQ_QuoteItem__c> quoteItemsQueryResult = Database.query(quoteItemsQuery);
- for(CPQ_QuoteItem__c qItem : Database.query(quoteItemsQuery)) {
- if(qItem.RecordType.Name == RT_QUITEM_PRODUCT) {
- products.add(qItem);
- productIds.add(qItem.Product_Definition__c);
- soldServiceIds.add(qItem.Existing_Object_ID__c);
- opportunityLineItemIds.add(qItem.OpportunityLineItem_ID__c);
- soldServiceToQuoteItemMap.put(qItem.Existing_Object_ID__c, qItem);
- } else if(qItem.RecordType.Name == RT_QUITEM_COMPONENT) {
- List<CPQ_QuoteItem__c> componentsList = components.get(qItem.Parent_Item__c);
- if(componentsList == null) {
- componentsList = new List<CPQ_QuoteItem__c>();
- components.put(qItem.Parent_Item__c, componentsList);
- }
- componentsList.add(qItem);
- componentIds.add(qItem.Component_Definition__c);
- soldServiceIds.add(qItem.Existing_Object_ID__c);
- opportunityLineItemIds.add(qItem.OpportunityLineItem_ID__c);
- soldServiceToQuoteItemMap.put(qItem.Existing_Object_ID__c, qItem);
- }
- }
- /* loading opportunity products */
- Map<Id, OpportunityLineItem> opportunityLineItems = new Map<Id, OpportunityLineItem>([select Id, OLI_No__c, CurrencyIsoCode from OpportunityLineItem where Id in :opportunityLineItemIds]);
- Map<String, Decimal> currencyMap = CurrencyConversation.CurrencyUtilitymethod();
- /* loading mapping configuration */
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Attributes = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Fields = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Constants = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>> soldServiceMappings_Numbers = new Map<Id, Map<Id, List<CPQ_Sold_Service_Mapping__c>>>();
- for(CPQ_Sold_Service_Mapping__c mapping : [select Id, Attribute__c, Component__c, Data_Type__c, Generate_ICC_Number__c, Generate_IC_Number__c, Product__c,
- Source_Constant_Value__c, Source_Field_Name__c, Target_Field_OLI__c, Target_Field_Sold_Service__c from CPQ_Sold_Service_Mapping__c where Product__c in :productIds]){
- Map<Id, List<CPQ_Sold_Service_Mapping__c>> componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_ATTRIBUTE_MAPPING) {
- componentMappings = soldServiceMappings_Attributes.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Attributes.put(mapping.Product__c, componentMappings);
- }
- } else if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_FIELD_MAPPING) {
- componentMappings = soldServiceMappings_Fields.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Fields.put(mapping.Product__c, componentMappings);
- }
- } else if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_CONSTANT_MAPPING) {
- componentMappings = soldServiceMappings_Constants.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Constants.put(mapping.Product__c, componentMappings);
- }
- } else if(mapping.Data_Type__c == CPQ_PRD03_SoldServiceMappingDesignCtrl.TYPE_NUMBER_MAPPING) {
- componentMappings = soldServiceMappings_Numbers.get(mapping.Product__c);
- if(componentMappings == null){
- componentMappings = new Map<Id, List<CPQ_Sold_Service_Mapping__c>>();
- soldServiceMappings_Numbers.put(mapping.Product__c, componentMappings);
- }
- }
- List<CPQ_Sold_Service_Mapping__c> mappings = componentMappings.get(mapping.Component__c);
- if(mappings == null) {
- mappings = new List<CPQ_Sold_Service_Mapping__c>();
- componentMappings.put(mapping.Component__c, mappings);
- }
- mappings.add(mapping);
- }
- // QUERY SOLD SERVICES
- String soldServiceQuery = 'select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('Sold_Service__c', null, null) + ', A_Port__r.Port_Type__c, A_Port__r.Homing_Gateway__c, A_Port__r.Homing_Gateway__r.Continent__c, B_Port__r.Port_Type__c, B_Port__r.Homing_Gateway__c, B_Port__r.Homing_Gateway__r.Continent__c, A_End__r.Name, B_End__r.Name, ' +
- '(select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('CPQ_Charge__c', null, null) + ' from Charges__r order by Id) ' +
- 'from Sold_Service__c where Id in :soldServiceIds or (CRM_Parent_Sold_Service__c in :soldServiceIds and Status__c = \'Active\')';
- Map<Id, Sold_Service__c> soldServices = new Map<Id, Sold_Service__c>((List<Sold_Service__c>)Database.query(soldServiceQuery));
- // QUERY TPS'ES AND BUILD MAP (ID of SOLD SERVICE WITH LIST OF ALL CHILD TPS'es)
- // MASIM - added due to "Aggregate query has too many rows for direct assignment - QC-1944"
- Map<Id, List<X3rd_Party_Services__c>> thirdPartyServicesMap = new Map<Id, List<X3rd_Party_Services__c>>();
- Set<Id> soldServicesIds = soldServices.keySet();
- String thirdPartyServiceQuery = 'select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('X3rd_Party_Services__c', null, null) + ', Parent__r.x3ParSerUID__c, Supplier_Account__r.Name ' +
- 'from X3rd_Party_Services__c where Sold_Service__c IN :soldServicesIds ORDER BY Sold_Service__c';
- List<X3rd_Party_Services__c> childThirdPartyServices = new List<X3rd_Party_Services__c>();
- //IT-6341 - Przemyslaw Tustanowski
- for (X3rd_Party_Services__c tps : (List<X3rd_Party_Services__c>)Database.query(thirdPartyServiceQuery)) {
- if(!thirdPartyServicesMap.containsKey(tps.Sold_Service__c)){
- thirdPartyServicesMap.put(tps.Sold_Service__c, new List<X3rd_Party_Services__c>());
- }
- thirdPartyServicesMap.get(tps.Sold_Service__c).add(tps);
- }
- // QUERY ATTRIBUTES
- String attributeDefinitionQuery = 'select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('CPQ_Attribute__c', null, null) + ', ' +
- '(select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('CPQ_AttributeValue__c', null, null) + ' from AttributeValues__r) ' +
- 'from CPQ_Attribute__c where Id in (select Attribute__c from CPQ_ProductAttribute__c where Product__c in :productIds or Component__c in :componentIds)';
- Map<Id, CPQ_Attribute__c> attributeDefinitions = new Map<Id, CPQ_Attribute__c>((List<CPQ_Attribute__c>)Database.query(attributeDefinitionQuery));
- /* try to find Date of Change within attributes definition */
- Id dateOfChangeAttributeId = null;
- Id billingCommDateAttributeId = null;
- Id contractLengthAttributeId = null;
- for(CPQ_Attribute__c attr : attributeDefinitions.values()) {
- if(attr.TECH_Definition_ID__c == ATTRIBUTE_DATE_OF_CHANGE) {
- dateOfChangeAttributeId = attr.Id;
- } else if(attr.TECH_Definition_ID__c == ATTRIBUTE_BILLING_COMM) {
- billingCommDateAttributeId = attr.Id;
- } else if(attr.TECH_Definition_ID__c == ATTRIBUTE_CONTRACT_LENGTH) {
- contractLengthAttributeId = attr.Id;
- }
- }
- Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
- List<CPQ_QuoteItemAttribute__c> attributesToInsert = new List<CPQ_QuoteItemAttribute__c>();
- List<CPQ_QuoteItem__c> quoteItemsToUpdate = new List<CPQ_QuoteItem__c>();
- List<X3rd_Party_Item__c> thirdPartyItemsToInsert = new List<X3rd_Party_Item__c>();
- List<X3rd_Party_Item__c> thirdPartyItemsToUpsertParent = new List<X3rd_Party_Item__c>();
- /* initializing fields for TPS -> TPI mapping */
- Map<String,String> tpsFieldMap = SoldServiceUtility.buildFieldMapping(ApplicationConstant.FIELD_MAPPING_THIRDPARTY);
- for(CPQ_QuoteItem__c product : products) {
- Sold_Service__c productSoldService = soldServices.get(product.Existing_Object_ID__c);
- if(productSoldService != null) {
- //product.Configuration_Loaded__c = true;
- product.Description__c = productSoldService.Service_Description__c;
- product.OTC_List__c = CPQ_PRI01_Manager.convertCurrencyAmount(productSoldService.List_OTC__c, productSoldService.CurrencyIsoCode, product.CurrencyIsoCode, currencyMap); // productSoldServi.List_OTC__c;
- product.OTC__c = CPQ_PRI01_Manager.convertCurrencyAmount(productSoldService.OTC__c, productSoldService.CurrencyIsoCode, product.CurrencyIsoCode, currencyMap); // productSoldService.OTC__c;
- product.MRC_List__c = CPQ_PRI01_Manager.convertCurrencyAmount(productSoldService.List_MRC__c, productSoldService.CurrencyIsoCode, product.CurrencyIsoCode, currencyMap); // productSoldService.List_MRC__c;
- product.MRC__c = CPQ_PRI01_Manager.convertCurrencyAmount(productSoldService.MRC__c, productSoldService.CurrencyIsoCode, product.CurrencyIsoCode, currencyMap); // productSoldService.MRC__c;
- product.MRC_Previous__c = CPQ_PRI01_Manager.convertCurrencyAmount(productSoldService.MRC_Total__c, productSoldService.CurrencyIsoCode, product.CurrencyIsoCode, currencyMap); // productSoldService.MRC_Total__c;
- product.MRE_Previous__c = CPQ_PRI01_Manager.convertCurrencyAmount(productSoldService.MRE__c, productSoldService.CurrencyIsoCode, product.CurrencyIsoCode, currencyMap); // productSoldService.MRE__c;
- product.Unit_Price_Previous__c = CPQ_PRI01_Manager.convertCurrencyAmount(productSoldService.Recommended_Base_Unit_Price__c, productSoldService.CurrencyIsoCode, product.CurrencyIsoCode, currencyMap); // productSoldService.Unit_Price__c;
- product.Previous_CDR__c = productSoldService.Previous_CDR__c; // productSoldService.Previous_CDR__c;
- product.A_Location__c = productSoldService.A_Location__c;
- product.A_Port__c = productSoldService.A_Port__c;
- product.A_Port_Option__c = productSoldService.A_Port_Option__c;
- product.A_Port_Type__c = productSoldService.A_Port_Type__c;
- product.A_Site__c = productSoldService.A_Site__c;
- product.A_Demarc_Site__c = productSoldService.A_Demarc_Site__c;
- product.A_Demarc_Type__c = productSoldService.A_Demarc_Type__c;
- product.A_Demarc_Comments__c = productSoldService.A_Demarc_Comments__c;
- product.A_Floor__c = productSoldService.A_Floor__c;
- product.A_Room__c = productSoldService.A_Room__c;
- product.A_Rack_Cabinet__c = productSoldService.A_Rack_Cabinet__c;
- product.A_Homing_Gateway_Location__c = productSoldService.A_Homing_Gateway_Location__c;
- /* Handling A End reference */
- if (!String.isBlank(productSoldService.A_End__c)) {
- if (soldServiceIds.contains(productSoldService.A_End__c)) {
- product.A_End_ID__c = soldServiceToQuoteItemMap.get(productSoldService.A_End__c).Id;
- product.A_End_Name__c = soldServiceToQuoteItemMap.get(productSoldService.A_End__c).Name;
- } else {
- product.A_End_ID__c = productSoldService.A_End__c;
- product.A_End_Name__c = productSoldService.A_End__r.Name;
- }
- }
- product.B_Location__c = productSoldService.B_Location__c;
- product.B_Port__c = productSoldService.B_Port__c;
- product.B_Port_Option__c = productSoldService.B_Port_Option__c;
- product.B_Port_Type__c = productSoldService.B_Port_Type__c;
- product.B_Site__c = productSoldService.B_Site__c;
- product.B_Demarc_Site__c = productSoldService.B_Demarc_Site__c;
- product.B_Demarc_Type__c = productSoldService.B_Demarc_Type__c;
- product.B_Demarc_Comments__c = productSoldService.B_Demarc_Comments__c;
- product.B_Floor__c = productSoldService.B_Floor__c;
- product.B_Room__c = productSoldService.B_Room__c;
- product.B_Rack_Cabinet__c = productSoldService.B_Rack_Cabinet__c;
- product.B_Homing_Gateway_Location__c = productSoldService.B_Homing_Gateway_Location__c;
- /* Handling B End reference */
- if (!String.isBlank(productSoldService.B_End__c)) {
- if (soldServices.containsKey(productSoldService.B_End__c)) {
- product.B_End_ID__c = soldServiceToQuoteItemMap.get(productSoldService.B_End__c).Id;
- product.B_End_Name__c = soldServiceToQuoteItemMap.get(productSoldService.B_End__c).Name;
- } else {
- product.B_End_ID__c = productSoldService.B_End__c;
- product.B_End_Name__c = productSoldService.B_End__r.Name;
- }
- }
- if(product.Item_Action__c == ACTION_DISMANTLE) {
- product.OTC_List__c = 0;
- product.OTC__c = 0;
- product.MRC_List__c = 0;
- product.MRC__c = 0;
- } else if(product.Item_Action__c == ACTION_RENEWAL) {
- product.OTC_List__c = 0;
- product.OTC__c = 0;
- }
- /* creating delivery date, billing date and contract length attributes */
- Boolean dateOfChangeAttributeCreated = false;
- Boolean billingCommDateAttributeCreated = false;
- Boolean contractLengthAttributeCreated = false;
- Date expectedDeliveryDate = product.Date_Expected_Delivery__c;
- if(expectedDeliveryDate != null) {
- if(dateOfChangeAttributeId != null) {
- attributesToInsert.add(createAttribute(product.Id, attributeDefinitions.get(dateOfChangeAttributeId), String.valueOf(expectedDeliveryDate), product.Product_Definition__c, false));
- dateOfChangeAttributeCreated = true;
- }
- if(billingCommDateAttributeId != null) {
- attributesToInsert.add(createAttribute(product.Id, attributeDefinitions.get(billingCommDateAttributeId), String.valueOf(expectedDeliveryDate), product.Product_Definition__c, false));
- billingCommDateAttributeCreated = true;
- }
- }
- if(product.Contract_Length__c != null && contractLengthAttributeId != null) {
- attributesToInsert.add(createAttribute(product.Id, attributeDefinitions.get(contractLengthAttributeId), String.valueOf(product.Contract_Length__c), product.Product_Definition__c, false));
- contractLengthAttributeCreated = true;
- }
- /* assigning attribute values */
- if(soldServiceMappings_Attributes.get(product.Product_Definition__c) != null && soldServiceMappings_Attributes.get(product.Product_Definition__c).get(null) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Attributes.get(product.Product_Definition__c).get(null)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- if(attributeDefinitions.get(mapping.Attribute__c) != null && attributeDefinitions.get(mapping.Attribute__c).Type__c != CPQ_ATR01_Manager.ATR_TYPE_CHARGE
- && (mapping.Attribute__c != dateOfChangeAttributeId || dateOfChangeAttributeCreated == false)
- && (mapping.Attribute__c != billingCommDateAttributeId || billingCommDateAttributeCreated == false)
- && (mapping.Attribute__c != contractLengthAttributeId || contractLengthAttributeCreated == false)) {
- String fieldValue = '';
- if(attributeDefinitions.get(mapping.Attribute__c).TECH_Definition_ID__c == ATTRIBUTE_PORTS_GRID) {
- List<Sold_Service__c> soldServiceComponents = new List<Sold_Service__c>();
- for(Sold_Service__c s : soldServices.values()){
- if(s.CRM_Parent_Sold_Service__c == product.Existing_Object_ID__c){
- soldServiceComponents.add(s);
- }
- }
- fieldValue = CalculateGridValueForPorts(attributeDefinitions.get(mapping.Attribute__c).Grid_Layout__c, null, soldServiceComponents);
- } else {
- fieldValue = productSoldService.get(mapping.Target_Field_Sold_Service__c) == null ? '' : String.valueOf(productSoldService.get(mapping.Target_Field_Sold_Service__c));
- }
- if(String.isNotEmpty(fieldValue)){
- attributesToInsert.add(createAttribute(product.Id, attributeDefinitions.get(mapping.Attribute__c), fieldValue, product.Product_Definition__c, false));
- }
- }
- }
- }
- }
- /* assigning charge attributes */
- Map<Id, List<CPQ_Charge__c>> chargesToProcess = new Map<Id, List<CPQ_Charge__c>>();
- if(productSoldService.Charges__r != null){
- for(CPQ_Charge__c c : productSoldService.Charges__r){
- if(c.Attribute__c != null) {
- List<CPQ_Charge__c> charges = chargesToProcess.get(c.Attribute__c);
- if(charges == null) {
- charges = new List<CPQ_Charge__c>();
- chargesToProcess.put(c.Attribute__c, charges);
- }
- charges.add(c);
- }
- }
- }
- System.debug('### chargesToProcess = ' + chargesToProcess);
- if(!chargesToProcess.isEmpty()) {
- for(Id attributeId : chargesToProcess.keySet()){
- String fieldValue = JSON.serialize(chargesToProcess.get(attributeId));
- attributesToInsert.add(createAttribute(product.Id, attributeDefinitions.get(attributeId), fieldValue, product.Product_Definition__c, false));
- }
- }
- /* assigning field values */
- if(soldServiceMappings_Fields.get(product.Product_Definition__c) != null && soldServiceMappings_Fields.get(product.Product_Definition__c).get(null) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Fields.get(product.Product_Definition__c).get(null)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- String fieldValue = String.valueOf(productSoldService.get(mapping.Target_Field_Sold_Service__c));
- CPQ_PRI01_Manager.setFieldValueFromString(product, mapping.Source_Field_Name__c, fieldValue, globalDescribe);
- }
- }
- }
- if(String.isNotEmpty(productSoldService.A_Port_Option__c) && (product.A_Port_Option__c == 'New Port' || product.A_Port_Option__c == 'Port from Opportunity')) {
- product.A_Port_Option__c = 'Existing Port';
- }
- if(String.isNotEmpty(productSoldService.B_Port_Option__c) && (product.B_Port_Option__c == 'New Port' || product.B_Port_Option__c == 'Port from Opportunity')) {
- product.B_Port_Option__c = 'Existing Port';
- }
- if(expectedDeliveryDate != null) {
- product.Date_Expected_Delivery__c = expectedDeliveryDate;
- }
- quoteItemsToUpdate.add(product);
- if(product.Item_Action__c != ACTION_DISMANTLE && product.Item_Action__c != ACTION_NO_ACTION && product.Item_Action__c != ACTION_CHANGE) {
- if(thirdPartyServicesMap.containsKey(productSoldService.Id)) {
- Map<Id, Id> tpsToParent = new Map<Id, Id>();
- Map<Id, String> tpsIdToTPIExternalId = new Map<Id, String>();
- List<X3rd_Party_Item__c> tpiToParentUpdate = new List<X3rd_Party_Item__c>();
- for(X3rd_Party_Services__c tps : thirdPartyServicesMap.get(productSoldService.Id)) {
- /* exclude TSIC TPSes & TPS Support for POP Expenses*/
- Boolean includeTPS = true;
- if((tps.Supplier_Account__c != null && tps.Supplier_Account__r.Name.startsWithIgnoreCase(ApplicationConstant.COMPANY_NAME)) || (tps.Primary_Generic_Supplier_Product__c == TPS_PRIMARY_PROD_SITE)) {
- includeTPS = false;
- }
- if(tps.Status__c == '10 Delivered' && includeTPS) {
- X3rd_Party_Item__c tpi = new X3rd_Party_Item__c();
- for (String f : tpsFieldMap.keySet()) {
- if(f != 'Id' && f != 'CreatedDate') {
- tpi.put(f, tps.get(tpsFieldMap.get(f)));
- System.debug('### Copying data from ' + tpsFieldMap.get(f) + ' (TPS) to ' + f + ' (TPI)');
- }
- }
- tpi.Chosen__c = true;
- tpi.Existing_X3rd_Party_Service__c = tps.Id;
- tpi.Opportunity__c = product.Opportunity__c;
- tpi.Opportunity_Line_Item__c = product.OpportunityLineItem_ID__c;
- tpi.OLI_No__c = opportunityLineItems.get(product.OpportunityLineItem_ID__c) != null ? opportunityLineItems.get(product.OpportunityLineItem_ID__c).OLI_No__c : '?';
- tpi.Sold_Service__c = product.Existing_Object_ID__c;
- tpi.Quote_Item__c = product.Id;
- tpi.Quantity__c = 1;
- tpi.Port__c = tps.Port__c;
- tpi.Initial_MRE__c = tps.MRE__c;
- //tpi.Initial_OTE_COGS__c = tps.OTE_COGS__c;
- //tpi.Initial_OTE_Capex__c = tps.OTE_Capex__c;
- tpi.Previous_MRE__c = tps.MRE__c;
- tpi.Unit_Initial_MRE_COGS__c = tps.MRE__c;
- tpi.Unit_Initial_OTE_COGS__c = 0;
- tpi.Unit_Initial_OTE_CAPEX__c = 0;
- /* clear values that might have been mapped */
- tpi.Unit_Final_MRE_COGS__c = null;
- tpi.Unit_Final_OTE_COGS__c = null;
- tpi.Unit_Final_OTE_CAPEX__c = null;
- tpi.Final_MRE__c = null;
- tpi.Final_OTE_COGS__c = null;
- tpi.Final_OTE_Capex__c = null;
- tpi.Initial_OTE_COGS__c = 0;
- tpi.Initial_OTE_Capex__c = 0;
- tpi.Status__c = TPI_STATUS_QUOTE_REQ;
- if(product.Item_Action__c == ACTION_RENEWAL || product.Item_Action__c == ACTION_CEASE) {
- tpi.TPI_Order_Type__c = 'Renew';
- } else {
- tpi.TPI_Order_Type__c = 'Upgrade / Downgrade';
- }
- if(String.isEmpty(tpi.Item_End_Type__c)) {
- tpi.Item_End_Type__c = 'A';
- if(product.B_Port__c != null && tpi.Port__c != null && product.B_Port__c == tpi.Port__c) {
- tpi.Item_End_Type__c = 'B';
- }
- }
- String h3 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String tpiUpsertKey = h3.substring(0,8)+ '-' + h3.substring(8,12) + '-' + h3.substring(12,16) + '-' + h3.substring(16,20) + '-' + h3.substring(20);
- tpi.Upsert_Key__c = tpiUpsertKey;
- if(tps.Parent__c != null && String.isNotEmpty(tps.Parent__r.x3ParSerUID__c)) {
- tpsToParent.put(tps.Id, tps.Parent__c);
- tpiToParentUpdate.add(tpi);
- }
- tpsIdToTPIExternalId.put(tps.Id, tpi.Upsert_Key__c);
- thirdPartyItemsToInsert.add(tpi);
- }
- }
- for(X3rd_Party_Item__c tpi : tpiToParentUpdate) {
- if(tpsToParent.keySet().contains(tpi.Existing_X3rd_Party_Service__c)) {
- if(tpsIdToTPIExternalId.get(tpsToParent.get(tpi.Existing_X3rd_Party_Service__c)) != null) {
- X3rd_Party_Item__c tpiParent = new X3rd_Party_Item__c(Upsert_Key__c = tpi.Upsert_Key__c);
- tpiParent.Parent__r = new X3rd_Party_Item__c(Upsert_Key__c = tpsIdToTPIExternalId.get(tpsToParent.get(tpi.Existing_X3rd_Party_Service__c)));
- thirdPartyItemsToUpsertParent.add(tpiParent);
- }
- }
- }
- }
- }
- }
- if(components.get(product.Id) != null) {
- for(CPQ_QuoteItem__c component : components.get(product.Id)) {
- Sold_Service__c componentSoldService = soldServices.get(component.Existing_Object_ID__c);
- //component.Configuration_Loaded__c = true;
- component.Description__c = componentSoldService.Service_Description__c;
- component.OTC_List__c = CPQ_PRI01_Manager.convertCurrencyAmount(componentSoldService.List_OTC__c, componentSoldService.CurrencyIsoCode, component.CurrencyIsoCode, currencyMap); // componentSoldService.List_OTC__c;
- component.OTC__c = CPQ_PRI01_Manager.convertCurrencyAmount(componentSoldService.OTC__c, componentSoldService.CurrencyIsoCode, component.CurrencyIsoCode, currencyMap); // componentSoldService.OTC__c;
- component.MRC_List__c = CPQ_PRI01_Manager.convertCurrencyAmount(componentSoldService.List_MRC__c, componentSoldService.CurrencyIsoCode, component.CurrencyIsoCode, currencyMap); // componentSoldService.List_MRC__c;
- component.MRC__c = CPQ_PRI01_Manager.convertCurrencyAmount(componentSoldService.MRC__c, componentSoldService.CurrencyIsoCode, component.CurrencyIsoCode, currencyMap); // componentSoldService.MRC__c;
- component.MRC_Previous__c = CPQ_PRI01_Manager.convertCurrencyAmount(componentSoldService.MRC_Total__c, componentSoldService.CurrencyIsoCode, component.CurrencyIsoCode, currencyMap); // componentSoldService.MRC__c;
- component.MRE_Previous__c = CPQ_PRI01_Manager.convertCurrencyAmount(componentSoldService.MRE__c, componentSoldService.CurrencyIsoCode, component.CurrencyIsoCode, currencyMap); // componentSoldService.MRE__c;
- component.Unit_Price_Previous__c = CPQ_PRI01_Manager.convertCurrencyAmount(componentSoldService.Recommended_Base_Unit_Price__c, productSoldService.CurrencyIsoCode, component.CurrencyIsoCode, currencyMap); // componentSoldService.Unit_Price__c;
- component.Previous_CDR__c = componentSoldService.Previous_CDR__c; // componentSoldService.Previous_CDR__c;
- component.A_Location__c = componentSoldService.A_Location__c;
- component.A_Port__c = componentSoldService.A_Port__c;
- component.A_Port_Option__c = componentSoldService.A_Port_Option__c;
- component.A_Port_Type__c = componentSoldService.A_Port_Type__c;
- component.A_Site__c = componentSoldService.A_Site__c;
- component.A_Demarc_Site__c = componentSoldService.A_Demarc_Site__c;
- component.A_Demarc_Type__c = componentSoldService.A_Demarc_Type__c;
- component.A_Demarc_Comments__c = componentSoldService.A_Demarc_Comments__c;
- component.A_Floor__c = componentSoldService.A_Floor__c;
- component.A_Room__c = componentSoldService.A_Room__c;
- component.A_Rack_Cabinet__c = componentSoldService.A_Rack_Cabinet__c;
- component.A_Homing_Gateway_Location__c = componentSoldService.A_Homing_Gateway_Location__c;
- /* Handling A End reference */
- if (!String.isBlank(componentSoldService.A_End__c)) {
- if (soldServices.containsKey(componentSoldService.A_End__c)) {
- component.A_End_ID__c = soldServiceToQuoteItemMap.get(componentSoldService.A_End__c).Id;
- component.A_End_Name__c = soldServiceToQuoteItemMap.get(componentSoldService.A_End__c).Name;
- } else {
- component.A_End_ID__c = componentSoldService.A_End__c;
- component.A_End_Name__c = componentSoldService.A_End__r.Name;
- }
- }
- component.B_Location__c = componentSoldService.B_Location__c;
- component.B_Port__c = componentSoldService.B_Port__c;
- component.B_Port_Option__c = componentSoldService.B_Port_Option__c;
- component.B_Port_Type__c = componentSoldService.B_Port_Type__c;
- component.B_Site__c = componentSoldService.B_Site__c;
- component.B_Demarc_Site__c = componentSoldService.B_Demarc_Site__c;
- component.B_Demarc_Type__c = componentSoldService.B_Demarc_Type__c;
- component.B_Demarc_Comments__c = componentSoldService.B_Demarc_Comments__c;
- component.B_Floor__c = componentSoldService.B_Floor__c;
- component.B_Room__c = componentSoldService.B_Room__c;
- component.B_Rack_Cabinet__c = componentSoldService.B_Rack_Cabinet__c;
- component.B_Homing_Gateway_Location__c = componentSoldService.B_Homing_Gateway_Location__c;
- /* Handling B End reference */
- if (!String.isBlank(componentSoldService.B_End__c)) {
- if (soldServices.containsKey(componentSoldService.B_End__c)) {
- component.B_End_ID__c = soldServiceToQuoteItemMap.get(componentSoldService.B_End__c).Id;
- component.B_End_Name__c = soldServiceToQuoteItemMap.get(componentSoldService.B_End__c).Name;
- } else {
- component.B_End_ID__c = componentSoldService.B_End__c;
- component.B_End_Name__c = componentSoldService.B_End__r.Name;
- }
- }
- if(component.Item_Action__c == ACTION_DISMANTLE) {
- component.OTC_List__c = 0;
- component.OTC__c = 0;
- component.MRC_List__c = 0;
- component.MRC__c = 0;
- } else if(component.Item_Action__c == ACTION_RENEWAL) {
- component.OTC_List__c = 0;
- component.OTC__c = 0;
- }
- /* creating delivery date, billing date and contract length attributes */
- Boolean dateOfChangeAttributeComponentCreated = false;
- Boolean billingCommDateAttributeComponentCreated = false;
- Boolean contractLengthAttributeComponentCreated = false;
- Date expectedDeliveryDateComponent = component.Date_Expected_Delivery__c;
- if(expectedDeliveryDateComponent != null) {
- if(dateOfChangeAttributeId != null) {
- attributesToInsert.add(createAttribute(component.Id, attributeDefinitions.get(dateOfChangeAttributeId), String.valueOf(expectedDeliveryDateComponent), component.Component_Definition__c, true));
- dateOfChangeAttributeComponentCreated = true;
- }
- if(billingCommDateAttributeId != null) {
- attributesToInsert.add(createAttribute(component.Id, attributeDefinitions.get(billingCommDateAttributeId), String.valueOf(expectedDeliveryDateComponent), component.Component_Definition__c, true));
- billingCommDateAttributeComponentCreated = true;
- }
- }
- if(component.Contract_Length__c != null && contractLengthAttributeId != null) {
- attributesToInsert.add(createAttribute(component.Id, attributeDefinitions.get(contractLengthAttributeId), String.valueOf(component.Contract_Length__c), component.Component_Definition__c, true));
- contractLengthAttributeComponentCreated = true;
- }
- /* assigning attribute values */
- if(soldServiceMappings_Attributes.get(product.Product_Definition__c) != null && soldServiceMappings_Attributes.get(product.Product_Definition__c).get(component.Component_Definition__c) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Attributes.get(product.Product_Definition__c).get(component.Component_Definition__c)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- if(attributeDefinitions.get(mapping.Attribute__c) != null && attributeDefinitions.get(mapping.Attribute__c).Type__c != CPQ_ATR01_Manager.ATR_TYPE_CHARGE
- && (mapping.Attribute__c != dateOfChangeAttributeId || dateOfChangeAttributeComponentCreated == null)
- && (mapping.Attribute__c != billingCommDateAttributeId || billingCommDateAttributeComponentCreated == null)
- && (mapping.Attribute__c != contractLengthAttributeId || contractLengthAttributeComponentCreated == null)) {
- String fieldValue = componentSoldService.get(mapping.Target_Field_Sold_Service__c) == null ? '' : String.valueOf(componentSoldService.get(mapping.Target_Field_Sold_Service__c));
- if(String.isNotEmpty(fieldValue)){
- attributesToInsert.add(createAttribute(component.Id, attributeDefinitions.get(mapping.Attribute__c), fieldValue, component.Component_Definition__c, true));
- }
- }
- }
- }
- }
- /* assigning charge attributes */
- Map<Id, List<CPQ_Charge__c>> chargesToProcess = new Map<Id, List<CPQ_Charge__c>>();
- if(componentSoldService.Charges__r != null){
- for(CPQ_Charge__c c : componentSoldService.Charges__r){
- if(c.Attribute__c != null) {
- List<CPQ_Charge__c> charges = chargesToProcess.get(c.Attribute__c);
- if(charges == null) {
- charges = new List<CPQ_Charge__c>();
- chargesToProcess.put(c.Attribute__c, charges);
- }
- charges.add(c);
- }
- }
- }
- if(!chargesToProcess.isEmpty()) {
- for(Id attributeId : chargesToProcess.keySet()){
- String fieldValue = JSON.serialize(chargesToProcess.get(attributeId));
- attributesToInsert.add(createAttribute(component.Id, attributeDefinitions.get(attributeId), fieldValue, component.Product_Definition__c, false));
- }
- }
- /* assigning field values */
- if(soldServiceMappings_Fields.get(product.Product_Definition__c) != null && soldServiceMappings_Fields.get(product.Product_Definition__c).get(null) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Fields.get(product.Product_Definition__c).get(null)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- String fieldValue = String.valueOf(productSoldService.get(mapping.Target_Field_Sold_Service__c));
- CPQ_PRI01_Manager.setFieldValueFromString(product, mapping.Source_Field_Name__c, fieldValue, globalDescribe);
- }
- }
- }
- /* assigning field values */
- if(soldServiceMappings_Fields.get(product.Product_Definition__c) != null && soldServiceMappings_Fields.get(product.Product_Definition__c).get(component.Component_Definition__c) != null) {
- for(CPQ_Sold_Service_Mapping__c mapping : soldServiceMappings_Fields.get(product.Product_Definition__c).get(component.Component_Definition__c)) {
- if(String.isNotEmpty(mapping.Target_Field_Sold_Service__c)) {
- String fieldValue = String.valueOf(componentSoldService.get(mapping.Target_Field_Sold_Service__c));
- CPQ_PRI01_Manager.setFieldValueFromString(component, mapping.Source_Field_Name__c, fieldValue, globalDescribe);
- }
- }
- }
- if(String.isNotEmpty(componentSoldService.A_Port_Option__c) && (component.A_Port_Option__c == 'New Port' || component.A_Port_Option__c == 'Port from Opportunity')) {
- component.A_Port_Option__c = 'Existing Port';
- }
- if(String.isNotEmpty(componentSoldService.B_Port_Option__c) && (component.B_Port_Option__c == 'New Port' || component.B_Port_Option__c == 'Port from Opportunity')) {
- component.B_Port_Option__c = 'Existing Port';
- }
- if(expectedDeliveryDateComponent != null) {
- component.Date_Expected_Delivery__c = expectedDeliveryDateComponent;
- }
- quoteItemsToUpdate.add(component);
- if(component.Item_Action__c != ACTION_DISMANTLE && component.Item_Action__c != ACTION_CHANGE) {
- if(thirdPartyServicesMap.containsKey(componentSoldService.Id)) {
- Map<Id, Id> tpsToParent = new Map<Id, Id>();
- Map<Id, String> tpsIdToTPIExternalId = new Map<Id, String>();
- List<X3rd_Party_Item__c> tpiToParentUpdate = new List<X3rd_Party_Item__c>();
- for(X3rd_Party_Services__c tps : thirdPartyServicesMap.get(componentSoldService.Id)) {
- /* exclude TSIC TPSes */
- Boolean includeTPS = true;
- if((tps.Supplier_Account__c != null && tps.Supplier_Account__r.Name.startsWithIgnoreCase(ApplicationConstant.COMPANY_NAME)) || (tps.Primary_Generic_Supplier_Product__c == TPS_PRIMARY_PROD_SITE)) {
- includeTPS = false;
- }
- if(tps.Status__c == '10 Delivered' && includeTPS) {
- X3rd_Party_Item__c tpi = new X3rd_Party_Item__c();
- for (String f : tpsFieldMap.keySet()) {
- if(f != 'Id' && f != 'CreatedDate') {
- tpi.put(f, tps.get(tpsFieldMap.get(f)));
- System.debug('### Copying data from ' + tpsFieldMap.get(f) + ' (TPS) to ' + f + ' (TPI)');
- }
- }
- tpi.Chosen__c = true;
- tpi.Existing_X3rd_Party_Service__c = tps.Id;
- tpi.Opportunity__c = component.Opportunity__c;
- tpi.Opportunity_Line_Item__c = component.OpportunityLineItem_ID__c;
- tpi.OLI_No__c = opportunityLineItems.get(component.OpportunityLineItem_ID__c) != null ? opportunityLineItems.get(component.OpportunityLineItem_ID__c).OLI_No__c : '?';
- tpi.Sold_Service__c = component.Existing_Object_ID__c;
- tpi.Quote_Item__c = component.Id;
- tpi.Quantity__c = 1;
- tpi.Port__c = tps.Port__c;
- tpi.Initial_MRE__c = tps.MRE__c;
- //tpi.Initial_OTE_COGS__c = tps.OTE_COGS__c;
- //tpi.Initial_OTE_Capex__c = tps.OTE_Capex__c;
- tpi.Previous_MRE__c = tps.MRE__c;
- tpi.Unit_Initial_MRE_COGS__c = tps.MRE__c;
- tpi.Unit_Initial_OTE_COGS__c = 0;
- tpi.Unit_Initial_OTE_CAPEX__c = 0;
- /* clear values that might have been mapped */
- tpi.Unit_Final_MRE_COGS__c = null;
- tpi.Unit_Final_OTE_COGS__c = null;
- tpi.Unit_Final_OTE_CAPEX__c = null;
- tpi.Final_MRE__c = null;
- tpi.Final_OTE_COGS__c = null;
- tpi.Final_OTE_Capex__c = null;
- tpi.Initial_OTE_COGS__c = 0;
- tpi.Initial_OTE_Capex__c = 0;
- tpi.Status__c = TPI_STATUS_QUOTE_REQ;
- if(component.Item_Action__c == ACTION_RENEWAL || component.Item_Action__c == ACTION_CEASE) {
- tpi.TPI_Order_Type__c = 'Renew';
- } else {
- tpi.TPI_Order_Type__c = 'Upgrade / Downgrade';
- }
- if(String.isEmpty(tpi.Item_End_Type__c)) {
- tpi.Item_End_Type__c = 'A';
- if(component.B_Port__c != null && tpi.Port__c != null && component.B_Port__c == tpi.Port__c) {
- tpi.Item_End_Type__c = 'B';
- }
- }
- String h3 = EncodingUtil.ConvertTohex(Crypto.GenerateAESKey(128));
- String tpiUpsertKey = h3.substring(0,8)+ '-' + h3.substring(8,12) + '-' + h3.substring(12,16) + '-' + h3.substring(16,20) + '-' + h3.substring(20);
- tpi.Upsert_Key__c = tpiUpsertKey;
- if(tps.Parent__c != null && String.isNotEmpty(tps.Parent__r.x3ParSerUID__c)) {
- System.debug('### TPS has a parent!');
- tpsToParent.put(tps.Id, tps.Parent__c);
- tpiToParentUpdate.add(tpi);
- }
- tpsIdToTPIExternalId.put(tps.Id, tpi.Upsert_Key__c);
- thirdPartyItemsToInsert.add(tpi);
- }
- }
- for(X3rd_Party_Item__c tpi : tpiToParentUpdate) {
- if(tpsToParent.keySet().contains(tpi.Existing_X3rd_Party_Service__c)) {
- if(tpsIdToTPIExternalId.get(tpsToParent.get(tpi.Existing_X3rd_Party_Service__c)) != null) {
- X3rd_Party_Item__c tpiParent = new X3rd_Party_Item__c(Upsert_Key__c = tpi.Upsert_Key__c);
- tpiParent.Parent__r = new X3rd_Party_Item__c(Upsert_Key__c = tpsIdToTPIExternalId.get(tpsToParent.get(tpi.Existing_X3rd_Party_Service__c)));
- thirdPartyItemsToUpsertParent.add(tpiParent);
- System.debug('### Adding tpi to upsert list');
- }
- }
- }
- }
- }
- }
- }
- }
- Integer j = 0;
- Set<Id> skippedQuoteItems = new Set<Id>();
- while (j < quoteItemsToUpdate.size()) {
- if(quoteItemsToUpdate.get(j).Configuration_Loaded__c == true) {
- skippedQuoteItems.add(quoteItemsToUpdate.get(j).Id);
- quoteItemsToUpdate.remove(j);
- } else {
- j++;
- }
- }
- updateQIfromQIAttr(attributesToInsert, quoteItemsToUpdate);
- update quoteItemsToUpdate;
- CPQ_Utils04_RequestStateCls.setOn('QLI_SET_CONFIGURATION_DESCRIPTION');
- // 03/03/2017 - MASIM - QC-1944 - ensure that duplicate attributes are not created
- List<CPQ_QuoteItemAttribute__c> attributesToUpsert = new List<CPQ_QuoteItemAttribute__c>();
- for (CPQ_QuoteItemAttribute__c atr : attributesToInsert) {
- if (!skippedQuoteItems.contains(atr.Quote_Item__c)) {
- atr.TECH_Unique_Key__c = atr.Quote_Item__c + '_' + atr.Attribute__c;
- attributesToUpsert.add(atr);
- }
- }
- upsert attributesToUpsert TECH_Unique_Key__c;
- /* disabling trigger setting Upsert_Key__c */
- CPQ_Utils04_RequestStateCls.setOn('SET_DEFAULTS_TPI');
- insert thirdPartyItemsToInsert;
- upsert thirdPartyItemsToUpsertParent Upsert_Key__c;
- /* A/B End handling - swop relationships */
- }
- //---Przemyslaw Tustanowski--
- //this part of code updates particullar quote items' fields from QIattribute, this is needed before configuration (i.e. to get full description - with banwitdth)
- private static void updateQIfromQIAttr(List<CPQ_QuoteItemAttribute__c> attributesToInsert, List<CPQ_QuoteItem__c> quoteItemsToUpdate){
- Map<String, CPQ_QuoteItem__c> mappedFieldsToUpdate = new Map<String,CPQ_QuoteItem__c>(); // QuoteItem id mapped to new QuoteItem - this is to keep appropierate fields types
- Map<String, Set<String>> listedFields4QI = new Map<String, Set<String>>(); // Mapped quoteItem Id to list of fields, which will be synced from attribute
- for(CPQ_QuoteItemAttribute__c attr : attributesToInsert){
- if(attr.Component_Field_to_Update__c != null || attr.Product_Field_to_Update__c != null){
- if(mappedFieldsToUpdate.isEmpty() || !mappedFieldsToUpdate.containsKey(attr.Quote_Item__c)){
- mappedFieldsToUpdate.put(attr.Quote_Item__c, new CPQ_QuoteItem__c());
- listedFields4QI.put(attr.Quote_Item__c, new Set<String>());
- }
- if(attr.Component_Field_to_Update__c != null){
- listedFields4QI.get(attr.Quote_Item__c).add(attr.Component_Field_to_Update__c);
- mappedFieldsToUpdate = findMapElements(mappedFieldsToUpdate, attr.Component_Field_to_Update__c, attr);
- }
- if(attr.Product_Field_to_Update__c != null){
- listedFields4QI.get(attr.Quote_Item__c).add(attr.Product_Field_to_Update__c);
- mappedFieldsToUpdate = findMapElements(mappedFieldsToUpdate, attr.Product_Field_to_Update__c, attr);
- }
- }
- }
- if(!mappedFieldsToUpdate.isEmpty() && !quoteItemsToUpdate.isEmpty()){
- for(CPQ_QuoteItem__c quotItem : quoteItemsToUpdate){
- if(mappedFieldsToUpdate.containsKey(quotItem.Id)){
- for(String filed2upd : listedFields4QI.get(quotItem.Id)){
- quotItem.put(filed2upd, mappedFieldsToUpdate.get(quotItem.Id).get(filed2upd));
- }
- }
- }
- }
- }
- private static Map<String, CPQ_QuoteItem__c> findMapElements(Map<String, CPQ_QuoteItem__c> mapStructure, String field2search, CPQ_QuoteItemAttribute__c attr){
- Map<String, CPQ_QuoteItem__c> mappedFieldsToUpdate = mapStructure;
- if(attr.Type__c == 'Radio' || attr.Type__c == 'Picklist'){
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Attribute__r.Name);
- } else if(attr.Type__c == 'Text' || attr.Type__c == 'Textarea' || attr.Type__c == 'Contract'){
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Text_Short__c);
- } else if(attr.Type__c == 'Boolean'){
- String bool2str = attr.Value_Boolean__c ? 'true' : 'false';
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, bool2str);
- } else if(attr.Type__c == 'Date'){
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Date__c);
- } else if(attr.Type__c == 'DateTime'){
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Datetime__c);
- } else if(attr.Type__c == 'Currency'){
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Currency__c);
- } else if(attr.Type__c == 'Percent'){
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Percent__c);
- } else if(attr.Type__c == 'Number'){
- try{
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Number__c); //some numeric value on QuoteItem Attribute are also numeric on quoteItem, and some are as text
- } catch (Exception e){
- try{
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, String.valueOf(attr.Value_Number__c));
- } catch (Exception ex){
- System.debug('Error: ' + ex + ' Ignore this field');
- }
- }
- } else if(attr.Type__c == 'Service Reference'){
- String serv = attr.Value_Sold_Service__c != null ? attr.Value_Sold_Service__r.Name : attr.Value_Quote_Item__r.Name;
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, serv);
- } else if(attr.Type__c == 'Charge' || attr.Type__c == 'Lookup'){
- mappedFieldsToUpdate.get(attr.Quote_Item__c).put(field2search, attr.Value_Text_Long_Trim__c);
- }
- return mappedFieldsToUpdate;
- }
- public static CPQ_QuoteItemAttribute__c createAttribute(Id parentId, CPQ_Attribute__c attributeDefinition, String value, Id parentDefinitionId, Boolean isComponent) {
- CPQ_QuoteItemAttribute__c attribute = new CPQ_QuoteItemAttribute__c();
- attribute.Quote_Item__c = parentId;
- attribute.Attribute__c = attributeDefinition.Id;
- attribute.Type__c = attributeDefinition.Type__c;
- if(!isComponent) {
- attribute.Component_Field_to_Update__c = attributeDefinition.Component_Field_to_Update__c;
- } else {
- attribute.Product_Field_to_Update__c = attributeDefinition.Product_Field_to_Update__c;
- }
- String name4descr;
- if(attribute.Type__c == CPQ_ATR01_Manager.ATR_TYPE_PICKLIST || attribute.Type__c == CPQ_ATR01_Manager.ATR_TYPE_RADIO){
- String oldValue = value;
- value = '';
- for(CPQ_AttributeValue__c av : attributeDefinition.AttributeValues__r){
- if(oldValue.equalsIgnoreCase(av.Name) && av.Global__c){
- //value = av.TECH_Value_ID__c;
- value = av.Id;
- name4descr = av.Name;
- break;
- } else if(oldValue.equalsIgnoreCase(av.Name) && av.Exclusive_for_Product__c == parentDefinitionId && !isComponent) {
- //value = av.TECH_Value_ID__c;
- value = av.Id;
- name4descr = av.Name;
- break;
- } else if(oldValue.equalsIgnoreCase(av.Name) && av.Exclusive_for_Component__c == parentDefinitionId && isComponent) {
- //value = av.TECH_Value_ID__c;
- value = av.Id;
- name4descr = av.Name;
- break;
- }
- }
- }
- CPQ_PRI01_Manager.setAttributeValueFromString(attribute, value);
- if(attribute.Value_Attribute__r != null && name4descr != ''){
- attribute.Value_Attribute__r.Name = name4descr;
- }
- return attribute;
- }
- public static String CalculateGridValueForPorts(String gridLayout, List<CPQ_QuoteItem__c> components, List<Sold_Service__c> soldServices) {
- if(components != null) {
- System.debug('### layout = "' + gridLayout + '"; components = "' + components.size() + '"');
- } else if(soldServices != null) {
- System.debug('### layout = "' + gridLayout + '"; components = "' + soldServices.size() + '"');
- }
- Map<String, Map<String, String>> columns = new Map<String, Map<String, String>>();
- Map<String, Map<String, Integer>> currentPorts = new Map<String, Map<String, Integer>>();
- String result = '';
- Set<Id> portsAlreadyAdded = new Set<Id>();
- /* checking values from components */
- if(components != null) {
- for(CPQ_QuoteItem__c component : components) {
- String portType = '';
- String continent = '';
- if(component.A_Port__c != null && !portsAlreadyAdded.contains(component.A_Port__c)) {
- portType = component.A_Port__r.Port_Type__c;
- continent = component.A_Port__r.Homing_Gateway__c == null ? '' : component.A_Port__r.Homing_Gateway__r.Continent__c;
- portsAlreadyAdded.add(component.A_Port__c);
- }
- if(String.isNotEmpty(portType) && String.isNotEmpty(continent)) {
- Map<String, Integer> portTypes = currentPorts.get(continent.toLowerCase());
- if(portTypes == null) {
- portTypes = new Map<String, Integer>();
- currentPorts.put(continent.toLowerCase(), portTypes);
- }
- Integer portCounter = portTypes.get(portType.toLowerCase());
- if(portCounter == null) portCounter = 0;
- portCounter = portCounter + 1;
- portTypes.put(portType.toLowerCase(), portCounter);
- }
- System.debug('### Port A type = "' + portType + '"; continent = "' + continent + '"');
- portType = '';
- continent = '';
- if(component.B_Port__c != null && !portsAlreadyAdded.contains(component.B_Port__c)) {
- portType = component.B_Port__r.Port_Type__c;
- continent = component.B_Port__r.Homing_Gateway__c == null ? '' : component.B_Port__r.Homing_Gateway__r.Continent__c;
- portsAlreadyAdded.add(component.B_Port__c);
- }
- if(String.isNotEmpty(portType) && String.isNotEmpty(continent)) {
- Map<String, Integer> portTypes = currentPorts.get(continent.toLowerCase());
- if(portTypes == null) {
- portTypes = new Map<String, Integer>();
- currentPorts.put(continent.toLowerCase(), portTypes);
- }
- Integer portCounter = portTypes.get(portType.toLowerCase());
- if(portCounter == null) portCounter = 0;
- portCounter = portCounter + 1;
- portTypes.put(portType.toLowerCase(), portCounter);
- }
- System.debug('### Port B type = "' + portType + '"; continent = "' + continent + '"');
- }
- } else if(soldServices != null) {
- for(Sold_Service__c soldService : soldServices) {
- String portType = '';
- String continent = '';
- if(soldService.A_Port__c != null && !portsAlreadyAdded.contains(soldService.A_Port__c)) {
- portType = soldService.A_Port__r.Port_Type__c;
- continent = soldService.A_Port__r.Homing_Gateway__c == null ? '' : soldService.A_Port__r.Homing_Gateway__r.Continent__c;
- portsAlreadyAdded.add(soldService.A_Port__c);
- }
- if(String.isNotEmpty(portType) && String.isNotEmpty(continent)) {
- Map<String, Integer> portTypes = currentPorts.get(continent.toLowerCase());
- if(portTypes == null) {
- portTypes = new Map<String, Integer>();
- currentPorts.put(continent.toLowerCase(), portTypes);
- }
- Integer portCounter = portTypes.get(portType.toLowerCase());
- if(portCounter == null) portCounter = 0;
- portCounter = portCounter + 1;
- portTypes.put(portType.toLowerCase(), portCounter);
- }
- System.debug('### Port A type = "' + portType + '"; continent = "' + continent + '"');
- portType = '';
- continent = '';
- if(soldService.B_Port__c != null && !portsAlreadyAdded.contains(soldService.B_Port__c)) {
- portType = soldService.B_Port__r.Port_Type__c;
- continent = soldService.B_Port__r.Homing_Gateway__c == null ? '' : soldService.B_Port__r.Homing_Gateway__r.Continent__c;
- portsAlreadyAdded.add(soldService.B_Port__c);
- }
- if(String.isNotEmpty(portType) && String.isNotEmpty(continent)) {
- Map<String, Integer> portTypes = currentPorts.get(continent.toLowerCase());
- if(portTypes == null) {
- portTypes = new Map<String, Integer>();
- currentPorts.put(continent.toLowerCase(), portTypes);
- }
- Integer portCounter = portTypes.get(portType.toLowerCase());
- if(portCounter == null) portCounter = 0;
- portCounter = portCounter + 1;
- portTypes.put(portType.toLowerCase(), portCounter);
- }
- System.debug('### Port B type = "' + portType + '"; continent = "' + continent + '"');
- }
- }
- System.debug('### currentPorts: ' + currentPorts);
- /* setting attribute value based on layout */
- if(String.isNotEmpty(gridLayout)) {
- Map<String, Object> gridMap = (Map<String, Object>)JSON.deserializeUntyped(gridLayout);
- List<Object> cols = (List<Object>)gridMap.get('cols');
- List<Object> rows = (List<Object>)gridMap.get('rows');
- System.debug('### cols: ' + cols);
- System.debug('### rows: ' + rows);
- result += '{';
- for(Object col : cols) {
- String colString = String.valueOf(col).replace('"','');
- result += '"' + colString + '":{';
- for(Object row : rows) {
- String rowString = String.valueOf(row).replace('"','');
- Integer count = 0;
- if(currentPorts.get(colString.toLowerCase()) != null && currentPorts.get(colString.toLowerCase()).get(rowString.toLowerCase()) != null){
- count = currentPorts.get(colString.toLowerCase()).get(rowString.toLowerCase());
- }
- if(count > 0) {
- result += '"' + rowString + '":"' + String.valueOf(count) + '",';
- } else {
- result += '"' + rowString + '":"",';
- }
- }
- result = result.removeEnd(',');
- result += '},';
- }
- result = result.removeEnd(',');
- result += '}';
- }
- System.debug('### Calculated IP Grid: ' + result);
- return result;
- }
- private static List<SoldService_Contact_Junction__c> initContacts(List<Sold_Service__c> soldServices, Id opportunityId) {
- System.debug('@@ START CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: initContacts');
- List<SoldService_Contact_Junction__c> conToInsert = new List<SoldService_Contact_Junction__c>();
- Map<Id, List<Sold_Service__c>> oliToSoldServices = new Map<Id, List<Sold_Service__c>>();
- for(Sold_Service__c ss : soldServices){
- if(String.isNotBlank(ss.OLI_Link_ID__c)) {
- List<Sold_Service__c> services = oliToSoldServices.get(ss.OLI_Link_ID__c);
- if(services == null){
- services = new List<Sold_Service__c>();
- oliToSoldServices.put(ss.OLI_Link_ID__c, services);
- }
- services.add(ss);
- }
- }
- // Target field on Contact > Destination field on Sold Service Contact
- Map<String,String> fieldMap = SoldServiceUtility.buildFieldMapping(ApplicationConstant.FIELD_MAPPING_CONTACT);
- String queryString = 'select ' + CPQ_Utils01_SObjectHelperCls.getFieldListForSOQL('Contact_Junction__c', null, null) + ' from Contact_Junction__c where Opportunity__c = :opportunityId Order by Role__c asc nulls last';
- List<Contact_Junction__c> contacts = Database.query(queryString);
- if(contacts == null || contacts.isEmpty()) {
- System.debug('@@ END CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: initContacts (null)');
- return null;
- }
- for (Contact_Junction__c curr : contacts) {
- if (String.isNotBlank(curr.Opportunity_Line_Item_ID__c) && oliToSoldServices.containsKey(curr.Opportunity_Line_Item_ID__c)) {
- if(curr.Contact__c != null && String.isNotBlank(curr.Role__c) && !contactIdByRole.containsKey(curr.Role__c.toLowerCase())){
- contactIdByRole.put(curr.Role__c.toLowerCase(), curr.Contact__c);
- }
- List<Sold_Service__c> lstSoldService = oliToSoldServices.get(curr.Opportunity_Line_Item_ID__c);
- for(Integer i = 0, j = lstSoldService.size(); i < j; i++){
- SoldService_Contact_Junction__c scj = new SoldService_Contact_Junction__c();
- scj.Sold_Service__c = lstSoldService[i].Id;
- scj.Name = 'Test Contact Junction';
- // Map fields using a custom setting 'SoldServiceMapping__c'
- for (String f : fieldMap.keySet())
- scj.put(fieldMap.get(f), curr.get(f));
- conToInsert.add(scj);
- }
- }
- }
- System.debug('@@ END CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: initContacts');
- return conToInsert;
- }
- private static void createChangeRequestAndLineItems(List<Sold_Service__c> lstSoldServices, Id opportunityId){
- System.debug('@@ START CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: createChangeRequestAndLineItems');
- Map<ID,Map<String, Change_Request__c>> changeRequestByIpBillingGroupSoldServiceId = new Map<ID,Map<String, Change_Request__c>>();
- Map<Id, Sold_Service__c> mapOfSoldServices = new Map<Id, Sold_Service__c>([SELECT A_Port__r.LAG_LACP__c, CRM_Parent_Sold_Service__r.Type_of_Service_Description__c, CRM_Parent_Sold_Service__c FROM Sold_Service__c WHERE Id IN :lstSoldServices]);
- Id ownerId = null;
- for(Opportunity opp : [select Id, OwnerId from Opportunity where Id = :opportunityId limit 1]){
- ownerId = opp.OwnerId;
- }
- for(Sold_Service__c ss :lstSoldServices){
- if(ApplicationConstant.ITEMNAME_IP_SERVICE.equalsIgnoreCase(ss.Item_Name__c)
- && ApplicationConstant.OLI_ITEM_ACTION_ADD.equalsIgnoreCase(ss.Item_Action__c)
- && mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__c != null
- && mapOfSoldServices.get(ss.Id).A_Port__r.LAG_LACP__c != 'Existing'
- && mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__r.Type_of_Service_Description__c != ApplicationConstant.ITEMNAME_IP_PEERING_GROUP
- ){
- if(!contactIdByRole.containsKey(ApplicationConstant.OPERATIONAL_CONTACT_ROLE_IP_CONFIGURATION.toLowerCase())) {
- System.debug('### ' + ApplicationConstant.OPERATIONAL_CONTACT_ROLE_IP_CONFIGURATION + ' contact role was not found');
- throw new TeliaException(ApplicationConstant.OPERATIONAL_CONTACT_ROLE_IP_CONFIGURATION + ' contact does not exist.');
- }
- if(!contactIdByRole.containsKey(ApplicationConstant.OPERATIONAL_CONTACT_ROLE_DELIVERY_CONTACT.toLowerCase())){
- System.debug('### ' + ApplicationConstant.OPERATIONAL_CONTACT_ROLE_DELIVERY_CONTACT + ' contact role was not found');
- throw new TeliaException(ApplicationConstant.OPERATIONAL_CONTACT_ROLE_DELIVERY_CONTACT + ' does not exist.');
- }
- Change_Request__c req = new Change_Request__c();
- req.Sold_Service__c = mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__c;
- req.Status__c = ApplicationConstant.CHANGE_REQUEST_STATUS_OPEN;
- req.Type__c = ApplicationConstant.CHANGE_REQUEST_TYPE_NEW;
- req.Contact_Requested_By__c = contactIdByRole.get(ApplicationConstant.OPERATIONAL_CONTACT_ROLE_IP_CONFIGURATION.toLowerCase());
- req.Contact_Sent_To__c = contactIdByRole.get(ApplicationConstant.OPERATIONAL_CONTACT_ROLE_IP_CONFIGURATION.toLowerCase());
- req.Additional_Contact_Recipient__c = contactIdByRole.get(ApplicationConstant.OPERATIONAL_CONTACT_ROLE_DELIVERY_CONTACT.toLowerCase());
- req.Send_Email__c = true;
- req.Sold_Service_Type__c = ss.Type_of_Service__c;
- req.OwnerId = ownerId;
- if (!changeRequestByIpBillingGroupSoldServiceId.containsKey(mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__c) ) {
- changeRequestByIpBillingGroupSoldServiceId.put(mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__c, new Map<String, Change_Request__c>());
- }
- changeRequestByIpBillingGroupSoldServiceId.get(mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__c).put(ss.Type_of_Service__c, req);
- }
- }
- if(!changeRequestByIpBillingGroupSoldServiceId.isEmpty()){
- System.debug('### Inserting Change Requests');
- List<Change_Request__c> listOfChangeRequests = new List<Change_Request__c>();
- for (Map<String, Change_Request__c> changeRequests : changeRequestByIpBillingGroupSoldServiceId.values()) {
- listOfChangeRequests.addAll(changeRequests.values());
- }
- upsert listOfChangeRequests;
- } else {
- System.debug('@@ END CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: createChangeRequestAndLineItems (null)');
- return;
- }
- List<Change_Request_Line_Item__c> lineItemToCreate = new List<Change_Request_Line_Item__c>();
- for(Sold_Service__c ss :lstSoldServices){
- if(ApplicationConstant.ITEMNAME_IP_SERVICE.equalsIgnoreCase(ss.Item_Name__c)
- && changeRequestByIpBillingGroupSoldServiceId.containsKey(mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__c)
- && ApplicationConstant.OLI_ITEM_ACTION_ADD.equalsIgnoreCase(ss.Item_Action__c)
- ){
- Map<String,Change_Request__c> listOfRequestsForSoldService = changeRequestByIpBillingGroupSoldServiceId.get(mapOfSoldServices.get(ss.Id).CRM_Parent_Sold_Service__c);
- for (String key : listOfRequestsForSoldService.keySet()) {
- if(key == ss.Type_of_Service__c && mapOfSoldServices.get(ss.Id).A_Port__r.LAG_LACP__c != 'Existing'){
- Change_Request_Line_Item__c requestLineItem = new Change_Request_Line_Item__c();
- requestLineItem.Sold_Service__c = ss.Id;
- requestLineItem.Change_Request__c = listOfRequestsForSoldService.get(key).Id;
- requestLineItem.Status__c = ApplicationConstant.CHANGE_REQUEST_STATUS_OPEN;
- requestLineItem.Type__c = ApplicationConstant.CHANGE_REQUEST_LINE_ITEM_TYPE_IP_CONFIGURATION_DETAILS;
- lineItemToCreate.add(requestLineItem);
- }
- }
- }
- }
- if(!lineItemToCreate.isEmpty()) insert lineItemToCreate;
- System.debug('@@ END CLASS: CPQ_PRD04_SoldServiceMappingHlpr METHOD: createChangeRequestAndLineItems');
- }
- public static void cloneObject(sObject sourceObject, sObject targetObject, String objectType) {
- Map<String, Schema.DescribeFieldResult> objectFields = CPQ_Utils01_SObjectHelperCls.getFieldDescriptions(objectType);
- set<String> excludedFields = new set<String>(new String[]{'CreatedDate', 'LastModifiedDate', 'LastViewedDate', 'SystemModstamp', 'LastReferencedDate', 'LastModifiedById', 'OwnerId', 'CreatedById'});
- for(String fieldName : objectFields.keySet()) {
- if(objectFields.get(fieldName).getType() != Schema.DisplayType.Reference && objectFields.get(fieldName).isCreateable() && !excludedFields.contains(fieldName)){
- if(objectFields.get(fieldName).getType() == Schema.DisplayType.Boolean) {
- if(sourceObject.get(fieldName) != null) {
- targetObject.put(fieldName, sourceObject.get(fieldName));
- } else {
- targetObject.put(fieldName, false);
- }
- } else {
- targetObject.put(fieldName, sourceObject.get(fieldName));
- }
- }
- }
- }
- public static String key(Integer i, Id id) {
- return String.valueOf(i)+String.valueOf(Id);
- }
- /************************************************************************
- Description : Data format returned from creating sold services. This is needed for execution of method from batch,
- so the information about created sold services is not lost.
- ************************************************************************/
- public class createSoldServicesResult {
- public Map<String, Id> quoteItemToSoldService = new Map<String, Id>();
- public Map<String, String> upsertKeyToQuoteItem = new Map<String, String>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement