/// <summary>
 /// Gets the last weeks worth of shifts for the current user.
 /// </summary>
 /// <param name="sessionID">The sessionID of the current session</param>
 /// <returns></returns>
 public List<ShiftData> GetLastWeeksShifts(string sessionID)
 {
     WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
     var lastWeekShifts = wta.GetDataByEmployeeID(Auth.getEmployeeID(sessionID), DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)));
     List<ShiftData> re = new List<ShiftData>();
     foreach (var shift in lastWeekShifts)
     {
         ShiftData sd = new ShiftData();
         sd.EmployeeID = shift.EmployeeID;
         if (shift.IsEndTimeNull()) sd.EndTime = null;
         else
             sd.EndTime = shift.EndTime;
         sd.StartTime = shift.StartTime;
         sd.Role = shift.Role;
         sd.ShiftID = shift.ID;
         if (sd.EndTime != null)
         {
             sd.HoursWorked = (decimal)sd.EndTime.Value.Subtract(sd.StartTime).TotalHours;
         }
         else
         {
             sd.HoursWorked = 0;
         }
         re.Add(sd);
     }
     return re;
 }
 /// <summary>
 /// Determines whether this instance can stamp out the current user.
 /// </summary>
 /// <param name="sessionID">The sessionID of the current session</param>
 /// <returns>
 /// 	<c>true</c> if this instance can stamp out the current user; otherwise, <c>false</c>.
 /// </returns>
 public bool CanStampOut(string sessionID)
 {
     WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
     var lastStartTime =  wta.GetDataByEmployeeID(Auth.getEmployeeID(sessionID), DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0))).FirstOrDefault();
     if (lastStartTime == null || !lastStartTime.IsEndTimeNull())
     {
         return false;
     }
     return true;
 }
 /// <summary>
 /// Adds a new shift.
 /// </summary>
 /// <param name="sessionID">The sessionID of the current session</param>
 /// <param name="EmployeeID">The employee ID of the employee who's shift this is.</param>
 /// <param name="role">The role for the shift.</param>
 /// <param name="start">The start time of the shift.</param>
 /// <param name="end">The end time of the shift.</param>
 public void AddShift(string sessionID, int EmployeeID, string role, DateTime start, DateTime end)
 {
     WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
     wta.Insert(start, end, role, EmployeeID);
 }
        /// <summary>
        /// Gets the employee payout.
        /// </summary>
        /// <param name="employee">The employee.</param>
        /// <param name="start">The start date and time </param>
        /// <param name="end">The end date and time</param>
        /// <param name="payoutTable">The payout table.</param>
        /// <returns></returns>
        private EmployeePayout GetEmployeePayout(
            CRySTALDataConnections.CRySTALDataSet.UsersRow employee, 
            DateTime start, DateTime end, 
            CRySTALDataConnections.CRySTALDataSet.RolePayrateDataTable payoutTable)
        {
            WorkerShiftsTableAdapter wsa = new WorkerShiftsTableAdapter();
            var shifts = wsa.GetDataByEmployeeIDInTimeFrame(employee.ID, end, start);
            if (shifts.Count == 0) return null;

            EmployeePayout re = new EmployeePayout();
            re.EmployeeID = employee.ID;
            re.EmployeeName = employee.LastName + ", " + employee.FirstName;
            re.Roles = new List<RolePayout>();
            List<string> roles = (from p in shifts select p.Role).Distinct().ToList();
            decimal payoutTotal = 0;
            foreach (string role in roles)
            {
                RolePayout rp = new RolePayout();
                rp.HoursWorked = (decimal)(from p in shifts
                                  where (!p.IsEndTimeNull() && p.Role==role)
                                  select p.EndTime.Subtract(p.StartTime).TotalHours).Sum();
                rp.Rate = (from p in payoutTable where p.Role == role select p.PayPerHour).FirstOrDefault();
                rp.RoleName = role;
                rp.TotalPayment = rp.HoursWorked * rp.Rate;
                re.Roles.Add(rp);
                payoutTotal += rp.TotalPayment;
            }
            re.TotalPayment = payoutTotal;
            return re;
        }
 public void RemoveShift(string sessionID, int shiftNumber)
 {
     WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
     wta.DeleteRow(shiftNumber);
 }
        /// <summary>
        /// Gets the statistics.
        /// </summary>
        /// <param name="sessionID">The sessionID of the current session</param>
        /// <param name="stat">The stat.</param>
        /// <param name="typeOfStat">The type of stat.</param>
        /// <param name="productID">The product ID.</param>
        /// <param name="start">The start date and time </param>
        /// <param name="end">The end date and time</param>
        /// <returns></returns>
        public List<double> GetStatistics(string sessionID, StatObject stat, StatType typeOfStat, int productID, DateTime start, DateTime end)
        {
            if (stat == StatObject.Income)
            {
                if (typeOfStat == StatType.ForEachDay)
                {

                    BillItemsTableAdapter bia = new BillItemsTableAdapter();
                    CustomerTransactionsTableAdapter cta = new CustomerTransactionsTableAdapter();
                    var transactions = cta.GetDataByTimeFrame(start, end);
                    int numOfDays = (int)(end.Date.Subtract(start.Date).TotalDays);

                    double[] re = new double[numOfDays+1];
                    foreach (var trans in transactions)
                    {
                        if (!trans.IsEndTimeNull())
                        {
                            int dayVal = (int)(trans.EndTime.Date.Subtract(start.Date).TotalDays);
                            var bills = bia.GetDataByWorkflowID(trans.WorkflowInstID);
                            re[dayVal] += (double)(from p in bills select p.Price).Sum();
                        }
                    }
                    return re.ToList();

                }
            }
            else if (stat == StatObject.PayroleExpence)
            {
                if (typeOfStat == StatType.ForEachDay)
                {
                    WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
                    var shifts = wta.GetDataByTimeFrame(start, end);
                    int numOfDays = (int)(end.Date.Subtract(start.Date).TotalDays);
                    RolePayrateTableAdapter rta = new RolePayrateTableAdapter();
                    var payoutTable = rta.GetData();
                    List<double> re = new List<double>();

                    DateTime i_start = start.Date;
                    DateTime i_end = i_start.Add(new TimeSpan(1, 0, 0, 0));
                    for (int i = 0; i < numOfDays; i++)
                    {
                        decimal v = (from p in shifts
                                     where p.StartTime > i_start && p.StartTime < i_end &&
                                     p.IsEndTimeNull() == false
                                     select (from q in payoutTable where q.Role == p.Role select q.PayPerHour).FirstOrDefault()
                                     * (decimal)(p.EndTime.Subtract(p.StartTime).TotalHours)).Sum();
                        re.Add((double)v);
                        i_start = i_end;
                        i_end = i_start.Add(new TimeSpan(1, 0, 0, 0));

                    }
                    return re;
                }
            }
            return null;
        }
        /// <summary>
        /// Gets a list of the shifts for employees during a timeframe.
        /// </summary>
        /// <param name="sessionID">The sessionID of the current session</param>
        /// <param name="start">The start of the timeframe.</param>
        /// <param name="end">The end of the timeframe.</param>
        /// <returns></returns>
        public List<ShiftData> GetShiftsForEmployees(string sessionID, DateTime start, DateTime end)
        {
            List<ShiftData> re = new List<ShiftData>();
            WorkerShiftsTableAdapter wsa = new WorkerShiftsTableAdapter();
            var rows = wsa.GetDataByTimeFrame(start, end);
            foreach (var row in rows)
            {
                ShiftData sd = new ShiftData();
                sd.EmployeeID = row.EmployeeID;
                sd.StartTime = row.StartTime;
                if (row.IsEndTimeNull()) sd.EndTime = null;
                else
                    sd.EndTime = row.EndTime;
                sd.ShiftID = row.ID;
                sd.Role = row.Role;
                if (sd.EndTime != null)
                    sd.HoursWorked = (decimal)sd.EndTime.Value.Subtract(sd.StartTime).TotalHours;
                else
                    sd.HoursWorked = 0;

                re.Add(sd);
            }
            return re;
        }
 /// <summary>
 /// Edits a shift based on its shift number.
 /// </summary>
 /// <param name="sessionID">The sessionID of the current session</param>
 /// <param name="shiftNumber">The shift number to be edited.</param>
 /// <param name="EmployeeId">The new employee id.</param>
 /// <param name="role">The new role.</param>
 /// <param name="start">The new start time of the shift.</param>
 /// <param name="end">The new end time of the shift.</param>
 public void EditShift(string sessionID, int shiftNumber, int EmployeeId, string role, DateTime start, DateTime end)
 {
     WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
     wta.Update(start, end, role, EmployeeId, shiftNumber);
 }
 /// <summary>
 /// Starts a new shift of the given logged in in the given role.
 /// </summary>
 /// <param name="sessionID">The sessionID of the current session</param>
 /// <param name="role">The role.</param>
 public void StampShiftStart(string sessionID, string role)
 {
     WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
     wta.Insert(DateTime.Now, null, role, Auth.getEmployeeID(sessionID));
     EmployeeStatusTableAdapter est = new EmployeeStatusTableAdapter();
     est.UpdateStatus(true, role, Auth.getEmployeeID(sessionID));
 }
        /// <summary>
        /// Ends the shift of the given user if they are in a shift.
        /// </summary>
        /// <param name="sessionID">The sessionID of the current session</param>
        /// <returns>
        /// Returns true if the user's shift could be ended, else returns false
        /// </returns>
        public bool StampShiftEnd(string sessionID)
        {
            WorkerShiftsTableAdapter wta = new WorkerShiftsTableAdapter();
            var lastStartTime = wta.GetDataByEmployeeID(Auth.getEmployeeID(sessionID), DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0))).FirstOrDefault();
            EmployeeStatusTableAdapter est = new EmployeeStatusTableAdapter();
            est.UpdateStatus(false, "", Auth.getEmployeeID(sessionID));

            if (lastStartTime == null || !lastStartTime.IsEndTimeNull())
            {
                return false;
            }
            else
            {
                lastStartTime.EndTime = DateTime.Now;
                wta.Update(lastStartTime);
                return true;
            }
        }