Esempio n. 1
0
        public static async Task GetADUser()
        {
            TimeSheetDb timesheetDb       = new TimeSheetDb();
            string      NTI_Staff_GroupID = ConfigurationManager.AppSettings["ida:NTI_Staff_GroupID"];
            var         userList          = new List <User>();

            try
            {
                ActiveDirectoryClient client = UserProfileController.GetActiveDirectoryClient();
                IGroup group = await client.Groups.GetByObjectId(NTI_Staff_GroupID).ExecuteAsync();

                IGroupFetcher groupFetcher = group as IGroupFetcher;
                IPagedCollection <IDirectoryObject> pagedCollection = await groupFetcher.Members.ExecuteAsync();

                if (pagedCollection != null)
                {
                    do
                    {
                        List <IDirectoryObject> directoryObjects = pagedCollection.CurrentPage.ToList();
                        foreach (IDirectoryObject directoryObject in directoryObjects)
                        {
                            if (directoryObject is User)
                            {
                                var user = (User)directoryObject;
                                userList.Add(user);
                            }
                        }
                        pagedCollection = await pagedCollection.GetNextPageAsync();
                    } while (pagedCollection != null);
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            if (userList != null)
            {
                List <ADUser> SystemUserList = timesheetDb.ADUsers.ToList();
                if (SystemUserList.Count != userList.Count)
                {
                    foreach (var item in userList)
                    {
                        if (timesheetDb.ADUsers.Find(item.Mail) == null)
                        {
                            timesheetDb.ADUsers.Add(new ADUser {
                                UserName   = item.DisplayName,
                                Email      = item.Mail,
                                JobCode    = item.JobTitle,
                                Department = item.Department
                            }
                                                    );
                        }
                    }

                    timesheetDb.SaveChanges();
                }
            }
        }
Esempio n. 2
0
        static public void UpdateLeaveBalance()
        {
            // Only updates when it is the end day of a pay period
            if ((DateTime.Now - FirstPayPeriod).Days % 14 == 0)
            {
                using (TimeSheetDb context = new TimeSheetDb())
                {
                    const double auunalAccuralRate = 0.076923;
                    const double sickAccuralRate   = 0.038462;
                    _leaveType   leaveType;
                    double       rate;

                    List <ADUser> users = context.ADUsers.Where(u => u.ContractHours != 0).ToList();

                    foreach (var user in users)
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            if (i == 0)
                            {
                                leaveType = _leaveType.annual;
                                rate      = auunalAccuralRate;
                            }
                            else
                            {
                                leaveType = _leaveType.sick;
                                rate      = sickAccuralRate;
                            }

                            // Update leaves balances
                            // Formula: Current leaves - leaves taken in the pay period + (accural Rate * contract hours)
                            LeaveBalance balance = context.LeaveBalances.Find(user.Email, leaveType);
                            if (balance == null)
                            {
                                balance = new LeaveBalance
                                {
                                    UserID              = user.Email,
                                    UserName            = user.UserName,
                                    LeaveType           = leaveType,
                                    AvailableLeaveHours = 0.0
                                };
                                context.LeaveBalances.Add(balance);
                            }
                            else
                            {
                                balance.AvailableLeaveHours += user.ContractHours * rate;
                                context.Entry(balance).State = EntityState.Modified;
                            }
                        }
                    }
                    context.SaveChanges();
                }
            }
        }
Esempio n. 3
0
        public List <TimeRecord> GetTimeRecords()
        {
            TimeSheetDb       contextDb = new TimeSheetDb();
            List <TimeRecord> records   = (from r in contextDb.TimeRecords
                                           where r.RecordDate >= StartTime &&
                                           r.RecordDate <= EndTime &&
                                           r.LeaveType != null &&
                                           r.LeaveTime != 0 &&
                                           r.UserID == UserID
                                           select r).ToList();

            return(records ?? new List <TimeRecord>());
        }
Esempio n. 4
0
        public string GetManagerList()
        {
            TimeSheetDb contextDb    = new TimeSheetDb();
            string      managerNames = "";
            var         firstManager = true;

            foreach (var managerId in _managerIDs)
            {
                if (!firstManager)
                {
                    managerNames += ", ";
                }
                firstManager  = false;
                managerNames += contextDb.ADUsers.Find(managerId).UserName;
            }

            return(managerNames);
        }
