public async Task <int> AddUpdateCustomerCollateral(credit_collateralcustomer model)
        {
            try
            {
                var creditCollateralCustomerId = 0;
                var collateralCode             = GenerateCollateralCode(model.CollateralTypeId);
                model.CollateralCode = collateralCode;
                if (model.CollateralCustomerId > 0)
                {
                    var itemToUpdate = await _dataContext.credit_collateralcustomer.FindAsync(model.CollateralCustomerId);

                    _dataContext.Entry(itemToUpdate).CurrentValues.SetValues(model);
                }
                else
                {
                    _dataContext.credit_collateralcustomer.Add(model);
                }
                await _dataContext.SaveChangesAsync();

                creditCollateralCustomerId = model.CollateralCustomerId;
                return(creditCollateralCustomerId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <ActionResult <CollateralCustomerRegRespObj> > AddOrUpdateLoanApplicationCollateralDocument()
        {
            try
            {
                var identity = await _serverRequest.UserDataAsync();

                var user   = identity.UserName;
                var file   = _httpContextAccessor.HttpContext.Request.Form.Files["document"];
                var isDone = false;
                var collateralCustomerId         = _httpContextAccessor.HttpContext.Request.Form.Files["collateralCustomerId"];
                var customerId                   = _httpContextAccessor.HttpContext.Request.Form["customerId"];
                var collateralTypeId             = _httpContextAccessor.HttpContext.Request.Form["collateralTypeId"];
                var currencyId                   = _httpContextAccessor.HttpContext.Request.Form["currencyId"];
                var collateralValue              = _httpContextAccessor.HttpContext.Request.Form["collateralValue"];
                var collateralVerificationStatus = _httpContextAccessor.HttpContext.Request.Form["collateralVerificationStatus"];
                var loanApplicationId            = _httpContextAccessor.HttpContext.Request.Form["loanApplicationId"];
                var collateralCode               = _httpContextAccessor.HttpContext.Request.Form["collateralCode"];
                var location = _httpContextAccessor.HttpContext.Request.Form["location"];


                //var file = _httpContextAccessor.HttpContext.Request.Form.Files.Count > 0 ? _httpContextAccessor.HttpContext.Request.Form.Files[0] : null;
                byte[] fileData = null;

                if (file != null && file.Length > 0)
                {
                    using (var binaryReader = new BinaryReader(file.OpenReadStream()))
                    {
                        fileData = binaryReader.ReadBytes(((int)file.Length));
                    }
                }
                var model = new credit_collateralcustomer
                {
                    CollateralCode               = collateralCode.ToString(),
                    Location                     = location.ToString(),
                    CollateralTypeId             = Convert.ToInt32(collateralTypeId),
                    CollateralCustomerId         = Convert.ToInt32(collateralCustomerId),
                    CollateralValue              = Convert.ToDecimal(collateralValue),
                    CollateralVerificationStatus = Convert.ToBoolean(collateralVerificationStatus),
                    CustomerId                   = Convert.ToInt32(customerId),
                    //LoanApplicationId = Convert.ToInt32(loanApplicationId),
                    CurrencyId = Convert.ToInt32(currencyId),
                    Active     = true,
                    CreatedBy  = user,
                    CreatedOn  = DateTime.Today,
                    Deleted    = false,
                    UpdatedBy  = user,
                    UpdatedOn  = DateTime.Today,
                };
                var response = await _repo.AddUpdateCustomerCollateral(model);

                if (response > 0)
                {
                    isDone = true;
                    var loanApplicationCollateralDocumentViewModel = new LoanApplicationCollateralDocumentObj
                    {
                        CollateralCustomerId = model.CollateralCustomerId,
                        CollateralTypeId     = Convert.ToInt32(collateralTypeId),
                        Document             = fileData,
                        LoanApplicationId    = Convert.ToInt32(loanApplicationId),
                        DocumentName         = file.FileName,
                    };

                    await _loanApplicationCollateralDocumentRespository.AddOrUpdateLoanApplicationCollateralDocumentAsync(loanApplicationCollateralDocumentViewModel);
                }
                return(new CollateralCustomerRegRespObj
                {
                    CollateralCustomerId = model.CollateralTypeId,
                    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 CollateralCustomerRegRespObj
                {
                    Status = new APIResponseStatus {
                        IsSuccessful = false, Message = new APIResponseMessage {
                            FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode
                        }
                    }
                });
            }
        }
        public async Task <ActionResult <CollateralCustomerRegRespObj> > AddUpdateCustomerCollateral([FromBody] AddUpdateCollateralCustomerObj entity)
        {
            try
            {
                var isDone = false;
                CollateralCustomerObj item = null;
                if (entity.CollateralCustomerId > 0)
                {
                    item = _repo.GetCollateralCustomer(entity.CollateralCustomerId);
                    if (item == null)
                    {
                        return new CollateralCustomerRegRespObj
                               {
                                   Status = new APIResponseStatus {
                                       IsSuccessful = false, Message = new APIResponseMessage {
                                           FriendlyMessage = "Item does not Exist"
                                       }
                                   }
                               }
                    }
                    ;
                }
                var identity = await _serverRequest.UserDataAsync();

                var user = identity.UserName;

                var domainObj = new credit_collateralcustomer();
                domainObj.CollateralCustomerId         = entity.CollateralCustomerId > 0 ? entity.CollateralCustomerId : 0;
                domainObj.CustomerId                   = entity.CustomerId;
                domainObj.CollateralTypeId             = entity.CollateralTypeId;
                domainObj.CurrencyId                   = entity.CurrencyId;
                domainObj.CollateralValue              = entity.CollateralValue;
                domainObj.CollateralVerificationStatus = entity.CollateralVerificationStatus;
                domainObj.CollateralCode               = "";
                domainObj.Location  = entity.Location;
                domainObj.Active    = true;
                domainObj.CreatedBy = user;
                domainObj.CreatedOn = DateTime.Today;
                domainObj.Deleted   = false;
                domainObj.UpdatedBy = user;
                domainObj.UpdatedOn = entity.CollateralTypeId > 0 ? DateTime.Today : DateTime.Today;

                var result = await _repo.AddUpdateCustomerCollateral(domainObj);

                if (result > 0)
                {
                    isDone = true;
                }
                return(new CollateralCustomerRegRespObj
                {
                    CollateralCustomerId = result,
                    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 CollateralCustomerRegRespObj
                {
                    Status = new APIResponseStatus {
                        IsSuccessful = false, Message = new APIResponseMessage {
                            FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode
                        }
                    }
                });
            }
        }
        public bool UploadCustomerCollateral(byte[] record, string createdBy)
        {
            try
            {
                if (record == null)
                {
                    return(false);
                }
                List <CollateralCustomerObj> uploadedRecord = new List <CollateralCustomerObj>();
                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++)
                        {
                            bool.TryParse(workSheet.Cells[i, 5].Value.ToString(), out bool collateralVerificationStatus);

                            uploadedRecord.Add(new CollateralCustomerObj
                            {
                                CustomerId                   = int.Parse(workSheet.Cells[i, 1].Value.ToString()),
                                CollateralTypeName           = workSheet.Cells[i, 2].Value.ToString(),
                                CollateralValue              = decimal.Parse(workSheet.Cells[i, 3].Value.ToString()),
                                Currency                     = workSheet.Cells[i, 4].Value.ToString(),
                                CollateralVerificationStatus = collateralVerificationStatus,
                                Location                     = workSheet.Cells[i, 6].Value.ToString(),
                            });
                        }
                    }
                if (uploadedRecord.Count > 0)
                {
                    foreach (var item in uploadedRecord)
                    {
                        //item.CurrencyId = _dataContext.cor_currency.Where(x => x.CurrencyName == item.Currency && x.Deleted == false).FirstOrDefault().CurrencyId;
                        item.CollateralTypeId = _dataContext.credit_collateraltype.Where(x => x.Name == item.CollateralTypeName && x.Deleted == false).FirstOrDefault().CollateralTypeId;
                        var category = _dataContext.credit_collateralcustomer.Where(x => x.CustomerId == item.CustomerId && x.CollateralTypeId == item.CollateralTypeId && x.CollateralValue == item.CollateralValue && x.Deleted == false).FirstOrDefault();
                        if (category != null)
                        {
                            category.CustomerId                   = item.CustomerId;
                            category.CollateralTypeId             = item.CollateralTypeId;
                            category.CurrencyId                   = item.CurrencyId;
                            category.CollateralValue              = item.CollateralValue;
                            category.CollateralVerificationStatus = item.CollateralVerificationStatus;
                            category.Location  = item.Location;
                            category.Active    = true;
                            category.Deleted   = false;
                            category.UpdatedBy = createdBy;
                            category.UpdatedOn = DateTime.Now;
                        }
                        else
                        {
                            var CollateralCode = GenerateCollateralCode(item.CollateralTypeId);
                            var structure      = new credit_collateralcustomer
                            {
                                CustomerId                   = item.CustomerId,
                                CollateralTypeId             = item.CollateralTypeId,
                                CurrencyId                   = item.CurrencyId,
                                CollateralValue              = item.CollateralValue,
                                CollateralVerificationStatus = item.CollateralVerificationStatus,
                                Location       = item.Location,
                                CollateralCode = CollateralCode,
                                Active         = true,
                                Deleted        = false,
                                CreatedBy      = createdBy,
                                CreatedOn      = DateTime.Now,
                            };
                            _dataContext.credit_collateralcustomer.Add(structure);
                        }
                    }
                }

                var response = _dataContext.SaveChanges() > 0;
                return(response);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }