/// <summary>
        ///  CREATOR: Kaleb Bachert
        ///  CREATED: 2020/4/15
        ///  APPROVER: Lane Sandburg
        ///
        ///  This method retrieves a User's scheduled hours for a given Schedule
        /// </summary>
        /// <remarks>
        /// UPDATER: NA
        /// UPDATED: NA
        /// UPDATE: NA
        ///
        /// </remarks>
        /// <param name="userID"></param>
        /// <param name="dateInSchedule"></param>
        public ScheduleWithHoursWorked SelectScheduleHoursByUserAndDate(int userID, DateTime dateInSchedule)
        {
            ScheduleWithHoursWorked scheduleHours = new ScheduleWithHoursWorked();

            var conn = DBConnection.GetConnection();
            var cmd  = new SqlCommand("sp_select_schedule_hours_by_user_and_date", conn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("UserID", userID);
            cmd.Parameters.AddWithValue("DateInSchedule", dateInSchedule);

            try
            {
                conn.Open();
                var reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    if (reader.Read())
                    {
                        scheduleHours.ScheduleID        = reader.GetInt32(0);
                        scheduleHours.ScheduleStartDate = reader.GetDateTime(1);
                        scheduleHours.ScheduleEndDate   = reader.GetDateTime(2);
                        scheduleHours.FirstWeekHours    = Convert.ToDouble(reader.GetDecimal(3));
                        scheduleHours.SecondWeekHours   = Convert.ToDouble(reader.GetDecimal(4));
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }

            return(scheduleHours);
        }
Esempio n. 2
0
        /// <summary>
        /// Creator: Kaleb Bachert
        /// Created: 4/28/2020
        /// Approver:
        ///
        /// Updates the Shift in the Database if a User was Selected
        /// </summary>
        /// <remarks>
        /// Updater: NA
        /// Updated: NA
        /// Update: NA
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            //No Replacement Employee is selected
            if (cmbShiftReplacementEmployees.SelectedItem.Equals("-- Select a Replacement --"))
            {
                WPFErrorHandler.ErrorMessage("You must first select a replacement Employee. If no replacement Employees appear, none fit the criteria for replacement.", "Input");
            }
            //Replacement Employee is selected
            else
            {
                //The date, startTime and endTime of the shift, and the length of the shift in hours
                DateTime shiftDate = Convert.ToDateTime(txtShiftDate.Text);
                TimeSpan startTime = TimeSpan.Parse(txtShiftStartTime.Text);
                TimeSpan endTime   = TimeSpan.Parse(txtShiftEndTime.Text);
                double   shiftHours;

                //If the shift stays in a single day
                if (endTime > startTime)
                {
                    shiftHours = (endTime - startTime).TotalHours;
                }
                //The Shift starts the previous night, and ends in the morning
                else
                {
                    //Last second of the night
                    TimeSpan dayEnd = new TimeSpan(23, 59, 59);
                    //First second of the morning
                    TimeSpan dayStart = new TimeSpan(0, 0, 0);

                    //Get difference between end of shift and start of day, add it to difference between start of shift and end of night
                    shiftHours = (endTime - dayStart).TotalHours + (dayEnd - startTime).TotalHours;
                }

                try
                {
                    //Converts selected Email (replacement User) to a PetUniverseUser
                    PetUniverseUser replacementUser = _userManager.getUserByEmail(cmbShiftReplacementEmployees.SelectedItem.ToString());

                    //Gets the start and end date of the schedule the shift is in, used to determine the hours worked that week
                    ScheduleWithHoursWorked scheduleContainingTheShift = _shiftManager.RetrieveScheduleHoursByUserAndDate(replacementUser.PUUserID, shiftDate);

                    //This will be the hours worked if the Shift change is confirmed
                    double projectedHoursWorked;

                    //The week of the schedule that this shift is in, 1 or 2
                    int weekNumber;

                    //If shiftDate is before the second Sunday of the Schedule
                    if (shiftDate < scheduleContainingTheShift.ScheduleStartDate.AddDays(7))
                    {
                        projectedHoursWorked = scheduleContainingTheShift.FirstWeekHours + shiftHours;
                        weekNumber           = 1;
                    }
                    //ShiftDate is on or after the second Sunday of the Schedule
                    else
                    {
                        projectedHoursWorked = scheduleContainingTheShift.SecondWeekHours + shiftHours;
                        weekNumber           = 2;
                    }

                    MessageBoxResult confirmApproval;

                    //The User will have more than 40 hours that week if this is approved
                    if (projectedHoursWorked > 40)
                    {
                        confirmApproval = MessageBox.Show("This change will set " + replacementUser.FirstName.ToUpper() + " " + replacementUser.LastName.ToUpper() +
                                                          "'s" + Environment.NewLine + "hours for the week to: " + projectedHoursWorked + Environment.NewLine +
                                                          "Are you certain you want to commit this schedule change?" + Environment.NewLine + "THIS ACTION CANNOT BE UNDONE!",
                                                          "Confirm Approval?", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    }
                    //Hours will not go over 40
                    else
                    {
                        confirmApproval = MessageBox.Show("Are you certain you want to give " + replacementUser.FirstName.ToUpper() + " " + replacementUser.LastName.ToUpper() +
                                                          " this shift?" + Environment.NewLine + "THIS ACTION CANNOT BE UNDONE!", "Confirm Approval?", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    }

                    //Shift change Confirmed and ready to be replaced with the selected replacement Employee
                    if (confirmApproval == MessageBoxResult.Yes)
                    {
                        int originalEmployeeID = _shiftVm.EmployeeID;

                        //Decrease old employee's hours worked, increase the new employee's hours, save the new employee in the Shift's User field
                        _shiftManager.EditEmployeeHoursWorked(originalEmployeeID, scheduleContainingTheShift.ScheduleID, weekNumber, shiftHours * -1);
                        _shiftManager.EditEmployeeHoursWorked(replacementUser.PUUserID, scheduleContainingTheShift.ScheduleID, weekNumber, shiftHours);
                        _shiftManager.EditShiftUserWorking(_shiftVm.ShiftID, replacementUser.PUUserID, originalEmployeeID);

                        WPFErrorHandler.SuccessMessage("Shift Change Successful!");

                        //Return to View Schedule page
                        this.DialogResult = true;
                    }
                }
                catch (Exception ex)
                {
                    WPFErrorHandler.ErrorMessage(ex.InnerException.Message, ex.Message);
                    this.Close();
                }
            }
        }