コード例 #1
0
ファイル: BusReport.cs プロジェクト: alex765022/IBN
        /// <summary>
        /// Generates the user report.
        /// </summary>
        /// <param name="userIdList">The user id list.</param>
        /// <param name="projectIdList">The project id list.</param>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="completion">The completion.</param>
        /// <param name="completionFrom">The completion from.</param>
        /// <param name="completionTo">The completion to.</param>
        /// <returns></returns>
        public static ReportResult GenerateUserReport(List <int> userIdList,
                                                      List <int> projectIdList,
                                                      DateTime from, DateTime to,
                                                      int completion, DateTime completionFromDate, DateTime completionToDate)
        {
            if (userIdList == null)
            {
                throw new ArgumentNullException("userIdList");
            }
            if (projectIdList == null)
            {
                throw new ArgumentNullException("projectIdList");
            }

            TimeZone tz = Security.CurrentUser.CurrentTimeZone;

            completionFromDate = tz.ToUniversalTime(completionFromDate);
            completionToDate   = tz.ToUniversalTime(completionToDate);

            ReportResult result = new ReportResult();

            Dictionary <int, int> stateMachineFinalStates = new Dictionary <int, int>();

            // Foreach userId in userIdList
            foreach (int userId in userIdList)
            {
                string userName = User.GetUserName(userId);

                UserReportResultItem userItem = new UserReportResultItem(ObjectTypes.User, userId, userName);

                // Foreach projectId in projectIdList
                foreach (int projectId in projectIdList)
                {
                    Dictionary <int, int> taskIdIndexDic = new Dictionary <int, int>();

                    // TODO: Check userId has TTBlock (projectId, userId = owner, from, to) And AreFinancesRegistered is True
                    if (TimeTrackingBlock.GetTotalCount(FilterElement.EqualElement("OwnerId", userId),
                                                        FilterElement.EqualElement("ProjectId", projectId),
                                                        new IntervalFilterElement("StartDate", from, to),
                                                        FilterElement.EqualElement("AreFinancesRegistered", true)) == 0)
                    {
                        continue;
                    }

                    string projectTitle = Project.GetProjectTitle(projectId);

                    UserReportResultItem projectItem = new UserReportResultItem(ObjectTypes.Project, projectId, projectTitle);

                    using (IDataReader reader = DbReport.GetUserReportInfo(from, to, userId, projectId, completion, completionFromDate, completionToDate))
                    {
                        // Enum TTEntries (ObjectType is equal to Task And ToDo (append to owner task)) and return
                        // - FinalApproved If State is Final State  = Day1 + ... + Day7
                        // - TotalApproved
                        // - Cost = TotalApproved * Rate
                        //

                        while (reader.Read())
                        {
                            int      taskId            = (int)reader["TaskId"];
                            string   taskName          = (string)reader["TaskName"];
                            int      mc_StateMachineId = (int)reader["mc_StateMachineId"];
                            int      mc_StateId        = (int)reader["mc_StateId"];
                            DateTime startDate         = (DateTime)reader["StartDate"];
                            double   day1          = (double)reader["Day1"];
                            double   day2          = (double)reader["Day2"];
                            double   day3          = (double)reader["Day3"];
                            double   day4          = (double)reader["Day4"];
                            double   day5          = (double)reader["Day5"];
                            double   day6          = (double)reader["Day6"];
                            double   day7          = (double)reader["Day7"];
                            double   totalApproved = (double)reader["TotalApproved"];
                            decimal  rate          = (decimal)reader["Rate"];

                            bool isCurrentStateFinal = false;

                            #region Check Final State Cache
                            if (!stateMachineFinalStates.ContainsKey(mc_StateMachineId))
                            {
                                int finalStateId = StateMachineManager.GetFinalStateId(DataContext.Current.GetMetaClass("TimeTrackingBlock"), mc_StateMachineId);
                                stateMachineFinalStates.Add(mc_StateMachineId, finalStateId);
                            }
                            #endregion

                            isCurrentStateFinal = (stateMachineFinalStates[mc_StateMachineId] == mc_StateId);

                            UserReportResultItem taskItem;

                            if (taskIdIndexDic.ContainsKey(taskId))
                            {
                                taskItem = (UserReportResultItem)projectItem.ChildItems[taskIdIndexDic[taskId]];
                            }
                            else
                            {
                                taskItem = new UserReportResultItem(ObjectTypes.Task, taskId, taskName);
                                projectItem.ChildItems.Add(taskItem);

                                taskIdIndexDic.Add(taskId, projectItem.ChildItems.Count - 1);
                            }

                            if (isCurrentStateFinal)
                            {
                                taskItem.ApprovedByManager += (day1 + day2 + day3 + day4 + day5 + day6 + day7);
                            }
                            else
                            {
                                taskItem.ApprovedByManager += 0;
                            }

                            taskItem.Total += totalApproved;
                            taskItem.Cost  += totalApproved * (double)rate / 60;
                        }
                    }

                    // Calculate Total projectItem
                    CalculateTotalForUserReportResultItem(projectItem);

                    userItem.ChildItems.Add(projectItem);
                }

                // Calculate Total userItem
                CalculateTotalForUserReportResultItem(userItem);

                result.Items.Add(userItem);
            }

            return(result);
        }