global class InsertTransaction {
WebService static string InsertRecord(string id) {
String OppId=id;
string ErrorMesg;
Map<ID,Member__c> mapmembers = new Map<ID,Member__c>();
List<ID> MemberIDs =new List<ID>();
List<ID> ProgramEnrIds =new List<ID>();
List<Transaction__c> listTrans=new List<Transaction__c>();
List<Member__c> lopp1=[select id ,Active__c,Program_Enrolled__c,Enrolled_Date__c,
Loyalty_Enrolled__c,Points__c from Member__c where id =:OppId limit 1];
integer counIn=[select count() from Transaction__c where Member__c =:OppId ];
set<string> setPromId = new set<string>();
List<Member__c> Members =new List<Member__c>();
if(counIn > 0) {
ErrorMesg ='You can not create a Transaction because this member having the Transaction ';
}
else {
for(Member__c m:lopp1){
Transaction__c tObj=new Transaction__c(Transaction_Date__c=system.today());
tObj.Member__c=m.id;
tObj.ProgramEnrolled__c=m.Program_Enrolled__c;
tObj.Sub_Type__c='Enrollment';
tObj.Status__c ='Processed';
tObj.Loyalty_Product__c='a0590000008at2y';
listTrans.add(tObj);
}
for(Transaction__c tObj1:listTrans){
ProgramEnrIds.add(tObj1.ProgramEnrolled__c);
MemberIDs.add(tObj1.Member__c);
}
list<Promotion__c > lisProm=[select id,Active__c,Program_Enrolled__c,Apply_To__c from Promotion__c where Program_Enrolled__c In : ProgramEnrIds];
System.debug('lisProm---->'+lisProm.size());
for(Promotion__c obj:lisProm){
setPromId.add(obj.id);
}
list<Rule_Action__c>ListObj=[Select PointsAssigned__c, id,Number_of_Points__c,Promotion__c,Promotion__r.Apply_To__c,
Object__c from Rule_Action__c where Promotion__c in:setPromId];
System.debug('ListObj---->'+ListObj.size());
Members =[Select Points__c FROM Member__c Where ID in:MemberIDs];
System.debug('Members ---->+Members )');
for(Member__c m:Members){
mapmembers.put(m.id,m);
System.debug('mapmembers---->'+mapmembers.size() );
}
for(Transaction__c tn:listTrans){
Member__c Member = mapmembers.get(tn.Member__c);
System.debug('Member ---->'+Member);
for(Rule_Action__c raObj:ListObj){
if(raObj.Promotion__r.Apply_To__c == 'Enrollment'){
Member.Points__c=raObj.Number_of_Points__c;
System.debug('Member Points---->'+Member.Points__c );
System.debug('raObj Points---->'+raObj.Number_of_Points__c);
}
mapmembers.put(Member.id,Member);
}
}
if(listTrans.size()>0){
Insert listTrans;
System.debug('mapmembers---->'+mapmembers.size());
}
update mapmembers.values();
ErrorMesg='Transction created succesfully';
}
return ErrorMesg;
}
}
WebService static string InsertRecord(string id) {
String OppId=id;
string ErrorMesg;
Map<ID,Member__c> mapmembers = new Map<ID,Member__c>();
List<ID> MemberIDs =new List<ID>();
List<ID> ProgramEnrIds =new List<ID>();
List<Transaction__c> listTrans=new List<Transaction__c>();
List<Member__c> lopp1=[select id ,Active__c,Program_Enrolled__c,Enrolled_Date__c,
Loyalty_Enrolled__c,Points__c from Member__c where id =:OppId limit 1];
integer counIn=[select count() from Transaction__c where Member__c =:OppId ];
set<string> setPromId = new set<string>();
List<Member__c> Members =new List<Member__c>();
if(counIn > 0) {
ErrorMesg ='You can not create a Transaction because this member having the Transaction ';
}
else {
for(Member__c m:lopp1){
Transaction__c tObj=new Transaction__c(Transaction_Date__c=system.today());
tObj.Member__c=m.id;
tObj.ProgramEnrolled__c=m.Program_Enrolled__c;
tObj.Sub_Type__c='Enrollment';
tObj.Status__c ='Processed';
tObj.Loyalty_Product__c='a0590000008at2y';
listTrans.add(tObj);
}
for(Transaction__c tObj1:listTrans){
ProgramEnrIds.add(tObj1.ProgramEnrolled__c);
MemberIDs.add(tObj1.Member__c);
}
list<Promotion__c > lisProm=[select id,Active__c,Program_Enrolled__c,Apply_To__c from Promotion__c where Program_Enrolled__c In : ProgramEnrIds];
System.debug('lisProm---->'+lisProm.size());
for(Promotion__c obj:lisProm){
setPromId.add(obj.id);
}
list<Rule_Action__c>ListObj=[Select PointsAssigned__c, id,Number_of_Points__c,Promotion__c,Promotion__r.Apply_To__c,
Object__c from Rule_Action__c where Promotion__c in:setPromId];
System.debug('ListObj---->'+ListObj.size());
Members =[Select Points__c FROM Member__c Where ID in:MemberIDs];
System.debug('Members ---->+Members )');
for(Member__c m:Members){
mapmembers.put(m.id,m);
System.debug('mapmembers---->'+mapmembers.size() );
}
for(Transaction__c tn:listTrans){
Member__c Member = mapmembers.get(tn.Member__c);
System.debug('Member ---->'+Member);
for(Rule_Action__c raObj:ListObj){
if(raObj.Promotion__r.Apply_To__c == 'Enrollment'){
Member.Points__c=raObj.Number_of_Points__c;
System.debug('Member Points---->'+Member.Points__c );
System.debug('raObj Points---->'+raObj.Number_of_Points__c);
}
mapmembers.put(Member.id,Member);
}
}
if(listTrans.size()>0){
Insert listTrans;
System.debug('mapmembers---->'+mapmembers.size());
}
update mapmembers.values();
ErrorMesg='Transction created succesfully';
}
return ErrorMesg;
}
}
No comments:
Post a Comment