worktimehero

git clone https://git.clttr.info/worktimehero.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

WorkTimeCalculator.cs (5513B)


      1 // BSD 3-Clause License
      2 // Copyright(c) 2018-2020 René Wagner
      3 // https://git.sr.ht/~rwa/worktimehero
      4 
      5 using System;
      6 
      7 namespace WorkTimeHero
      8 {
      9     public class WorkTimeCalculator
     10     {
     11         public bool NotificationShown
     12         {
     13             get;
     14             set;
     15         }
     16 
     17         public DateTime StartTime
     18         {
     19             get;
     20             private set;
     21         }
     22 
     23         /// <summary>
     24         /// Anwesenheitszeit
     25         /// </summary>
     26         public TimeSpan BruttoTime
     27         {
     28             get
     29             {
     30                 if (StartTime.TimeOfDay > DateTime.Now.TimeOfDay)
     31                 {
     32                     return new TimeSpan();
     33                 }
     34 
     35                 return new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0) - StartTime.TimeOfDay;
     36             }
     37         }
     38 
     39         /// <summary>
     40         /// Sollzeit
     41         /// </summary>
     42         public TimeSpan TargetTime
     43         {
     44             get;
     45             set;
     46         }
     47 
     48         /// <summary>
     49         /// minimale Anwesenheitszeit für Sollzeit
     50         /// </summary>
     51         public TimeSpan TimeDue
     52         {
     53             get
     54             {
     55                 return StartTime.TimeOfDay + TargetTime + PlannedBreakTime;
     56             }
     57         }
     58 
     59         /// <summary>
     60         /// Erledigte Arbeitszeit
     61         /// </summary>
     62         public TimeSpan DoneWorkTime
     63         {
     64             get
     65             {
     66                 TimeSpan nettoTime = BruttoTime - EffectiveBreakTime;
     67                 if (nettoTime.TotalMinutes < 0) { return TimeSpan.Zero; }
     68                 return nettoTime;
     69             }
     70         }
     71 
     72         /// <summary>
     73         /// Pausenzeit für Sollzeit
     74         /// </summary>
     75         public TimeSpan PlannedBreakTime
     76         {
     77             get
     78             {
     79                 if (IndividualBreakTime.Date == DateTime.Now.Date)
     80                 {
     81                     return IndividualBreakTime.TimeOfDay;
     82                 }
     83 
     84                 return TargetTime.CalculateBreak();
     85             }
     86         }
     87 
     88         /// <summary>
     89         /// aktueller, effektiver Pausenabzug
     90         /// </summary>
     91         public TimeSpan EffectiveBreakTime
     92         {
     93             get
     94             {
     95                 if (IndividualBreakTime.Date == DateTime.Now.Date)
     96                 {
     97                     return IndividualBreakTime.TimeOfDay + CurrentBreakTime;
     98                 }
     99 
    100                 return BruttoTime.CalculateBreak();
    101             }
    102         }
    103 
    104         /// <summary>
    105         /// Überstunden
    106         /// </summary>
    107         public TimeSpan AdditionalWorkTime
    108         {
    109             get
    110             {
    111                 TimeSpan additionalWorkTime = DoneWorkTime - TargetTime;
    112                 return additionalWorkTime < new TimeSpan() ? new TimeSpan() : additionalWorkTime;
    113             }
    114         }
    115 
    116         /// <summary>
    117         /// Verbleibende Arbeitszeit
    118         /// </summary>
    119         public TimeSpan RemainingWorkTime
    120         {
    121             get
    122             {
    123                 return TimeDue - DateTime.Now.TimeOfDay;
    124             }
    125         }
    126 
    127         public int WorkTimePercentage
    128         {
    129             get
    130             {
    131                 int percentage = Convert.ToInt32((BruttoTime.TotalMinutes / (TargetTime.TotalMinutes + PlannedBreakTime.TotalMinutes)) * 100);
    132                 return percentage > 100 ? 100 : percentage;
    133             }
    134         }
    135 
    136         /// <summary>
    137         /// individuelle Pausenzeit
    138         /// <summary>
    139         public DateTime IndividualBreakTime
    140         {
    141             get;
    142             set;
    143         }
    144 
    145         /// <summary>
    146         /// Startzeit der aktuellen Pause
    147         /// <summary>
    148         public TimeSpan CurrentBreakStartTime
    149         {
    150             get;
    151             set;
    152         }
    153 
    154         /// <summary>
    155         /// Dauer der aktuellen Pause
    156         /// <summary>
    157         private TimeSpan CurrentBreakTime
    158         {
    159             get
    160             {
    161                 if (IsInBreak)
    162                 {
    163                     return DateTime.Now.TimeOfDay - CurrentBreakStartTime;
    164                 }
    165 
    166                 return TimeSpan.Zero;
    167             }
    168         }
    169 
    170         public bool IsInBreak
    171         {
    172             get
    173             {
    174                 if (CurrentBreakStartTime == TimeSpan.Zero) { return false; }
    175                 return true;
    176             }
    177         }
    178 
    179 
    180         public void StartBreak()
    181         {
    182             CurrentBreakStartTime = DateTime.Now.TimeOfDay;
    183         }
    184 
    185         public void StopBreak()
    186         {
    187             double breakTimeInMinutes = DateTime.Now.TimeOfDay.TotalMinutes - CurrentBreakStartTime.TotalMinutes; ;
    188             if (IndividualBreakTime.Date == DateTime.Now.Date)
    189             {
    190                 IndividualBreakTime = IndividualBreakTime.AddMinutes(breakTimeInMinutes);
    191             }
    192             else
    193             {
    194                 IndividualBreakTime = DateTime.Now.Date.AddMinutes(breakTimeInMinutes);
    195             }
    196             CurrentBreakStartTime = TimeSpan.Zero;
    197         }
    198 
    199         public WorkTimeCalculator(DateTime startTime, TimeSpan plannedWorkTime, DateTime individualBreakTime, TimeSpan currentBreakStartTime)
    200         {
    201             StartTime = startTime;
    202             TargetTime = plannedWorkTime;
    203             IndividualBreakTime = individualBreakTime;
    204             CurrentBreakStartTime = currentBreakStartTime;
    205             NotificationShown = false;
    206         }
    207     }
    208 }