Browse Source

Fixed Color Handling in Employee Planner

frogsoftware 11 months ago
parent
commit
2c418fb28e

+ 5 - 4
prs.desktop/Panels/EmployeePlanner/EmployeeResourcePlanner.xaml

@@ -12,21 +12,22 @@
         <Style x:Key="DateHeaderStyle" TargetType="{x:Type Syncfusion:GridHeaderCellControl}">
             <Setter Property="Background" Value="LightSkyBlue"/>
             <Setter Property="Foreground" Value="Black"/>
-            <Setter Property="BorderBrush" Value="Black"/>
-            <Setter Property="BorderThickness" Value="0.5,0.5,0.5,0.5"/>
+            <Setter Property="BorderBrush" Value="DimGray"/>
+            <Setter Property="BorderThickness" Value="0,0,0.75,0.75"/>
             <Setter Property="HorizontalContentAlignment" Value="Center"/>
             <Setter Property="Padding" Value="5,3"/>
             <Setter Property="FontFamily" Value="Segoe UI"/>
             <Setter Property="FontSize" Value="14"/>
             <Setter Property="FontWeight" Value="Normal"/>
             <Setter Property="IsTabStop" Value="False"/>
+            <Setter Property="Margin" Value="0,0,0,0.5"/>
         </Style>
 
         <Style x:Key="ContentHeaderStyle" TargetType="{x:Type Syncfusion:GridHeaderCellControl}">
             <Setter Property="Background" Value="LightSkyBlue"/>
             <Setter Property="Foreground" Value="Black"/>
-            <Setter Property="BorderBrush" Value="Black"/>
-            <Setter Property="BorderThickness" Value="0.5,0.5,0.5,0.5"/>
+            <Setter Property="BorderBrush" Value="DimGray"/>
+            <Setter Property="BorderThickness" Value="0.75,0,0,0.75"/>
             <Setter Property="HorizontalContentAlignment" Value="Left"/>
             <Setter Property="Padding" Value="5,3"/>
             <Setter Property="FontFamily" Value="Segoe UI"/>

+ 117 - 51
prs.desktop/Panels/EmployeePlanner/EmployeeResourcePlanner.xaml.cs

@@ -40,18 +40,18 @@ public class EmployeeResourcePlannerValue
     public Brush Foreground { get; set; }
     public String Text { get; set; }
 
-    private String _color = "";
-    public String Color
-    {
-        get { return _color; }
-        set
-        {
-            _color = value;
-            var color = String.IsNullOrWhiteSpace(value) ? Colors.Transparent : (Color)ColorConverter.ConvertFromString(value);
-            Background = new SolidColorBrush(color) { Opacity = 0.8 };
-            Foreground = new SolidColorBrush(ImageUtils.GetForegroundColor(color));
-        }
-    }
+    //private String _color = "";
+    // public String Color
+    // {
+    //     get { return _color; }
+    //     set
+    //     {
+    //         _color = value;
+    //         var color = String.IsNullOrWhiteSpace(value) ? Colors.Transparent : (Color)ColorConverter.ConvertFromString(value);
+    //         Background = new SolidColorBrush(color) { Opacity = 0.8 };
+    //         Foreground = new SolidColorBrush(ImageUtils.GetForegroundColor(color));
+    //     }
+    // }
 }
 
 public enum EmployeePlannerDisplayMode
@@ -259,7 +259,7 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
                     {
                         var value = new EmployeeResourcePlannerValue();
                         // Note use of short-circuiting here.
-                        var bOK = CheckAssignments(employee, curdate, curdate + period, assignments, value)
+                        var bOK = CheckAssignments(employee, curdate, curdate + period, assignments, standardHours, value)
                             || CheckStandardLeave(leavevalue, value)
                             || CheckLeaveRequest(employee, curdate, curdate + period, _leaverequests, value)
                             || CheckRoster(employee, curdate, curdate + period, standardHours, value);
@@ -289,7 +289,7 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
                     {
                         var leavevalue = GetStandardLeave(curdate, _standardleaves);
                         var value = new EmployeeResourcePlannerValue();
-                        var bOK = CheckAssignments(employee, curdate, curdate + period, assignments, value)
+                        var bOK = CheckAssignments(employee, curdate, curdate + period, assignments, standardHours, value)
                             || CheckStandardLeave(leavevalue, value)
                             || CheckLeaveRequest(employee, curdate, curdate + period, _leaverequests, value)
                             || CheckRoster(employee, curdate, curdate + period, standardHours, value);
@@ -305,33 +305,76 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
             dataGrid.ItemsSource = data;
         }
     }
