/// <summary> /// Generates the project task business score report. /// </summary> /// <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 GenerateProjectTaskBusinessScoreReport(List <int> projectIdList, DateTime from, DateTime to, int completion, DateTime completionFromDate, DateTime completionToDate) { if (projectIdList == null) { throw new ArgumentNullException("projectIdList"); } // test only; //projectIdList = new List<int>(new int[] { 624, 544, 665, 680, 686, 687, 688, 689, 681, 691, 781, 633, 809, 803, 801, 838, 407, 852, 849, 853, 841, 856, 819, 848 }); TimeZone tz = Security.CurrentUser.CurrentTimeZone; completionFromDate = tz.ToUniversalTime(completionFromDate); completionToDate = tz.ToUniversalTime(completionToDate); ReportResult result = new ReportResult(); foreach (int projectId in projectIdList) { // Get Project Title string projectTitleName = Project.GetProjectTitle(projectId); ReportResultItem projectItem = new ReportResultItem(ObjectTypes.Project, projectId, projectTitleName); #region Load Task List // Get Task(Id,Name) List by projectId and fill projectItem.Items using (IDataReader reader = DbReport.GetTasks(projectId, completion, completionFromDate, completionToDate)) { while (reader.Read()) { int taskId = (int)reader["TaskId"]; string taskTitle = (string)reader["Title"]; ReportResultItem taskItem = new ReportResultItem(ObjectTypes.Task, taskId, taskTitle); // Add taskItem to projectItem projectItem.ChildItems.Add(taskItem); } } #endregion #region Create SpreadSheetDocument and SpreadSheetView // Create a new Spread Sheet Document SpreadSheetDocument ssDocument = new SpreadSheetDocument(SpreadSheetDocumentType.Total); // Load Template ssDocument.Template.Load(Path.Combine(ProjectSpreadSheet.TemplateDirectory, "ProfitAndLossStatementSimple_RU.xml")); ProjectSpreadSheetRow[] projectSpreadSheets = ProjectSpreadSheetRow.List(projectId); ProjectSpreadSheetRow projectSpreadSheet = projectSpreadSheets[0]; // Add user rows if (projectSpreadSheet.UserRows != string.Empty) { try { ssDocument.Template.LoadXml(projectSpreadSheet.UserRows); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); } } SpreadSheetView ssView = new SpreadSheetView(ssDocument); #endregion // Load Business Score //BusinessScore[] businessScoreList = BusinessScore.List(); List <ReportResultItem> taskWithoutFinancesRemoveIt = new List <ReportResultItem>(); #region Calcualte Business Score // Calcualte Business Score foreach (ReportResultItem taskItem in projectItem.ChildItems) { // Cleare Spread Sheet Document ssView.Document.DeleteAllCells(); // Get Actual Finances for current task + task/todo ActualFinances[] finances = ActualFinances.List(taskItem.ObjectId, taskItem.ObjectType, from, to); if (finances.Length > 0) { // Append Actual Finances to SpreadSheetDocument foreach (ActualFinances finance in finances) { string columnId = SpreadSheetView.GetColumnByDate(ssView.Document.DocumentType, finance.Date); string rowId = finance.RowId; Cell cell = ssView.Document.GetCell(columnId, rowId); if (cell == null) { cell = ssView.Document.AddCell(columnId, rowId, CellType.Common, 0); } cell.Value += finance.Value; } // Add Business Scope ReportResultItem and Calculate Business Scope total. // TODO: Localize ReportResultItem.Name taskItem.ChildItems.Add(new ReportResultItem(ObjectTypes.UNDEFINED, -1, "Доходы" /*"Revenues"*/)); taskItem.ChildItems.Add(new ReportResultItem(ObjectTypes.UNDEFINED, -1, "Расходы" /*Expenses"*/)); taskItem.ChildItems.Add(new ReportResultItem(ObjectTypes.UNDEFINED, -1, "Чистый доход" /*"NetIncome"*/)); taskItem.ChildItems[0].Total = ssView["TT", "Revenues"]; taskItem.ChildItems[1].Total = ssView["TT", "Expenses"]; taskItem.ChildItems[2].Total = ssView["TT", "NetIncome"]; // Copy NetIncome to total taskItem.Total = taskItem.ChildItems[2].Total; } else { taskWithoutFinancesRemoveIt.Add(taskItem); } } // Remove Empty Tasks foreach (ReportResultItem item in taskWithoutFinancesRemoveIt) { projectItem.ChildItems.Remove(item); } #endregion projectItem.Total = CalculateTotal(projectItem.ChildItems); // Add projectItem to result result.Items.Add(projectItem); } // Calculate Report Total result.Total = CalculateTotal(result.Items); // Final return result return(result); }