Example #1
0
        private static DataTable CalculateLengthOfCommitment(
            LengthOfCommitmentReportTDSPmStaffDataTable ACommitmentTable,
            DateTime AReportStartDate,
            DateTime AReportEndDate,
            List <Int32> ASpecialAnniversaries)
        {
            LengthOfCommitmentReportTDSPmStaffDataTable Anniversaries = new LengthOfCommitmentReportTDSPmStaffDataTable();

            // commitments are sorted by start date
            ACommitmentTable.DefaultView.Sort = LengthOfCommitmentReportTDSPmStaffDataTable.GetPartnerKeyDBName() + "," +
                                                LengthOfCommitmentReportTDSPmStaffDataTable.GetStartOfCommitmentDBName();

            LengthOfCommitmentReportTDSPmStaffDataRow PreviousRow = null;
            Int64 CurrentPartnerKey  = -1;
            Int64 PreviousPartnerKey = -1;

            // add up time, ignore overlaps
            DateTime previousStartDate = DateTime.MinValue;
            DateTime?previousEndDate   = new Nullable <DateTime>(); // null value: open ended commitment

            // we need the overall time that someone has worked with us
            // only count full months
            int monthsServed = 0;

            foreach (DataRowView rv in ACommitmentTable.DefaultView)
            {
                LengthOfCommitmentReportTDSPmStaffDataRow row = (LengthOfCommitmentReportTDSPmStaffDataRow)rv.Row;
                CurrentPartnerKey = row.PartnerKey;

                if (CurrentPartnerKey != PreviousPartnerKey)
                {
                    if (PreviousPartnerKey != -1)
                    {
                        StoreCommitmentTotal(
                            PreviousRow,
                            monthsServed,
                            AReportStartDate,
                            AReportEndDate,
                            previousStartDate,
                            previousEndDate,
                            Anniversaries,
                            ASpecialAnniversaries);
                    }

                    monthsServed       = 0;
                    PreviousPartnerKey = CurrentPartnerKey;
                    PreviousRow        = row;
                    previousStartDate  = row.StartOfCommitment;
                    previousEndDate    = row.IsEndOfCommitmentNull() ? new Nullable <DateTime>() : row.EndOfCommitment;
                }
                else
                {
                    // another commitment for the current person
                    if (!previousEndDate.HasValue)
                    {
                        // previous commitment was open ended
                        continue;
                    }

                    if (row.StartOfCommitment > AReportEndDate)
                    {
                        // outside of current period for this report
                        continue;
                    }

                    // overlapping commitment?
                    if (row.StartOfCommitment <= previousEndDate)
                    {
                        if (!row.IsEndOfCommitmentNull() && (row.EndOfCommitment <= previousEndDate))
                        {
                            // ignore this commitment, since it is already covered by the previous commitment
                        }
                        else
                        {
                            // modify previous commitment to be longer
                            previousEndDate = row.IsEndOfCommitmentNull() ? new Nullable <DateTime>() : row.EndOfCommitment;
                        }
                    }
                    else
                    {
                        // store previous commitment time, and start a new commitment
                        monthsServed     += ElapsedDays(previousEndDate.Value - previousStartDate);
                        previousStartDate = row.StartOfCommitment;
                        previousEndDate   = row.IsEndOfCommitmentNull() ? new Nullable <DateTime>() : row.EndOfCommitment;
                    }
                }
            }

            if (PreviousRow != null)
            {
                StoreCommitmentTotal(
                    PreviousRow,
                    monthsServed,
                    AReportStartDate,
                    AReportEndDate,
                    previousStartDate,
                    previousEndDate,
                    Anniversaries,
                    ASpecialAnniversaries);
            }

            return(Anniversaries);
        }