+
+    private static Color ParseColor(string? color, Color defaultColor)
+    {
+        if (String.IsNullOrWhiteSpace(color))
+            return defaultColor;
+        
+        try
+        {
+            return ImageUtils.StringToMediaColor(color);
+        }
+        catch
+        {
+            try
+            {
+                return (Color)ColorConverter.ConvertFromString(color);
+            }
+            catch (Exception e)
+            {
+                Logger.Send(LogType.Error,"",$"EmployeePlanner Color [{color}] is not a valid color!\n{e.Message}\n{e.StackTrace}");
+                return defaultColor;
+            }
+        }
+    }
     
-    private static bool CheckAssignments(EmployeeResourceModel employee, DateTime from, DateTime to, AssignmentModel[] assignments, EmployeeResourcePlannerValue value)
+    private static bool CheckAssignments(EmployeeResourceModel employee, DateTime from, DateTime to, AssignmentModel[] assignments, double standardhours, EmployeeResourcePlannerValue value)
     {
         var dateAssignments = assignments.Where(x => (x.EmployeeID == employee.ID) && (x.Date >= from.Date && x.Date < to.Date)).ToArray();
         if (dateAssignments.Length == 0)
             return false;
 
         value.IDs = assignments.Select(x => x.ID).ToArray();
-        value.Text = dateAssignments.Length == 1
-            ? (dateAssignments[0].ID != Guid.Empty ? (dateAssignments[0].JobNumber ?? "") : "XX")
-            : $"{dateAssignments.Select(x => x.JobID).Distinct().Count()} jobs";
+        var jobs = dateAssignments.Where(x=>x.JobID != Guid.Empty).Select(x => x.JobNumber).Distinct().ToArray();
+        value.Text = (jobs.Length == 0
+            ? "(No Job)"
+            : jobs.Length == 1
+                ? jobs[0]
+                : $"{jobs.Length} jobs") ?? string.Empty;
         value.Assignments = dateAssignments;
-
-        var colors = dateAssignments.GroupBy(x => x.Color ?? "#FFFFFF", x => x.BookedDuration)
+        
+        var groups = dateAssignments.GroupBy(x => x.Color ?? "#FFFFFF", x => x.BookedDuration)
             .Select(x => new Tuple<String,double>(x.Key,x.Sum(c=>c.TotalHours)))
             .OrderByDescending(x => x.Item2)
-            .Take(2)
             .ToArray();
-        value.Color = colors.Length < 1
-            ? Colors.White.ToString()
-            : colors.Length == 1
-                ? colors.First().Item1
-                : ImageUtils.StringToColor(colors.First().Item1).MixColors(
-                    colors.First().Item2 / (colors.Sum(x => x.Item2)),
-                    ImageUtils.StringToColor(colors.Last().Item1))
-                .ToString();
+        
+        var rostered = Math.Max(Math.Max(standardhours,GetTotalRostered(employee, from, to)), groups.Sum(x=>x.Item2));
+        
+        var background = new LinearGradientBrush() { StartPoint = new Point(0, 1), EndPoint = new Point(1, 1), Opacity = 0.8};
+        var foreground = new LinearGradientBrush() { StartPoint = new Point(0, 1), EndPoint = new Point(1, 1) };
+        double x = groups.First().Item2;
+        var bg = ParseColor(groups.First().Item1, Colors.Transparent);
+        var fg = ParseColor(groups.First().Item1, Colors.Transparent.GetForegroundColor());
+        background.GradientStops.Add(new GradientStop(bg.AdjustLightness(20), 0.0));
+        foreground.GradientStops.Add(new GradientStop(fg, 0.0));
+        foreach (var group in groups.Skip(1))
+        {
+            bg = ParseColor(group.Item1, bg);
+            fg = bg.GetForegroundColor();
+            background.GradientStops.Add(new GradientStop(bg.AdjustLightness(20),x/rostered));
+            foreground.GradientStops.Add(new GradientStop(fg,x/rostered));
+            x += group.Item2;
+        }
+
+        if (x < rostered)
+        {
+            background.GradientStops.Add(new GradientStop(Colors.LightGray,1.0));
+            foreground.GradientStops.Add(new GradientStop(Colors.LightGray.GetForegroundColor(),1.0));
+        }
 
+        value.Background = background;
+        value.Foreground = new SolidColorBrush(Colors.Black);
         return true;
     }
 
