private void SetTotals(CheckoutViewModel model, IMyMentorRepository repository, CurrencyDto currencyDto) { var loggedInUser = Session.GetLoggedInUser(); var userAdminData = Task.Run(() => loggedInUser.GetPointerObject<UserAdminData>("adminData")).Result; var currentBalance = userAdminData.Balance; var minimumTransaction = repository.FindGlobalTeacherCommission().MinimumTransaction; var basketPrice = model.PurchasesForUser.Sum(x => x.RegularPrice); var basketPriceWithCoupon = model.PurchasesForUser.Sum(x => { if (x.HasCoupon) return x.PriceWithCoupon; return x.RegularPrice; }); var paymentTransactionWithCoupon = currentBalance < 0 ? basketPriceWithCoupon - currentBalance : basketPriceWithCoupon >= currentBalance ? basketPriceWithCoupon - currentBalance : 0; var paymentTransactionNis = CurrencyConverter.ConvertToNis(paymentTransactionWithCoupon,currencyDto,repository); paymentTransactionWithCoupon = paymentTransactionNis < minimumTransaction ? 0 : paymentTransactionWithCoupon; var reduceFromBalance = basketPriceWithCoupon == 0 || paymentTransactionWithCoupon > basketPriceWithCoupon || currentBalance <= 0 ? 0 : basketPriceWithCoupon - paymentTransactionWithCoupon; model.BasketPrice = basketPrice.ToCurrency(currencyDto); model.BasketPriceWithCouopn = basketPriceWithCoupon.Equals(basketPrice) ? string.Empty : basketPriceWithCoupon.ToCurrency(currencyDto); model.BasketPriceWithCouopnForCalc = basketPriceWithCoupon; model.Balance = currentBalance.ToCurrency(currencyDto); model.BalanceForCalc = currentBalance; model.ReduceFromBalance = reduceFromBalance.ToCurrency(currencyDto); model.PaymentTransaction = paymentTransactionWithCoupon.ToCurrency(currencyDto); model.PaymentTransactionForCalc = paymentTransactionWithCoupon; model.RequiresPayment = paymentTransactionWithCoupon > 0; }
private async Task CreatePaymentTransaction(IMyMentorRepository repository, CreateCouponViewModel createCouponVm) { var currencyRetriever = new CurrencyRetriver(HttpContext, Session, repository); var globalCommissionsTable = repository.FindGlobalTeacherCommission(); var maam = globalCommissionsTable.Maam; var paysMaam = createCouponVm.TeacherData.LivesInIsrael; var paymentTransaction = createCouponVm.TeacherData.AmountForPayment; var gapToPay = createCouponVm.TeacherData.GapToPay; var offsetFromBalance = gapToPay > paymentTransaction ? gapToPay - paymentTransaction : 0; var maamPaymentCredit = paysMaam ? (paymentTransaction * maam)/(100 + maam) : 0; var maamFromBalance = paysMaam ? offsetFromBalance * maam / (100 + maam) : 0; var maamHitkabel = maamFromBalance + maamPaymentCredit; var amountForSiteAccountCredit = createCouponVm.TeacherData.GapToPay - maamHitkabel; var transactionUsers = GetTransactionUsers(createCouponVm.TeacherData,globalCommissionsTable,repository); var teacher = transactionUsers.Single(x => x.ObjectId == createCouponVm.TeacherData.TeacherId); var siteAccount = transactionUsers.Single(x => x.ObjectId == globalCommissionsTable.SiteAccountId); var maamAccount = transactionUsers.Single(x => x.ObjectId == globalCommissionsTable.MaamAccountId); var maamBalanceAccount = transactionUsers.Single(x=>x.ObjectId == globalCommissionsTable.MaamBalanceAccountId); var agentAccount = transactionUsers.SingleOrDefault(x => x.ObjectId == createCouponVm.TeacherData.AgentId); var accountStatementBuilder = new AccountStatementBuilder(repository, currencyRetriever) { CouponId = createCouponVm.CouponId, EventId = createCouponVm.EventId }; var accountStatements = new List<AccountStatementDto>(); var teacherAccountCreditStatement = TeacherAccountCreditStatement(createCouponVm, accountStatementBuilder, paymentTransaction, accountStatements, teacher, currencyRetriever); SiteAccountCreditStatement(createCouponVm, accountStatementBuilder, amountForSiteAccountCredit, accountStatements, siteAccount, currencyRetriever); if (paysMaam) { VatCreditStatement(createCouponVm, maamPaymentCredit, accountStatementBuilder, accountStatements,teacherAccountCreditStatement, maam, maamFromBalance, maamAccount, maamBalanceAccount, currencyRetriever); } if (!string.IsNullOrEmpty(createCouponVm.TeacherData.AgentId)) { AgentCreditStatement(createCouponVm, repository, agentAccount, teacher, siteAccount, globalCommissionsTable, accountStatementBuilder, accountStatements, currencyRetriever); } for (int i = 0; i < accountStatements.Count(); i++) { accountStatements[i].Order = i; } createCouponVm.AccountStatements = accountStatements.ToArray(); await CreateUpdateEvent(createCouponVm, EventStatus.EventStarted, repository); }