Example #1
0
        public JsonResult Update([FromBody] DocumentTypeModel obj)
        {
            var msg = new JMessage {
                Title = "", Error = false
            };

            try
            {
                var item = _context.DispatchesCategorys.FirstOrDefault(x => x.Id == obj.Id && x.IsDeleted == false);
                if (item != null)
                {
                    var data = _context.DispatchesCategorys.FirstOrDefault(x => x.Code == obj.Code && x.IsDeleted == false && x.Type == EnumHelper <DocumentTypeEnum> .GetDisplayValue(DocumentTypeEnum.LVB));
                    if (data == null || (data != null && data.Id == item.Id))
                    {
                        item.Code           = obj.Code;
                        item.Name           = obj.Name;
                        item.ExpriedProcess = obj.ExpriedProcess;
                        item.UpdatedTime    = DateTime.Now;
                        item.UpdatedBy      = ESEIM.AppContext.UserName;
                        _context.Update(item);
                        _context.SaveChanges();
                        msg.Title = "Cập nhập loại văn bản thành công";
                    }
                    else
                    {
                        msg.Error = true;
                        msg.Title = "Mã loại văn bản đã dùng cho loại văn bản trọng khác, vui lòng thử lại!";
                    }
                }
                else
                {
                    msg.Error = true;
                    msg.Title = "Loại văn bản không tồn tại, vui lòng làm mới trang";
                }
            }
            catch
            {
                msg.Error = true;
                msg.Title = "Cập nhập loại văn bản bị lỗi!";
            }
            return(Json(msg));
        }
Example #2
0
        public JsonResult Update([FromBody] CategoryModel obj)
        {
            var msg = new JMessage {
                Title = "", Error = false
            };

            try
            {
                var item = _context.DispatchesCategorys.FirstOrDefault(x => x.Id == obj.Id && x.IsDeleted == false);
                if (item != null)
                {
                    var data = _context.DispatchesCategorys.FirstOrDefault(x => x.Code == obj.Code && x.IsDeleted == false && x.Type == EnumHelper <DocumentTypeEnum> .GetDisplayValue(DocumentTypeEnum.DM));
                    if (data == null || (data != null && data.Id == item.Id))
                    {
                        item.Code        = obj.Code;
                        item.Name        = obj.Name;
                        item.UpdatedTime = DateTime.Now;
                        item.UpdatedBy   = ESEIM.AppContext.UserName;
                        _context.Update(item);
                        _context.SaveChanges();
                        msg.Title = String.Format(CommonUtil.ResourceValue("COM_MSG_UPDATE_SUCCESS"), CommonUtil.ResourceValue("DDC_MSG_TITLE_DDC"));
                    }
                    else
                    {
                        msg.Error = true;
                        msg.Title = String.Format(CommonUtil.ResourceValue("DDC_ERR_CODE_NOT_DDC"));
                    }
                }
                else
                {
                    msg.Error = true;
                    msg.Title = String.Format(CommonUtil.ResourceValue("DDC_ERR_COCUMENT_REFESH"));
                }
            }
            catch
            {
                msg.Error = true;
                msg.Title = String.Format(CommonUtil.ResourceValue("COM_MSG_UPDATE_FAILED"), CommonUtil.ResourceValue("DDC_MSG_TITLE_DDC"));
            }
            return(Json(msg));
        }
        public JsonResult Delete([FromBody] List <int> ids)
        {
            var msg = new JMessage {
                Error = false, Title = ""
            };

            try
            {
                int success = 0;
                foreach (var item in ids)
                {
                    var data = _context.DispatchesCategorys.FirstOrDefault(x => x.Id == item);
                    var checkExistDispatches = _context.DispatchesHeaders.FirstOrDefault(x => x.DocumentCode == data.Code);
                    if (checkExistDispatches != null)
                    {
                        msg.Title = String.Format(CommonUtil.ResourceValue("DCD_ERR_DOCUMENT_EXIST"));
                    }
                    else
                    {
                        data.IsDeleted = true;
                        _context.Update(data);
                        _context.SaveChanges();
                        success++;
                    }
                }
                if (success != 0)
                {
                    msg.Title = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_SUCCESS"), CommonUtil.ResourceValue("DCD_MSG_TITLE_DCD"));
                }
                else
                {
                    msg.Error = true;
                }
            }
            catch (Exception ex)
            {
                msg.Title = String.Format(CommonUtil.ResourceValue("COM_MSG_DELETE_FAIL"), CommonUtil.ResourceValue("DCD_MSG_TITLE_DCD"));
                msg.Error = true;
            }
            return(Json(msg));
        }
