• Andrei Hogas
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

public class OpportunityHelper {
	

public static void CreateURL_SendComingSoonEmail(){
        List<Id> oppsToQuery = new List<Id>();
        for (Tabella_consultazioni__c tc : (List<Tabella_consultazioni__c>)trigger.new){
          //invio email solo se tipologia è uguale a Sessione
			  if(tc.Tipologia__c=='Sessione' ) {
                if (tc.Opportunita__c != NULL && 
                ((Tabella_consultazioni__c)trigger.oldMap.get(tc.Id)).Opportunita__c != tc.Opportunita__c){
                    oppsToQuery.add(tc.Opportunita__c);
                }
            }
        }
        if (oppsToQuery.isEmpty()){
            return;
        }
        List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
        List<DatiVersamento__c> dvToInsert = new List<DatiVersamento__c>();
        List<Opportunity> oppsToUpdate = new List<Opportunity>();
        Map<Id, DatiVersamento__c> insertedDVs = new Map<Id, DatiVersamento__c>();
        EmailTemplate emailTemp = [SELECT htmlValue FROM EmailTemplate WHERE name = 'Filippo_ComingSoonTemplate'][0];
        Map<Id, Opportunity> oppQuery = new Map<Id, Opportunity>(
            						[SELECT Id, amount, URL_Dati_Anticipo__c,
                                     Account.FirstName, Account.LastName, Account.PersonEmail
                                     FROM Opportunity WHERE Id IN :oppsToQuery]);
        for (Opportunity opp : oppQuery.values()){
            dvToInsert.add(new DatiVersamento__c(Opportunity__c = opp.Id,
                                                Nome_Paziente__c = opp.Account.FirstName,
                                                Cognome_Paziente__c = opp.Account.LastName));
        }
        insert dvToInsert;
        Site_domain__c sd = Site_domain__c.getInstance();
        String url = sd.URL__c+'DatiVersamento';
        for (DatiVersamento__c dv : dvToInsert){
            insertedDVs.put(dv.Opportunity__c, dv);
        }
        for (Opportunity opp : oppQuery.values()){
            opp.URL_Dati_Anticipo__c = '<a href="'+url+'?Id='+insertedDVs.get(opp.Id).Id+'">'+url+'?Id='+insertedDVs.get(opp.Id).Id+'</a>';
            oppsToUpdate.add(opp);
        }
        update oppsToUpdate;
        for (Tabella_consultazioni__c tc : (List<Tabella_consultazioni__c>)trigger.new){
            if (tc.Opportunita__c != NULL && 
            ((Tabella_consultazioni__c)trigger.oldMap.get(tc.Id)).Opportunita__c != tc.Opportunita__c){
                Opportunity opp = oppQuery.get(tc.Opportunita__c);
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                String body = emailtemp.htmlValue.replace('{!Opportunity.Account}', 
                                                          opp.Account.FirstName+' '+opp.Account.LastName);
                String subject = 'Acconto Sessione bSBS '+opp.Account.FirstName+' '+opp.Account.LastName;
                if (opp.URL_Dati_Anticipo__c == NULL || opp.Account.PersonEmail == NULL){
                    tc.addError('Errore: mancano i campi Email, Url o Amount nella opportunità scelta.');
                }
                body = body.replace('<![CDATA[','');
                body = body.replace(']]>','');
                body = body.replace('{!date}', tc.Data__c.day()+'/'+tc.Data__c.month()+'/'+tc.Data__c.year());
                body = body.replace('{!time}',tc.Ora_inizio_appoggio__c+'.'+tc.Minuto_inizio_appoggio__c);
                body = body.replace('{!IBAN}',IBAN);
                body = body.replace('{!SWIFT}',SWIFT);
                body = body.replace('{!BENEFICIARY}',BENEFICIARY);
                body = body.replace('{!BANK}',BANK);
                body = body.replace('{!subject}',subject);
                body = body.replace('{!amount}','600,00');
                body = body.replace('{!expireDate}',system.Date.today().addDays(2).day()+'/'+system.Date.today().addDays(2).month()+'/'+system.Date.today().addDays(2).year());
                body = body.replace('{!URLdatidacompilare}',opp.URL_Dati_Anticipo__c);
                email.setToAddresses(new List<String>{opp.Account.PersonEmail});
                email.setSubject(subject);
                email.setHtmlBody(body);
                if (opp.Account.PersonEmail == NULL) {
                    continue;
                } else {
                	emailsToSend.add(email);
                }
            }
            
        }
        Messaging.SendEmailResult[] results = Messaging.sendEmail(emailsToSend, TRUE);
        return;
    }
    
    
    public static void PopulateSMSField(){
        List<Id> oppIDsToQuery = new List<Id>();
		for (Tabella_Consultazioni__c tcs : (List<Tabella_Consultazioni__c>)trigger.new){
            	oppIDsToQuery.add(tcs.Opportunita__c);
        }
        Map<Id, Opportunity> oppsToUpdate = new Map<Id, Opportunity>([SELECT Id, OwnerId
                                                FROM Opportunity
                                                WHERE Id IN :oppIDsToQuery]);
        Map<Id, User> users = new Map<Id, User>([SELECT Id, FirstName, LastName
                                                FROM User]);
        Configurazione_SMS__c smsPC = Configurazione_SMS__c.getValues('SMS Prenotazione Consultazione');
        Configurazione_SMS__c smsPS = Configurazione_SMS__c.getValues('SMS Prenotazione Sessione');
        for (Tabella_Consultazioni__c tcs : (List<Tabella_Consultazioni__c>)trigger.new){
            if (tcs.Tipologia__c == 'Consultazione' && tcs.opportunita__c != NULL){
                String str=smsPC.Test_SMS__c;
                str = str.Replace('{!DATA}',FormatDate(System.date.today().day())+'/'+FormatDate(System.date.today().month())+'/'+System.date.today().year());
                str = str.Replace('{!ORA}',FormatDate(system.now().hour())+':'+FormatDate(system.now().minute()));
                str = str.Replace('{!COMMNAME}',users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).FirstName + ' ' + users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).LastName);
            	oppsToUpdate.get(tcs.Opportunita__c).SMS_Prenotazione_Consultazione__c = str;
            } else if (tcs.Tipologia__c == 'Sessione' && tcs.opportunita__c != NULL){
                String str=smsPS.Test_SMS__c+' '+smsPS.Test_SMS_2__c;
                str = str.Replace('{!DATA}',FormatDate(System.date.today().day())+'/'+FormatDate(System.date.today().month())+'/'+System.date.today().year());
                str = str.Replace('{!ORA}',FormatDate(system.now().hour())+':'+FormatDate(system.now().minute()));
                str = str.Replace('{!COMMNAME}',users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).FirstName + ' ' + users.get(oppsToUpdate.get(tcs.Opportunita__c).OwnerId).LastName);
            	oppsToUpdate.get(tcs.Opportunita__c).SMS_Prenotazione_Sessione__c = str;
            }
        }
        update oppsToUpdate.Values();
    }
    
    public static String FormatDate(integer inte){
        if (inte<10){
            return '0'+inte;
        } else{
            return String.ValueOf(inte);
        }
    }
    
    
    public static void PopulateSMSFieldFromDate(){
        Configurazione_SMS__c cSMS = Configurazione_SMS__c.getValues('SMS Conferma Sessione');
        for (Opportunity opp :(List<Opportunity>)trigger.new){
            if( (trigger.isInsert || ( opp.data_pagamento__c != NULL && ((Opportunity)trigger.oldMap.get(opp.Id)).data_pagamento__c == NULL) ) ){
                   opp.SMS_Conferma_Sessione__c = cSMS.Test_SMS__c;
               }
        }
    }
    
}