public async Task <bool> AddUpdateCompanyStructureInfoAsync(cor_companystructure model) { if (model.CompanyId > 0) { var item = await _dataContext.cor_companystructure.FindAsync(model.CompanyId); _dataContext.Entry(item).CurrentValues.SetValues(model); } else { await _dataContext.cor_companystructure.AddAsync(model); } return(await _dataContext.SaveChangesAsync() > 0); }
public async Task <UserDataResponseObj> FetchLoggedInUserDetailsAsync(string userId) { try { var currentUser = await _userManager?.FindByIdAsync(userId); if (currentUser == null) { return new UserDataResponseObj { Status = new APIResponseStatus { IsSuccessful = true } } } ; cor_staff currentUserStaffDetails = null; cor_companystructure currentUserCompanyStructDetails = null; UserDataResponseObj profile = null; List <string> activities = new List <string>(); var additionalActivityIds = _dataContext.cor_userroleadditionalactivity.Where(x => x.UserId == currentUser.Id).Select(x => x.ActivityId); var userRoleActivityIds = (from a in _dataContext.UserRoles join b in _dataContext.cor_userrole on a.RoleId equals b.Id join c in _dataContext.cor_userroleactivity on b.Id equals c.RoleId where a.UserId == currentUser.Id select c.ActivityId).ToList(); if (userRoleActivityIds.Count() > 0) { activities = _dataContext.cor_activity .Where(x => additionalActivityIds .Contains(x.ActivityId) || userRoleActivityIds .Contains(x.ActivityId)).Select(x => x.ActivityName.ToLower()).ToList(); } var userRoles = await _userManager.GetRolesAsync(currentUser); if (userRoles.Contains(StaticRoles.GODP)) { activities = await _dataContext.cor_activity.Where(x => x.Deleted == false).Select(s => s.ActivityName).ToListAsync(); } if (currentUser.StaffId > 0) { currentUserStaffDetails = await _dataContext.cor_staff.FirstOrDefaultAsync(z => z.StaffId == currentUser.StaffId); currentUserCompanyStructDetails = await _dataContext.cor_companystructure.SingleOrDefaultAsync(m => m.CompanyStructureId == currentUserStaffDetails.StaffOfficeId); if (currentUserCompanyStructDetails != null) { return(profile = new UserDataResponseObj { BranchId = currentUserCompanyStructDetails.CompanyStructureId, StaffId = currentUserStaffDetails.StaffId, BranchName = currentUserCompanyStructDetails.Name, CompanyId = currentUserCompanyStructDetails.CompanyStructureId, CompanyName = currentUserCompanyStructDetails.Name, CountryId = currentUserStaffDetails.CountryId, CustomerName = currentUserStaffDetails.FirstName + " " + currentUserStaffDetails.LastName, StaffName = currentUserStaffDetails.FirstName + " " + currentUserStaffDetails.LastName, LastLoginDate = DateTime.Now, UserId = currentUser.Id, UserName = currentUser.UserName, Roles = await _userManager.GetRolesAsync(currentUser), Activities = activities.Count() < 1 ? null : activities, Email = currentUser.Email, DepartmentId = currentUserStaffDetails.StaffOfficeId ?? 0, Status = new APIResponseStatus { IsSuccessful = true } }); } } profile = new UserDataResponseObj { LastLoginDate = DateTime.Now, UserId = currentUser.Id, UserName = currentUser.UserName, StaffId = currentUser.StaffId, Activities = activities.Count() < 1 ? null : activities, Roles = await _userManager.GetRolesAsync(currentUser), Email = currentUser.Email, Status = new APIResponseStatus { IsSuccessful = true } }; if (profile == null) { return(new UserDataResponseObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Unable to fetch user details" } } }); } return(profile); } catch (Exception ex) { #region Log error var errorCode = ErrorID.Generate(4); _logger.Error($"ErrorID : FetchLoggedInUserDetailsAsync{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new UserDataResponseObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Please tyr again later", MessageId = errorCode, TechnicalMessage = $"ErrorID : FetchLoggedInUserDetailsAsync{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <bool> UploadCompanyStructureAsync(byte[] record, string createdBy) { try { if (record == null) { return(false); } List <CompanyStructureObj> uploadedRecord = new List <CompanyStructureObj>(); using (MemoryStream stream = new MemoryStream(record)) using (ExcelPackage excelPackage = new ExcelPackage(stream)) { //Use first sheet by default ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[1]; int totalRows = workSheet.Dimension.Rows; //First row is considered as the header for (int i = 2; i <= totalRows; i++) { uploadedRecord.Add(new CompanyStructureObj { Name = workSheet.Cells[i, 1].Value != null ? workSheet.Cells[i, 1].Value.ToString() : null, StructureTypeName = workSheet.Cells[i, 2].Value != null ? workSheet.Cells[i, 2].Value.ToString() : null, HeadStaffName = workSheet.Cells[i, 3].Value != null ? workSheet.Cells[i, 3].Value.ToString() : null, CountryName = workSheet.Cells[i, 4].Value != null ? workSheet.Cells[i, 4].Value.ToString() : null, ParentCompanyName = workSheet.Cells[i, 5].Value != null ? workSheet.Cells[i, 5].Value.ToString() : null, Address = workSheet.Cells[i, 6].Value != null ? workSheet.Cells[i, 6].Value.ToString() : null, }); } } List <cor_companystructure> structures = new List <cor_companystructure>(); if (uploadedRecord.Count > 0) { foreach (var item in uploadedRecord) { int structureTypeId = 0; int headStaffId = 0; int countryId = 0; int parentCompanyID = 0; if (item.StructureTypeName != null) { var data = await _dataContext.cor_companystructuredefinition.FirstOrDefaultAsync(x => x.Definition.ToLower().Contains(item.StructureTypeName.ToLower())); if (data != null) { structureTypeId = data.StructureDefinitionId; } } if (item.CountryName != null) { var data = await _dataContext.cor_country.FirstOrDefaultAsync(x => x.CountryName.ToLower().Contains(item.CountryName.ToLower())); if (data != null) { countryId = data.CountryId; } } if (item.HeadStaffName != null) { var data = await _dataContext.cor_staff.FirstOrDefaultAsync(x => x.FirstName.ToLower().Contains(item.HeadStaffName.ToLower())); if (data != null) { headStaffId = data.StaffId; } } if (item.ParentCompanyName != null) { var title = await _dataContext.cor_companystructure.FirstOrDefaultAsync(x => x.Name.ToLower().Contains(item.ParentCompanyName.ToLower())); if (title != null) { parentCompanyID = title.CompanyStructureId; } } var accountTypeExist = await _dataContext.cor_companystructure.FirstOrDefaultAsync(x => x.Name.ToLower() == item.Name.ToLower()); if (accountTypeExist != null) //if (structureTypeId != 0 && headStaffId != 0 && countryId != 0 && parentCompanyID != 0) { accountTypeExist.Name = item.Name; accountTypeExist.StructureTypeId = structureTypeId; accountTypeExist.CountryId = countryId; accountTypeExist.Address = item.Address; accountTypeExist.HeadStaffId = headStaffId; accountTypeExist.ParentCompanyID = parentCompanyID; accountTypeExist.Parent = item.ParentCompanyName; accountTypeExist.CompanyId = item.CompanyId ?? 0; accountTypeExist.Active = true; accountTypeExist.Deleted = false; accountTypeExist.UpdatedBy = item.UpdatedBy; accountTypeExist.UpdatedOn = DateTime.Now; } else { var accountType = new cor_companystructure { Name = item.Name, StructureTypeId = structureTypeId, CountryId = countryId, Address = item.Address, HeadStaffId = headStaffId, ParentCompanyID = parentCompanyID, Parent = item.ParentCompanyName, CompanyId = item.CompanyId ?? 0, Active = true, Deleted = false, CreatedBy = createdBy, CreatedOn = DateTime.Now, }; _dataContext.cor_companystructure.Add(accountType); } } } var response = _dataContext.SaveChanges() > 0; return(response); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <CompanyStructureRegRespObj> Handle(AddUpdateCompanyStructureInfoCommand request, CancellationToken cancellationToken) { try { var currentUserId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value; var user = await _userManger.FindByIdAsync(currentUserId); var structure = new cor_companystructure { Code = request.CompanyCode, CountryId = request.CountryId, Address = request.Address1, HeadStaffId = request.HeadStaffId, CompanyId = request.CompanyId, Address1 = request.Address1, Address2 = request.Address2, Telephone = request.Telephone, Fax = request.Fax, Email = request.Email, RegistrationNumber = request.RegistrationNumber, TaxId = request.TaxId, NoOfEmployees = request.NoOfEmployees, WebSite = request.WebSite, Logo = request.Logo, LogoType = request.LogoType, State = request.State, City = request.City, CurrencyId = request.CurrencyId, ReportCurrencyId = request.ReportingCurrencyId, ApplyRegistryTemplate = request.ApplyRegistryTemplate, RegistryTemplate = request.RegistryTemplate, PostalCode = request.PostalCode, IsMultiCompany = request.IsMultiCompany, Subsidairy_Level = request.Subsidairy_Level, Description = request.Description, Active = true, Deleted = false, CreatedBy = user.UserName, CreatedOn = DateTime.Now, ParentCompanyID = request.ParentCompanyID, Name = request.CountryName }; if (request.CompanyStructureId > 1) { request.CompanyStructureId = request.CompanyStructureId; } await _repo.AddUpdateCompanyStructureInfoAsync(structure); var actionTaken = request.CompanyId > 0 ? "updated" : "added"; return(new CompanyStructureRegRespObj { CompanyStructureId = structure.CompanyStructureId, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = $"Company structure successfully {actionTaken}", } } }); } catch (Exception ex) { #region Log error to file var errorCode = ErrorID.Generate(4); _logger.Error($"ErrorID : AddUpdateCompanyStructureInfoCommandHandler{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new CompanyStructureRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Unable to process item", MessageId = errorCode, TechnicalMessage = $"ErrorID : AddUpdateCompanyStructureInfoCommandHandler{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <CompanyStructureRegRespObj> Handle(AddUpdateCompanyStructureCommand request, CancellationToken cancellationToken) { try { var currentUserId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value; var user = await _userManger.FindByIdAsync(currentUserId); var logo = _httpContextAccessor.HttpContext.Request.Form.Files["logo"]; var fSTemplate = _httpContextAccessor.HttpContext.Request.Form.Files["fSTemplate"]; var logoBytes = new byte[0]; var fSTemplateBytes = new byte[0]; if (logo != null && logo.Length > 0) { using (var ms = new MemoryStream()) { await logo.CopyToAsync(ms); logoBytes = ms.ToArray(); } } if (fSTemplate != null && fSTemplate.Length > 0) { using (var ms = new MemoryStream()) { await fSTemplate.CopyToAsync(ms); fSTemplateBytes = ms.ToArray(); } } string parentName = null; if (request.ParentCompanyID > 0) { parentName = _dataContext.cor_companystructure.FirstOrDefault(x => x.CompanyStructureId == request.ParentCompanyID)?.Name; } var structure = new cor_companystructure { Name = request.Name, StructureTypeId = request.StructureTypeId, CountryId = request.CountryId, Address = request.Address, HeadStaffId = request.HeadStaffId, ParentCompanyID = request.ParentCompanyID, Parent = parentName, CompanyId = request.CompanyId ?? 0, Active = true, Deleted = false, CreatedBy = user.UserName, CreatedOn = DateTime.Now, UpdatedBy = user.UserName, UpdatedOn = DateTime.Now, CompanyStructureId = request.CompanyStructureId, Address1 = request.Address1, Address2 = request.Address2, ApplyRegistryTemplate = request.ApplyRegistryTemplate, City = request.City, Code = request.Code, CurrencyId = request.CurrencyId, Description = request.Description, Email = request.Email, Fax = request.Fax, IsMultiCompany = request.IsMultiCompany, Logo = logoBytes, FSTemplateName = fSTemplate?.Name, LogoType = request.LogoType, NoOfEmployees = request.NoOfEmployees, PostalCode = request.PostalCode, RegistryTemplate = request.RegistryTemplate, RegistrationNumber = request.RegistrationNumber, State = request.State, TaxId = request.TaxId, Telephone = request.Telephone, WebSite = request.WebSite, ReportCurrencyId = request.ReportCurrencyId, Subsidairy_Level = request.Subsidairy_Level, //FSTemplate = fSTemplateBytes }; await _repo.AddUpdateCompanyStructureAsync(structure); var actionTaken = request.CompanyId > 0 ? "updated" : "added"; return(new CompanyStructureRegRespObj { CompanyStructureId = structure.CompanyStructureId, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = $"Company structure successfully {actionTaken}", } } }); } catch (Exception ex) { #region Log error to file var errorCode = ErrorID.Generate(4); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new CompanyStructureRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Unable to process item", MessageId = errorCode, TechnicalMessage = $"ErrorID : AddUpdateCompanyStructureCommandHandler{errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <FileUploadRespObj> Handle(UploadCompanyStructureCommand request, CancellationToken cancellationToken) { var response = new FileUploadRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage() } }; List <CompanyStructureObj> uploadedRecord = new List <CompanyStructureObj>(); var files = _accessor.HttpContext.Request.Form.Files; var byteList = new List <byte[]>(); foreach (var fileBit in files) { if (fileBit.Length > 0) { using (var ms = new MemoryStream()) { await fileBit.CopyToAsync(ms); byteList.Add(ms.ToArray()); } } } try { if (byteList.Count() > 0) { foreach (var item in byteList) { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (MemoryStream stream = new MemoryStream(item)) using (ExcelPackage excelPackage = new ExcelPackage(stream)) { ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[0]; int totalRows = workSheet.Dimension.Rows; int totalColumns = workSheet.Dimension.Columns; if (totalColumns != 6) { response.Status.Message.FriendlyMessage = $"Six (6) Column Expected"; return(response); } for (int i = 2; i <= totalRows; i++) { uploadedRecord.Add(new CompanyStructureObj { ExcelLineNumber = i, Name = workSheet.Cells[i, 1].Value != null ? workSheet.Cells[i, 1].Value.ToString() : null, StructureTypeName = workSheet.Cells[i, 2].Value != null ? workSheet.Cells[i, 2].Value.ToString() : null, CountryName = workSheet.Cells[i, 3].Value != null ? workSheet.Cells[i, 3].Value.ToString() : null, ParentCompanyName = workSheet.Cells[i, 4].Value != null ? workSheet.Cells[i, 4].Value.ToString() : null, HeadStaffName = workSheet.Cells[i, 5].Value != null ? workSheet.Cells[i, 5].Value.ToString() : null, StaffCode = workSheet.Cells[i, 6].Value != null ? workSheet.Cells[i, 6].Value.ToString() : null, }); } } } } } catch (Exception ex) { response.Status.Message.FriendlyMessage = $" {ex?.Message}"; return(response); } List <cor_companystructure> structures = new List <cor_companystructure>(); var _CompanyStructureDefinition = await _repo.GetAllCompanyStructureDefinitionAsync(); var _CompanyStructure = await _repo.GetAllCompanyStructureAsync(); var _Common = await _comRepo.GetAllCountryAsync(); var _StaffList = await _adminRepo.GetAllStaffAsync(); try { if (uploadedRecord.Count > 0) { foreach (var item in uploadedRecord) { if (string.IsNullOrEmpty(item.Name)) { response.Status.Message.FriendlyMessage = $"Company Name cannot be empty detected on line {item.ExcelLineNumber}"; return(response); } if (string.IsNullOrEmpty(item.StaffCode)) { response.Status.Message.FriendlyMessage = $"Staff Code cannot be empty detected on line {item.ExcelLineNumber}"; return(response); } if (item.StructureTypeName != null) { var data = _CompanyStructureDefinition.FirstOrDefault(x => x.Definition.ToLower().Contains(item.StructureTypeName.ToLower())); if (data == null) { response.Status.Message.FriendlyMessage = $"UnIdentified Company Structure type detected on line {item.ExcelLineNumber}"; return(response); } item.StructureTypeId = data.StructureDefinitionId; } else { response.Status.Message.FriendlyMessage = $"Structure Type name cannot be empty detected on line {item.ExcelLineNumber}"; return(response); } if (item.CountryName != null) { var data = _Common.FirstOrDefault(x => x.CountryName.ToLower().Contains(item.CountryName.ToLower())); if (data == null) { response.Status.Message.FriendlyMessage = $"UnIdentified Country name detected on line {item.ExcelLineNumber}"; return(response); } item.CountryId = data.CountryId; } else { response.Status.Message.FriendlyMessage = $"Country name cannot be empty detected on line {item.ExcelLineNumber}"; return(response); } if (string.IsNullOrEmpty(item.HeadStaffName)) { response.Status.Message.FriendlyMessage = $"Staff Name can not be empty detected on line {item.ExcelLineNumber}"; return(response); } else { var firstname = item.HeadStaffName.Split(" ")[0]; var lastname = item.HeadStaffName.Split(" ")[1]; var staff = _StaffList.FirstOrDefault(a => a.FirstName.Trim().ToLower() == firstname.Trim().ToLower() && a.LastName.Trim().ToLower() == lastname.Trim().ToLower() && a.StaffCode.Trim().ToLower() == item.StaffCode.Trim().ToLower()); if (staff == null) { response.Status.Message.FriendlyMessage = $"Unidentified Head Staff Detected on line {item.ExcelLineNumber}"; return(response); } item.HeadStaffId = staff.StaffId; } if (string.IsNullOrEmpty(item.ParentCompanyName) && item.ExcelLineNumber > 2) { response.Status.Message.FriendlyMessage = $"Parent Company Name can not be empty detected on line {item.ExcelLineNumber}"; return(response); } else { var title = _CompanyStructure.FirstOrDefault(x => x.Name.ToLower().Contains(item.ParentCompanyName.ToLower())); if (title == null) { response.Status.Message.FriendlyMessage = $"Parent Company name detected on line {item.ExcelLineNumber}"; return(response); } item.ParentCompanyID = title.CompanyStructureId; } var compStructFrmRepo = _CompanyStructure.FirstOrDefault(x => x.Name.ToLower() == item.Name.ToLower()); if (compStructFrmRepo != null) { compStructFrmRepo.CompanyStructureId = compStructFrmRepo.CompanyStructureId; compStructFrmRepo.Name = item.Name; compStructFrmRepo.StructureTypeId = item.StructureTypeId; compStructFrmRepo.HeadStaffId = item.HeadStaffId; compStructFrmRepo.CountryId = item.CountryId; compStructFrmRepo.ParentCompanyID = item.ParentCompanyID; await _repo.AddUpdateCompanyStructureAsync(compStructFrmRepo); } else { var newStructFrmRepo = new cor_companystructure(); newStructFrmRepo.Name = item.Name; newStructFrmRepo.StructureTypeId = item.StructureTypeId; newStructFrmRepo.HeadStaffId = item.HeadStaffId; newStructFrmRepo.CountryId = item.CountryId; newStructFrmRepo.ParentCompanyID = item.ParentCompanyID; await _repo.AddUpdateCompanyStructureAsync(newStructFrmRepo); } } } response.Status.IsSuccessful = true; response.Status.Message.FriendlyMessage = "Successful"; return(response); } catch (Exception ex) { response.Status.IsSuccessful = true; response.Status.Message.FriendlyMessage = ex?.Message; return(response); } }