Example #4
0
        public async Task <IActionResult> UpdateBranchReference([FromBody] SearchBranchModel search)
        {
            var msg = new JMessage()
            {
                Error = false
            };

            try
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == search.UserId);

                if (user != null)
                {
                    var oldBranchRef = user.OrgReference;
                    user.OrgReference = search.ListBranch.Count > 0 ? string.Join(",", search.ListBranch) : "";
                    user.UpdatedDate  = DateTime.Now;
                    user.UpdatedBy    = ESEIM.AppContext.UserName;
                    _context.Update(user);
                    await _context.SaveChangesAsync();

                    msg.Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_SUCCESS"), CommonUtil.ResourceValue("BRANCH").ToLower());
                    _actionLog.InsertActionLog("ASP_NET_USERS", "Update success branch reference of user: "******"Update");
                }
                else
                {
                    msg.Error = true;
                    msg.Title = string.Format(CommonUtil.ResourceValue("NOT_EXIST_ITEM"), CommonUtil.ResourceValue("USER").ToLower());
                }
            }
            catch (Exception e)
            {
                msg.Error = true;
                msg.Title = "An error occurred while update";
                _actionLog.InsertActionLog("ASP_NET_USERS", "Update failed branch reference of user", null, null, "Error");
            }

            return(Json(msg));
        }
Example #5
0
        public object Update([FromBody] FundCurrency data)
        {
            var msg = new JMessage()
            {
                Error = false
            };

            try
            {
                var obj  = _context.FundCurrencys.FirstOrDefault(x => x.CurrencyCode.ToLower() == data.CurrencyCode.ToLower() && x.Id != data.Id && x.IsDeleted == false);
                var obj1 = _context.FundCurrencys.FirstOrDefault(x => x.CurrencyCode.ToLower() == data.CurrencyCode.ToLower() && x.Id != data.Id && x.IsDeleted == true);
                var obj2 = _context.FundCurrencys.FirstOrDefault(x => x.Id != data.Id);

                if (obj != null)
                {
                    msg.Error = true;
                    msg.Title = "Đã tồn tại loại tiền !";
                }
                else if (obj1 != null)
                {
                    var query = _context.FundCurrencys.ToList();
                    foreach (var item in query)
                    {
                        if (item.Id == data.Id)
                        {
                            item.IsDeleted      = true;
                            item.DefaultPayment = false;
                            _context.FundCurrencys.Update(item);
                        }
                        else if (item.CurrencyCode.ToLower() == data.CurrencyCode.ToLower())
                        {
                            item.IsDeleted      = false;
                            item.DefaultPayment = data.DefaultPayment;
                            _context.FundCurrencys.Update(item);
                        }
                    }
                    _context.SaveChanges();
                    msg.Title = "Cập nhật thành công";
                }
                else
                {
                    var listObj = _context.FundCurrencys.Where(x => !x.IsDeleted).ToList();
                    foreach (var item in listObj)
                    {
                        if (item.CurrencyCode.ToLower() == data.CurrencyCode.ToLower())
                        {
                            if (data.DefaultPayment == true)
                            {
                                item.CurrencyCode   = data.CurrencyCode;
                                item.DefaultPayment = true;
                                item.Note           = data.Note;
                                item.UpdatedBy      = ESEIM.AppContext.UserName;
                                item.UpdatedTime    = DateTime.Now;
                                _context.FundCurrencys.Update(item);
                            }
                            else
                            {
                                item.CurrencyCode   = data.CurrencyCode;
                                item.DefaultPayment = false;
                                item.Note           = data.Note;
                                item.UpdatedBy      = ESEIM.AppContext.UserName;
                                item.UpdatedTime    = DateTime.Now;
                                _context.FundCurrencys.Update(item);
                            }
                        }
                        else
                        {
                            if (data.DefaultPayment == true)
                            {
                                item.DefaultPayment = false;
                                _context.FundCurrencys.Update(item);
                            }
                            else
                            {
                                var query1 = _context.FundCurrencys.FirstOrDefault(x => x.IsDeleted == false && x.CurrencyCode.ToLower() != data.CurrencyCode.ToLower());
                                query1.DefaultPayment = true;
                                _context.Update(query1);
                            }
                        }
                    }

                    _context.SaveChanges();
                    msg.Title = "Cập nhật thành công";
                }
            }
            catch (Exception ex)
            {
                msg.Error = true;
                msg.Title = String.Format(CommonUtil.ResourceValue("FCC_MSG_UPDATE_ERROR"));//"Có lỗi xảy ra khi cập nhật!";
            }
            return(Json(msg));
        }
