Beispiel #1
0
        public TimeKeeperViewModel GetTimeKeeperViewModel(string name)
        {
            var model = new TimeKeeperViewModel();

            model.CustomerName = name;
            model.VadIds       = _context.Companies.Select(c => new SelectListItem(c.Name, c.VatId)).ToList();

            return(model);
        }
Beispiel #2
0
        public IActionResult TangCaNhanVien(int code, int approve, string secure)
        {
            var viewModel = new TimeKeeperViewModel
            {
                Approve = approve
            };

            #region Extensions
            var builderTraining = Builders <Trainning> .Filter;
            var filterTraining  = builderTraining.Eq(m => m.Enable, true);
            filterTraining = filterTraining & builderTraining.Eq(m => m.Type, "anh-van");
            var listTraining = dbContext.Trainnings.Find(filterTraining).Limit(10).SortByDescending(m => m.CreatedOn).ToList();
            viewModel.ListTraining = listTraining;
            #endregion

            var list = dbContext.OvertimeEmployees.Find(m => m.Code.Equals(code) && m.Secure.Equals(secure)).ToList();

            if (list != null && list.Count > 0)
            {
                var filter = Builders <OvertimeEmployee> .Filter.Eq(m => m.CodeInt, code) & Builders <OvertimeEmployee> .Filter.Eq(m => m.Secure, secure);

                var update = Builders <OvertimeEmployee> .Update
                             .Set(m => m.Secure, DateTime.Now.ToString("yyyyMMddHHmmssfff"))
                             .Set(m => m.Status, approve)
                             .Set(m => m.ApprovedOn, DateTime.Now);

                dbContext.OvertimeEmployees.UpdateMany(filter, update);

                #region Send mail AN NINH
                if (approve == (int)EOvertime.Ok)
                {
                    #region parameters
                    //{0} : Subject
                    //{1} : Gender Nguoi nhan
                    //{2} : Fullname nguoi nhan
                    //{3} : Gender nguoi yeu cau
                    //{4} : Fullname nguoi yeu cau
                    //{5} : Chức vụ
                    //{6} : Email
                    //{7} : Phone
                    //{8} : Noi dung . Example: bang tang ca ngày dd/MM/yyyy
                    //{9}: Link chi tiet
                    //{10}: Website
                    #endregion

                    var overtime = list.First();
                    var date     = overtime.Date;

                    var directorE = dbContext.Employees.Find(m => m.Id.Equals(overtime.ApprovedBy)).FirstOrDefault();

                    var securityPosition = dbContext.ChucVus.Find(m => m.Code.Equals("CHUCVU86")).FirstOrDefault();

                    var securityE = dbContext.Employees.Find(m => m.Enable.Equals(true) && m.Leave.Equals(false) && m.ChucVu.Equals(securityPosition.Id)).FirstOrDefault();

                    var genderDE      = directorE.Gender == "Nam" ? "Anh" : "Chị";
                    var genderDELower = directorE.Gender == "Nam" ? "anh" : "chị";
                    var genderSE      = securityE.Gender == "Nam" ? "anh" : "chị";

                    var phone = string.Empty;
                    if (directorE.Mobiles != null && directorE.Mobiles.Count > 0)
                    {
                        phone = directorE.Mobiles[0].Number;
                    }

                    var webRoot    = Environment.CurrentDirectory;
                    var pathToFile = _env.WebRootPath
                                     + Path.DirectorySeparatorChar.ToString()
                                     + "Templates"
                                     + Path.DirectorySeparatorChar.ToString()
                                     + "EmailTemplate"
                                     + Path.DirectorySeparatorChar.ToString()
                                     + "OvertimeSecurity.html";

                    var tos = new List <EmailAddress>
                    {
                        new EmailAddress {
                            Name = securityE.FullName, Address = securityE.Email
                        }
                    };

                    var subject = "Kiểm tra Bảng tăng ca ngày " + date.ToString("dd/MM/yyyy");
                    var title   = "Bảng tăng ca ngày " + date.ToString("dd/MM/yyyy");

                    var linkDetail = Constants.System.domain + "/" + Constants.LinkTimeKeeper.Main + "/" + Constants.LinkTimeKeeper.Overtime + "?Tu=" + date.ToString("MM-dd-yyyy") + "&Den=" + date.ToString("MM-dd-yyyy");

                    var bodyBuilder = new BodyBuilder();
                    using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
                    {
                        bodyBuilder.HtmlBody = SourceReader.ReadToEnd();
                    }
                    string messageBody = string.Format(bodyBuilder.HtmlBody,
                                                       subject,
                                                       genderSE,
                                                       securityE.FullName,
                                                       genderDE,
                                                       directorE.FullName,
                                                       directorE.ChucVuName,
                                                       genderDELower,
                                                       directorE.Email,
                                                       phone,
                                                       title,
                                                       linkDetail,
                                                       Constants.System.domain
                                                       );

                    var emailMessage = new EmailMessage()
                    {
                        ToAddresses = tos,
                        Subject     = subject,
                        BodyContent = messageBody,
                        Type        = "overtime-security",
                        EmployeeId  = overtime.Code.ToString()
                    };

                    var scheduleEmail = new ScheduleEmail
                    {
                        Status     = (int)EEmailStatus.Schedule,
                        To         = emailMessage.ToAddresses,
                        CC         = emailMessage.CCAddresses,
                        BCC        = emailMessage.BCCAddresses,
                        Type       = emailMessage.Type,
                        Title      = emailMessage.Subject,
                        Content    = emailMessage.BodyContent,
                        EmployeeId = emailMessage.EmployeeId
                    };

                    dbContext.ScheduleEmails.InsertOne(scheduleEmail);
                }
                #endregion

                ViewData["Status"] = "Cám ơn đã xác nhận, kết quả đang gửi cho người liên quan.";
                return(View(viewModel));
            }

            ViewData["Status"] = Constants.ErrorParameter;
            viewModel.Error    = true;
            return(View(viewModel));
        }
