Exemplo n.º 1
0
        /// <summary>
        /// Starts a new session for employee, returns bool val for success state
        /// </summary>
        /// <param name="EmployeeID"></param>
        /// <returns></returns>
        public bool StartSession(int EmployeeID, int SalaryId)
        {
            using (var ctx = new EmployeesContext())

            {
                //Query here to be in the same context
                EmployeeModel Employee   = ctx.Employees.Find(EmployeeID);
                var           Job        = ctx.JobModels.Find(Employee.JobId);
                var           query      = ctx.Salaries.Where(salary => salary.Jobs.Any(job => job.JobName == Job.JobName)).ToList();
                var           collection = new ObservableCollection <Salary>(query);

                if (Employee != null)
                {
                    //Sets clocked in time to now
                    Employee.ClockedInTime = TimeSpan.Parse(DateTime.Now.ToString("HH:mm:ss"));
                    //Find employee's salary object
                    var indicatedSalary = collection.Where(x => x.SalaryId == SalaryId).First();
                    //Set current salary object to the one found by indicatedSalary
                    Employee.CurrentSalary = indicatedSalary;
                    Employee.IsClockedIn   = true;


                    ctx.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Ends a current session, by replacing the clock in time, calculating the hours worked, and inputting that into Employee's totalhours
        /// </summary>
        /// <remarks>
        /// NOTE THAT HOURS ARE NOT ROUNDED AND ANY VALUES NOT IN HOUR'S PLACE ARE TRUNCATED
        /// </remarks>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool EndSession(int id)
        {
            using (var ctx = new EmployeesContext())
            {
                EmployeeModel Employee = ctx.Employees
                                         .Include(x => x.CurrentSalary)
                                         .Where(x => x.EmployeeId == id).First();


                var currentTime = TimeSpan.Parse(DateTime.Now.ToString("HH:mm:ss"));
                if (Employee != null && Employee.ClockedInTime != null)
                {
                    if (Employee.ClockedInTime <= currentTime)
                    {
                        //Calculate time worked in hours
                        TimeSpan TimeWorked = ((TimeSpan)(currentTime - Employee.ClockedInTime));

                        //Add hours worked to employee's record
                        Employee.HoursWorked += TimeWorked.Hours;

                        //Reset employee's clocked in time, note that ClockedInTime is null, as datetime will be assigned by StartSession()
                        Employee.ClockedInTime = null;

                        //Calculate pay for session
                        double TotalPay = (TimeWorked.Hours * Employee.CurrentSalary.PayPerHour);

                        //Assign pay for session to Employee Records
                        Employee.TotalPay += TotalPay;

                        //Remove CurrentSalary to be null
                        Employee.CurrentSalary = null;

                        Employee.IsClockedIn = false;
                        ctx.SaveChanges();
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
        }