Created
December 19, 2016 21:38
-
-
Save johndstein/fda550e8334086ca6362234da2fff7af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class MembershipMigrator { | |
//public MembershipMigrator() { | |
//} | |
// 1968 total | |
public List<Account> listMemberAccounts() { | |
return [ | |
select Id, | |
Name, | |
BillingCountry, | |
Certified_Date__c, | |
Member_Migrated__c | |
from Account | |
where Certified_Date__c != null | |
and Member_Migrated__c = false | |
limit 100 | |
]; | |
} | |
// 1535 total | |
public Map<Id, List<Opportunity>> listMembershipOpps() { | |
Map<Id, List<Opportunity>> m = new Map<Id, List<Opportunity>>(); | |
List<Opportunity> opps = [ | |
select Id, | |
AccountId, | |
Name, | |
Amount, | |
CloseDate | |
from Opportunity | |
where Type = 'Fee for Service' | |
and isWon = true | |
and Transaction_Type__c = 'Event Registration' | |
]; | |
for (Opportunity o : opps) { | |
List<Opportunity> oo = m.get(o.AccountId); | |
if (oo == null) { | |
oo = new List<Opportunity>(); | |
m.put(o.AccountId, oo); | |
} | |
oo.add(o); | |
} | |
return m; | |
} | |
public void migrate() { | |
List<Account> accounts = listMemberAccounts(); | |
Map<Id, List<Opportunity>> oldOppMap = listMembershipOpps(); | |
List<Opportunity> newOpps = new List<Opportunity>(); | |
for (Account act : accounts) { | |
newOpps.add(createOpp(act, act.Certified_Date__c, 'New')); | |
newOpps.addAll(createMembershipRenewals(act)); | |
act.Member_Migrated__c = true; | |
} | |
insert newOpps; | |
List<OpportunityLineItem> olis = addProducts(newOpps, oldOppMap); | |
insert olis; | |
update accounts; | |
} | |
public List<OpportunityLineItem> addProducts( | |
List<Opportunity> newOpps, | |
Map<Id, List<Opportunity>> oldOppMap) { | |
String pbid = MembershipSettings__c.getOrgDefaults().Price_Book_Id__c; | |
MembershipPricebookService svc = new MembershipPricebookService(pbid); | |
List<OpportunityLineItem> olis = new List<OpportunityLineItem>(); | |
for (Opportunity o : newOpps) { | |
// If no fee for service opp for a given account just create a | |
// new membership with cost of 0. | |
// Renewals are always 0 for history. | |
Decimal amount = 0; | |
String newRenew = 'Membership Renewal'; | |
if (o.npe01__Membership_Origin__c == 'New') { | |
newRenew = 'New Membership'; | |
List<Opportunity> oldOpps = oldOppMap.get(o.AccountId); | |
if (oldOpps != null) { | |
Opportunity oldOpp = oldOpps.get(0); | |
amount = oldOpp.Amount; | |
} | |
} | |
PricebookEntry pbe = svc.getPricebookEntry(o.Account.BillingCountry, newRenew); | |
OpportunityLineItem li = new OpportunityLineItem(); | |
olis.add(li); | |
li.OpportunityId = o.Id; | |
li.PricebookEntryId = pbe.Id; | |
li.Quantity = 1; | |
li.TotalPrice = amount; | |
} | |
return olis; | |
} | |
// we will create a membership for each year starting at expiration of original | |
// as long as start date is in the past. | |
public List<Opportunity> createMembershipRenewals(Account account) { | |
List<Opportunity> opps = new List<Opportunity>(); | |
Date start = account.Certified_Date__c.addMonths(12); | |
while (start < System.today()) { | |
Opportunity o = createOpp(account, start, 'Renewal'); | |
opps.add(o); | |
start = start.addMonths(12); | |
} | |
return opps; | |
} | |
public Opportunity createOpp(Account account, Date start, String newOrRenewal) { | |
Opportunity opp = new Opportunity(); | |
opp.AccountId = account.Id; | |
String oppName = 'New Membership'; | |
if (newOrRenewal == 'Renewal') { | |
oppName = 'Membership Renewal'; | |
} | |
opp.Name = (oppName + ' ' + account.Name + ' ' + start.format()).abbreviate(120); | |
opp.StageName = 'Payment Received'; | |
opp.Paid__c = true; | |
opp.RecordTypeId = '012d0000000xH9HAAU'; | |
opp.CloseDate = start; | |
opp.npe01__Membership_Origin__c = newOrRenewal; | |
opp.npe01__Membership_Start_Date__c = start; | |
opp.npe01__Membership_End_Date__c = start.addMonths(12) - 1; | |
return opp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment