예제 #1
0
        private static void Send(AppointmentEntry graphAppointmentEntry, PXCache sourceCache, Guid setupID, int?branchID, IDictionary <string, string> reportParams, IList <Guid?> attachments = null)
        {
            string emailToAccounts, emailBCCAccounts;

            emailToAccounts  = string.Empty;
            emailBCCAccounts = string.Empty;

            FSAppointment fsAppointmentRow = graphAppointmentEntry.AppointmentRecords.Current;

            Guid?refNoteId    = fsAppointmentRow.NoteID;
            Guid?parentNoteId = null;

            string srvOrdType = fsAppointmentRow.SrvOrdType;

            NotificationSource source = GetSource(graphAppointmentEntry, srvOrdType, setupID, branchID);

            if (source == null)
            {
                //The current Notification Type (MailingID) is not configured for this Service Order Type
                return;
            }

            var accountId = source.EMailAccountID ?? DefaultEMailAccountId;

            if (accountId == null)
            {
                sourceCache.RaiseExceptionHandling <FSAppointment.srvOrdType>(
                    sourceCache.Current,
                    ((FSAppointment)sourceCache.Current).SrvOrdType,
                    new PXSetPropertyException(
                        TX.Warning.DEFAULT_EMAIL_NOT_CONFIGURED,
                        PXErrorLevel.Warning));
            }

            RecipientList recipients = GetRecipients(graphAppointmentEntry, source.SourceID, accountId);

            if (recipients == null || recipients.Count() == 0)
            {
                return;
            }

            GetsRecipientsFields(recipients, ref emailToAccounts, ref emailBCCAccounts);

            var sent = false;

            if (source.ReportID != null)
            {
                var sender = new ReportNotificationGenerator(source.ReportID)
                {
                    MailAccountId       = accountId,
                    Format              = source.Format,
                    AdditionalRecipents = recipients,
                    Parameters          = reportParams,
                    NotificationID      = source.NotificationID
                };

                sent |= sender.Send().Any();
            }
            else if (source.NotificationID != null)
            {
                var sender = TemplateNotificationGenerator.Create(fsAppointmentRow, (int)source.NotificationID);
                if (source.EMailAccountID != null)
                {
                    sender.MailAccountId = accountId;
                }

                string notificationBody = sender.Body;
                FSAppointment.ReplaceWildCards(graphAppointmentEntry, ref notificationBody, fsAppointmentRow);

                sender.Body         = notificationBody;
                sender.BodyFormat   = source.Format;
                sender.RefNoteID    = refNoteId;
                sender.ParentNoteID = parentNoteId;
                sender.To           = emailToAccounts;
                sender.Bcc          = emailBCCAccounts;

                if (attachments != null)
                {
                    foreach (var attachment in attachments)
                    {
                        if (attachment != null)
                        {
                            sender.AddAttachmentLink(attachment.Value);
                        }
                    }
                }

                sent |= sender.Send().Any();
            }

            if (!sent)
            {
                throw new PXException(PX.Objects.CR.Messages.EmailNotificationError);
            }
        }
예제 #2
0
        public static RecipientList GetRecipients(AppointmentEntry graphAppointmentEntry, int?sourceID, int?sourceEmailID)
        {
            RecipientList recipients   = new RecipientList();
            bool          allEmailsBCC = true;

            PXResultset <NotificationRecipient> notificationRecipientSet = PXSelect <NotificationRecipient,
                                                                                     Where <
                                                                                         NotificationRecipient.sourceID, Equal <Required <NotificationRecipient.sourceID> >,
                                                                                         And <NotificationRecipient.active, Equal <True> > >,
                                                                                     OrderBy <Asc <NotificationRecipient.notificationID> > >
                                                                           .Select(graphAppointmentEntry, sourceID);

            //This loop can't be included in the following one because if all fields are BCC, the origin email account has to be placed in the first position of the array
            //so Acumatica can use it as the "To:" email account
            foreach (NotificationRecipient notificationRecipientRow in notificationRecipientSet)
            {
                if (notificationRecipientRow.Hidden == false)
                {
                    allEmailsBCC = false;
                    break;
                }
            }

            if (allEmailsBCC)
            {
                AddEmailSource(graphAppointmentEntry, sourceEmailID, recipients);
            }

            var staffMemberRowsGrouped = graphAppointmentEntry.AppointmentEmployees.Select().GroupBy(
                p => ((FSAppointmentEmployee)p).EmployeeID,
                (key, group) => new
            {
                Group = (FSAppointmentEmployee)group.First()
            })

                                         .Select(g => g.Group).ToList();

            foreach (NotificationRecipient notificationRecipientRow in notificationRecipientSet)
            {
                switch (notificationRecipientRow.ContactType)
                {
                case FSNotificationContactType.Customer:
                    if (graphAppointmentEntry.ServiceOrderRelated.Current.CustomerID != null)
                    {
                        AddCustomerRecipient(graphAppointmentEntry, notificationRecipientRow, recipients);
                    }

                    break;

                case FSNotificationContactType.EmployeeStaff:
                    foreach (FSAppointmentEmployee fsAppointmentEmployeeRow in staffMemberRowsGrouped)
                    {
                        AddEmployeeStaffRecipient(graphAppointmentEntry, fsAppointmentEmployeeRow.EmployeeID, fsAppointmentEmployeeRow.Type, notificationRecipientRow, recipients);
                    }

                    break;

                case FSNotificationContactType.VendorStaff:
                    foreach (FSAppointmentEmployee fsAppointmentEmployeeRow in staffMemberRowsGrouped)
                    {
                        AddVendorStaffRecipient(graphAppointmentEntry, fsAppointmentEmployeeRow, notificationRecipientRow, recipients);
                    }

                    break;

                case FSNotificationContactType.Employee:
                    AddEmployeeRecipient(graphAppointmentEntry, notificationRecipientRow, recipients);
                    break;

                case FSNotificationContactType.Billing:
                    AddBillingRecipient(graphAppointmentEntry, notificationRecipientRow, recipients);
                    break;

                case FSNotificationContactType.GeoZoneStaff:
                    AddGeoZoneStaffRecipient(graphAppointmentEntry, notificationRecipientRow, recipients);
                    break;

                case FSNotificationContactType.Salesperson:
                    AddSalespersonRecipient(graphAppointmentEntry, notificationRecipientRow, recipients);
                    break;

                default:
                    break;
                }
            }

            //The only element in the list is the From and the email shouldn't be sent
            if (recipients.Count() == 1 && allEmailsBCC == true)
            {
                recipients = null;
            }

            return(recipients);
        }