public ActionResult Revise(ProductTypeIndexViewModel model)
        {
            try
            {
                var data       = _productTypeService.Find(model.ViewModel.ProdCode);
                var parameters = new Dictionary <string, string>();

                parameters.Add("product_type", data.PRODUCT_TYPE);
                parameters.Add("date", DateTime.Now.ToString("dddd, dd MMM yyyy")); // without time
                //parameters.Add("date", DateTime.Now.ToString("dddd, dd MMM yyyy hh:mm:ss")); // with time
                parameters.Add("approver", String.Format("{0} {1}", CurrentUser.FIRST_NAME, CurrentUser.LAST_NAME));
                parameters.Add("approval_status", _refService.GetReferenceByKey(ReferenceKeys.ApprovalStatus.Rejected).REFF_VALUE);
                parameters.Add("url_detail", Url.Action("Detail", "ProductType", new { id = data.PROD_CODE }, this.Request.Url.Scheme));
                parameters.Add("url_edit", Url.Action("Edit", "ProductType", new { id = data.PROD_CODE }, this.Request.Url.Scheme));
                parameters.Add("remark", model.ViewModel.RevisionData.Comment);

                var mailContent = _refService.GetMailContent((int)ReferenceKeys.EmailContent.ProductTypeRejected, parameters);
                var sender      = _refService.GetUserEmail(CurrentUser.USER_ID);
                var display     = String.Format("{0} [{1} {2}]", ReferenceLookup.Instance.GetReferenceKey(ReferenceKeys.EmailSender.AdminApprover), CurrentUser.FIRST_NAME, CurrentUser.LAST_NAME);
                var sendTo      = data.CREATOR.EMAIL;

                ExecuteApprovalAction(model, ReferenceKeys.ApprovalStatus.Edited, Enums.ActionType.Reject, mailContent.EMAILCONTENT, mailContent.EMAILSUBJECT, sender, display, sendTo);
            }
            catch (Exception ex)
            {
                AddMessageInfo("Submit Failed : " + ex.Message, Enums.MessageInfoType.Error);
            }
            return(RedirectToAction("Index"));
        }
        public ActionResult Edit(ProductTypeIndexViewModel model)
        {
            if (!ModelState.IsValid)
            {
                AddMessageInfo("Operation not allowed", Enums.MessageInfoType.Error);
                model = GenerateProperties(model, true);
                //var obj = service.Find(Convert.ToInt64(id));
                model.EnableFormInput     = true;
                model.EditMode            = true;
                model.ViewModel.IsCreator = CurrentUser.USER_ID == model.ViewModel.CreatedBy;

                var history = _changesHistoryBll.GetByFormTypeAndFormId(Enums.MenuList.ProductType, model.ViewModel.ProdCode.ToString()).ToList();
                model.ChangesHistoryList = Mapper.Map <List <ChangesHistoryItemModel> >(history);
                model.WorkflowHistory    = GetWorkflowHistory(model.ViewModel.ProdCode);

                return(View("Edit", model));
            }
            //if (CurrentUser.UserRole == Enums.UserRole.Viewer || CurrentUser.UserRole == Enums.UserRole.Controller)
            //{
            //    AddMessageInfo("Operation not allowed", Enums.MessageInfoType.Error);
            //    return RedirectToAction("Index");
            //}
            ExecuteEdit(model, ReferenceKeys.ApprovalStatus.Edited, ReferenceKeys.EmailContent.ProductTypeApprovalRequest);
            return(RedirectToAction("Index"));
        }
        //
        // GET: /ProductType/
        public ActionResult Index()
        {
            var data = new ProductTypeIndexViewModel()
            {
                MainMenu         = _mainMenu,
                CurrentMenu      = PageInfo,
                IsNotViewer      = (CurrentUser.UserRole != Enums.UserRole.Viewer && !IsCreatorPRD(CurrentUser.USER_ID) && CurrentUser.UserRole != Enums.UserRole.Controller),
                ListProductTypes = Mapper.Map <List <ProductTypeFormViewModel> >(
                    _productTypeService.GetAll().OrderByDescending(item => item.PROD_CODE)),
                IsAdminApprover = _refService.IsAdminApprover(CurrentUser.USER_ID)
            };

            var list = new List <ProductTypeFormViewModel>(data.ListProductTypes);

            data.ListProductTypes = new List <ProductTypeFormViewModel>();

            var approvalStatusApproved  = _refService.GetReferenceByKey(ReferenceKeys.ApprovalStatus.Completed).REFF_ID;
            var approvalStatusSubmitted = _refService.GetReferenceByKey(ReferenceKeys.ApprovalStatus.AwaitingAdminApproval).REFF_ID;

            foreach (var item in list)
            {
                item.IsCreator   = CurrentUser.USER_ID == item.CreatedBy;
                item.IsApproved  = item.ApprovalStatus == approvalStatusApproved;
                item.IsSubmitted = item.ApprovalStatus == approvalStatusSubmitted;
                data.ListProductTypes.Add(item);
            }
            return(View("Index", data));
        }
        // GET: ProductTypes
        public async Task <IActionResult> Index()
        {
            ProductTypeIndexViewModel model = new ProductTypeIndexViewModel(_context);
            var user = await GetCurrentUserAsync();

            return(View(model));

            // return View(await _context.ProductType.ToListAsync());
        }
        public ActionResult Create(ProductTypeIndexViewModel model)
        {
            try
            {
                if (CurrentUser.UserRole == Enums.UserRole.Viewer || IsCreatorPRD(CurrentUser.USER_ID) || CurrentUser.UserRole == Enums.UserRole.Controller)
                {
                    AddMessageInfo("Operation not allowed", Enums.MessageInfoType.Error);
                    return(RedirectToAction("Index"));
                }

                var obj = model.ViewModel;
                if (obj.IsDeleted == true)
                {
                    obj.IsDeleted = false;
                }
                else
                {
                    obj.IsDeleted = true;
                }
                obj.CreatedBy    = CurrentUser.USER_ID;
                obj.CreatedDate  = DateTime.Now;
                obj.ModifiedBy   = CurrentUser.USER_ID;
                obj.ModifiedDate = DateTime.Now;
                model.ViewModel  = obj;

                if (!ModelState.IsValid)
                {
                    AddMessageInfo("Operation not allowed", Enums.MessageInfoType.Error);
                }
                else
                {
                    obj.ApprovalStatus = _refService.GetReferenceByKey(ReferenceKeys.ApprovalStatus.Draft).REFF_ID;

                    var inserted = _productTypeService.Create(Mapper.Map <CustomService.Data.MASTER_PRODUCT_TYPE>(obj), (int)Enums.MenuList.ProductType, (int)Enums.ActionType.Created, (int)CurrentUser.UserRole, CurrentUser.USER_ID);

                    AddMessageInfo(Constans.SubmitMessage.Saved, Enums.MessageInfoType.Success);

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                AddMessageInfo("Save Failed : " + ex.Message, Enums.MessageInfoType.Error);
            }

            model = GenerateProperties(model, true);
            return(View(model));
        }
        private void ExecuteApprovalAction(ProductTypeIndexViewModel model, ReferenceKeys.ApprovalStatus statusApproval, Enums.ActionType actionType, string email, string subject, string sender, string display, string sendTo)
        {
            var comment = (model.ViewModel.RevisionData != null) ? model.ViewModel.RevisionData.Comment : null;
            var updated = _productTypeService.ChangeStatus(model.ViewModel.ProdCode, statusApproval, (int)Enums.MenuList.ProductType, (int)actionType, (int)CurrentUser.UserRole, CurrentUser.USER_ID, comment);

            if (updated != null)
            {
                //AddMessageInfo(Constans.SubmitMessage.Updated + " and sending email", Enums.MessageInfoType.Success);
                List <string> mailAddresses = new List <string>();
                if (statusApproval == ReferenceKeys.ApprovalStatus.AwaitingAdminApproval)
                {
                    var approvers = _refService.GetAdminApprovers().ToList();
                    foreach (var appr in approvers)
                    {
                        var _email = _refService.GetUserEmail(appr.REFF_VALUE.Trim());
                        if (!string.IsNullOrEmpty(_email))
                        {
                            mailAddresses.Add(_email);
                        }
                    }
                }
                else
                {
                    var admins = _refService.GetAdmins().ToList();
                    foreach (var adm in admins)
                    {
                        var _email = _refService.GetUserEmail(adm.USER_ID);
                        if (!string.IsNullOrEmpty(_email) && _email != sender)
                        {
                            mailAddresses.Add(_email);
                        }
                    }
                    //mailAddresses.Add(sendTo);
                }
                bool mailStatus = ItpiMailer.Instance.SendEmail(mailAddresses.ToArray(), null, null, null, subject, email, true, sender, display);
                if (!mailStatus)
                {
                    AddMessageInfo("Send email failed!", Enums.MessageInfoType.Warning);
                }
                else
                {
                    AddMessageInfo(Constans.SubmitMessage.Updated, Enums.MessageInfoType.Success);
                }
            }
        }
        private ProductTypeIndexViewModel GenerateProperties(ProductTypeIndexViewModel source, bool update)
        {
            var data = source;

            if (!update || data == null)
            {
                data = new ProductTypeIndexViewModel();
            }

            data.ViewModel.ProdCode = GenerateCodeSequence();

            data.MainMenu            = _mainMenu;
            data.CurrentMenu         = PageInfo;
            data.IsNotViewer         = CurrentUser.UserRole == Enums.UserRole.Administrator;
            data.ShowActionOptions   = data.IsNotViewer;
            data.EditMode            = false;
            data.EnableFormInput     = true;
            data.ViewModel.IsCreator = false;

            return(data);
        }
        private void ExecuteEdit(ProductTypeIndexViewModel model, ReferenceKeys.ApprovalStatus statusApproval, ReferenceKeys.EmailContent emailTemplate, bool sendEmail = false)
        {
            try
            {
                var obj  = model.ViewModel;
                var data = _productTypeService.Find(model.ViewModel.ProdCode);
                var old  = Mapper.Map <ProductTypeFormViewModel>(data);

                obj.CreatedBy   = old.CreatedBy;
                obj.CreatedDate = old.CreatedDate;

                if (statusApproval == ReferenceKeys.ApprovalStatus.Edited)
                {
                    // do nothing
                }
                else if (statusApproval == ReferenceKeys.ApprovalStatus.AwaitingAdminApproval)
                {
                    obj = old;
                }
                else if (statusApproval == ReferenceKeys.ApprovalStatus.Completed)
                {
                    obj = old;
                    obj.LastApprovedBy   = CurrentUser.USER_ID;
                    obj.LastApprovedDate = DateTime.Now;
                }

                if (obj.IsDeleted == true)
                {
                    obj.IsDeleted = false;
                }
                else
                {
                    obj.IsDeleted = true;
                }

                obj.ModifiedBy     = CurrentUser.USER_ID;
                obj.ModifiedDate   = DateTime.Now;
                obj.ApprovalStatus = _refService.GetReferenceByKey(statusApproval).REFF_ID;
                model.ViewModel    = obj;

                var parameters = new Dictionary <string, string>();

                parameters.Add("product_type", data.PRODUCT_TYPE);
                parameters.Add("date", DateTime.Now.ToString("dddd, MMM dd yyyy")); // without time
                //parameters.Add("date", DateTime.Now.ToString("dddd, MMM dd yyyy hh:mm:ss")); // with time
                parameters.Add("creator", String.Format("{0} {1}", data.CREATOR.FIRST_NAME, data.CREATOR.LAST_NAME));
                parameters.Add("approval_status", data.APPROVALSTATUS.REFF_VALUE);
                parameters.Add("url_detail", Url.Action("Detail", "ProductType", new { id = data.PROD_CODE }, this.Request.Url.Scheme));
                parameters.Add("url_approve", Url.Action("Approve", "ProductType", new { id = data.PROD_CODE }, this.Request.Url.Scheme));

                bool success = _productTypeService.Edit(Mapper.Map <CustomService.Data.MASTER_PRODUCT_TYPE>(obj), (int)Enums.MenuList.ProductType, (int)Enums.ActionType.Modified, (int)CurrentUser.UserRole, CurrentUser.USER_ID);
                if (success)
                {
                    if (sendEmail)
                    {
                        var mailContent = _refService.GetMailContent((int)emailTemplate, parameters);
                        var sender      = _refService.GetUserEmail(CurrentUser.USER_ID);
                        var display     = ReferenceLookup.Instance.GetReferenceKey(ReferenceKeys.EmailSender.AdminCreator);
                        var sendToId    = _refService.GetReferenceByKey(ReferenceKeys.Approver.AdminApprover).REFF_VALUE;
                        var sendTo      = _refService.GetUserEmail(sendToId);
                        AddMessageInfo(Constans.SubmitMessage.Updated + "<br />Sending email", Enums.MessageInfoType.Success);
                        bool mailStatus = ItpiMailer.Instance.SendEmail(new string[] { sendTo }, null, null, null, mailContent.EMAILSUBJECT, mailContent.EMAILCONTENT, true, sender, display);
                        if (!mailStatus)
                        {
                            AddMessageInfo("Send email failed! Please try again", Enums.MessageInfoType.Warning);
                        }
                        else
                        {
                            AddMessageInfo("Email sent!", Enums.MessageInfoType.Success);
                        }
                    }
                    else
                    {
                        AddMessageInfo(Constans.SubmitMessage.Updated, Enums.MessageInfoType.Success);
                    }
                }
                else
                {
                    AddMessageInfo("Submit failed! Please try again", Enums.MessageInfoType.Error);
                }
            }
            catch (Exception ex)
            {
                var msg = String.Format("Message: {0}\nStack Trace: {1}\nInner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException);
                AddMessageInfo(msg, Enums.MessageInfoType.Error);
            }
        }