global class TransactionProcessingUtility_1 {
WebService static void processTransaction (id transactionids) {
List<Transaction__c> TransactionsToUpdate =new List<Transaction__c>();
List<ID> MemberIDs =new List<ID>();
List<ID> ProductIDs =new List<ID>();
List<ID> ProgramEnrIds =new List<ID>();
List<Member__c> Members =new List<Member__c>();
List<LoyaltyProduct__c> Products =new List<LoyaltyProduct__c>();
Map<ID,Member__c> mapmembers = new Map<ID,Member__c>();
Map<ID,LoyaltyProduct__c> mapproducts = new Map<ID,LoyaltyProduct__c>();
Map<ID,Transaction__c> maptransactions = new Map<ID,Transaction__c>();
List<String> transfilter = new List<String>{'Processed','Cancelled','Failed'};
if(transactionids != null) {
List<Transaction__c> listofTransactions =[SELECT ID,
Member__c,
Processing_Comment__c,
Loyalty_Product__c,
Status__c,
Sub_Type__c,ProgramEnrolled__c,
Type__c,
Transaction_Date__c
FROM Transaction__c WHERE id =:transactionids AND
Status__c NOT IN :transfilter];
for(Transaction__c t:listofTransactions){
MemberIDs.add(t.Member__c);
ProductIDs.add(t.Loyalty_Product__c);
maptransactions.put(t.id, t);
ProgramEnrIds.add(t.ProgramEnrolled__c);
}
set<string> setPromId = new set<string>();
Map<string,Rule_Action__c > updateMap = new Map<string,Rule_Action__c >();
list<Promotion__c > lisProm=[select id,Active__c,Program_Enrolled__c,Apply_To__c from Promotion__c where Program_Enrolled__c In : ProgramEnrIds];
for(Promotion__c obj:lisProm){
setPromId.add(obj.id);
}
list<Rule_Action__c>ListObj=[Select id,Number_of_Points__c,Promotion__c,Promotion__r.Apply_To__c, Object__c from Rule_Action__c where Promotion__c in:setPromId];
Members =[Select Points__c FROM Member__c Where ID in:MemberIDs];
Products =[Select Product_points__c FROM LoyaltyProduct__c Where ID in:ProductIDs];
for(Member__c m:Members){
mapmembers.put(m.id,m);
}
for(LoyaltyProduct__c p:Products){
mapproducts.put(p.id,p);
}
for(Transaction__c t:listofTransactions){
Member__c Member = mapmembers.get(t.Member__c);
LoyaltyProduct__c product = mapproducts.get(t.Loyalty_Product__c);
for(Rule_Action__c raObj:ListObj)
{
if(t.Type__c =='Accrual' && t.Sub_Type__c =='Product' && raObj.Promotion__r.Apply_To__c == 'Accrual-Product' ){
Member.Points__c=Member.Points__c+raObj.Number_of_Points__c;
t.Status__c ='Processed';
t.Processing_Comment__c ='Points added to member';
}
else if( t.Type__c =='Redemption' && t.Sub_Type__c =='Product' && raObj.Promotion__r.Apply_To__c == 'Redemption-Product' ) {
if( raObj.Number_of_Points__c < Member.Points__c )
{
Member.Points__c=Member.Points__c - raObj.Number_of_Points__c;
t.Status__c ='Processed';
t.Processing_Comment__c ='Points redeemed from member';
}
else if(raObj.Number_of_Points__c > Member.Points__c ) {
t.Status__c ='Failed';
t.Processing_Comment__c ='Not enough points to redeem';
}
}
maptransactions.put(t.id,t);
}
}
update mapmembers.values();
update maptransactions.values();
}
}
}
=============================================================
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}
var result = sforce.apex.execute(
"TransactionProcessingUtility_1", //class name
"processTransaction", //method name
{ transactionids: '{!Transaction__c.Id}' // method parameter
});
window.alert("Are you sure want to proceed!!" );
window.location.reload(true);