Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 32.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using iBoris.Broadcapp.DailyPlannings;
  5. using iBoris.Broadcapp.Exports.Npoi;
  6. using iBoris.Unicorn;
  7. using NPOI.HSSF.Util;
  8. using NPOI.SS.UserModel;
  9. using iBoris.Broadcapp.CentralPlan;
  10. using iBoris.Broadcapp.Resources;
  11. using iBoris.Broadcapp.PlanningDepartments;
  12. using iBoris.Broadcapp.Contacts;
  13. using iBoris.Broadcapp.Programs;
  14. using iBoris.Broadcapp.Security;
  15. using iBoris.Unicorn.Security;
  16. using NPOI.HSSF.UserModel;
  17.  
  18. namespace iBoris.Broadcapp.Exports
  19. {
  20.     public class ExportPlanBoardDetailsExcelReportService : IExportPlanBoardDetailsExcelReportService
  21.     {
  22.         public ExportPlanBoardDetailsExcelReportService(
  23.             ICentralPlanDataProvider centralPlanDataProvider,
  24.             ICentralPlanDateProvider centralPlanDateProvider)
  25.         {
  26.             this.centralPlanDataProvider = centralPlanDataProvider;
  27.             this.centralPlanDateProvider = centralPlanDateProvider;
  28.         }
  29.  
  30.         public byte[] ExportExcelReport(CentralPlanFilter filter, bool showManagementDetails)
  31.         {
  32.             //Create Report
  33.             List<NpoiSheet> npoiSheets = new List<NpoiSheet>();
  34.             npoiSheets.Add(new NpoiSheet("Plan Board Details", 0, false));
  35.             NpoiReport report = new NpoiReport(npoiSheets);
  36.  
  37.             Person person = SecurityContext.User as Person;
  38.             SecurityMarker securityMarker = new SecurityMarkerManager(person).Read();
  39.             bool canSeeBudgetSales = securityMarker.HasFlag(SecurityMarker.Management) ||
  40.                 securityMarker.HasFlag(SecurityMarker.Sales) ||
  41.                 securityMarker.HasFlag(SecurityMarker.Financials);
  42.             bool canSeeCosts = securityMarker.HasFlag(SecurityMarker.Budget); // TODO: check
  43.  
  44.             CreateCellStyles(report);
  45.             InitializeColumns(report, showManagementDetails, canSeeCosts);
  46.  
  47.             // Render Header
  48.             AddHeaderRow(report, canSeeBudgetSales, showManagementDetails, canSeeCosts);
  49.  
  50.             // Load Data
  51.             IList<CategoryValue> categoryValues = ExtractCategoryValues(filter, person, securityMarker);
  52.  
  53.             List<int> resourceIds = categoryValues.Select(cv => cv.Resource.Id).ToList();
  54.             List<Contact> contacts = new List<Contact>();
  55.             ContactFilter cFilter = new ContactFilter()
  56.             {
  57.                 SkipDzjitoContributors = false
  58.             };
  59.             cFilter.JointAssociations.Add("Nominal");
  60.  
  61.             Dictionary<int, List<int>> resourceIdDict = resourceIds.GroupIds<int, List<int>>();
  62.  
  63.             for (int i = 0; i < resourceIdDict.Count(); i++)
  64.             {
  65.                 cFilter.ResourceIds = resourceIdDict[i].ToArray();
  66.                 contacts.AddRange(Persistence.GetLoader<Contact, ContactFilter>(cFilter).List());
  67.             }
  68.  
  69.             // Render Data
  70.             GenerateReport(categoryValues, contacts, canSeeBudgetSales, canSeeCosts, showManagementDetails, report);
  71.  
  72.             return report.GetBinaryData();
  73.         }
  74.  
  75.         #region Private members
  76.         private IList<CategoryValue> ExtractCategoryValues(CentralPlanFilter filter, Person person, SecurityMarker marker)
  77.         {
  78.             ResourceFilter resourceFilter = GetResourceFilter(filter);
  79.             IEnumerable<Resource> resources = Persistence.GetLoader<Resource, ResourceFilter>(resourceFilter).List();
  80.             List<int> resourceIds = resources.Select(x => x.Id).ToList();
  81.             List<CategoryValue> categoryValues = new List<CategoryValue>();
  82.  
  83.             if (resourceIds.Count > 0)
  84.             {
  85.                 List<PlanningItemType> piTypes = new List<PlanningItemType>();
  86.                 piTypes.Add(PlanningItemType.Booking);
  87.                 piTypes.Add(PlanningItemType.Absence);
  88.  
  89.                 CategoryValueFilter categoryValueFilter = centralPlanDataProvider.GetCategoryValueFilter(
  90.                     filter,
  91.                     null,
  92.                     piTypes,
  93.                     false,
  94.                     true,
  95.                     true,
  96.                     fetchAssociationsForMapping: true);
  97.  
  98.                 if (!marker.HasFlag(SecurityMarker.Management))
  99.                 {
  100.                     categoryValueFilter.HasDepartmentOrProgramRightsUserId = person.Id;
  101.                 }
  102.                 categoryValueFilter.JointAssociations.Add("PlanningItem.Program.SalesBudget");
  103.  
  104.                 Dictionary<int, List<int>> resourceIdDict = resourceIds.GroupIds<int, List<int>>();
  105.                 for (int i = 0; i < resourceIdDict.Count(); i++)
  106.                 {
  107.                     categoryValueFilter.ResourceIds = resourceIdDict[i].ToArray();
  108.                     categoryValues.AddRange(Persistence.GetLoader<CategoryValue, CategoryValueFilter>(categoryValueFilter).List());
  109.                 }
  110.             }
  111.  
  112.             return categoryValues;
  113.         }
  114.  
  115.         private ResourceFilter GetResourceFilter(CentralPlanFilter filter)
  116.         {
  117.             bool hasGroupFilter = filter.PlanningDepartmentGroupIds != null && filter.PlanningDepartmentGroupIds.Count() > 0;
  118.             ResourceFilter resourceFilter = new ResourceFilter();
  119.  
  120.             if (hasGroupFilter)
  121.             {
  122.                 DepartmentGroupResourceFilter groupResourceFilter = centralPlanDataProvider.GetDepartmentGroupResourceFilter(filter);
  123.                 List<DepartmentGroupResource> departmentGroupResources = Persistence.GetLoader<DepartmentGroupResource, DepartmentGroupResourceFilter>(groupResourceFilter).List().ToList();
  124.                 List<int> ids = departmentGroupResources.Select(x => x.Id).ToList();
  125.                 ids.Add(-1); // if departmentGroupResources.Count = 0 ->> not show anything;
  126.                 resourceFilter.DepartmentGroupResourcesIds = ids.ToArray();
  127.             }
  128.             else
  129.             {
  130.                 resourceFilter = centralPlanDataProvider.GetResourceFilter(filter);
  131.             }
  132.  
  133.             return resourceFilter;
  134.         }
  135.  
  136.         private void CreateCellStyles(NpoiReport report)
  137.         {
  138.             IFont normalFont10 = new NpoiFont(report.GetWorkbook(), HSSFColor.BLACK.index, 10).GetFont();
  139.             IFont boldFont10 = new NpoiFont(report.GetWorkbook(), HSSFColor.BLACK.index, 10, true).GetFont();
  140.  
  141.             HSSFPalette palette = ((HSSFWorkbook)report.GetWorkbook()).GetCustomPalette();
  142.             palette.SetColorAtIndex(HSSFColor.LIGHT_GREEN.index,
  143.                 (byte)198,
  144.                 (byte)224,
  145.                 (byte)164
  146.             );
  147.  
  148.             columnHeaderBorderedLeftAlignedStyle = new NpoiStyle(report.GetWorkbook(), HSSFColor.LIGHT_GREEN.index, FillPatternType.SOLID_FOREGROUND, HorizontalAlignment.LEFT, boldFont10, BorderStyle.THIN, null);
  149.             columnHeaderBorderedRightAlignedStyle = new NpoiStyle(report.GetWorkbook(), HSSFColor.LIGHT_GREEN.index, FillPatternType.SOLID_FOREGROUND, HorizontalAlignment.RIGHT, boldFont10, BorderStyle.THIN, null);
  150.             columnNormalLeftNonBorderedStyle = new NpoiStyle(report.GetWorkbook(), 0, FillPatternType.NO_FILL, HorizontalAlignment.LEFT, normalFont10, BorderStyle.NONE, null);
  151.             columnNormalRightNonBorderedStyle = new NpoiStyle(report.GetWorkbook(), 0, FillPatternType.NO_FILL, HorizontalAlignment.RIGHT, normalFont10, BorderStyle.NONE, null);
  152.             columnDateRightNonBorderedStyle = new NpoiStyle(report.GetWorkbook(), 0, FillPatternType.NO_FILL, HorizontalAlignment.RIGHT, normalFont10, BorderStyle.NONE, "dd/MM/yyyy");
  153.             rightAligned2Decimals = new NpoiStyle(report.GetWorkbook(), horizontalAlignment: HorizontalAlignment.RIGHT, dataFormat: "€ * #,##0.00");
  154.             rightAlignedPercentage = new NpoiStyle(report.GetWorkbook(), horizontalAlignment: HorizontalAlignment.RIGHT, dataFormat: "% * #,##0.00");
  155.             rightAligned = new NpoiStyle(report.GetWorkbook(), horizontalAlignment: HorizontalAlignment.RIGHT);
  156.         }
  157.         private void InitializeColumns(NpoiReport report, bool showManagementDetails, bool canSeeCosts)
  158.         {
  159.             // Planning Department  
  160.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  161.             // Department Group
  162.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  163.             // Production Name
  164.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  165.             // Title
  166.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  167.             //Date From
  168.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  169.             // Date To
  170.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  171.             // Company
  172.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  173.             // Category Type
  174.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  175.             // Category/Role
  176.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  177.             // Resource
  178.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  179.             // Actual Start    
  180.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  181.             //Actual End
  182.             report.AddColumn(0, String.Empty, 30, CellType.STRING);
  183.             //Actual Break
  184.             report.AddColumn(0, String.Empty, 30, CellType.NUMERIC);
  185.             if (canSeeCosts)
  186.             {
  187.                 // Cost
  188.                 report.AddColumn(0, String.Empty, 20, CellType.NUMERIC);
  189.             }
  190.             // Number of Days
  191.             report.AddColumn(0, String.Empty, 20, CellType.NUMERIC);
  192.             // Kilometers
  193.             report.AddColumn(0, String.Empty, 20, CellType.NUMERIC);
  194.             // Production Cost Center
  195.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  196.             // Analytical Code
  197.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  198.             // Planning Department Cost Center
  199.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  200.             // Shift Name
  201.             report.AddColumn(0, String.Empty, 40, CellType.STRING);
  202.             // Shift Start Time
  203.             report.AddColumn(0, String.Empty, 40, CellType.STRING);
  204.             // Shift End Time
  205.             report.AddColumn(0, String.Empty, 40, CellType.STRING);
  206.             // Shift Break
  207.             report.AddColumn(0, String.Empty, 40, CellType.NUMERIC);
  208.             // Planning Type
  209.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  210.             // Status
  211.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  212.             // Actual Overtime
  213.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  214.             // Actual Workingtime
  215.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  216.             // Shift Workingtime
  217.             report.AddColumn(0, String.Empty, 20, CellType.STRING);
  218.  
  219.             if (showManagementDetails)
  220.             {
  221.                 // Production Internal Company
  222.                 report.AddColumn(0, String.Empty, 20, CellType.STRING);
  223.                 // Production Status
  224.                 report.AddColumn(0, String.Empty, 20, CellType.STRING);
  225.                 // Production Total Sales Budget
  226.                 report.AddColumn(0, String.Empty, 20, CellType.STRING);
  227.                 // Production Is Internal
  228.                 report.AddColumn(0, String.Empty, 20, CellType.STRING);
  229.                 // Production Company
  230.                 report.AddColumn(0, String.Empty, 20, CellType.STRING);
  231.                 // Person Contact Type
  232.                 report.AddColumn(0, String.Empty, 20, CellType.STRING);
  233.                 // Person Contact Hours
  234.                 report.AddColumn(0, String.Empty, 20, CellType.NUMERIC);
  235.             }
  236.  
  237.             report.SetLastDataRowIndex(0, 0);
  238.         }
  239.         private void AddHeaderRow(NpoiReport report, bool canSeeBudgetSales, bool showManagementDetails, bool canSeeCosts)
  240.         {
  241.             List<RowCell> rowCells = new List<RowCell>();
  242.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_PlanningDepartment, columnHeaderBorderedLeftAlignedStyle));
  243.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_DepartmentGroup, columnHeaderBorderedLeftAlignedStyle));
  244.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_Production, columnHeaderBorderedLeftAlignedStyle));
  245.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_Title, columnHeaderBorderedLeftAlignedStyle));
  246.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_DateFrom, columnHeaderBorderedRightAlignedStyle));
  247.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_DateTo, columnHeaderBorderedRightAlignedStyle));
  248.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_Company, columnHeaderBorderedLeftAlignedStyle));
  249.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_CategoryType, columnHeaderBorderedLeftAlignedStyle));
  250.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_CategoryRole, columnHeaderBorderedLeftAlignedStyle));
  251.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_Resource, columnHeaderBorderedLeftAlignedStyle));
  252.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ActualStart, columnHeaderBorderedRightAlignedStyle));
  253.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ActualEnd, columnHeaderBorderedRightAlignedStyle));
  254.             rowCells.Add(new RowCell(CellType.NUMERIC, Strings.ExportPlanBoardDetailsExcelReportService_ActualBreak, columnHeaderBorderedRightAlignedStyle));
  255.  
  256.             if (canSeeCosts)
  257.             {
  258.                 rowCells.Add(new RowCell(CellType.NUMERIC, Strings.ExportPlanBoardDetailsExcelReportService_Cost, columnHeaderBorderedRightAlignedStyle));
  259.             }
  260.  
  261.             rowCells.Add(new RowCell(CellType.NUMERIC, Strings.CostType, columnHeaderBorderedRightAlignedStyle));
  262.             rowCells.Add(new RowCell(CellType.NUMERIC, Strings.ExportPlanBoardDetailsExcelReportService_NumberOfDays, columnHeaderBorderedRightAlignedStyle));
  263.             rowCells.Add(new RowCell(CellType.NUMERIC, Strings.ExportPlanBoardDetailsExcelReportService_Kilometers, columnHeaderBorderedRightAlignedStyle));
  264.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ProductionCostCenter, columnHeaderBorderedLeftAlignedStyle));
  265.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_AnalyticalCode, columnHeaderBorderedLeftAlignedStyle));
  266.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_PlanningDepartmentCostCenter, columnHeaderBorderedLeftAlignedStyle));
  267.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ShiftName, columnHeaderBorderedLeftAlignedStyle));
  268.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ShiftStartTime, columnHeaderBorderedRightAlignedStyle));
  269.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ShiftEndTime, columnHeaderBorderedRightAlignedStyle));
  270.             rowCells.Add(new RowCell(CellType.NUMERIC, Strings.ExportPlanBoardDetailsExcelReportService_ShiftBreak, columnHeaderBorderedRightAlignedStyle));
  271.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_PlanningType, columnHeaderBorderedLeftAlignedStyle));
  272.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_Status, columnHeaderBorderedLeftAlignedStyle));
  273.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ActualOvertime, columnHeaderBorderedRightAlignedStyle));
  274.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ActualWorkingtime, columnHeaderBorderedRightAlignedStyle));
  275.             rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ShiftWorkingtime, columnHeaderBorderedRightAlignedStyle));
  276.  
  277.             if (showManagementDetails)
  278.             {
  279.                 rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ProductionInternalCompany, columnHeaderBorderedLeftAlignedStyle));
  280.                 rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ProductionStatus, columnHeaderBorderedLeftAlignedStyle));
  281.  
  282.                 if (canSeeBudgetSales)
  283.                 {
  284.                     rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ProductionTotalSalesBudget, columnHeaderBorderedLeftAlignedStyle));
  285.                 }
  286.  
  287.                 rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ProductionIsInternal, columnHeaderBorderedLeftAlignedStyle));
  288.                 rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_ProductionCompany, columnHeaderBorderedLeftAlignedStyle));
  289.                 rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_PersonContractType, columnHeaderBorderedLeftAlignedStyle));
  290.                 rowCells.Add(new RowCell(CellType.STRING, Strings.ExportPlanBoardDetailsExcelReportService_PersonContractHours, columnHeaderBorderedRightAlignedStyle));
  291.             }
  292.  
  293.             report.AddRowWithTypes(0, rowCells.ToArray());
  294.         }
  295.  
  296.         #region Generation Logic
  297.         private void GenerateReport(IList<CategoryValue> categoryValues, IEnumerable<Contact> contacts, bool canSeeBudgetSales, bool canSeeCosts, bool showManagementDetails, NpoiReport report)
  298.         {
  299.             foreach (var categoryValue in categoryValues)
  300.             {
  301.                 if (categoryValue.PlanningItem == null)
  302.                 {
  303.                     continue;
  304.                 }
  305.                 List<RowCell> cells = new List<RowCell>();
  306.                 cells.AddRange(GeneratePlanningItemCells(categoryValue.PlanningItem, categoryValue, contacts, canSeeBudgetSales, canSeeCosts, showManagementDetails));
  307.  
  308.                 report.AddRowWithTypes(0, cells.ToArray());
  309.             }
  310.         }
  311.         private IEnumerable<RowCell> GeneratePlanningItemCells(PlanningItem planningItem, CategoryValue categoryValue, IEnumerable<Contact> contacts, bool canSeeBudgetSales, bool canSeeCosts, bool showManagementDetails)
  312.         {
  313.             List<RowCell> rowCells = new List<RowCell>();
  314.  
  315.             // Planning Department
  316.             string planningDepartmentName = (planningItem.PlanningDepartment != null ? planningItem.PlanningDepartment.Name : string.Empty);
  317.             rowCells.Add(new RowCell(CellType.STRING, planningDepartmentName, columnNormalLeftNonBorderedStyle));
  318.  
  319.             // Department Group
  320.             string planningDepartmentGroupName = (planningItem.DepartmentGroup != null ? planningItem.DepartmentGroup.Name : string.Empty);
  321.             rowCells.Add(new RowCell(CellType.STRING, planningDepartmentGroupName, columnNormalLeftNonBorderedStyle));
  322.  
  323.             // Production Name
  324.             string planningItemProgramName = (planningItem.Program != null ? planningItem.Program.Name : string.Empty);
  325.             rowCells.Add(new RowCell(CellType.STRING, planningItemProgramName, columnNormalLeftNonBorderedStyle));
  326.  
  327.             // Title
  328.             rowCells.Add(new RowCell(CellType.STRING, planningItem.Title, columnNormalLeftNonBorderedStyle));
  329.  
  330.             // Date From
  331.             rowCells.Add(new RowCell(CellType.STRING, GetTextOrEmpty(planningItem.Date.ToShortDateString()), columnDateRightNonBorderedStyle));
  332.  
  333.             // Date To
  334.             rowCells.Add(new RowCell(CellType.STRING, GetTextOrEmpty(planningItem.ToDate.ToShortDateString()), columnDateRightNonBorderedStyle));
  335.  
  336.             // Company
  337.             string categoryValueCompanyName = (categoryValue.Company != null ? categoryValue.Company.Name : string.Empty);
  338.             rowCells.Add(new RowCell(CellType.STRING, categoryValueCompanyName, columnNormalLeftNonBorderedStyle));
  339.  
  340.             // Category Type
  341.             string categoryValueCategoryTypeName = (categoryValue.CategoryType != null ? categoryValue.CategoryType.Name : string.Empty);
  342.             rowCells.Add(new RowCell(CellType.STRING, categoryValueCategoryTypeName, columnNormalLeftNonBorderedStyle));
  343.  
  344.             // Category/Role
  345.             string categoryValueCategoryName = (categoryValue.Category != null ? categoryValue.Category.Name : string.Empty);
  346.             rowCells.Add(new RowCell(CellType.STRING, categoryValueCategoryName, columnNormalLeftNonBorderedStyle));
  347.  
  348.             // Resource
  349.             string categoryValueResourceName = (categoryValue.Resource != null ? categoryValue.Resource.Name : string.Empty);
  350.             rowCells.Add(new RowCell(CellType.STRING, categoryValueResourceName, columnNormalLeftNonBorderedStyle));
  351.  
  352.             // Actual Start
  353.             rowCells.Add(new RowCell(CellType.STRING, GetTextOrEmpty(categoryValue.ActualStart.ToTimeString()), columnNormalRightNonBorderedStyle));
  354.  
  355.             // Actual End
  356.             rowCells.Add(new RowCell(CellType.STRING, GetTextOrEmpty(categoryValue.ActualEnd.ToTimeString()), columnNormalRightNonBorderedStyle));
  357.  
  358.             // Actual Break
  359.             rowCells.Add(new RowCell(CellType.NUMERIC, categoryValue.ActualBreak, columnNormalRightNonBorderedStyle));
  360.  
  361.             if (canSeeCosts)
  362.             {
  363.                 // Cost
  364.                 rowCells.Add(new RowCell(CellType.NUMERIC, categoryValue.CostPerDay, columnNormalRightNonBorderedStyle));
  365.             }
  366.  
  367.             // Cost Type
  368.             rowCells.Add(new RowCell(CellType.NUMERIC, Enum.GetName(typeof(CostType), categoryValue.CostType), columnNormalRightNonBorderedStyle));
  369.  
  370.             // Number of Days            
  371.             if (planningItem.ToDate != null || planningItem.Date != null)
  372.             {
  373.                 double days = (planningItem.ToDate.Value - planningItem.Date).TotalDays;
  374.                 days = Math.Round((days == 0 ? 1 : days));
  375.  
  376.                 rowCells.Add(new RowCell(CellType.NUMERIC, days, columnNormalRightNonBorderedStyle));
  377.             }
  378.             else
  379.             {
  380.                 // Empty cells
  381.                 rowCells.Add(new RowCell(CellType.STRING, String.Empty, columnNormalLeftNonBorderedStyle));
  382.             }
  383.  
  384.             // Kilometers
  385.             rowCells.Add(new RowCell(CellType.NUMERIC, categoryValue.Kilometers, columnNormalRightNonBorderedStyle));
  386.  
  387.             // Production Cost Center
  388.             if (planningItem.Program != null)
  389.             {
  390.                 string planningItemProgramCostCenterName = (planningItem.Program.CostCenter != null ? planningItem.Program.CostCenter.Name : string.Empty);
  391.                 rowCells.Add(new RowCell(CellType.STRING, planningItemProgramCostCenterName, columnNormalLeftNonBorderedStyle));
  392.             }
  393.             else
  394.             {
  395.                 // Empty cells
  396.                 rowCells.Add(new RowCell(CellType.STRING, String.Empty, columnNormalLeftNonBorderedStyle));
  397.             }
  398.  
  399.             // Analytical Code
  400.             string analyticalCode = (planningItem.Program != null ? planningItem.Program.AnalyticalCode : string.Empty);
  401.             rowCells.Add(new RowCell(CellType.STRING, analyticalCode, columnNormalLeftNonBorderedStyle));
  402.  
  403.             // Planning Department Cost Center
  404.             if (planningItem.PlanningDepartment != null)
  405.             {
  406.                 string planningItemProgramCostCenterName = (planningItem.PlanningDepartment.CostCenter != null ? planningItem.PlanningDepartment.CostCenter.Name : string.Empty);
  407.                 rowCells.Add(new RowCell(CellType.STRING, planningItemProgramCostCenterName, columnNormalLeftNonBorderedStyle));
  408.             }
  409.             else
  410.             {
  411.                 // Empty cells
  412.                 rowCells.Add(new RowCell(CellType.STRING, String.Empty, columnNormalLeftNonBorderedStyle));
  413.             }
  414.  
  415.             // Shift Name
  416.             string planningItemShiftName = (planningItem.Shift != null ? planningItem.Shift.Name : string.Empty);
  417.             rowCells.Add(new RowCell(CellType.STRING, planningItemShiftName, columnNormalLeftNonBorderedStyle));
  418.  
  419.             // Shift Start Time
  420.             string planningItemShiftStartTime = (planningItem.Shift != null ? planningItem.Shift.StartTime.ToTimeString() : string.Empty);
  421.             rowCells.Add(new RowCell(CellType.STRING, planningItemShiftStartTime, columnNormalRightNonBorderedStyle));
  422.  
  423.             // Shift End Time
  424.             string planningItemShiftEndTime = (planningItem.Shift != null ? planningItem.Shift.EndTime.ToTimeString() : string.Empty);
  425.             rowCells.Add(new RowCell(CellType.STRING, planningItemShiftEndTime, columnNormalRightNonBorderedStyle));
  426.  
  427.             // Shift Break
  428.             if (planningItem.Shift != null)
  429.             {
  430.                 rowCells.Add(new RowCell(CellType.NUMERIC, planningItem.Shift.BreakTime, columnNormalRightNonBorderedStyle));
  431.             }
  432.             else
  433.             {
  434.                 // Empty cells
  435.                 rowCells.Add(new RowCell(CellType.NUMERIC, String.Empty, columnNormalRightNonBorderedStyle));
  436.             }
  437.  
  438.             // Planning Type
  439.             string planningItemPlanningTypeName = (planningItem.PlanningType != null ? planningItem.PlanningType.Name : string.Empty);
  440.             rowCells.Add(new RowCell(CellType.STRING, planningItemPlanningTypeName, columnNormalLeftNonBorderedStyle));
  441.  
  442.             // Status
  443.             rowCells.Add(new RowCell(CellType.STRING, Enum.GetName(typeof(BookingStatus), categoryValue.Confirmed), columnNormalLeftNonBorderedStyle));
  444.  
  445.             // Actual Overtime
  446.             double? overtime = null;
  447.             if (categoryValue.Overtime.HasValue)
  448.             {
  449.                 double overtimeDec = new TimeSpan(0, categoryValue.Overtime.Value, 0).TotalHours;
  450.                 overtime = Math.Round(overtimeDec, 2);
  451.             }
  452.             rowCells.Add(new RowCell(CellType.NUMERIC, overtime, columnNormalRightNonBorderedStyle));
  453.  
  454.             // Actual Working Time
  455.             double workingtimeDec = centralPlanDateProvider.GetBookingDuration(categoryValue).TotalHours;
  456.             double workingtime = Math.Round(workingtimeDec, 2);
  457.             rowCells.Add(new RowCell(CellType.NUMERIC, workingtime, columnNormalRightNonBorderedStyle));
  458.  
  459.             // Shift Working Time
  460.             double? shiftWorkingtime = null;
  461.             if (categoryValue.PlanningItem.Shift != null)
  462.             {
  463.                 Shift shift = categoryValue.PlanningItem.Shift;
  464.                 double shiftWorkingtimeDec = centralPlanDateProvider.GetDuration(shift.StartTime, shift.EndTime, shift.BreakTime).TotalHours;
  465.                 shiftWorkingtime = Math.Round(shiftWorkingtimeDec, 2);
  466.             }
  467.             rowCells.Add(new RowCell(CellType.NUMERIC, shiftWorkingtime, columnNormalRightNonBorderedStyle));
  468.  
  469.             if (showManagementDetails)
  470.             {
  471.                 // Production Internal Company
  472.                 rowCells.Add(new RowCell(CellType.STRING, planningItem.Program?.InternalCompany?.Name, columnNormalLeftNonBorderedStyle));
  473.  
  474.                 // Production Status
  475.                 string status = planningItem.Program?.Status != null ? Enum.GetName(typeof(ProgramStatus), planningItem.Program?.Status) : string.Empty;
  476.                 rowCells.Add(new RowCell(CellType.STRING, status, columnNormalLeftNonBorderedStyle));
  477.  
  478.                 // Production Total Sales Budget
  479.                 if (canSeeBudgetSales)
  480.                 {
  481.                     rowCells.Add(new RowCell(CellType.NUMERIC, categoryValue.PlanningItem.Program?.SalesBudget?.Total, columnNormalRightNonBorderedStyle));
  482.                 }
  483.  
  484.                 // Production Is Internal
  485.                 string isInternal;
  486.                 if (planningItem.Program != null)
  487.                 {
  488.                     isInternal = planningItem.Program?.IsInternal == true ? Strings.Yes : Strings.No;
  489.                 }
  490.                 else
  491.                 {
  492.                     isInternal = string.Empty;
  493.                 }
  494.  
  495.                 rowCells.Add(new RowCell(CellType.STRING, isInternal, columnNormalLeftNonBorderedStyle));
  496.  
  497.                 // Production Company
  498.                 rowCells.Add(new RowCell(CellType.STRING, planningItem.Program?.Company?.Name, columnNormalLeftNonBorderedStyle));
  499.  
  500.                 // Person Contact Type
  501.                 Contact currentContact = contacts.Where(c => c.Resource.Id == categoryValue.Resource.Id).FirstOrDefault();
  502.                 string contactType = currentContact != null ? Enum.GetName(typeof(ContactType), currentContact?.ContactType) : string.Empty;
  503.                 rowCells.Add(new RowCell(CellType.STRING, contactType, columnNormalLeftNonBorderedStyle));
  504.  
  505.                 // Person Contact Hours
  506.                 string contactHours = currentContact != null ? GetPersonContactHours(currentContact, planningItem).ToString() : String.Empty;
  507.                 rowCells.Add(new RowCell(CellType.NUMERIC, contactHours, columnNormalRightNonBorderedStyle));
  508.             }
  509.  
  510.             return rowCells;
  511.         }
  512.         #endregion
  513.         private string GetTextOrEmpty(string value)
  514.         {
  515.             return value ?? string.Empty;
  516.         }
  517.         private decimal GetPersonContactHours(Contact contact, PlanningItem pi)
  518.         {
  519.             decimal hours = 0;
  520.             int piDuration = (pi.ToDate - pi.Date).Value.Days + 1;
  521.  
  522.             foreach (var nominal in contact.Nominals)
  523.             {
  524.                 var piDate = pi.Date.Date;
  525.  
  526.                 if (piDate >= nominal.DateFrom.Date && (!nominal.DateTo.HasValue || piDate <= nominal.DateTo.Value.Date))
  527.                 {
  528.                     if (nominal.NominalSchemeType == NominalSchemeType.Variable)
  529.                     {
  530.                         hours = nominal.DailyNominal != null ? nominal.DailyNominal.Value : 0;
  531.                         hours = hours * piDuration;
  532.                     }
  533.                     else if (nominal.NominalSchemeType == NominalSchemeType.Fixed)
  534.                     {
  535.                         for (int i = 0; i < piDuration; i++)
  536.                         {
  537.                             DayOfWeek dayofWeek = pi.Date.DayOfWeek + i;
  538.                             hours += GetPlanningItemFixedNominalHours(nominal, dayofWeek);
  539.                         }
  540.                     }
  541.  
  542.                     break;
  543.                 }
  544.             }
  545.  
  546.             return hours;
  547.         }
  548.  
  549.         private decimal GetPlanningItemFixedNominalHours(Nominal n, DayOfWeek dayOfWeek)
  550.         {
  551.             decimal hours = 0;
  552.  
  553.             switch (dayOfWeek)
  554.             {
  555.                 case DayOfWeek.Sunday:
  556.                     hours = n.SundayNominal.Value;
  557.                     break;
  558.                 case DayOfWeek.Monday:
  559.                     hours = n.MondayNominal.Value;
  560.                     break;
  561.                 case DayOfWeek.Tuesday:
  562.                     hours = n.TuesdayNominal.Value;
  563.                     break;
  564.                 case DayOfWeek.Wednesday:
  565.                     hours = n.WednesdayNominal.Value;
  566.                     break;
  567.                 case DayOfWeek.Thursday:
  568.                     hours = n.ThursdayNominal.Value;
  569.                     break;
  570.                 case DayOfWeek.Friday:
  571.                     hours = n.FridayNominal.Value;
  572.                     break;
  573.                 case DayOfWeek.Saturday:
  574.                     hours = n.SaturdayNominal.Value;
  575.                     break;
  576.                 default:
  577.                     break;
  578.             }
  579.  
  580.             return hours;
  581.         }
  582.        
  583.         private NpoiStyle columnHeaderBorderedLeftAlignedStyle;
  584.         private NpoiStyle columnHeaderBorderedRightAlignedStyle;
  585.         private NpoiStyle columnNormalLeftNonBorderedStyle;
  586.         private NpoiStyle columnNormalRightNonBorderedStyle;
  587.         private NpoiStyle columnDateRightNonBorderedStyle;
  588.         private NpoiStyle rightAligned2Decimals;
  589.         private NpoiStyle rightAlignedPercentage;
  590.         private NpoiStyle rightAligned;
  591.         private ICentralPlanDataProvider centralPlanDataProvider;
  592.         private ICentralPlanDateProvider centralPlanDateProvider;
  593.         #endregion
  594.     }
  595. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement