private void calculateClaimButton_Click(object sender, System.EventArgs e)
 {
     try
     {
         ClaimMatrixDictionary claimMatrix = CalculateClaim();
         //claimMatrix.Keys.Sort();
         WeekDayHoursDictionary weekDayTotal = new WeekDayHoursDictionary();
         // getting result
         ClaimDataForm resultForm = new ClaimDataForm();
         string maxString = "";
         foreach (ClaimCode claimCode in claimMatrix.Keys)
         {
             ListViewItem currItem = resultForm.ClaimListView.Items.Add(claimCode.ToString());
             currItem.SubItems.AddRange(new string[] {"", "", "", "", "", "", ""});
             if (claimCode.ToString().Length > maxString.Length)
                 maxString = claimCode.ToString();
             foreach (DayOfWeek weekDay in claimMatrix[claimCode].Keys)
             {
                 currItem.SubItems[(Convert.ToInt32(weekDay) + 1 - (int)((Convert.ToInt32(weekDay) + 1) / 7) * 7) + 1].Text = claimMatrix[claimCode][weekDay].ToString(); //Math.Round(claimMatrix[claimCode][weekDay], 1).ToString();
                 weekDayTotal[weekDay] += claimMatrix[claimCode][weekDay];
             }
         }
         ListViewItem totalItem = resultForm.ClaimListView.Items.Add("Total");
         totalItem.Font = new Font(totalItem.Font, FontStyle.Bold);
         totalItem.SubItems.AddRange(new string[]
         {
             weekDayTotal[DayOfWeek.Saturday].ToString(),
             weekDayTotal[DayOfWeek.Sunday].ToString(),
             weekDayTotal[DayOfWeek.Monday].ToString(),
             weekDayTotal[DayOfWeek.Tuesday].ToString(),
             weekDayTotal[DayOfWeek.Wednesday].ToString(),
             weekDayTotal[DayOfWeek.Thursday].ToString(),
             weekDayTotal[DayOfWeek.Friday].ToString()
         });
         resultForm.columnClaimCode.Width = (int)Graphics.FromImage(new Bitmap(1, 1)).MeasureString(maxString, resultForm.ClaimListView.Items[0].Font).Width;
         resultForm.Text = String.Format(Properties.Resources.ClaimResultWindowCaption, _startDay.ToShortDateString(), _startDay.AddDays(6).ToShortDateString());
         resultForm.Show();
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message, "Claim calculation error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
 private WeekDayHoursDictionary PrepareStandByHours(OncallDateStruct[] OncallDays)
 {
     WeekDayHoursDictionary tmp = new WeekDayHoursDictionary();
     if (OncallDays != null)
     {
         foreach (OncallDateStruct oncallDay in OncallDays)
         {
             tmp[oncallDay.Date.DayOfWeek] = oncallDay.OncallHours * 60;
         }
     }
     return tmp;
 }
        private ClaimMatrixDictionary CalculateClaim()
        {
            double tmp;
            WeekDayHoursDictionary officeWorkHours = new WeekDayHoursDictionary();
            ClaimMatrixDictionary claimHours = new ClaimMatrixDictionary();
            ClaimCode defaultClaimCode = _service.GetDefaultClaimCode(_settings["AdminID"], _clientVersion);
            OncallDateStruct[] oncallDays = _service.GetOncallSchedule(_settings["AdminID"], _clientVersion, _startDay, _startDay.AddDays(6));
            ClaimCode[] oncallStandByGroups = PrepareStandByOncallGroups(oncallDays != null);
            WeekDayHoursDictionary standByHours = PrepareStandByHours(oncallDays);
            DateTime[] publicHolidays = _service.GetPublicHolidays();

            if (_claimData != null)
            {
                foreach (ClaimStruct claimItem in _claimData)
                {
                    DayOfWeek weekDay = claimItem.Activity.StartTime.DayOfWeek;
                    double workDuration = claimItem.Activity.WorkDuration; //(float)Math.Round(claimItem.Activity.WorkDuration / 60F, 2);
                    ClaimCode claimCode = claimItem.ClaimCode;

                    if (claimCode.Overtime)
                    {
                        if (claimItem.PlannedOvertime)
                        {
                            if (!claimHours.Contains(claimCode))
                                claimHours.Add(claimCode);
                            claimHours[claimCode][weekDay] += workDuration;
                        }
                        else
                        {
                            if (workDuration > standByHours[weekDay])
                                throw new Exception("Oncall work duration is more oncall hours for this day (" + claimItem.Activity.StartTime.ToShortDateString() + ")");
                            standByHours[weekDay] -= workDuration;
                            claimCode.CalledIn = true;
                            if (!claimHours.Contains(claimCode))
                                claimHours.Add(claimCode);
                            tmp = claimHours[claimCode][weekDay];
                            claimHours[claimCode][weekDay] += workDuration; // * ONCALL_WORK; //(float)Math.Round(workDuration * ONCALL_WORK, 2);
                            tmp = claimHours[claimCode][weekDay];
                        }
                    }
                    else
                    {
                        officeWorkHours[weekDay] += workDuration;
            #if DEBUG
                        System.Diagnostics.Debug.WriteLine(weekDay + "\t" + officeWorkHours[weekDay]);
            #endif

                        if (!claimHours.Contains(claimCode))
                            claimHours.Add(claimCode);
                        claimHours[claimCode][weekDay] += workDuration;
            #if DEBUG
                        tmp = claimHours[claimCode][weekDay];
                        System.Diagnostics.Debug.WriteLine(claimCode + "\t" + weekDay + "\t" + claimHours[claimCode][weekDay]);
            #endif
                    }
                }
            }

            // rounding to 8 hours per day
            foreach (DayOfWeek weekDay in officeWorkHours.Keys)
            {
                if ((weekDay == DayOfWeek.Saturday) || (weekDay == DayOfWeek.Sunday) ||
                    IsPublicHoliday(weekDay, publicHolidays))
                    continue;
            #if DEBUG
                tmp = claimHours[defaultClaimCode] != null ? claimHours[defaultClaimCode][weekDay] : 0;
                tmp = officeWorkHours[weekDay];
            #endif
                double dayWorkDurationDifference = 480 - officeWorkHours[weekDay]; //(float)Math.Round(8 - officeWorkHours[weekDay], 2);
                if (dayWorkDurationDifference != 0)
                {
                    if (!claimHours.Contains(defaultClaimCode))
                        claimHours.Add(defaultClaimCode);
                    claimHours[defaultClaimCode][weekDay] = claimHours[defaultClaimCode][weekDay] + dayWorkDurationDifference; //(float)Math.Round(claimHours[defaultClaimCode][weekDay] + dayWorkDurationDifference, 1);
                }
            #if DEBUG
                tmp = claimHours[defaultClaimCode][weekDay];
            #endif
            }

            // adding standby oncall hours
            foreach (DayOfWeek weekDay in standByHours.Keys)
            {
                if (standByHours[weekDay] == 0)
                    continue;
                tmp = standByHours[weekDay];
                double standByHoursPerGroup = standByHours[weekDay] / oncallStandByGroups.Length; //Math.Round(standByHours[weekDay] / oncallStandByGroups.Length, 1);
                ClaimCode firstStandByClaimCode = null;
                //				float tmp = standByHours[weekDay];
                //				tmp = tmp * 0.123F;
                //				tmp = (float)Math.Round(tmp, 1);
                double standByHoursRemain = standByHours[weekDay]; // Math.Round(standByHours[weekDay] /60 * ONCALL_STANDBY, 1) * 60; //Math.Round(standByHours[weekDay] * ONCALL_STANDBY, 1);
                foreach (ClaimCode standByClaimCode in oncallStandByGroups)
                {
                    if (!claimHours.Contains(standByClaimCode))
                        claimHours.Add(standByClaimCode);
                    if (firstStandByClaimCode == null)
                    {
                        firstStandByClaimCode = standByClaimCode;
                    }
                    else
                    {
                        double hoursToClaim = standByHoursPerGroup; // * ONCALL_STANDBY; //Math.Round(standByHoursPerGroup * ONCALL_STANDBY, 1);
                        standByHoursRemain -= Math.Round(hoursToClaim / 60, 1) * 60;
                        tmp = claimHours[standByClaimCode][weekDay];
                        claimHours[standByClaimCode][weekDay] += hoursToClaim;
                        tmp = claimHours[standByClaimCode][weekDay];
                    }
                }
            #if DEBUG
                tmp = claimHours[firstStandByClaimCode][weekDay];
            #endif
                claimHours[firstStandByClaimCode][weekDay] += standByHoursRemain;
            #if DEBUG
                tmp = claimHours[firstStandByClaimCode][weekDay];
            #endif
            }

            //string testResult = "";
            foreach (ClaimCode code in claimHours.Keys)
            {
                //testResult += code.ToString();
                //System.Diagnostics.Debug.Write(code.ToString());
                foreach (DayOfWeek weekDay in claimHours[code].Keys)
                {
                    //testResult += "\t" + claimHours[code][weekDay].ToString();
                    //System.Diagnostics.Debug.Write("\t" + Math.Round(claimHours[code][weekDay] / 60, 1).ToString());
                    claimHours[code][weekDay] = Math.Round(claimHours[code][weekDay] / 60, 1);
                }
                //testResult += "\n";
                //System.Diagnostics.Debug.WriteLine("");
            }
            return claimHours;
        }