private void butGenerate_Click(object sender, EventArgs e) { //Generate payperiods based on settings if (numPayPeriods.errorProvider1.GetError(numPayPeriods) != "") { MsgBox.Show(this, numPayPeriods.errorProvider1.GetError(numPayPeriods)); return; } if (numDaysAfterPayPeriod.Enabled && numDaysAfterPayPeriod.errorProvider1.GetError(numDaysAfterPayPeriod) != "") { MsgBox.Show(this, numDaysAfterPayPeriod.errorProvider1.GetError(numDaysAfterPayPeriod)); return; } if (numDaysAfterPayPeriod.Enabled && numDaysAfterPayPeriod.Text == "0") { MsgBox.Show(this, "# Days After Pay Period cannot be zero."); return; } _listPayPeriods.Clear(); int numPeriods = PIn.Int(numPayPeriods.Text); PayPeriodInterval payPeriodInterval = PayPeriodInterval.Weekly; if (radioBiWeekly.Checked) { payPeriodInterval = PayPeriodInterval.BiWeekly; } else if (radioMonthly.Checked) { payPeriodInterval = PayPeriodInterval.Monthly; } DateTime startDate = dateTimeStart.Value; //Original value for (int i = 0; i < numPeriods; i++) { PayPeriod payPeriod = new PayPeriod(); payPeriod.DateStart = startDate; //Make PayDate information switch (payPeriodInterval) //Add time to "startDate" to get the new start date for the next iteration as well as figuring out the end date for current payperiod. { case PayPeriodInterval.Weekly: payPeriod.DateStop = startDate.AddDays(6); startDate = startDate.AddDays(7); break; case PayPeriodInterval.BiWeekly: payPeriod.DateStop = startDate.AddDays(13); startDate = startDate.AddDays(14); break; case PayPeriodInterval.Monthly: payPeriod.DateStop = startDate.AddMonths(1).Subtract(TimeSpan.FromDays(1)); startDate = startDate.AddMonths(1); break; } if (comboDay.Enabled) { //Find the closest day specified after the end of the pay period. payPeriod.DatePaycheck = GetDateOfDay(payPeriod.DateStop, (DayOfWeek)(comboDay.SelectedIndex - 1)); } else //# days specified, use "Exclude Weekends" checkbox as well as "Pay Before" and "Pay After" buttons. { payPeriod.DatePaycheck = payPeriod.DateStop.AddDays(PIn.Int(numDaysAfterPayPeriod.Text)); if (payPeriod.DatePaycheck.DayOfWeek == DayOfWeek.Saturday && checkExcludeWeekends.Checked) { if (radioPayBefore.Checked) { if (payPeriod.DatePaycheck.Subtract(TimeSpan.FromDays(1)) <= payPeriod.DateStop) //Can't move the paycheck date to the same day (or before) than the date end. { payPeriod.DatePaycheck = payPeriod.DatePaycheck.Add(TimeSpan.FromDays(2)); //Move it forward to monday } else { payPeriod.DatePaycheck = payPeriod.DatePaycheck.Subtract(TimeSpan.FromDays(1)); //Move it back to friday } } else //radioPayAfter { payPeriod.DatePaycheck = payPeriod.DatePaycheck.Add(TimeSpan.FromDays(2)); //Move it forward to monday } } else if (payPeriod.DatePaycheck.DayOfWeek == DayOfWeek.Sunday && checkExcludeWeekends.Checked) { if (radioPayBefore.Checked) { if (payPeriod.DatePaycheck.Subtract(TimeSpan.FromDays(2)) <= payPeriod.DateStop) //Can't move the paycheck date to the same day (or before) than the date end. { payPeriod.DatePaycheck = payPeriod.DatePaycheck.Add(TimeSpan.FromDays(1)); //Move it forward to monday } else { payPeriod.DatePaycheck = payPeriod.DatePaycheck.Subtract(TimeSpan.FromDays(2)); //Move it back to friday } } else //radioPayAfter { payPeriod.DatePaycheck = payPeriod.DatePaycheck.Add(TimeSpan.FromDays(1)); //Move it forward to monday } } } _listPayPeriods.Add(payPeriod); } FillGrid(); }
private void FormPayPeriodManager_Load(object sender, System.EventArgs e) { PayPeriod payPeriod = PayPeriods.GetMostRecent(); if (payPeriod == null) { dateTimeStart.Value = DateTime.Today; } else { dateTimeStart.Value = payPeriod.DateStop.AddDays(1); } PayPeriodInterval payPeriodInterval = (PayPeriodInterval)PrefC.GetInt(PrefName.PayPeriodIntervalSetting); if (payPeriodInterval == PayPeriodInterval.Weekly) { radioWeekly.Checked = true; numPayPeriods.Text = "52"; } else if (payPeriodInterval == PayPeriodInterval.BiWeekly) { radioBiWeekly.Checked = true; numPayPeriods.Text = "21"; } else if (payPeriodInterval == PayPeriodInterval.Monthly) { radioMonthly.Checked = true; numPayPeriods.Text = "12"; } int dayOfWeek = PrefC.GetInt(PrefName.PayPeriodPayDay); if (dayOfWeek != 0) //They have a day of the week selected { comboDay.SelectedIndex = dayOfWeek; numDaysAfterPayPeriod.Enabled = false; checkExcludeWeekends.Enabled = false; radioPayBefore.Enabled = false; radioPayAfter.Enabled = false; } else { comboDay.SelectedIndex = 0; numDaysAfterPayPeriod.Text = PrefC.GetString(PrefName.PayPeriodPayAfterNumberOfDays); checkExcludeWeekends.Checked = PrefC.GetBool(PrefName.PayPeriodPayDateExcludesWeekends); if (checkExcludeWeekends.Checked) { if (PrefC.GetBool(PrefName.PayPeriodPayDateBeforeWeekend)) { radioPayBefore.Checked = true; } else { radioPayAfter.Checked = true; } } if (!checkExcludeWeekends.Checked) { radioPayBefore.Checked = false; radioPayBefore.Enabled = false; radioPayAfter.Checked = false; radioPayAfter.Enabled = false; } else { radioPayBefore.Enabled = true; radioPayAfter.Enabled = true; } } _listPayPeriods = new List <PayPeriod>(); FillGrid(); }