project QadFinancials > class MfgNotification > method ProcessMfgNotification

Description

Handles processing of e-mail notifications sent to users associated with appropriate roles.


Parameters


iiComponentCompanyIDinputintegerCompany identifier for component being created.
icComponentTypeinputcharacterName of created component type as either "EMPLOYEE", "SUPPLIER" or "CUSTOMER".
icBusinessKeysinputcharacterA char(2) delimited list of business keys values for the created component.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCreditor.AdditionalUpdates
method BDebtor.AdditionalUpdates
method BDebtorEndUser.AdditionalUpdates
method BEmployee.AdditionalUpdates


program code (program1/mfgnotification.p)

assign oiReturnStatus = -98.

/* ============================================================================== */
/* A noticiation is only sent in case the data was replicated to he mfgpro tables */
/* ============================================================================== */  
if vlOperationalReplicationEnabled = ?
then do :
     assign vlOperationalReplicationEnabled =  <M-24 IsReplicationEnabled  () in MfgNotification>.
end.

if vlOperationalReplicationEnabled <> true
then do :
    /* We can return - we don't have to do anything */
    assign oiReturnStatus = 0.
    return.
end.

/* ============================================================================== */
/* In case we find tNotification already from the prev call, we can 'reuse' this  */
/* ============================================================================== */                       
if can-find (first tNotifications where 
                   tNotifications.tcComponentType      = icComponentType and
                   tNotifications.tiComponentCompanyID = iiComponentCompanyID)
then do :
     for each tNotifications where
              tNotifications.tcComponentType      = icComponentType and
              tNotifications.tiComponentCompanyID = iiComponentCompanyID
         break by tNotifications.tcUsrLangCode :
         
         /* Get the Subject and Content again in the correct language */                                     
         if first-of (tNotifications.tcUsrLangCode)
         then do:
            /* Retrieve translated subject */    
            assign vcBusinessKeys  = icBusinessKeys.        
                        
            <M-25 run GetSubject
               (input  tNotifications.tcUsrLangCode (icLangCode), 
                output vcSubjectText (ocSubjectText), 
                output viFcReturnSuper (oiReturnStatus)) in MfgNotification>
                    
            if viFcReturnSuper < 0 
            then do:
                assign oiReturnStatus = viFcReturnSuper.
                return.
            end.
            
            /* Retrieve translated content */
            assign vcDomainList    = tNotifications.tcDomainList.
                           
            
            <M-26 run GetContent
               (input  tNotifications.tcUsrLangCode (icLangCode), 
                output vcContentText (ocContentText), 
                output viFcReturnSuper (oiReturnStatus)) in MfgNotification>    
            
            if viFcReturnSuper < 0
            then do:
                assign oiReturnStatus = viFcReturnSuper.
                return.
            end.         
         end. /* first-of */
         
         assign tNotifications.tcContent = vcContentText
                tNotifications.tcSubject = vcSubjectText.             
     end. /* for each tNotifications */