@@ -339,6 +382,35 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
     {
         value.Text = "";
 
+        var totalRostered = GetTotalRostered(employee, from, to);
+
+        var color1 = Colors.LightGray;
+        var color2 = Colors.LightYellow;
+
+        var percent = Math.Min(totalRostered / standardHours, 1.0);
+
+        //value.Foreground = new SolidColorBrush(ImageUtils.MixColors(color1, 1 - percent, color2)) { Opacity = 0.8 };
+        if (percent.IsEffectivelyEqual(0.0))
+            value.Background = new SolidColorBrush(Colors.LightGray.AdjustLightness(20)) { Opacity = 0.8 };
+        else if (percent.IsEffectivelyEqual(1.0))
+            value.Background = new SolidColorBrush(Colors.LightYellow.AdjustLightness(20)) { Opacity = 0.8};
+        else
+        {
+            var brush = new LinearGradientBrush() { Opacity = 0.8 };
+            brush.StartPoint = new Point(0,0);
+            brush.GradientStops.Add(new GradientStop(Colors.LightYellow.AdjustLightness(20), percent));
+            brush.GradientStops.Add(new GradientStop(Colors.LightGray.AdjustLightness(20), 1.0));
+            brush.EndPoint = new Point(1,0);
+
+            value.Background = brush;
+        }
+
+
+        return totalRostered.IsEffectivelyEqual(0.0);
+    }
+
+    private static double GetTotalRostered(EmployeeResourceModel employee, DateTime from, DateTime to)
+    {
         var totalRostered = 0.0;
 
         for(var date = from; date < to; date = date.AddDays(1))
@@ -350,25 +422,9 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
             }
         }
 
-        var color1 = Colors.LightGray;
-        var color2 = Colors.LightYellow;
-
-        var percent = Math.Min(totalRostered / standardHours, 1.0);
-
-        value.Foreground = new SolidColorBrush(ImageUtils.MixColors(color1, 1 - percent, color2)) { Opacity = 0.8 };
-
-        var brush = new LinearGradientBrush { Opacity = 0.8 };
-        brush.StartPoint = new Point(0,0);
-        brush.GradientStops.Add(new GradientStop(Colors.LightYellow, 0.0));
-        brush.GradientStops.Add(new GradientStop(ImageUtils.MixColors(Colors.LightYellow, percent, Colors.LightGray), percent));
-        brush.GradientStops.Add(new GradientStop(Colors.LightGray, 1.0));
-        brush.EndPoint = new Point(1,0);
-
-        value.Background = brush;
-
-        return totalRostered.IsEffectivelyEqual(0.0);
+        return totalRostered;
     }
-    
+
     private static EmployeeResourcePlannerValue? GetStandardLeave(DateTime curdate, StandardLeaveModel[] standardleaves)
     {
         var standardleave = standardleaves.FirstOrDefault(x => 
@@ -376,7 +432,12 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
             && (x.To.Add(x.ToTime) > curdate)
         );
         return (standardleave != null)
-            ? new EmployeeResourcePlannerValue() { Text = standardleave.Code, Color = standardleave.Color}
+            ? new EmployeeResourcePlannerValue()
+            {
+                Text = standardleave.Code ?? string.Empty, 
+                Background = new SolidColorBrush(ParseColor(standardleave.Color, Colors.Transparent).AdjustLightness(20)) { Opacity = 0.8 },
+                Foreground = new SolidColorBrush(ParseColor(standardleave.Color, Colors.Transparent).AdjustLightness(20).GetForegroundColor())
+            }
             : null;
     }
 
@@ -385,7 +446,8 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
         if (leavevalue == null)
             return false;
         value.Text = leavevalue.Text;
-        value.Color = leavevalue.Color;
+        value.Background = leavevalue.Background;
+        value.Foreground = leavevalue.Foreground;
         return true;
     }
     
@@ -400,7 +462,11 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
         if (leaverequest == null)
             return false;
         value.Text = leaverequest.Code;
-        value.Color = (leaverequest.Status == LeaveRequestStatus.Approved) ? leaverequest.Color : Colors.DimGray.ToString();
+        var c = leaverequest.Status == LeaveRequestStatus.Approved
+            ? ParseColor(leaverequest.Color, Colors.Transparent)
+            : Colors.DimGray;
+        value.Background = new SolidColorBrush(c.AdjustLightness(20));
+        value.Foreground = new SolidColorBrush(c.AdjustLightness(20).GetForegroundColor());
         return true;
     }
     
@@ -627,7 +693,7 @@ public partial class EmployeeResourcePlanner : UserControl, IPropertiesPanel<Emp
                         var textBlock = new TextBlock();
                         textBlock.Inlines.Add(new Run
                         {
-                            Text = assignment.JobNumber,
+                            Text = String.IsNullOrWhiteSpace(assignment.JobNumber) ? "(No Job)" : assignment.JobNumber,
                             FontWeight = FontWeights.Bold
                         });
                         textBlock.Inlines.Add(new Run