private static decimal Calc(WorkSchedule obj, DateTime from, DateTime until, CalcModes what) { decimal result = 0; var rules = obj.RulesAndParents(); while (from <= until) { WorkScheduleRule foundRule = null; // Find YearlyRule foundRule = rules.OfType<YearlyWorkScheduleRule>().FirstOrDefault(r => r.AppliesTo(from)); // Find DayOfWeekRule if (foundRule == null) { foundRule = rules.OfType<DayOfWeekWorkScheduleRule>().FirstOrDefault(r => r.AppliesTo(from)); } // Find CommonRule if (foundRule == null) { foundRule = rules.OfType<CommonWorkScheduleRule>().FirstOrDefault(r => r.AppliesTo(from)); } if (foundRule != null) { switch (what) { case CalcModes.WorkingHours: result += foundRule.WorkingHours; break; case CalcModes.WorkingDays: if (foundRule.IsWorkingDay) result++; break; case CalcModes.OffDays: if (!foundRule.IsWorkingDay) result++; break; } } else { Logging.Log.WarnFormat("No Calendar rule found in {0} for {1}", obj.Name, from); } from = from.AddDays(1); } return result; }