public async Task <SuppliertypeRegRespObj> Handle(AddUpdateSuppliertypeCommand request, CancellationToken cancellationToken) { try { var user = await _serverRequest.UserDataAsync(); cor_suppliertype sup = new cor_suppliertype(); sup.Deleted = true; sup.CreatedOn = request.SupplierTypeId > 0 ? (DateTime?)null : DateTime.Now; sup.CreatedBy = user.UserName; sup.UpdatedBy = user.UserName; sup.SupplierTypeName = request.SupplierTypeName; sup.TaxApplicable = string.Join(',', request.TaxApplicable); sup.CreditGL = request.CreditGL; sup.DebitGL = request.DebitGL; sup.Deleted = false; sup.SupplierTypeId = request.SupplierTypeId > 0 ? request.SupplierTypeId : 0; await _supRepo.AddUpdateSupplierTypeAsync(sup); var actionTaken = request.SupplierTypeId < 1 ? "created" : "updated"; return(new SuppliertypeRegRespObj { SuppliertypeId = sup.SupplierTypeId, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = $"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 SuppliertypeRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Unable to process request", MessageId = errorCode, TechnicalMessage = $"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <FileUploadRespObj> Handle(UploadSupplierTypeCommand request, CancellationToken cancellationToken) { try { var apiResponse = new FileUploadRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage() } }; 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()); } } } var user = await _serverRequest.UserDataAsync(); var uploadedRecord = new List <SupplierTypeObj>(); ExcelPackage.LicenseContext = LicenseContext.NonCommercial; if (byteList.Count() > 0) { foreach (var byteItem in byteList) { using (MemoryStream stream = new MemoryStream(byteItem)) using (ExcelPackage excelPackage = new ExcelPackage(stream)) { ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[0]; int totalRows = workSheet.Dimension.Rows; int totalColumns = workSheet.Dimension.Columns; if (totalColumns != 2) { apiResponse.Status.Message.FriendlyMessage = "2 Columns Expected"; return(apiResponse); } for (int i = 2; i <= totalRows; i++) { var item = new SupplierTypeObj { ExcelLineNumber = i, SupplierTypeName = workSheet.Cells[i, 1]?.Value != null ? workSheet.Cells[i, 1]?.Value.ToString() : string.Empty, TaxApplicableName = workSheet.Cells[i, 2]?.Value != null ? workSheet.Cells[i, 2]?.Value.ToString() : string.Empty, }; uploadedRecord.Add(item); } } } } var ItemList = await _repo.GetAllSupplierTypeAsync(); var TaxList = await _repo.GetAllTaxSetupAsync(); if (uploadedRecord.Count > 0) { var listOftaxt = new List <int>(); foreach (var item in uploadedRecord) { if (string.IsNullOrEmpty(item.TaxApplicableName)) { apiResponse.Status.Message.FriendlyMessage = $"No Tax Applicable found Detected on line {item.ExcelLineNumber}"; return(apiResponse); } else { var taxNames = item.TaxApplicableName.Trim().ToLower().Split(','); foreach (var tx in taxNames) { var taxes = TaxList.FirstOrDefault(a => taxNames.Contains(a.TaxName.Trim().ToLower())); if (taxes == null) { apiResponse.Status.Message.FriendlyMessage = $"Unidentified Tax name Detected on line {item.ExcelLineNumber}"; return(apiResponse); } listOftaxt.Add(taxes.TaxSetupId); } } if (string.IsNullOrEmpty(item.SupplierTypeName)) { apiResponse.Status.Message.FriendlyMessage = $"Empty Supplier Type Name Detected on line {item.ExcelLineNumber}"; return(apiResponse); } var itemFrmRepo = ItemList.FirstOrDefault(c => c.SupplierTypeName.ToLower().Trim() == item.SupplierTypeName.ToLower().Trim()); if (itemFrmRepo != null) { itemFrmRepo.SupplierTypeName = item?.SupplierTypeName; itemFrmRepo.TaxApplicable = string.Join(',', listOftaxt); await _repo.AddUpdateSupplierTypeAsync(itemFrmRepo); } else { var newItem = new cor_suppliertype { SupplierTypeName = item?.SupplierTypeName, TaxApplicable = string.Join(',', listOftaxt), }; await _repo.AddUpdateSupplierTypeAsync(newItem); } } } apiResponse.Status.IsSuccessful = true; apiResponse.Status.Message.FriendlyMessage = "Successful"; return(apiResponse); } catch (Exception ex) { throw ex; } }