using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Transaction; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests; using ReserveManagementSystemLib.Models; namespace ReserveManagementSystemApi.Mappers { public static class ExportMapper { //used for Chargeback transactions private const string GeneralLedgerReceipt = "GLR"; private const string DealerInterestRebate = "DRIR"; private const string ChargeBkDesc = "ChrgBk"; private const string ChargeBkAdjDesc = "ChrgBk Adj"; private const string GeneralLedgerCategory = "NOTE"; private const string GeneralLedgerType = "BAL"; //used for Reserve transactions private const string DealerExpense = "DLRE"; private const string DealerIncome = "DLRI"; private const string ReserveAdjDesc = "Adjustment"; //used for all transaction types private const string UpfrontCategory = "UPFR"; private const string DealerType = "DLR"; private const string ElectronicFundType = "EL"; private const string NoHoldClearCategory = "IMED"; private const string FundTypeDetail = "INTR"; public static GroupedMonetaryTransactionRequest MapLoanToIndirectLoanForReserve(Loan loan) { return new GroupedMonetaryTransactionRequest { MonetaryTransactionRequests = BuildReserveTransactions(loan), ParentRequestNumber = loan.LoanNumber }; } private static List BuildReserveTransactions(Loan loan) { var listTransaction = new List(); var isAdjustment = loan.Adjustment != null && loan.Adjustment != 0; var reserveRounded = Math.Round(loan.ReserveAmount, 4, MidpointRounding.AwayFromZero); //this represents the amount saved to DB var amount = isAdjustment ? Math.Round((decimal)loan.Adjustment, 2, MidpointRounding.AwayFromZero) : Math.Round(reserveRounded, 2, MidpointRounding.AwayFromZero); var transactionDescription = isAdjustment ? ReserveAdjDesc : null; var loanAmount = (double)amount * -1; listTransaction.Add(BuildParentTransaction(loanAmount, loan.LoanNumber, DealerExpense, transactionDescription)); var dealerAmount = (double)amount; listTransaction.Add(BuildDealerTransaction(dealerAmount, loan.Dealer.DealerAccountNumber, DealerIncome, transactionDescription)); return listTransaction; } public static GroupedMonetaryTransactionRequest MapLoanToIndirectLoanForChargeback(Loan loan, MinorAccountType accountType) { var request = new GroupedMonetaryTransactionRequest(); if (accountType != null) { request.MonetaryTransactionRequests = BuildChargebackTransactions(loan, accountType.GeneralLedgerAccountNumber); request.ParentRequestNumber = loan.LoanNumber; } return request; } private static List BuildChargebackTransactions(Loan loan, string accountNumber) { var listTransaction = new List(); _balanceTypeList = new List(); _fundList = new List(); _fundBalanceTypeList = new List(); var isAdjustment = loan.Adjustment != null && loan.Adjustment != 0; var chargeBackRounded = Math.Round((decimal)loan.ChargeBack, 4, MidpointRounding.AwayFromZero); //this represents the amount saved to DB var amount = isAdjustment ? Math.Round((decimal)loan.Adjustment, 2, MidpointRounding.AwayFromZero) : Math.Round(chargeBackRounded, 2, MidpointRounding.AwayFromZero); var transactionDescription = isAdjustment ? $"{ChargeBkAdjDesc}-{loan.LoanNumber}" : $"{ChargeBkDesc}-{loan.LoanNumber}"; var loanAmount = (double)amount; listTransaction.Add(BuildParentTransaction(loanAmount, accountNumber, GeneralLedgerReceipt, transactionDescription)); var dealerAmount = (double)amount * -1; listTransaction.Add(BuildDealerTransaction(dealerAmount, loan.Dealer.DealerAccountNumber, DealerInterestRebate, transactionDescription)); return listTransaction; } private static MonetaryTransactionRequest BuildParentTransaction(double amount, string accountNumber, string transactionType, string transactionDescription) { //if a reserve the parent transaction contains the loan number, if chargeback the account is the ledger account for minor account type _balanceTypeList = new List(); _fundList = new List(); _fundBalanceTypeList = new List(); _balanceTypeList.Add(BuildBalanceType(amount, transactionType)); _fundList.Add(BuildFund(amount)); _fundBalanceTypeList.Add(BuildFundBalanceType(amount)); var transaction = new MonetaryTransactionRequest() { AccountNumber = (long)System.Convert.ToUInt64(accountNumber), TransactionAmount = (transactionType != GeneralLedgerReceipt) ? amount : (double?)null, RequestNumber = "1", ParentRequestNumber = null, TransactionTypeCode = transactionType, Description = transactionDescription, BalanceTypes = _balanceTypeList, Funds = _fundList, FundBalanceTypes = (transactionType == GeneralLedgerReceipt) ? _fundBalanceTypeList : null, }; return transaction; } private static MonetaryTransactionRequest BuildDealerTransaction(double amount, string dealerAccount, string transactionType, string transactionDescription) { //for second request take the offset amount value of parent request amount _balanceTypeList = new List(); _fundList = new List(); _fundBalanceTypeList = new List(); _balanceTypeList.Add(BuildBalanceType(amount, transactionType)); _fundList.Add(BuildFund(amount)); _fundBalanceTypeList.Add(BuildFundBalanceType(amount)); var transaction = new MonetaryTransactionRequest() { AccountNumber = (string.IsNullOrEmpty(dealerAccount) == true) ? 0 : (long)System.Convert.ToUInt64(dealerAccount), TransactionAmount = amount, RequestNumber = "2", ParentRequestNumber = "1", TransactionTypeCode = transactionType, Description = transactionDescription, BalanceTypes = _balanceTypeList, Funds = _fundList, }; return transaction; } private static List _fundBalanceTypeList; private static FundBalanceType BuildFundBalanceType(double amount) { return new FundBalanceType { FundBalanceAmount = amount, FundTypeCode = ElectronicFundType, FundTypeDetailCode = NoHoldClearCategory }; } private static List _balanceTypeList; private static BalanceType1 BuildBalanceType(double amount, string transactionType) { return new BalanceType1 { BalanceTypeAmount = amount, BalanceCategoryCode = (transactionType == GeneralLedgerReceipt) ? GeneralLedgerCategory : UpfrontCategory, BalanceTypeCode = (transactionType == GeneralLedgerReceipt) ? GeneralLedgerType : DealerType, }; } private static List _fundList; private static Fund BuildFund(double amount) { return new Fund { ClearingCategoryCode = NoHoldClearCategory, FundAmount = amount, FundTypeCode = ElectronicFundType, FundTypeDetailCode = FundTypeDetail, NumberOfItems = 1, }; } } }