Example #6
0
        public async Task <IActionResult> UpdateUserInOrg([FromBody] OrgUserModel model)
        {
            var msg = new JMessage()
            {
                Error = false
            };

            try
            {
                if (string.IsNullOrEmpty(model.OrgAddonCode))
                {
                    msg.Error = true;
                    msg.Title = string.Format(CommonUtil.ResourceValue("ERR_REQUIRED"), CommonUtil.ResourceValue("BRANCH").ToLower());
                }
                else
                {
                    var org = await _context.AdOrganizations.FirstOrDefaultAsync(x => x.OrgAddonCode == model.OrgAddonCode);

                    if (org == null)
                    {
                        msg.Error = true;
                        msg.Title = model.OrgAddonCode.StartsWith("d_") ? "This is Division, please select branch level!" : "Branch is not exists!";
                    }
                    else
                    {
                        if (model.ListUser.Count > 0)
                        {
                            // Add user to branch
                            var listAdd = _context.Users.Where(x => model.ListUser.Any(y => y == x.Id) && x.BranchId != model.OrgAddonCode);
                            if (listAdd.Any())
                            {
                                foreach (var user in listAdd)
                                {
                                    if (user.BranchId == model.OrgAddonCode)
                                    {
                                        continue;
                                    }
                                    var oldBranch = user.BranchId;
                                    user.BranchId    = model.OrgAddonCode;
                                    user.UpdatedDate = DateTime.Now;
                                    user.UpdatedBy   = ESEIM.AppContext.UserName;
                                    _context.Update(user);

                                    //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName);
                                }
                            }
                            // Remove user out branch
                            var listDel = _context.Users.Where(x => model.ListUser.All(y => y != x.Id) && x.BranchId == model.OrgAddonCode);
                            if (listDel.Any())
                            {
                                foreach (var user in listDel)
                                {
                                    var oldBranch = user.BranchId;
                                    user.BranchId    = null;
                                    user.UpdatedDate = DateTime.Now;
                                    user.UpdatedBy   = ESEIM.AppContext.UserName;
                                    _context.Update(user);

                                    //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName);
                                }
                            }
                        }
                        else
                        {
                            var listUser = _context.Users.Where(x => x.BranchId == model.OrgAddonCode);
                            if (listUser.Any())
                            {
                                foreach (var user in listUser)
                                {
                                    var oldBranch = user.BranchId;
                                    user.BranchId    = null;
                                    user.UpdatedDate = DateTime.Now;
                                    user.UpdatedBy   = ESEIM.AppContext.UserName;
                                    _context.Update(user);

                                    //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName);
                                }
                            }
                        }

                        await _context.SaveChangesAsync();

                        msg.Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_SUCCESS"), "tài khoản");
                    }
                }
            }
            catch (Exception e)
            {
                msg.Error = true;
                msg.Title = "Lỗi khi cập nhập";
                _actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user failed: " + e.Message, null, null, "Error");
            }

            return(Json(msg));
        }
        public async Task <JsonResult> UpdateUserPermission([FromBody] UserPerModel model)
        {
            JMessage msg = new JMessage {
                Error = true, Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_FAIL"), CommonUtil.ResourceValue("PERMISSION").ToLower())
            };
            string userName = string.Empty;

            try
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == model.UserId);

                if (user != null)
                {
                    var app = await _context.AdApplications.FirstOrDefaultAsync(x => x.ApplicationCode == model.AppCode);

                    if (app != null)
                    {
                        // Update user is exceeded permission
                        user.IsExceeded = model.IsExceeded;

                        //// Update Branch Reference
                        //var oldBranchRef = user.OrgReference;
                        //user.OrgReference = model.BranchRefs.Count > 0 ? string.Join(",", model.BranchRefs) : "";
                        user.UpdatedDate = DateTime.Now;
                        user.UpdatedBy   = ESEIM.AppContext.UserName;
                        _context.Update(user);
                        //_actionLog.InsertActionLog("ASP_NET_USERS", "Update success branch reference of user: "******"Update", false, user.UserName);

                        // Update Group User and Permission
                        var listOldGroup         = _context.AdUserInGroups.Where(x => x.UserId == user.Id && x.ApplicationCode == app.ApplicationCode).ToList();
                        var listOldPermissionAll = _context.AdPermissions.Where(x => x.ApplicationCode == app.ApplicationCode && x.UserId == user.Id && listOldGroup.Any(y => y.GroupUserCode == x.GroupUserCode)).ToList();

                        if (model.GroupUsers.Count > 0)
                        {
                            var listOldGroupDel = listOldGroup.Where(x => model.GroupUsers.All(y => y.GroupCode != x.GroupUserCode)).ToList();
                            _context.RemoveRange(listOldGroupDel);      // Remove all old group not selected
                            var listOldPermissionDel = listOldPermissionAll.Where(x => model.GroupUsers.All(y => y.GroupCode != x.GroupUserCode || (y.GroupCode == x.GroupUserCode && y.RoleId != x.RoleId))).ToList();
                            _context.RemoveRange(listOldPermissionDel); // Remove all old permission

                            var listPermissionAll     = _context.AdPermissions.Where(x => x.ApplicationCode == model.AppCode && model.GroupUsers.Any(y => y.GroupCode == x.GroupUserCode && y.RoleId == x.RoleId)).ToList();
                            var listPermissionDefault = listPermissionAll.Where(x => x.UserId == null).ToList();
                            var listPermissionUser    = listPermissionAll.Where(x => x.UserId == model.UserId).ToList();

                            foreach (var groupUser in model.GroupUsers)
                            {
                                var oldGroup            = listOldGroup.FirstOrDefault(x => x.GroupUserCode == groupUser.GroupCode && x.ApplicationCode == model.AppCode);
                                var listPerDefaultGroup = listPermissionDefault.Where(x => x.GroupUserCode == groupUser.GroupCode && x.ApplicationCode == model.AppCode);
                                if (oldGroup != null)
                                {
                                    //if (groupUser.RoleId != oldGroup.RoleId)
                                    //{
                                    // Update user in group
                                    oldGroup.BranchReference = model.BranchRefs.Count > 0 ? string.Join(",", model.BranchRefs) : "";
                                    if (!oldGroup.IsMain)
                                    {
                                        oldGroup.RoleId = groupUser.RoleId;
                                    }
                                    oldGroup.GrantAll = true;
                                    _context.Update(oldGroup); // Update entity
                                    //}
                                }
                                else
                                {
                                    // Add user to group
                                    var userInGroup = new AdUserInGroup();
                                    userInGroup.UserId          = model.UserId;
                                    userInGroup.GroupUserCode   = groupUser.GroupCode;
                                    userInGroup.ApplicationCode = model.AppCode;
                                    userInGroup.RoleId          = groupUser.RoleId;
                                    userInGroup.GrantAll        = true;
                                    userInGroup.BranchReference = model.BranchRefs.Count > 0 ? string.Join(",", model.BranchRefs) : "";
                                    _context.Add(userInGroup); // Add entity
                                }

                                // Add or Update permission
                                if (groupUser.Resources != null && groupUser.Resources.Count > 0)
                                {
                                    groupUser.Resources = groupUser.Resources.Where(x => x.HasPermission && !x.IsFunction).ToList();
                                    // Get all permission need remove
                                    var permissionDel = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupCode && x.RoleId == groupUser.RoleId && !groupUser.Resources.Any(y => y.FunctionCode == x.FunctionCode && y.Code == x.ResourceCode));
                                    _context.RemoveRange(permissionDel); // Remove all permission not in selected
                                    // Get all permission need update
                                    var permissionUpdate = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupCode && x.RoleId == groupUser.RoleId && groupUser.Resources.Any(y => y.FunctionCode == x.FunctionCode && y.Code == x.ResourceCode)).ToList();
                                    if (permissionUpdate.Count > 0)
                                    {
                                        foreach (var perUpdate in permissionUpdate)
                                        {
                                            var resUpdate = groupUser.Resources.FirstOrDefault(y => y.FunctionCode == perUpdate.FunctionCode && y.Code == perUpdate.ResourceCode);
                                            perUpdate.ExpiredDate = resUpdate?.ExpiredDate;
                                        }
                                    }
                                    // Get all permission need add
                                    var permissionAdd = groupUser.Resources.Where(x => !listPermissionUser.Any(y => y.FunctionCode == x.FunctionCode && y.ResourceCode == x.Code && y.GroupUserCode == groupUser.GroupCode && y.RoleId == groupUser.RoleId))
                                                        .Select(x => new AdPermission
                                    {
                                        ApplicationCode = model.AppCode,
                                        FunctionCode    = x.FunctionCode,
                                        ResourceCode    = x.Code,
                                        GroupUserCode   = groupUser.GroupCode,
                                        UserId          = model.UserId,
                                        RoleId          = groupUser.RoleId,
                                        ExpiredDate     = x.ExpiredDate,
                                    }).ToList();
                                    _context.AddRange(permissionAdd); // Add entity
                                }
                                else
                                {
                                    //var permissionDel = listPermissionUser.Where(x => x.GroupUserCode == groupUser.GroupCode && x.RoleId == groupUser.RoleId && !listPerDefaultGroup.Any(y => y.FunctionCode == x.FunctionCode && y.ResourceCode == x.ResourceCode));
                                    //_context.RemoveRange(permissionDel); // Remove all permission not in selected

                                    var permissionAdd = listPerDefaultGroup.Where(x => !listPermissionUser.Any(y => y.FunctionCode == x.FunctionCode && y.ResourceCode == x.ResourceCode && y.GroupUserCode == groupUser.GroupCode && y.RoleId == groupUser.RoleId))
                                                        .Select(x => new AdPermission
                                    {
                                        ApplicationCode = x.ApplicationCode,
                                        FunctionCode    = x.FunctionCode,
                                        ResourceCode    = x.ResourceCode,
                                        GroupUserCode   = x.GroupUserCode,
                                        UserId          = model.UserId,
                                        RoleId          = x.RoleId,
                                    }).ToList();
                                    _context.AddRange(permissionAdd); // Add entity
                                }
                            }
                        }
                        else
                        {
                            _context.RemoveRange(listOldGroup);         // Remove all old group
                            _context.RemoveRange(listOldPermissionAll); // Remove all old permission
                        }
                        //_actionLog.InsertActionLog("VIB_PERMISSION", "Update success permission for user: "******"Update", false, user.UserName);

                        var result = await _context.SaveChangesAsync();

                        msg.Error = false;
                        msg.Title = "Update user permission successfully";
                    }
                    else
                    {
                        msg.Title = "Application is not exists in system!";
                    }
                }
                else
                {
                    msg.Title = "User is not exists in system!";
                }
            }
            catch (Exception ex)
            {
                //_actionLog.InsertActionLog("VIB_PERMISSION", "Update failed permission for user " + userName + " : " + ex.Message, null, null, "Error", true, userName);
                msg.Object = ex;
            }

            return(Json(msg));
        }