Exemplo n.º 1
0
        /// <summary>
        /// Returns the PLocationRow of the 'Best Address'.
        /// </summary>
        /// <remarks>One of the 'DetermineBestAddress' Methods must have been run before on the PartnerLocation
        /// Table that gets passed in in the <paramref name="APartnerLocationDT" /> Argument!!!</remarks>
        /// <param name="APartnerLocationDT">Typed PartnerLocation Table that was already processed by one of the
        /// 'DetermineBestAddress' Methods.</param>
        /// <param name="ALocationDT">Location Table that contains all Location records that are referenced in
        /// <paramref name="APartnerLocationDT" />.</param>
        /// <returns>Location Row of the 'Best Address'.</returns>
        public static PLocationRow FindBestAddressLocation(PartnerEditTDSPPartnerLocationTable APartnerLocationDT,
                                                           PLocationTable ALocationDT)
        {
            PartnerEditTDSPPartnerLocationRow CheckDR;
            string       NameOfBestAddrColumn = PartnerEditTDSPPartnerLocationTable.GetBestAddressDBName();
            var          BestLocationPK       = new TLocationPK(-1, -1);
            PLocationRow BestLocationDR;

            for (int Counter = 0; Counter < APartnerLocationDT.Count; Counter++)
            {
                CheckDR = APartnerLocationDT[Counter];

                if (CheckDR[NameOfBestAddrColumn] == ((object)1))
                {
                    BestLocationPK = new TLocationPK(CheckDR.SiteKey, CheckDR.LocationKey);
                }
            }

            if ((BestLocationPK.SiteKey == -1) &&
                (BestLocationPK.LocationKey == -1))
            {
                throw new EOPAppException(
                          "FindBestAddressLocation Method was unable to determine the 'Best Address' (PPartnerLocation error)! (Was 'DetermineBestAddress' run before?)");
            }

            BestLocationDR = (PLocationRow)ALocationDT.Rows.Find(
                new object[] { BestLocationPK.SiteKey, BestLocationPK.LocationKey });

            if (BestLocationDR == null)
            {
                throw new EOPAppException(
                          "FindBestAddressLocation Method was unable to determine the 'Best Address' (PLocation error)! (Was 'DetermineBestAddress' run before?)");
            }

            return(BestLocationDR);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines which address is the 'Best Address' of a Partner, and marks it in the DataColumn 'BestAddress'.
        /// </summary>
        /// <remarks>There are convenient overloaded server-side Methods, Ict.Petra.Server.MPartner.ServerCalculations.DetermineBestAddress,
        /// which work by specifying the PartnerKey of a Partner in an Argument.</remarks>
        /// <param name="APartnerLocationsDT">DataTable containing the addresses of a Partner.</param>
        /// <returns>A <see cref="TLocationPK" /> which points to the 'Best Address'. If no 'Best Address' was found,
        /// SiteKey and LocationKey of this instance will be both -1.</returns>
        public static TLocationPK DetermineBestAddress(DataTable APartnerLocationsDT)
        {
            TLocationPK ReturnValue;

            DataRow[]    OrderedRows;
            System.Int32 CurrentRow;
            System.Int32 BestRow;
            System.Int16 FirstRowAddrOrder;
            bool         FirstRowMailingAddress;

            System.DateTime BestRowDate;
            System.DateTime TempDate;
            CurrentRow = 0;
            BestRow    = 0;
            bool Unchanged = false;

            TLogging.LogAtLevel(8, "Calculations.DetermineBestAddress: processing " + APartnerLocationsDT.Rows.Count.ToString() + " rows...");

            if (APartnerLocationsDT == null)
            {
                throw new ArgumentException("Argument APartnerLocationsDT must not be null");
            }

            if (!APartnerLocationsDT.Columns.Contains(PARTNERLOCATION_BESTADDR_COLUMN))
            {
                DeterminePartnerLocationsDateStatus(APartnerLocationsDT, DateTime.Today);
            }

            /*
             *  Add custom DataColumn if its not part of the DataTable yet
             */
            if (!APartnerLocationsDT.Columns.Contains(PARTNERLOCATION_BESTADDR_COLUMN))
            {
                APartnerLocationsDT.Columns.Add(new System.Data.DataColumn(PARTNERLOCATION_BESTADDR_COLUMN, typeof(Boolean)));
            }

            /*
             * Order tables' rows: first all records with p_send_mail_l = true, these are ordered
             * ascending by Icon, then all records with p_send_mail_l = false, these are ordered
             * ascending by Icon.
             */
            OrderedRows = APartnerLocationsDT.Select(APartnerLocationsDT.DefaultView.RowFilter,
                                                     PPartnerLocationTable.GetSendMailDBName() + " DESC, " + PartnerEditTDSPPartnerLocationTable.GetIconDBName() + " ASC",
                                                     DataViewRowState.CurrentRows);

            if (OrderedRows.Length > 1)
            {
                FirstRowAddrOrder      = Convert.ToInt16(OrderedRows[0][PartnerEditTDSPPartnerLocationTable.GetIconDBName()]);
                FirstRowMailingAddress = Convert.ToBoolean(OrderedRows[0][PPartnerLocationTable.GetSendMailDBName()]);

                // determine pBestRowDate
                if (FirstRowAddrOrder != 3)
                {
                    BestRowDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateEffectiveDBName()]);
                }
                else
                {
                    BestRowDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateGoodUntilDBName()]);
                }

                // iterate through the sorted rows
                for (CurrentRow = 0; CurrentRow <= OrderedRows.Length - 1; CurrentRow += 1)
                {
                    Unchanged = OrderedRows[CurrentRow].RowState == DataRowState.Unchanged;

                    // reset any row that might have been marked as 'best' before
                    OrderedRows[CurrentRow][PartnerEditTDSPPartnerLocationTable.GetBestAddressDBName()] = ((object)0);

                    // We do not want changing the BestAddress column to enable save. So revert row status to original.
                    if (Unchanged)
                    {
                        OrderedRows[CurrentRow].AcceptChanges();
                    }

                    // determine pTempDate
                    if (FirstRowAddrOrder != 3)
                    {
                        TempDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateEffectiveDBName()]);
                    }
                    else
                    {
                        TempDate = TSaveConvert.ObjectToDate(OrderedRows[CurrentRow][PPartnerLocationTable.GetDateGoodUntilDBName()]);
                    }

                    // still the same ADDR_ORDER than the ADDR_ORDER of the first row and
                    // still the same Mailing Address than the Mailing Address flag of the first row > proceed
                    if ((Convert.ToInt16(OrderedRows[CurrentRow][PartnerEditTDSPPartnerLocationTable.GetIconDBName()]) == FirstRowAddrOrder) &&
                        (Convert.ToBoolean(OrderedRows[CurrentRow][PPartnerLocationTable.GetSendMailDBName()]) == FirstRowMailingAddress))
                    {
                        switch (FirstRowAddrOrder)
                        {
                        case 1:
                        case 3:

                            // find the Row with the highest p_date_effective_d (or p_date_good_until_d) date
                            if (TempDate > BestRowDate)
                            {
                                BestRowDate = TempDate;
                                BestRow     = CurrentRow;
                            }

                            break;

                        case 2:

                            // find the Row with the lowest p_date_effective_d date
                            if (TempDate < BestRowDate)
                            {
                                BestRowDate = TempDate;
                                BestRow     = CurrentRow;
                            }

                            break;
                        }
                    }
                }

                Unchanged = OrderedRows[0].RowState == DataRowState.Unchanged;

                // mark the location that was determined to be the 'best'
                OrderedRows[BestRow][PartnerEditTDSPPartnerLocationTable.GetBestAddressDBName()] = ((object)1);

                // We do not want changing the BestAddress column to enable save. So revert row status to original.
                if (Unchanged)
                {
                    OrderedRows[0].AcceptChanges();
                }

                ReturnValue =
                    new TLocationPK(Convert.ToInt64(OrderedRows[BestRow][PLocationTable.GetSiteKeyDBName()]),
                                    Convert.ToInt32(OrderedRows[BestRow][PLocationTable.GetLocationKeyDBName()]));
            }
            else
            {
                if (OrderedRows.Length == 1)
                {
                    Unchanged = OrderedRows[0].RowState == DataRowState.Unchanged;

                    // mark the only location to be the 'best'
                    OrderedRows[0][PartnerEditTDSPPartnerLocationTable.GetBestAddressDBName()] = ((object)1);

                    // We do not want changing the BestAddress column to enable save. So revert row status to original.
                    if (Unchanged)
                    {
                        OrderedRows[0].AcceptChanges();
                    }

                    ReturnValue = new TLocationPK(Convert.ToInt64(OrderedRows[0][PLocationTable.GetSiteKeyDBName()]),
                                                  Convert.ToInt32(OrderedRows[0][PLocationTable.GetLocationKeyDBName()]));
                }
                else
                {
                    ReturnValue = new TLocationPK();
                }
            }

            return(ReturnValue);
        }