public void DTO2DB_AnnualLeaveTracking(int userID, DTO.AnnualLeaveTrackingDTO dtoItem, ref AnnualLeaveTracking dbItem)
        {
            // Mapping workcenter.
            AutoMapper.Mapper.Map <DTO.AnnualLeaveTrackingDTO, DAL.AnnualLeaveTracking>(dtoItem, dbItem);
            dbItem.StatusUpdatedDate = dtoItem.StatusUpdatedDate.ConvertStringToDateTime();
            dbItem.UpdatedDate       = dtoItem.UpdatedDate.ConvertStringToDateTime();

            // Mapping workcenter details.
            if (dtoItem.AnnualLeaveTrackingDetailDTOs != null)
            {
                foreach (AnnualLeaveTrackingDetail dbWorkCenterDetail in dbItem.AnnualLeaveTrackingDetail.ToList())
                {
                    if (!dtoItem.AnnualLeaveTrackingDetailDTOs.Select(s => s.AnnualLeaveTrackingDetailID).Contains(dbWorkCenterDetail.AnnualLeaveTrackingDetailID))
                    {
                        dbItem.AnnualLeaveTrackingDetail.Remove(dbWorkCenterDetail);
                    }
                }

                foreach (DTO.AnnualLeaveTrackingDetailDTO dtoWorkCenterDetail in dtoItem.AnnualLeaveTrackingDetailDTOs.ToList())
                {
                    AnnualLeaveTrackingDetail dbWorkCenterDetail;

                    if (dtoWorkCenterDetail.AnnualLeaveTrackingDetailID < 0)
                    {
                        dbWorkCenterDetail = new AnnualLeaveTrackingDetail();
                        dbItem.AnnualLeaveTrackingDetail.Add(dbWorkCenterDetail);
                    }
                    else
                    {
                        dbWorkCenterDetail = dbItem.AnnualLeaveTrackingDetail.FirstOrDefault(o => o.AnnualLeaveTrackingDetailID == dtoWorkCenterDetail.AnnualLeaveTrackingDetailID);
                    }

                    if (dbWorkCenterDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.AnnualLeaveTrackingDetailDTO, AnnualLeaveTrackingDetail>(dtoWorkCenterDetail, dbWorkCenterDetail);
                        //dbWorkCenterDetail.FromDate = dtoWorkCenterDetail.FromDate.ConvertStringToDateTime();
                        //dbWorkCenterDetail.ToDate = dtoWorkCenterDetail.ToDate.ConvertStringToDateTime();
                        if (!string.IsNullOrEmpty(dtoWorkCenterDetail.ToDate) && !string.IsNullOrEmpty(dtoWorkCenterDetail.ToTime))
                        {
                            DateTime tmpDate;
                            if (DateTime.TryParse(dtoWorkCenterDetail.ToDate + " " + dtoWorkCenterDetail.ToTime, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                            {
                                dbWorkCenterDetail.ToDate = tmpDate;
                            }
                        }
                        if (!string.IsNullOrEmpty(dtoWorkCenterDetail.FromDate) && !string.IsNullOrEmpty(dtoWorkCenterDetail.FromTime))
                        {
                            DateTime tmpDate;
                            if (DateTime.TryParse(dtoWorkCenterDetail.FromDate + " " + dtoWorkCenterDetail.FromTime, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                            {
                                dbWorkCenterDetail.FromDate = tmpDate;
                            }
                        }

                        //dbWorkCenterDetail.UpdatedBy = userID;
                        //dbWorkCenterDetail.UpdatedDate = DateTime.Now;

                        //if (dtoWorkCenterDetail.WorkCenterDetailID <= 0)
                        //{
                        //    dbWorkCenterDetail.CreatedBy = userID;
                        //    dbWorkCenterDetail.CreatedDate = DateTime.Now;
                        //}
                    }
                }
            }
        }
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            AnnualLeaveTrackingDTO annualLeaveTrackingDTO = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <AnnualLeaveTrackingDTO>();

            notification = new Notification()
            {
                Type = NotificationType.Success
            };

            try
            {
                using (var context = CreateContext())
                {
                    AnnualLeaveTracking dbItem;

                    if (id == 0)
                    {
                        dbItem = new AnnualLeaveTracking();
                        context.AnnualLeaveTrackings.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.AnnualLeaveTrackings.FirstOrDefault(o => o.AnnualLeaveTrackingID == id);

                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not found data!";

                            return(false);
                        }
                        else
                        {
                            int?statusID = dbItem.StatusID;
                            if (statusID.HasValue && statusID == 3)
                            {
                                throw new Exception("Request has approved. You can not changed");
                            }
                        }
                    }
                    converter.DTO2DB_AnnualLeaveTracking(userId, annualLeaveTrackingDTO, ref dbItem);

                    context.AnnualLeaveTrackingDetail.Local.Where(o => o.AnnualLeaveTracking == null).ToList().ForEach(o => context.AnnualLeaveTrackingDetail.Remove(o));

                    context.SaveChanges();

                    //send notify
                    string emailBody    = string.Empty;
                    string emailSubject = string.Empty;
                    string sendToEmail  = string.Empty;

                    AnnualLeaveMng_Email_View info = context.AnnualLeaveMng_Email_View.Where(s => s.AnnualLeaveTrackingID == dbItem.AnnualLeaveTrackingID).FirstOrDefault();
                    if (dbItem.StatusID == 1 && !string.IsNullOrEmpty(info.EmailApprover)) // pending
                    {
                        sendToEmail  = info.EmailApprover;
                        emailSubject = "Please check and approve the leave request from: " + info.RequestNM;
                        emailBody    = "<p>" +
                                       "Dear Sir / Madam," +
                                       "<br/>" +
                                       "Could you please check and approve my leave request as below:" +
                                       "<br/>" +
                                       "       Request for: " + info.RequestNM +
                                       "<br/>" +
                                       "       Reason: " + info.Remark +
                                       "<br/>" +
                                       "       Requested Day Off: " + info.RequestedTimeOff +
                                       "<br/>" +
                                       "       From Day Off: " + info.FromDate + " To Date: " + info.ToDate +
                                       "<br/>" +
                                       "Could you please click here to approve: http://app.tilsoft.bg/AnnualLeaveMng/Edit/" + info.AnnualLeaveTrackingID +
                                       "<br/>" +
                                       "Have a nice day!" +
                                       "<br/>" +
                                       "This is an automated message, please do not reply!" +
                                       "</p>";

                        EmailNotificationMessage dbEmail = new EmailNotificationMessage();
                        dbEmail.EmailBody    = emailBody;
                        dbEmail.EmailSubject = emailSubject;
                        dbEmail.SendTo       = sendToEmail;
                        context.EmailNotificationMessage.Add(dbEmail);

                        NotificationMessage notificationMessage = new NotificationMessage();
                        notificationMessage.UserID = info.ApproverUserID;
                        notificationMessage.NotificationMessageTag     = Module.Framework.ConstantIdentifier.MOBILE_APP_MESSAGE_TAG_SALES;
                        notificationMessage.NotificationMessageTitle   = emailSubject;
                        notificationMessage.NotificationMessageContent = emailBody;
                        context.NotificationMessage.Add(notificationMessage);

                        context.SaveChanges();
                    }
                    else if (dbItem.StatusID == 3 && !string.IsNullOrEmpty(info.EmailRequest)) //approve
                    {
                        dbItem.ApproveBy = userId;
                        sendToEmail      = info.EmailRequest;
                        emailSubject     = "Your request has been Approved";
                        emailBody        = "<p>" +
                                           "Dear," +
                                           "<br/>" +
                                           "Your leave request: http://app.tilsoft.bg/AnnualLeaveMng/Edit/" + info.AnnualLeaveTrackingID + " has been approved by manager, please check" +
                                           "<br/>" +
                                           "Have a nice day!" +
                                           "<br/>" +
                                           "This is an automated message, please do not reply!" +
                                           "</p>";

                        EmailNotificationMessage dbEmail = new EmailNotificationMessage();
                        dbEmail.EmailBody    = emailBody;
                        dbEmail.EmailSubject = emailSubject;
                        dbEmail.SendTo       = sendToEmail;
                        context.EmailNotificationMessage.Add(dbEmail);

                        NotificationMessage notificationMessage = new NotificationMessage();
                        notificationMessage.UserID = info.RequestUserID;
                        notificationMessage.NotificationMessageTag     = Module.Framework.ConstantIdentifier.MOBILE_APP_MESSAGE_TAG_SALES;
                        notificationMessage.NotificationMessageTitle   = emailSubject;
                        notificationMessage.NotificationMessageContent = emailBody;
                        context.NotificationMessage.Add(notificationMessage);

                        context.SaveChanges();
                    }
                    else if (dbItem.StatusID == 4 && !string.IsNullOrEmpty(info.EmailRequest)) //reject
                    {
                        sendToEmail  = info.EmailRequest;
                        emailSubject = "Your request has been Rejected:";
                        emailBody    = "<p>" +
                                       "Dear," +
                                       "<br/>" +
                                       "Your leave request: http://app.tilsoft.bg/AnnualLeaveMng/Edit/" + info.AnnualLeaveTrackingID + " has been rejected: by manager, please check" +
                                       "<br/>" +
                                       "Have a nice day!" +
                                       "<br/>" +
                                       "This is an automated message, please do not reply!" +
                                       "</p>";

                        EmailNotificationMessage dbEmail = new EmailNotificationMessage();
                        dbEmail.EmailBody    = emailBody;
                        dbEmail.EmailSubject = emailSubject;
                        dbEmail.SendTo       = sendToEmail;
                        context.EmailNotificationMessage.Add(dbEmail);

                        NotificationMessage notificationMessage = new NotificationMessage();
                        notificationMessage.UserID = info.RequestUserID;
                        notificationMessage.NotificationMessageTag     = Module.Framework.ConstantIdentifier.MOBILE_APP_MESSAGE_TAG_SALES;
                        notificationMessage.NotificationMessageTitle   = emailSubject;
                        notificationMessage.NotificationMessageContent = emailBody;
                        context.NotificationMessage.Add(notificationMessage);

                        context.SaveChanges();
                    }

                    dtoItem = GetData(userId, dbItem.AnnualLeaveTrackingID, out notification);
                }

                return(true);
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;

                return(false);
            }
        }