public async Task <ActionResult <InfProductRegRespObj> > AddUpdateProduct([FromBody] AddUpdateInfProductObj entity) { try { var user = await _serverRequest.UserDataAsync(); InfProductObj item = null; if (entity.ProductId > 0) { item = _repo.GetProduct(entity.ProductId); if (item == null) { return new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Item does not Exist" } } } } ; } var domainObj = new inf_product(); domainObj.ProductId = entity.ProductId > 0 ? entity.ProductId : 0; domainObj.ProductCode = entity.ProductCode; domainObj.ProductName = entity.ProductName; domainObj.Rate = entity.Rate; domainObj.ProductTypeId = entity.ProductTypeId; domainObj.ProductLimit = entity.ProductLimit; domainObj.ProductLimitId = entity.ProductLimitId; domainObj.InterestRateMax = entity.InterestRateMax; domainObj.InterestRepaymentTypeId = entity.InterestRepaymentTypeId; domainObj.ScheduleMethodId = entity.ScheduleMethodId; domainObj.FrequencyId = entity.FrequencyId; domainObj.MaximumPeriod = entity.MaximumPeriod; domainObj.InterestRateAnnual = entity.InterestRateAnnual; domainObj.InterestRateFrequency = entity.InterestRateFrequency; domainObj.ProductPrincipalGl = entity.ProductPrincipalGl; domainObj.ReceiverPrincipalGl = entity.ReceiverPrincipalGl; domainObj.InterstExpenseGl = entity.InterstExpenseGl; domainObj.InterestPayableGl = entity.InterestPayableGl; domainObj.EarlyTerminationCharge = entity.EarlyTerminationCharge; domainObj.Active = true; domainObj.Createdby = user.UserName; domainObj.Createdon = DateTime.Today; domainObj.Deleted = false; domainObj.Updatedby = user.UserName; domainObj.Updatedon = entity.ProductId > 0 ? DateTime.Today : DateTime.Today; var isDone = _repo.AddUpdateProduct(domainObj); return(new InfProductRegRespObj { ProductId = domainObj.ProductId, Status = new APIResponseStatus { IsSuccessful = isDone ? true : false, Message = new APIResponseMessage { FriendlyMessage = isDone ? "Successful" : "Unsuccessful" } } }); } catch (Exception ex) { var errorCode = ErrorID.Generate(5); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode } } }); } }
public InfProductRegRespObj UploadProduct(List <byte[]> record, string createdBy) { try { List <InfProductObj> uploadedRecord = new List <InfProductObj>(); if (record == null) { return new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Unsuccessful" } } } } ; ExcelPackage.LicenseContext = LicenseContext.NonCommercial; if (record.Count() > 0) { foreach (var byteItem in record) { using (MemoryStream stream = new MemoryStream(byteItem)) using (ExcelPackage excelPackage = new ExcelPackage(stream)) { ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[0]; int totalRows = workSheet.Dimension.Rows; for (int i = 2; i <= totalRows; i++) { var item = new InfProductObj { ProductCode = workSheet.Cells[i, 1].Value != null ? workSheet.Cells[i, 1].Value.ToString() : null, ProductName = workSheet.Cells[i, 2].Value != null ? workSheet.Cells[i, 2].Value.ToString() : null, ProductTypeName = workSheet.Cells[i, 3].Value != null ? workSheet.Cells[i, 3].Value.ToString() : null, ProductLimit = workSheet.Cells[i, 4].Value != null?int.Parse(workSheet.Cells[i, 4].Value.ToString()) : 0, InterestRepaymentTypeName = workSheet.Cells[i, 5].Value != null ? workSheet.Cells[i, 5].Value.ToString() : null, ScheduleMethodName = workSheet.Cells[i, 6].Value != null ? workSheet.Cells[i, 6].Value.ToString() : null, FrequencyName = workSheet.Cells[i, 7].Value != null ? workSheet.Cells[i, 7].Value.ToString() : null, MaximumPeriod = workSheet.Cells[i, 8].Value != null?decimal.Parse(workSheet.Cells[i, 8].Value.ToString()) : 0, InterestRateAnnual = workSheet.Cells[i, 9].Value != null?decimal.Parse(workSheet.Cells[i, 9].Value.ToString()) : 0, EarlyTerminationCharge = workSheet.Cells[i, 10].Value != null?decimal.Parse(workSheet.Cells[i, 10].Value.ToString()) : 0, }; uploadedRecord.Add(item); } } } } if (uploadedRecord.Count > 0) { foreach (var item in uploadedRecord) { if (item.ProductName == "" || item.InterestRepaymentTypeName == "" || item.ProductTypeName == "" || item.FrequencyName == "" || item.ScheduleMethodName == "") { return(new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Please include all fields" } } }); } var productId = 0; var productobj = _dataContext.inf_product.Where(x => x.ProductName.ToLower().Trim() == item.ProductName.ToLower().Trim()).FirstOrDefault(); if (productobj != null) { productId = productobj.ProductId; } var productType = _dataContext.inf_producttype.Where(x => x.Name.ToLower().Trim() == item.ProductTypeName.ToLower().Trim()).FirstOrDefault(); if (productType == null) { return(new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Please include a valid product type name" } } }); } var frequencyType = _dataContext.credit_frequencytype.Where(x => x.Mode.ToLower().Trim() == item.FrequencyName.ToLower().Trim()).FirstOrDefault(); if (frequencyType == null) { return(new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Please include a valid frequency type name" } } }); } var interestRepayment = _dataContext.credit_repaymenttype.Where(x => x.RepaymentTypeName.ToLower().Trim() == item.InterestRepaymentTypeName.ToLower().Trim()).FirstOrDefault(); if (interestRepayment == null) { return(new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Please include a valid interestRepayment type name" } } }); } var ScheduleType = _dataContext.credit_loanscheduletype.Where(x => x.LoanScheduleTypeName.ToLower().Trim() == item.ScheduleMethodName.ToLower().Trim()).FirstOrDefault(); if (ScheduleType == null) { return(new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Please include a valid ScheduleType name" } } }); } var productexist = _dataContext.inf_product.Find(productId); if (productexist != null) { productexist.ProductCode = item.ProductCode; productexist.ProductName = item.ProductName; productexist.ProductTypeId = productType.ProductTypeId; productexist.ProductLimit = item.ProductLimit; productexist.InterestRepaymentTypeId = interestRepayment.RepaymentTypeId; productexist.ScheduleMethodId = ScheduleType.LoanScheduleTypeId; productexist.FrequencyId = frequencyType.FrequencyTypeId; productexist.MaximumPeriod = item.MaximumPeriod; productexist.InterestRateAnnual = item.InterestRateAnnual; productexist.ProductPrincipalGl = item.ProductPrincipalGl; productexist.ReceiverPrincipalGl = item.ReceiverPrincipalGl; productexist.InterstExpenseGl = item.InterstExpenseGl; productexist.InterestPayableGl = item.InterestPayableGl; productexist.EarlyTerminationCharge = item.EarlyTerminationCharge; productexist.Active = true; productexist.Deleted = false; productexist.Updatedby = createdBy; productexist.Updatedon = DateTime.Now; } else { var product = new inf_product { ProductCode = item.ProductCode, ProductName = item.ProductName, ProductTypeId = productType.ProductTypeId, ProductLimit = item.ProductLimit, InterestRepaymentTypeId = interestRepayment.RepaymentTypeId, ScheduleMethodId = ScheduleType.LoanScheduleTypeId, FrequencyId = frequencyType.FrequencyTypeId, MaximumPeriod = item.MaximumPeriod, InterestRateAnnual = item.InterestRateAnnual, ProductPrincipalGl = item.ProductPrincipalGl, ReceiverPrincipalGl = item.ReceiverPrincipalGl, InterstExpenseGl = item.InterstExpenseGl, InterestPayableGl = item.InterestPayableGl, EarlyTerminationCharge = item.EarlyTerminationCharge, Active = true, Deleted = false, Createdby = createdBy, Createdon = DateTime.Now, }; _dataContext.inf_product.Add(product); } } } var response = _dataContext.SaveChanges() > 0; return(new InfProductRegRespObj { Status = new APIResponseStatus { IsSuccessful = response ? true : false, Message = new APIResponseMessage { FriendlyMessage = response ? "Successful" : "Unsuccessful" } } }); } catch (Exception ex) { throw new Exception(ex.Message); } }