Esempio n. 5
0
        public static async Task SendEmail(string EmailReceiver, string CC, string EmailType, string id)
        {
            int    ID       = Convert.ToInt32(id);
            string link     = "https://hr.nantien.edu.au/";
            string body     = string.Empty;
            string subject  = string.Empty;
            string username = string.Empty;

            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            string path = Directory.GetCurrentDirectory();// path for loading email Templates file

            using (TimeSheetDb timesheetDb = new TimeSheetDb())
            {
                switch (EmailType)
                {
                case "TimesheetApplication":
                    link   += "TimesheetApproval/ApprovalDetail/";
                    link   += id;
                    subject = "TimesheetApplicaiton";
                    path    = path + @"\Templates\TimesheetApplication.txt";
                    using (var sr = new StreamReader(path))
                    {
                        body = sr.ReadToEnd();
                    }
                    TimeRecordForm form = timesheetDb.TimeRecordForms.Find(ID);
                    username = timesheetDb.ADUsers.Find(form.UserID).UserName ?? form.UserID;
                    subject  = subject + " From " + username;
                    body     = string.Format(body, username, form.Period, form.Year, link);
                    break;

                case "LeaveApplication":
                    link   += "LeaveApproval/ApprovalDetail/";
                    link   += id;
                    subject = "LeaveApplicaiton";
                    path    = path + @"\Templates\LeaveApplication.txt";
                    using (var sr = new StreamReader(path))
                    {
                        body = sr.ReadToEnd();
                    }
                    LeaveApplication leaveModel = timesheetDb.LeaveApplications.Find(ID);
                    username = leaveModel.UserName ?? leaveModel.UserID;
                    subject  = subject + " From " + username;
                    body     = string.Format(body, username, leaveModel.StartTime, leaveModel.EndTime, leaveModel.Comment, link);
                    File.AppendAllText(@"C:\Users\Public\TestFolder\test.txt", body + ".");
                    break;

                case "TimesheetApproval":
                    subject = "TimesheetApproval";
                    path    = path + @"\Templates\TimesheetApproval.txt";
                    using (var sr = new StreamReader(path))
                    {
                        body = sr.ReadToEnd();
                    }
                    TimeRecordForm formModel = timesheetDb.TimeRecordForms.Find(ID);
                    ADUser         manager   = timesheetDb.ADUsers.Find(formModel.ApprovedBy);
                    string         comment   = string.Empty;
                    if (formModel.Comments != null)
                    {
                        comment = formModel.Comments;
                    }
                    else
                    {
                        comment = "No comments";
                    }
                    body = string.Format(body, formModel.Period, formModel.Year, formModel.status, manager.UserName, comment);
                    break;

                case "LeaveApproval":
                    subject = "Leave Approval";
                    path    = path + @"\Templates\LeaveApproval.txt";
                    using (var sr = new StreamReader(path))
                    {
                        body = sr.ReadToEnd();
                    }
                    LeaveApplication laModel      = timesheetDb.LeaveApplications.Find(ID);
                    ADUser           leavemanager = timesheetDb.ADUsers.Find(laModel.ApprovedBy);
                    body = string.Format(body, laModel.StartTime, laModel.EndTime, laModel.status, leavemanager.UserName);
                    break;
                }
            }

            var message = new MailMessage();

            message.To.Add(new MailAddress(EmailReceiver));
            message.Subject    = subject;
            message.Body       = body;
            message.IsBodyHtml = true;
            if (CC != "")
            {
                string[] CopyReceiver = CC.Split(';');
                foreach (var cc in CopyReceiver)
                {
                    message.CC.Add(cc);
                }
            }
            try
            {
                Console.WriteLine(message);
                using (var smtp = new SmtpClient())
                {
                    await smtp.SendMailAsync(message);
                }
            }
            catch (Exception ex)
            {
                if (ex.Source != null)
                {
                    Console.WriteLine("IOException source: {0}", ex.Source);
                }
                throw;
            }
        }