end.                                                                 
else do:
    /* Get the Role Name */	
    if icComponentType = {&NOTIFICATION-SUPPLIER} 
    then vcRoleName = {&NOTIFICATION-SUPPLIERNOTIFY}.
    else
    if icComponentType = {&NOTIFICATION-CUSTOMER}
    then vcRoleName = {&NOTIFICATION-CUSTOMERNOTIFY}.
    else
    if icComponentType = {&NOTIFICATION-ENDUSER} 
    then vcRoleName = {&NOTIFICATION-ENDUSERNOTIFY}.
    else assign vcRoleName = {&NOTIFICATION-EMPLOYEENOTIFY}.
    
    /* Check if any user is linked to one of these notification roles */              
            
    <Q-27 assign vlFcQueryRecordsAvailable = UsrRoleByRole (NoCache)
       (input ?, (RoleID)
        input vcRoleName, (RoleName)) in BRole >            

    if vlFcQueryRecordsAvailable <> false
    then do:  
    
        /* Get the Role ID */	 
              
        
        <Q-28 run RoleByNameId (all) (Read) (NoCache)
           (input ?, (RoleID)
            input vcRoleName, (RoleName)
            output dataset tqRoleByNameId) in BRole >
        
        find first tqRoleByNameId where tqRoleByNameId.tcRoleName =  vcRoleName no-error .
        
        if not available tqRoleByNameId
        then next.
        
        assign viComponentCompanyID = iiComponentCompanyID
               vcComponentType      = icComponentType
               vcBusinessKeys       = icBusinessKeys
               vcSharedSetType = 
                    if(vcComponentType = {&NOTIFICATION-SUPPLIER}) 
                    then {&NOTIFICATION-SHAREDSET-CREDITOR}
                    else 
                    if(vcComponentType = {&NOTIFICATION-CUSTOMER} or vcComponentType = {&NOTIFICATION-ENDUSER}) 
                    then {&NOTIFICATION-SHAREDSET-DEBTOR}
                    else ?.
                                
        vcDomainList = "".
        if(vcSharedSetType <> ?) 
        then do:                    
                        
            /* Get the SharedSet linked to the entity */
                       
            <Q-29 run CompanySSByCompanyIDCodeSSType (all) (Read) (NoCache)
               (input iiComponentCompanyID, (CompanyId)
                input vcSharedSetType, (SharedSetType)
                input ?, (CompanyCode)
                output dataset tqCompanySSByCompanyIDCodeSSType) in BCompany >    
        
            for each tqCompanySSByCompanyIDCodeSSType where
                tqCompanySSByCompanyIDCodeSSType.tiCompany_ID = iiComponentCompanyID and
                tqCompanySSByCompanyIDCodeSSType.tcSharedSetTypeCode = vcSharedSetType:
                
                /* Get all the domains which use the same sharedset */
                
                    
                <Q-30 run DomainByDomainSharedSet (all) (Read) (NoCache)
                   (input ?, (DomainID)
                    input tqCompanySSByCompanyIDCodeSSType.tiSharedSet_ID, (SharedSetID)
                    input ?, (SharedSetCode)
                    input ?, (SharedSetType)
                    output dataset tqDomainByDomainSharedSet) in BDomain >      
                    
                for each tqDomainByDomainSharedSet where 
                         tqDomainByDomainSharedSet.tiSharedSet_ID = tqCompanySSByCompanyIDCodeSSType.tiSharedSet_ID:
                         
                    assign vcDomainList = 
                                if(vcDomainList <> '') 
                                then vcDomainList + "," + tqDomainByDomainSharedSet.tcDomainCode
                                else tqDomainByDomainSharedSet.tcDomainCode.
                end.    /* for each tqDomainByDomainSharedSet */

            end. /* for each tqCompanySSByCompanyIDCodeSSType */
        
        end. /* if(vcSharedSetType <> ?) */    
        else do:                   
            assign vcDomainList = vcDomainCode.            
        end.
        
        do viCounter = num-entries(vcDomainList,",") to 1 by -1  :

            assign vcDomainFromList = entry(vicounter, vcDomainList, ",").

            /* Get the DomainCode */            
                            
            <Q-31 run DomainsByCode (all) (Read) (NoCache)
               (input ?, (DomainID)
                input trim(vcDomainFromList), (DomainCode)
                output dataset tqDomainsByCode) in BDomain >    
                
            find first tqDomainsByCode where 
                       tqDomainsByCode.tcDomainCode = trim(vcDomainFromList) 
                       no-error.
                                                  
            if available tqDomainsByCode 
            then do:

                /* Get all users linked to this role in this domain */
                
                <Q-32 run UserRoleDomainForMail (all) (Read) (NoCache)
                   (input tqDomainsByCode.tiDomain_ID, (DomainId)
                    input tqRoleByNameId.tiRole_ID, (RoleId)
                    output dataset tqUserRoleDomainForMail) in BUserRole >    
                    
                for each tqUserRoleDomainForMail 
                    break by tqUserRoleDomainForMail.tiUsr_ID:

                        if first-of (tqUserRoleDomainForMail.tiUsr_ID)      and
                           tqUserRoleDomainForMail.tcUsrMailAddress <> '':U and 
                           tqUserRoleDomainForMail.tcUsrMailAddress <> ? and
                           not can-find (first tNotifications where
                                               tNotifications.tcComponentType      = icComponentType        and
                                               tNotifications.tiComponentCompanyID = iiComponentCompanyID   and                          
                                               tNotifications.tcRecipient = tqUserRoleDomainForMail.tcUsrName)                             
                        then do:   
                    
                            /* Retrieve translated subject and content */
                                                            
                            <M-33 run GetSubject
                               (input  tqUserRoleDomainForMail.tcLngCode (icLangCode), 
                                output vcSubjectText (ocSubjectText), 
                                output viFcReturnSuper (oiReturnStatus)) in MfgNotification>
                                    
                            if(viFcReturnSuper < 0) then do:
                                oiReturnStatus = viFcReturnSuper.
                                return.
                            end.
                
                                                          
                                
                            <M-34 run GetContent
                               (input  tqUserRoleDomainForMail.tcLngCode (icLangCode), 
                                output vcContentText (ocContentText), 
                                output viFcReturnSuper (oiReturnStatus)) in MfgNotification>
                                               
                            if(viFcReturnSuper < 0) then do:
                                oiReturnStatus = viFcReturnSuper.
                                return.
                            end.
            
                            /* Queue up e-mail data */
                            create tNotifications.
                            assign tNotifications.tcEMailAddress    = tqUserRoleDomainForMail.tcUsrMailAddress
                               tNotifications.tcSubject             = vcSubjectText
                               tNotifications.tcContent             = vcContentText
                               tNotifications.tiUserID              = tqUserRoleDomainForMail.tiUsr_ID
                               tNotifications.tcComponentDomain     = vcComponentDomain
                               tNotifications.tcRecipient           = tqUserRoleDomainForMail.tcUsrName
                               tNotifications.tcComponentType       = icComponentType
                               tNotifications.tiComponentCompanyID  = iiComponentCompanyID
                               tNotifications.tcUsrLangCode         = tqUserRoleDomainForMail.tcLngCode
                               tNotifications.tcDomainList          = vcDomainList.
       
                        end. /* tcUsrMailAddress and tnotifications*/
                    
                end. /* for each  tqUserRoleDomainForMail */
        
            end. /* if available tqDomainsBycode*/                   
                
        end. /* do viCounter */

    end. /* vlFcQueryRecordsAvailable */

