示例#1
0
        public static SponsorshipTDS GetChildDetails(Int64 APartnerKey,
                                                     Int32 ALedgerNumber,
                                                     bool AWithPhoto,
                                                     out string ASponsorshipStatus)
        {
            SponsorshipTDS MainDS = new SponsorshipTDS();

            TDBTransaction Transaction = new TDBTransaction();

            DBAccess.ReadTransaction(ref Transaction,
                                     delegate
            {
                PPartnerAccess.LoadByPrimaryKey(MainDS, APartnerKey, Transaction);
                PFamilyAccess.LoadByPrimaryKey(MainDS, APartnerKey, Transaction);
                PPartnerTypeAccess.LoadViaPPartner(MainDS, APartnerKey, Transaction);
                PPartnerCommentAccess.LoadViaPPartner(MainDS, APartnerKey, Transaction);
                PPartnerReminderAccess.LoadViaPPartner(MainDS, APartnerKey, Transaction);

                if (!AWithPhoto && (MainDS.PFamily.Rows.Count == 1))
                {
                    MainDS.PFamily[0].Photo = "";
                }

                int SponsorshipBatchNumber = GetRecurringGiftBatchForSponsorship(ALedgerNumber, Transaction);

                if (SponsorshipBatchNumber > -1)
                {
                    ARecurringGiftBatchAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, SponsorshipBatchNumber, Transaction);
                    ARecurringGiftAccess.LoadViaARecurringGiftBatch(MainDS, ALedgerNumber, SponsorshipBatchNumber, Transaction);
                    ARecurringGiftDetailAccess.LoadViaARecurringGiftBatch(MainDS, ALedgerNumber, SponsorshipBatchNumber, Transaction);

                    GiftBatchTDS GiftDS = new GiftBatchTDS();
                    TGiftTransactionWebConnector.LoadGiftDonorRelatedData(GiftDS, true, ALedgerNumber, SponsorshipBatchNumber, Transaction);

                    for (int i = 0; i < MainDS.ARecurringGiftDetail.Count;)
                    {
                        SponsorshipTDSARecurringGiftDetailRow gdr = MainDS.ARecurringGiftDetail[i];
                        // drop all recurring gift details, that are not related to this child (RecipientKey)
                        if (gdr.RecipientKey != APartnerKey)
                        {
                            MainDS.ARecurringGiftDetail.Rows.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                            // set the donor key from the appropriate recurring gift
                            MainDS.ARecurringGift.DefaultView.RowFilter = String.Format("{0} = {1}",
                                                                                        ARecurringGiftTable.GetGiftTransactionNumberDBName(),
                                                                                        gdr.GiftTransactionNumber);

                            // there should be only one row
                            foreach (DataRowView drv in MainDS.ARecurringGift.DefaultView)
                            {
                                ARecurringGiftRow recurrGiftRow = (ARecurringGiftRow)drv.Row;
                                gdr.DonorKey         = recurrGiftRow.DonorKey;
                                PPartnerRow donorRow = (PPartnerRow)GiftDS.DonorPartners.Rows.Find(recurrGiftRow.DonorKey);
                                gdr.DonorName        = donorRow.PartnerShortName;
                                gdr.CurrencyCode     = MainDS.ARecurringGiftBatch[0].CurrencyCode;
                            }
                        }
                    }

                    // drop all unrelated gift rows, that don't have a detail for this child
                    for (int i = 0; i < MainDS.ARecurringGift.Count;)
                    {
                        ARecurringGiftRow gr = MainDS.ARecurringGift[0];
                        MainDS.ARecurringGiftDetail.DefaultView.RowFilter = String.Format("{0} = {1}",
                                                                                          ARecurringGiftDetailTable.GetGiftTransactionNumberDBName(),
                                                                                          gr.GiftTransactionNumber);

                        if (MainDS.ARecurringGiftDetail.DefaultView.Count == 0)
                        {
                            MainDS.ARecurringGift.Rows.RemoveAt(i);
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
            });

            bool isSponsoredChild = false;

            ASponsorshipStatus = "[N/A]";

            foreach (PPartnerTypeRow type in MainDS.PPartnerType.Rows)
            {
                if (type.TypeCode == "CHILDREN_HOME" || type.TypeCode == "HOME_BASED" || type.TypeCode == "BOARDING_SCHOOL" || type.TypeCode == "PREVIOUS_CHILD" || type.TypeCode == "CHILD_DIED")
                {
                    isSponsoredChild = true;
                }
                ASponsorshipStatus = type.TypeCode;
            }

            if (!isSponsoredChild)
            {
                return(new SponsorshipTDS());
            }

            return(MainDS);
        }
示例#2
0
        /// <summary>
        /// Gets called in regular intervals from a Timer in Class TTimedProcessing.
        /// </summary>
        /// <param name="ADBAccessObj">Already instatiated DB Access object with opened DB connection.</param>
        /// <param name="ARunManually">this is true if the process was called manually from the server admin console</param>
        public static void Process(TDataBase ADBAccessObj, bool ARunManually)
        {
            TDBTransaction        ReadWriteTransaction;
            bool                  NewTransaction;
            bool                  LastReminderDateAcquired;
            DateTime              LastReminderDate;
            DataSet               ReminderResultsDS;
            SSystemDefaultsRow    SystemDefaultsDR;
            PPartnerReminderTable PartnerReminderDT;
            int       ReminderFreqency;
            TDataBase DBAccessObj;

            if (TLogging.DebugLevel >= 6)
            {
                TLogging.Log("Entering TProcessPartnerReminders.Process...");
            }

            // TODO: it is quite ipossible at the moment to use ADBAccessObj instead of DBAccess.GDBAccessObj due to SubmitChanges etc
            //DBAccessObj = ADBAccessObj;
            DBAccessObj = DBAccess.GDBAccessObj;

            // SubmitChanges references a user
            TPetraIdentity PetraIdentity = new TPetraIdentity(
                "SYSADMIN", "", "", "", "", DateTime.MinValue,
                DateTime.MinValue, DateTime.MinValue, 0, -1, -1, false,
                false);

            UserInfo.GUserInfo = new TPetraPrincipal(PetraIdentity, null);

            ReadWriteTransaction = DBAccessObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted,
                                                                           out NewTransaction);

            /*
             * This whole process must either succeed or fail, therefore the whole thing is in a try-catch.
             */
            try
            {
                /*
                 * Obtain date when PartnerReminders last ran. This is stored in a SystemDefault. If it doesn't exist already,
                 * a new SystemDefault with an ancient date is created for us.
                 */
                LastReminderDateAcquired = GetLastReminderDate(out LastReminderDate, out SystemDefaultsDR, ReadWriteTransaction);

                if (!LastReminderDateAcquired)
                {
                    TLogging.Log(
                        TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                        ": Could not send Partner Reminders because Petra couldn't create the required SystemDefault setting for the Last Reminder Date!");

                    DBAccessObj.RollbackTransaction();

                    return;
                }

                // Retrieve all PartnerReminders we need to process.
                ReminderResultsDS = GetRemindersToProcess(LastReminderDate, out PartnerReminderDT,
                                                          DBAccessObj, ReadWriteTransaction);

                /*
                 * We now have a Typed DataTable with the PartnerReminders that we need to process.
                 * Iterate through the PartnerReminders, update data, and send an email for each PartnerReminder.
                 */
                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                    TLogging.Log("PartnerReminders data before we start processing all PartnerReminders....");
                    TLogging.Log(ReminderResultsDS.GetXml().ToString());
                }

                foreach (PPartnerReminderRow PartnerReminderDR in PartnerReminderDT.Rows)
                {
                    if (TLogging.DebugLevel >= 4)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Processing Reminder ID {0} for Partner {1}.", PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                    }

                    ReminderFreqency = (PartnerReminderDR.IsReminderFrequencyNull()) ? 0 : PartnerReminderDR.ReminderFrequency;

                    PartnerReminderDR.BeginEdit();
                    PartnerReminderDR.LastReminderSent = DateTime.Now.Date;
                    PartnerReminderDR.NextReminderDate = DateTime.Now.Date.AddDays(ReminderFreqency);

                    if (!PartnerReminderDR.IsEventDateNull())   // Reminder has an Event Date
                    {
                        if (PartnerReminderDR.NextReminderDate > PartnerReminderDR.EventDate)
                        {
                            if (TLogging.DebugLevel >= 5)
                            {
                                TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                           ": Deactivating Reminder ID {0} for Partner {1} as its Event Date is in the past.",
                                                           PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                            }

                            PartnerReminderDR.ReminderActive = false;
                        }
                    }

                    if (TLogging.DebugLevel >= 4)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Sending email for Reminder ID {0} for Partner {1}.", PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                    }

                    if (SendReminderEmail(PartnerReminderDR, ReadWriteTransaction))
                    {
                        // Accept the edit
                        if (TLogging.DebugLevel >= 4)
                        {
                            TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                       ": Reminder ID {0} for Partner {1} accepted by SMTP server.", PartnerReminderDR.ReminderId,
                                                       PartnerReminderDR.PartnerKey));
                        }

                        PartnerReminderDR.EndEdit();
                    }
                    else
                    {
                        // Cancel the edit
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Reminder ID {0} for Partner {1} REJECTED by SMTP server.", PartnerReminderDR.ReminderId,
                                                   PartnerReminderDR.PartnerKey));

                        PartnerReminderDR.CancelEdit();
                    }
                }

                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                    TLogging.Log("PartnerReminders data after processing all PartnerReminders, before writing it to DB....");
                    TLogging.Log(PartnerReminderDT.DataSet.GetXml().ToString());
                }

                // Update all the changed PartnerReminder Rows
                PPartnerReminderAccess.SubmitChanges(PartnerReminderDT, ReadWriteTransaction);

                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                }

                /*
                 * Update the SystemDefault that keeps track of when Partner Reminders last ran.
                 * (SystemDefaultsDR will point to the row we loaded earlier on, OR the row we added earlier on
                 * if there wasn't already a SystemDefault row.)
                 */
                UpdateLastReminderDate(SystemDefaultsDR, ReadWriteTransaction);

                if (NewTransaction)
                {
                    DBAccess.GDBAccessObj.CommitTransaction();
                }

                TLogging.LogAtLevel(1, TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing + " ran succesfully.");
            }
            catch (Exception Exc)
            {
                TLogging.Log(
                    TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing + " encountered an Exception:" + Environment.NewLine +
                    Exc.ToString());

                if (NewTransaction)
                {
                    DBAccess.GDBAccessObj.RollbackTransaction();
                }

                throw;
            }
        }
        /// <summary>
        /// Gets called in regular intervals from a Timer in Class TTimedProcessing.
        /// </summary>
        /// <param name="ADataBaseObj">Already instatiated DB Access object with opened DB connection.</param>
        /// <param name="ARunManually">this is true if the process was called manually from the server admin console</param>
        public static void Process(TDataBase ADataBaseObj, bool ARunManually)
        {
            TDBTransaction        ReadWriteTransaction;
            bool                  NewTransaction;
            bool                  LastReminderDateAcquired;
            DateTime              LastReminderDate;
            DataSet               ReminderResultsDS;
            SSystemDefaultsRow    SystemDefaultsDR;
            PPartnerReminderTable PartnerReminderDT;
            int         ReminderFreqency;
            TSmtpSender Sender = null;

            if (TLogging.DebugLevel >= 6)
            {
                TLogging.Log("Entering TProcessPartnerReminders.Process...");
            }

            ReadWriteTransaction = ADataBaseObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted,
                                                                            out NewTransaction);

            /*
             * This whole process must either succeed or fail, therefore the whole thing is in a try-catch.
             */
            try
            {
                /*
                 * Obtain date when PartnerReminders last ran. This is stored in a SystemDefault. If it doesn't exist already,
                 * a new SystemDefault with an ancient date is created for us.
                 */
                LastReminderDateAcquired = GetLastReminderDate(out LastReminderDate, out SystemDefaultsDR, ReadWriteTransaction);

                if (!LastReminderDateAcquired)
                {
                    TLogging.Log(
                        TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                        ": Could not send Partner Reminders because Petra couldn't create the required SystemDefault setting for the Last Reminder Date!");

                    ADataBaseObj.RollbackTransaction();

                    return;
                }

                try
                {
                    Sender = new TSmtpSender();
                }
                catch (ESmtpSenderInitializeException e)
                {
                    TLogging.Log(
                        TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                        ": " + e.Message);

                    if (e.InnerException != null)
                    {
                        TLogging.Log(e.InnerException.ToString(), TLoggingType.ToLogfile);
                    }

                    return;
                }

                // Retrieve all PartnerReminders we need to process.
                ReminderResultsDS = GetRemindersToProcess(LastReminderDate, out PartnerReminderDT,
                                                          ADataBaseObj, ReadWriteTransaction);

                /*
                 * We now have a Typed DataTable with the PartnerReminders that we need to process.
                 * Iterate through the PartnerReminders, update data, and send an email for each PartnerReminder.
                 */
                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                    TLogging.Log("PartnerReminders data before we start processing all PartnerReminders....");
                    TLogging.Log(ReminderResultsDS.GetXml().ToString());
                }

                foreach (PPartnerReminderRow PartnerReminderDR in PartnerReminderDT.Rows)
                {
                    if (TLogging.DebugLevel >= 4)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Processing Reminder ID {0} for Partner {1}.", PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                    }

                    ReminderFreqency = (PartnerReminderDR.IsReminderFrequencyNull()) ? 0 : PartnerReminderDR.ReminderFrequency;

                    PartnerReminderDR.BeginEdit();
                    PartnerReminderDR.LastReminderSent = DateTime.Now.Date;
                    PartnerReminderDR.NextReminderDate = DateTime.Now.Date.AddDays(ReminderFreqency);

                    if (!PartnerReminderDR.IsEventDateNull())   // Reminder has an Event Date
                    {
                        if (PartnerReminderDR.NextReminderDate > PartnerReminderDR.EventDate)
                        {
                            if (TLogging.DebugLevel >= 5)
                            {
                                TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                           ": Deactivating Reminder ID {0} for Partner {1} as its Event Date is in the past.",
                                                           PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                            }

                            PartnerReminderDR.ReminderActive = false;
                        }
                    }

                    if (TLogging.DebugLevel >= 4)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Sending email for Reminder ID {0} for Partner {1}.", PartnerReminderDR.ReminderId, PartnerReminderDR.PartnerKey));
                    }

                    Boolean emailSentOk = false;
                    try
                    {
                        emailSentOk = SendReminderEmail(PartnerReminderDR, ReadWriteTransaction, Sender);
                    }
                    catch (ESmtpSenderInitializeException e) // if an exception was thrown, assume the email didn't go.
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Reminder ID {0} for Partner {1}: {2}", PartnerReminderDR.ReminderId,
                                                   PartnerReminderDR.PartnerKey,
                                                   e.Message));

                        if (e.InnerException != null)
                        {
                            TLogging.Log(e.InnerException.Message);
                        }
                    }
                    catch (ESmtpSenderSendException e)
                    {
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Reminder ID {0} for Partner {1}: {2}", PartnerReminderDR.ReminderId,
                                                   PartnerReminderDR.PartnerKey,
                                                   e.Message));

                        if (e.InnerException != null)
                        {
                            TLogging.Log(e.InnerException.Message);
                        }
                    }
                    catch (Exception e)
                    {
                        TLogging.Log(e.Message);
                    }

                    if (emailSentOk)
                    {
                        // Accept the edit
                        if (TLogging.DebugLevel >= 4)
                        {
                            TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                       ": Reminder ID {0} for Partner {1} accepted by SMTP server.", PartnerReminderDR.ReminderId,
                                                       PartnerReminderDR.PartnerKey));
                        }

                        PartnerReminderDR.EndEdit();
                    }
                    else
                    {
                        // Cancel the edit
                        TLogging.Log(String.Format(TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing +
                                                   ": Reminder ID {0} for Partner {1} REJECTED by SMTP server.", PartnerReminderDR.ReminderId,
                                                   PartnerReminderDR.PartnerKey));

                        PartnerReminderDR.CancelEdit();
                    }
                }

                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                    TLogging.Log("PartnerReminders data after processing all PartnerReminders, before writing it to DB....");
                    TLogging.Log(PartnerReminderDT.DataSet.GetXml().ToString());
                }

                // Update all the changed PartnerReminder Rows
                PPartnerReminderAccess.SubmitChanges(PartnerReminderDT, ReadWriteTransaction);

                if (TLogging.DebugLevel >= 6)
                {
                    TLogging.Log("_---------------------------------_");
                }

                /*
                 * Update the SystemDefault that keeps track of when Partner Reminders last ran.
                 * (SystemDefaultsDR will point to the row we loaded earlier on, OR the row we added earlier on
                 * if there wasn't already a SystemDefault row.)
                 */
                UpdateLastReminderDate(SystemDefaultsDR, ReadWriteTransaction);

                if (NewTransaction)
                {
                    ADataBaseObj.CommitTransaction();
                }

                TLogging.LogAtLevel(1, TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing + " ran succesfully.");
            }
            catch (Exception Exc)
            {
                TLogging.Log(
                    TTimedProcessing.StrAutomaticProcessing + StrRemindersProcessing + " encountered an Exception:" + Environment.NewLine +
                    Exc.ToString());

                if (NewTransaction)
                {
                    ADataBaseObj.RollbackTransaction();
                }

                throw;
            }
            finally
            {
                if (Sender != null)
                {
                    Sender.Dispose();
                }
            }
        }