Beispiel #3
0
        public IActionResult Cong(string id, int approve, string secure)
        {
            var viewModel = new TimeKeeperViewModel
            {
                Approve = approve
            };

            #region Extensions: Trainning
            var builderTraining = Builders <Trainning> .Filter;
            var filterTraining  = builderTraining.Eq(m => m.Enable, true);
            filterTraining &= builderTraining.Eq(m => m.Type, "anh-van");
            var listTraining = dbContext.Trainnings.Find(filterTraining).Limit(10).SortByDescending(m => m.CreatedOn).ToList();
            viewModel.ListTraining = listTraining;
            #endregion

            var timelog = dbContext.EmployeeWorkTimeLogs.Find(m => m.Id.Equals(id)).FirstOrDefault();
            if (timelog == null || (timelog.SecureCode != secure && timelog.Status != 2))
            {
                ViewData["Status"] = Constants.ErrorParameter;
                viewModel.Error    = true;
                return(View(viewModel));
            }

            viewModel.EmployeeWorkTimeLog = timelog;

            #region Update status
            var filter = Builders <EmployeeWorkTimeLog> .Filter.Eq(m => m.Id, id);

            var update = Builders <EmployeeWorkTimeLog> .Update
                         .Set(m => m.SecureCode, Helper.HashedPassword(Guid.NewGuid().ToString("N").Substring(0, 12)))
                         .Set(m => m.WorkDay, 1)
                         .Set(m => m.Status, approve)
                         .Set(m => m.ConfirmDate, DateTime.Now.Date);

            dbContext.EmployeeWorkTimeLogs.UpdateOne(filter, update);
            #endregion

            #region update Summary
            if (approve == (int)EStatusWork.DongY)
            {
                var monthDate        = Utility.EndWorkingMonthByDate(timelog.Date);
                var builderUpdateSum = Builders <EmployeeWorkTimeMonthLog> .Filter;
                var filterUpdateSum  = builderUpdateSum.Eq(m => m.EmployeeId, timelog.EmployeeId);
                filterUpdateSum &= builderUpdateSum.Eq(m => m.Year, monthDate.Year);
                filterUpdateSum &= builderUpdateSum.Eq(m => m.Month, monthDate.Month);

                double dateInc     = 0;
                double worktimeInc = 0;
                double lateInc     = 0;
                double earlyInc    = 0;
                if (!timelog.In.HasValue && !timelog.Out.HasValue)
                {
                    dateInc     += 1;
                    worktimeInc += new TimeSpan(8, 0, 0).TotalMilliseconds;
                }
                else if (!timelog.In.HasValue || !timelog.Out.HasValue)
                {
                    dateInc     += 0.5;
                    worktimeInc += new TimeSpan(4, 0, 0).TotalMilliseconds;
                }
                if (timelog.Late.TotalMilliseconds > 0)
                {
                    worktimeInc += timelog.Late.TotalMilliseconds;
                    lateInc     += timelog.Late.TotalMilliseconds;
                }
                if (timelog.Early.TotalMilliseconds > 0)
                {
                    worktimeInc += timelog.Early.TotalMilliseconds;
                    earlyInc    += timelog.Early.TotalMilliseconds;
                }
                var updateSum = Builders <EmployeeWorkTimeMonthLog> .Update
                                .Inc(m => m.Workday, dateInc)
                                .Inc(m => m.WorkTime, worktimeInc)
                                .Inc(m => m.Late, -(lateInc))
                                .Inc(m => m.Early, -(earlyInc))
                                .Set(m => m.LastUpdated, DateTime.Now);

                dbContext.EmployeeWorkTimeMonthLogs.UpdateOne(filterUpdateSum, updateSum);
            }
            #endregion

            #region Send email to user
            var approvement = dbContext.Employees.Find(m => m.Id.Equals(timelog.ConfirmId)).FirstOrDefault();
            var employee    = dbContext.Employees.Find(m => m.Id.Equals(timelog.EmployeeId)).FirstOrDefault();
            var requester   = employee.FullName;
            var tos         = new List <EmailAddress>
            {
                new EmailAddress {
                    Name = employee.FullName, Address = employee.Email
                }
            };
            if (approve == (int)EStatusWork.DongY)
            {
                var hrs = Utility.EmailGet(Constants.Rights.NhanSu, (int)ERights.Edit);
                if (hrs != null && hrs.Count > 0)
                {
                    requester += " , HR";
                    foreach (var item in hrs)
                    {
                        if (tos.Count(m => m.Address.Equals(item.Address)) == 0)
                        {
                            tos.Add(item);
                        }
                    }
                }
            }

            var ccs = new List <EmailAddress>();

            var webRoot    = Environment.CurrentDirectory;
            var pathToFile = _env.WebRootPath
                             + Path.DirectorySeparatorChar.ToString()
                             + "Templates"
                             + Path.DirectorySeparatorChar.ToString()
                             + "EmailTemplate"
                             + Path.DirectorySeparatorChar.ToString()
                             + "TimeKeeperConfirm.html";

            var subject   = "Xác nhận công.";
            var status    = approve == 3 ? "Đồng ý" : "Không duyệt";
            var inTime    = timelog.In.HasValue ? timelog.In.Value.ToString(@"hh\:mm") : string.Empty;
            var outTime   = timelog.Out.HasValue ? timelog.Out.Value.ToString(@"hh\:mm") : string.Empty;
            var lateTime  = timelog.Late.TotalMilliseconds > 0 ? Math.Round(timelog.Late.TotalMinutes, 0).ToString() : "0";
            var earlyTime = timelog.Early.TotalMilliseconds > 0 ? Math.Round(timelog.Early.TotalMinutes, 0).ToString() : "0";
            var sumTime   = string.Empty;
            if (string.IsNullOrEmpty(inTime) && string.IsNullOrEmpty(outTime))
            {
                sumTime = "1 ngày";
            }
            else if (string.IsNullOrEmpty(inTime) || string.IsNullOrEmpty(outTime))
            {
                sumTime = "0.5 ngày";
            }
            var minutesMissing = TimeSpan.FromMilliseconds(timelog.Late.TotalMilliseconds + timelog.Early.TotalMilliseconds).TotalMinutes;
            if (minutesMissing > 0)
            {
                if (!string.IsNullOrEmpty(sumTime))
                {
                    sumTime += ", ";
                }
                sumTime += Math.Round(minutesMissing, 0) + " phút";
            }

            var detailTimeKeeping = "Ngày: " + timelog.Date.ToString("dd/MM/yyyy") + "; thiếu: " + sumTime;
            if (!string.IsNullOrEmpty(inTime))
            {
                detailTimeKeeping += " | giờ vào: " + inTime + "; trễ: " + lateTime;
            }
            else
            {
                detailTimeKeeping += " | giờ vào: --; trễ: --";
            }
            if (!string.IsNullOrEmpty(outTime))
            {
                detailTimeKeeping += "; giờ ra: " + outTime + "; sớm: " + earlyTime;
            }
            else
            {
                detailTimeKeeping += "; giờ ra: --; sớm: --";
            }
            // Api update, generate code.
            var linkDetail  = Constants.System.domain;
            var bodyBuilder = new BodyBuilder();
            using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile))
            {
                bodyBuilder.HtmlBody = SourceReader.ReadToEnd();
            }
            string messageBody = string.Format(bodyBuilder.HtmlBody,
                                               subject,
                                               requester,
                                               status,
                                               approvement.FullName,
                                               approvement.Email,
                                               approvement.ChucVuName,
                                               detailTimeKeeping,
                                               timelog.Reason,
                                               timelog.ReasonDetail,
                                               linkDetail,
                                               Constants.System.domain
                                               );

            var emailMessage = new EmailMessage()
            {
                ToAddresses = tos,
                CCAddresses = ccs,
                Subject     = subject,
                BodyContent = messageBody,
                Type        = "xac-nhan-cong",
                EmployeeId  = timelog.EmployeeId
            };

            var scheduleEmail = new ScheduleEmail
            {
                Status     = (int)EEmailStatus.Schedule,
                To         = emailMessage.ToAddresses,
                CC         = emailMessage.CCAddresses,
                BCC        = emailMessage.BCCAddresses,
                Type       = emailMessage.Type,
                Title      = emailMessage.Subject,
                Content    = emailMessage.BodyContent,
                EmployeeId = emailMessage.EmployeeId
            };
            dbContext.ScheduleEmails.InsertOne(scheduleEmail);
            #endregion

            ViewData["Status"] = "Cám ơn đã xác nhận, kết quả đang gửi cho người liên quan.";

            return(View(viewModel));
        }