end. /* else do : */


/* ============================================================== */
/* Send the mails                                                 */
/* ============================================================== */
if can-find (first tNotifications where
                   tNotifications.tcComponentType      = icComponentType and
                   tNotifications.tiComponentCompanyID = iiComponentCompanyID)
then do :
    
    assign vcEmailAddressList = ''.
                  
    <I-35 {bFcStartAndOpenInstance
         &ADD-TO-TRANSACTION = "false"
         &CLASS              = "Mail"}>            

    /* Send queued e-mails */
    for each tNotifications where
             tNotifications.tcComponentType      = icComponentType and
             tNotifications.tiComponentCompanyID = iiComponentCompanyID
             break by tNotifications.tcContent
             :
                           
        assign vcEmailAddressList = IF vcEmailAddressList = ''
                                    THEN tNotifications.tcEMailAddress
                                    ELSE vcEmailAddressList + "|":U + tNotifications.tcEMailAddress.
                         
        if last-of (tNotifications.tcContent) or length (vcEmailAddressList,"CHARACTER") > 10000
        then do:                        
            <M-36 run Send
               (input  vcSmtpSender (icFrom), 
                input  vcEmailAddressList (icTo), 
                input  tNotifications.tcSubject (icSubject), 
                input  tNotifications.tcContent (icBody), 
                output viFcReturnSuper (oiReturnStatus)) in Mail>    
            
            if(viFcReturnSuper <> 0 and oiReturnStatus = -98) 
            then oiReturnStatus = viFcReturnSuper.
        
            if(viFcReturnSuper < 0) 
            then do:
                           
                <M-37 run SetMessage
                   (input  #T-24'Unable to send e-mail to $1.':100(999890586)T-24# (icMessage), 
                    input  tNotifications.tcRecipient (icArguments), 
                    input  '':U (icFieldName), 
                    input  tNotifications.tcRecipient (icFieldValue), 
                    input  'W':U (icType), 
                    input  3 (iiSeverity), 
                    input  '':U (icRowid), 
                    input  'QadFin-9146':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in MfgNotification>   
            end.
            
            assign vcEmailAddressList = ''.    
                            
         end. /* if last-of */

    end. /* for each */
             
    <I-38 {bFcCloseAndStopInstance
         &CLASS           = "Mail"}>     

    for each tFcMessages where
        tFcMessages.tcFcMsgNumber = 'FC-102':U or
        tFcMessages.tcFcMsgNumber = 'QadFin-8771':U:
        
        assign  tFcMessages.tcFcType = 'W':U
                tFcMessages.tiFcSeverity = 1.
    end.       
end.

if oiReturnStatus = -98 then assign oiReturnStatus = 0.
if oiReturnStatus < 0 then assign oiReturnStatus = 1.