using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.IndirectLoan; using ReserveManagementSystemApi.Models; using ReserveManagementSystemLib.Models; namespace ReserveManagementSystemApi.Helpers { public class ImportHelper { private string _firstName; private string _middleInitial; private const double MonthInDays = 30.4375; public ImportHelper() { } public List TransformNewLoan(List iImportLoans, int iImportId) { var result = new List(); foreach (var iLoan in iImportLoans) { Loan loan = new Loan(); loan.ImportId = iImportId; loan.MemberNumber = iLoan.MemberNumber; loan.LoanNumber = iLoan.LoanNumber; SplitFirstMiddleName(iLoan.FirstAndMiddleName); loan.MemberFirstName = _firstName?.ToUpper(); loan.MemberInitial = _middleInitial?.ToUpper(); loan.MemberLastName = iLoan.LastName?.ToUpper(); loan.DealerOrganizationNumber = iLoan.DealerOrganizationNumber; loan.LoanDate = iLoan.LoanDate; loan.PostedDate = iLoan.PostedDate; loan.FirstPmtDate = iLoan.FirstPmtDate; loan.FinalPmtDate = iLoan.FinalPmtDate; loan.LoanAmount = iLoan.LoanAmount; loan.BuyRate = Math.Round(iLoan.BuyRate, 3, MidpointRounding.AwayFromZero); loan.ContractRate = Math.Round(iLoan.ContractRate, 3, MidpointRounding.AwayFromZero); loan.MonthlyPmt = iLoan.MonthlyPmt; loan.LoanTypeId = iLoan.LoanType; loan.ChargeBack = null; loan.PaidLoanDate = null; loan.ApplicationId = iLoan.ApplicationId; loan.AccountType = iLoan.AccountType; loan.Gap = !string.IsNullOrEmpty(iLoan.Gap) && Convert.ToBoolean(iLoan.Gap); loan.Warranty = !string.IsNullOrEmpty(iLoan.Warranty) && Convert.ToBoolean(iLoan.Warranty); loan.CreditLife = !string.IsNullOrEmpty(iLoan.CreditLife) && Convert.ToBoolean(iLoan.CreditLife); loan.CreditDisability = !string.IsNullOrEmpty(iLoan.CreditDisability) && Convert.ToBoolean(iLoan.CreditDisability); loan.Mbp = !string.IsNullOrEmpty(iLoan.Mbp) && Convert.ToBoolean(iLoan.Mbp); loan.Term = (int)Math.Round(Convert.ToDecimal(iLoan.OriginalTermInDays) / (decimal)MonthInDays, 0); Vehicle vehicle = new Vehicle(); vehicle.VINNumber = !string.IsNullOrEmpty(iLoan.VINNumber) ? iLoan.VINNumber : null; vehicle.Make = iLoan.Make; vehicle.Model = iLoan.Model; vehicle.Year = iLoan.Year; vehicle.Mileage = ValidateMileage(iLoan.Mileage); vehicle.Color = iLoan.Color; loan.Vehicle = vehicle; result.Add(loan); } return result; } public List TransformLoanFromDNA(List importLoans, int iImportId) { var result = new List(); if (!importLoans.Any()) { return result; } foreach (var iLoan in importLoans) { Loan loan = new Loan { ImportId = iImportId, MemberNumber = iLoan.MemberNumber, LoanNumber = iLoan.LoanNumber, MemberFirstName = iLoan.MemberFirstName?.ToUpper(), MemberInitial = iLoan.MemberInitial?.ToUpper(), MemberLastName = iLoan.MemberLastName?.ToUpper(), DealerOrganizationNumber = (!String.IsNullOrWhiteSpace(iLoan.DealerOrganizationNumber)) ? iLoan.DealerOrganizationNumber : string.Empty, LoanDate = iLoan.LoanDate, FirstPmtDate = iLoan.FirstPmtDate, FinalPmtDate = iLoan.FinalPmtDate, LoanAmount = iLoan.LoanAmount, BuyRate = (iLoan.BuyRate != 0) ? Math.Round(iLoan.BuyRate, 3, MidpointRounding.AwayFromZero) : 0, ContractRate = Math.Round(iLoan.ContractRate, 3, MidpointRounding.AwayFromZero), MonthlyPmt = iLoan.MonthlyPmt, LoanTypeId = iLoan.LoanType, ApplicationId = iLoan.ApplicationId, Gap = iLoan.GAP, Warranty = iLoan.Warranty, CreditLife = iLoan.CreditLife, CreditDisability = iLoan.CreditDisability, Mbp = iLoan.Mbp, AccountType = iLoan.AccountType, PaidLoanDate = iLoan.PaidLoanDate, Term = (int)Math.Round(Convert.ToDecimal(iLoan.OriginalTermInDays) / (decimal)MonthInDays, 0) }; if (iLoan.PostedDate == DateTime.MinValue) { loan.PostedDate = iLoan.LoanDate; } else { loan.PostedDate = iLoan.PostedDate; } loan.Vehicle = new Vehicle { VINNumber = !string.IsNullOrEmpty(iLoan.Vin) ? iLoan.Vin : null, Make = iLoan.Make, Model = iLoan.Model, Year = iLoan.Year.ToString(), Mileage = ValidateMileage(iLoan.Mileage.ToString()), Color = iLoan.Color }; loan.Dealer = new Dealer { DealerCode = (!String.IsNullOrWhiteSpace(iLoan.DealerCode)) && (iLoan.DealerCode.Length <= 4) ? iLoan.DealerCode : string.Empty, Name = (!String.IsNullOrWhiteSpace(iLoan.DealerName)) ? iLoan.DealerName : string.Empty, DealerAccountNumber = (!String.IsNullOrWhiteSpace(iLoan.DealerAccountNumber)) ? iLoan.DealerAccountNumber : string.Empty, DealerOrganizationNumber = (!String.IsNullOrWhiteSpace(iLoan.DealerOrganizationNumber)) ? iLoan.DealerOrganizationNumber : string.Empty }; result.Add(loan); } return result; } public List TransformBoatLoan(List importLoans) { var result = new List(); if (!importLoans.Any()) { return result; } foreach (var iLoan in importLoans) { Loan loan = new Loan { LoanNumber = iLoan.LoanNumber, ReserveAmount = (!String.IsNullOrWhiteSpace(iLoan.IsChargeback) && iLoan.IsChargeback.ToUpper() == "FALSE") ? iLoan.Amount : 0, ChargeBack = (!String.IsNullOrWhiteSpace(iLoan.IsChargeback) && iLoan.IsChargeback.ToUpper() == "TRUE") ? iLoan.Amount : 0, Adjustment = (!String.IsNullOrWhiteSpace(iLoan.IsAdjustment) && iLoan.IsAdjustment.ToUpper() == "TRUE") ? iLoan.Amount : 0, AccountType = iLoan.AccountType }; loan.Dealer = new Dealer { DealerAccountNumber = iLoan.DealerAccountNumber }; result.Add(loan); } return result; } private double ValidateMileage(string mileage) { double convertedMileage; if (double.TryParse(mileage, out convertedMileage)) { return convertedMileage; } return convertedMileage; } public IEnumerable TransformClosedLoans(List importLoans, int importId) { return importLoans.Select(x => new Loan { ImportId = importId, LoanNumber = x.LoanNumber, PaidLoanDate = x.PaidLoanDate //will update LoanStatus when saving }); } private void SplitFirstMiddleName(string name) { //initial first name last name rules with first name character max length set in DB as 20, and middle initial max as 1 _middleInitial = String.Empty; _firstName = name; string[] names = name.Trim().Split(" "); if (names != null && names.Length > 1) //when true there is a middle initial { _firstName = _firstName.Substring(0, name.Length - names.Last().Length).Trim(); _middleInitial = names.Last().Substring(0, 1); } else { _firstName = name; //no middle initial } } } }