using System; using System.Collections.Generic; using ASPNET.StarterKit.DataAccessLayer; using System.Runtime.Caching; namespace ASPNET.StarterKit.BusinessLogicLayer { public class TimeEntry { /*** FIELD PRIVATE ***/ private string _CreatorUserName; private int _ProjectId; private DateTime _DateCreated; private string _Description; private decimal _Duration; private int _Id; private Project _Project; private DateTime _ReportedDate; private string _UserName; private string _creditDepartment; private decimal _rate; /*** CONSTRUCTOR ***/ public TimeEntry(string creatorUserName, int projectId, decimal duration, DateTime reportedDate, string userName, string costcenter, decimal rate) : this(creatorUserName, projectId, DefaultValues.GetDateTimeMinValue(), string.Empty, duration, DefaultValues.GetTimeEntryIdMinValue(), reportedDate, userName, costcenter, rate) { } public TimeEntry(string creatorUserName, int projectId, DateTime dateCreated, string description, decimal duration, int id, DateTime reportedDate, string userName, string costcenter, decimal rate) { if (String.IsNullOrEmpty(creatorUserName)) throw (new NullReferenceException("creatorUserName")); if (projectId <= DefaultValues.GetProjectIdMinValue()) throw (new ArgumentOutOfRangeException("projectId")); if (duration <= DefaultValues.GetDurationMinValue()) throw (new ArgumentOutOfRangeException("duration")); if (reportedDate <= DefaultValues.GetDateTimeMinValue()) throw (new ArgumentOutOfRangeException("reportedDate")); if (String.IsNullOrEmpty(userName)) throw (new NullReferenceException("userName")); _CreatorUserName = creatorUserName == null ? "" : creatorUserName.Trim(); _ProjectId = projectId; _DateCreated = dateCreated; _Description = description == null ? "" : description.Trim(); _Duration = duration; _Id = id; _ReportedDate = reportedDate; _UserName = userName == null ? "" : userName.Trim(); _creditDepartment = costcenter == null ? "" : costcenter.Trim(); _rate = rate; } /*** PROPERTIES ***/ public string CreatorUserName { get { if (String.IsNullOrEmpty(_CreatorUserName)) return string.Empty; else return _CreatorUserName; } } //public int CategoryId { // get { return _CategoryId; } // set { // Project currentProject; // if (_Category == null) { // _Category = Category.GetCategoryByCategoryId(_CategoryId); // if (_Category == null) // throw (new NullReferenceException("Can not find existing category")); // } // currentProject = Project.GetProjectById(_Category.ProjectId); // if (currentProject == null) // throw (new NullReferenceException("NewProject is not a valid one")); // Project newProject; // Category newCategory = Category.GetCategoryByCategoryId(value); // if (newCategory == null) // throw (new NullReferenceException("NewCategory is not a valid one")); // newProject = Project.GetProjectById(newCategory.ProjectId); // if (newProject == null) // throw (new NullReferenceException("NewProject is not a valid one")); // //this ensure data consistency // if (newProject.Id == currentProject.Id) { // _CategoryId = value; // _Category = null; // } // } //} public int ProjectId { get { return this._ProjectId; } } //public string CategoryName { // get { // if (_Category == null) { // _Category = Category.GetCategoryByCategoryId(CategoryId); // if (_Category == null) // return (string.Empty); // } // return _Category.Name; // } //} public DateTime DateCreated { get { return _DateCreated; } } public string Description { get { if (String.IsNullOrEmpty(_Description)) return string.Empty; else return _Description; } set { _Description = value; } } public decimal Duration { get { return _Duration; } set { _Duration = value; } } public int Id { get { return _Id; } } public string CreditDepartment { get { return this._creditDepartment; } } public decimal Rate { get { return this._rate; } } public DateTime ReportedDate { get { return _ReportedDate; } set { _ReportedDate = value; } } public string ProjectName { get { if (_ProjectId == DefaultValues.GetProjectIdMinValue()) { return (string.Empty); } _Project = Project.GetProjectById(this._ProjectId,false); if (_Project == null) return (string.Empty); return _Project.Name; } } public string ProjectNumber { get { if (_ProjectId == DefaultValues.GetProjectIdMinValue()) { return (string.Empty); } _Project = Project.GetProjectById(this._ProjectId,false); if (_Project == null) return (string.Empty); return _Project.ProjectNumber; } } public string UserName { get { if (String.IsNullOrEmpty(_UserName)) return string.Empty; else return _UserName; } set { _UserName = value; } } private MembershipAppUser _User; private MembershipAppUser User { get { if (_User == null) _User = MembershipAppUser.GetUser(this._UserName, false); return _User; } } public string EmployeeTitle { get { if (User != null) return User.Title; else return ""; } } public string EmployeeEmail { get { if (User != null) return User.Email; else return ""; } } public DateTime WeekEndingDate { get { return this.GetWeekEndingDate(); } } /*** METHODS ***/ public bool Delete() { if (this.Id > DefaultValues.GetProjectIdMinValue()) { DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return DALLayer.DeleteTimeEntry(this.Id); } else return false; } private DateTime GetWeekEndingDate() { ObjectCache cache = MemoryCache.Default; string startonDay = cache["WeekStartDay"] as string; if (startonDay == null) startonDay = "Monday"; string durationStr = cache["WorkWeekDuration"] as string; if (durationStr == null) durationStr = "5"; int duration = Int32.Parse(durationStr); DateTime WeekstartDate = getStartOfWeekDate(startonDay, _ReportedDate); DateTime Weekending = WeekstartDate.AddDays(duration); return Weekending; } private DateTime getStartOfWeekDate(string dayofweek, DateTime fromdate) { while (fromdate.DayOfWeek.ToString().ToLower() != dayofweek.ToLower()) { fromdate = fromdate.AddDays(-1); } return fromdate; } public bool Save() { DataAccess DALLayer = DataAccessHelper.GetDataAccess(); if (Id <= DefaultValues.GetTimeEntryIdMinValue()) { int TempId = DALLayer.CreateNewTimeEntry(this); if (TempId > DefaultValues.GetTimeEntryIdMinValue()) { _Id = TempId; return true; } else return false; } else return (DALLayer.UpdateTimeEntry(this)); } /*** METHOD STATIC ***/ public static bool DeleteTimeEntry(int Id) { if (Id <= DefaultValues.GetTimeEntryIdMinValue()) throw (new ArgumentOutOfRangeException("Id")); DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.DeleteTimeEntry(Id)); } public static List GetAllTimeEntriess() { DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.GetAllTimeEntries()); } public static List GetTimeEntries(int projectId, string userName) { if (projectId <= DefaultValues.GetProjectIdMinValue()) return (new List()); if (String.IsNullOrEmpty(userName)) return (new List()); DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.GetTimeEntries(projectId, userName)); } public static List GetTimeEntriesByDateRange(string userName, DateTime WeekStart) { if (WeekStart <= DefaultValues.GetDateTimeMinValue()) return (new List()); if (String.IsNullOrEmpty(userName)) return (new List()); DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.GetTimeEntries(userName, WeekStart)); } public static List GetWeekEndingDates(string userName) { if (String.IsNullOrEmpty(userName)) return (new List()); DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.GetWeekEndingDates(userName)); } public static TimeEntry GetTimeEntryById(int timeEntryId) { if (timeEntryId <= DefaultValues.GetTimeEntryIdMinValue()) return (null); DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.GetTimeEntryById(timeEntryId)); } public static List GetAllUnSubmittedTimeEntries() { DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.GetAllUnSubmittedTimeEntries()); } public static List GetTimeEntriesByUserNameAndDates(string userName, DateTime startingDate, DateTime endDate) { if (String.IsNullOrEmpty(userName)) return (new List()); DataAccess DALLayer = DataAccessHelper.GetDataAccess(); return (DALLayer.GetTimeEntriesByUserNameAndDates(userName, startingDate, endDate)); } public static bool UpdateTimeEntry(string ProjectName, int Id, string Description, decimal Duration, DateTime ReportedDate, string UserName) { TimeEntry timeEntry = TimeEntry.GetTimeEntryById(Id); if (timeEntry == null) return (false); if (timeEntry != null) { timeEntry.Description = Description; timeEntry.Duration = Duration; timeEntry.ReportedDate = ReportedDate; timeEntry.UserName = UserName; return (timeEntry.Save()); } else return false; } public static List GetDaysOfWeekForPeriod(DateTime Start, string userName) { DataAccess DALLayer = DataAccessHelper.GetDataAccess(); List rt = (DALLayer.GetWeekEndingForStartDate(Start)); List times = DALLayer.GetTimeEntriesByUserNameForDuration(userName, Start); foreach (TimeEntry t in times) { DaysInPeriod DowEntry = rt.Find(x => x.Name == t.ReportedDate.ToString("dddd")); if (DowEntry.Hours == null) DowEntry.Hours = t.Duration; else DowEntry.Hours += t.Duration; } return rt; } } }