public async Task RunCalculationForAllUsersAsync(DateTime calculationDate) { List <string> users = await _context.Users.Select(x => x.Id).ToListAsync(); foreach (string userId in users) { DateTime now = calculationDate.Date; IEnumerable <SavingsDeposit> savingsDeposits = await _context.SavingsDeposits .Where(x => x.Owner == userId && now >= x.StartDate && now <= x.EndDate.AddDays(1)) .ToListAsync(); foreach (SavingsDeposit savingsDeposit in savingsDeposits) { if (savingsDeposit.LastCalculation.Date < now) { DepositHistory depositHistory = PerformDepositCalculation(savingsDeposit); depositHistory.CalculationDate = calculationDate; savingsDeposit.LastCalculation = calculationDate; savingsDeposit.CurrentProfitAfterTax = depositHistory.TotalProfitAfterTax; _context.DepositsHistory.Add(depositHistory); } _context.Entry(savingsDeposit).State = EntityState.Modified; } await _context.SaveChangesAsync(); } }
public async Task <DepositHistory> GetDepositHistory(DateTime startDate) { var toReturn = new DepositHistory { Success = true, DepositList = new List <Deposit>() }; while (true) { if (startDate > DateTime.Today) { break; } DateTime endDate = startDate.AddDays(90); var parameters = $"startTime={startDate.ToUnixDateTime()}&endTime={endDate.ToUnixDateTime()}"; parameters = _restClient.AddTimestampAndSign(parameters); var result = await _restClient.CallAsync(HttpMethod.Get, "/wapi/v3/depositHistory.html", parameters); var resultTyped = JsonConvert.DeserializeObject <DepositHistory>(result); if (!resultTyped.Success) { throw new Exception(); } toReturn.DepositList.AddRange(resultTyped.DepositList ?? Enumerable.Empty <Deposit>()); startDate = endDate.AddDays(1); } return(toReturn); }
public UserDepositHistoryModel(DepositHistory history) : base(history) { if (history != null) { OptUser_Id = history.OptUser_Id; User_Id = history.User_Id; Application_Id = history.Application_Id; Money = history.Money; Time = history.Time; IsAgent = history.IsAgent.HasValue?(history.IsAgent.Value?1:0):0; Score = history.Score; } }
public void TestSavingsDepositsCalculation() { var calculationService = new SavingsComputationService(null); SavingsDeposit savingsDeposit = new SavingsDeposit { InitialAmount = 100_000, TaxPercentage = 15, YearlyInterestPercentage = 5 }; DepositHistory depositHistory = calculationService.PerformDepositCalculation(savingsDeposit); savingsDeposit.CurrentProfitAfterTax = depositHistory.TotalProfitAfterTax; savingsDeposit.LastCalculation = DateTime.Today; depositHistory.CalculationDate = DateTime.Today; Assert.Equal(13.89m, depositHistory.ProfitBeforeTax); Assert.Equal(2.08m, depositHistory.ProfitTax); Assert.Equal(11.81m, depositHistory.ProfitAfterTax); Assert.Equal(11.81m, depositHistory.TotalProfitAfterTax); depositHistory = calculationService.PerformDepositCalculation(savingsDeposit); savingsDeposit.CurrentProfitAfterTax = depositHistory.TotalProfitAfterTax; savingsDeposit.LastCalculation = DateTime.Today; depositHistory.CalculationDate = DateTime.Today; Assert.Equal(13.89m, depositHistory.ProfitBeforeTax); Assert.Equal(2.08m, depositHistory.ProfitTax); Assert.Equal(11.81m, depositHistory.ProfitAfterTax); Assert.Equal(23.62m, depositHistory.TotalProfitAfterTax); SavingsDeposit credit = new SavingsDeposit { InitialAmount = 100_000, TaxPercentage = 15, YearlyInterestPercentage = -20 }; depositHistory = calculationService.PerformDepositCalculation(credit); Assert.Equal(-55.56m, depositHistory.ProfitBeforeTax); Assert.Equal(depositHistory.ProfitBeforeTax, depositHistory.ProfitAfterTax); Assert.Equal(0m, depositHistory.ProfitTax); }
/// <summary> /// Fetch deposit history. /// </summary> /// <param name="asset">Asset you want to see the information for.</param> /// <param name="status">Deposit status.</param> /// <param name="startTime">Start time. </param> /// <param name="endTime">End time.</param> /// <param name="recvWindow">Specific number of milliseconds the request is valid for.</param> /// <returns></returns> public async Task <DepositHistory> GetDepositHistory(string asset, DepositStatus?status = null, DateTime?startTime = null, DateTime?endTime = null, long recvWindow = 50000) { if (string.IsNullOrWhiteSpace(asset)) { throw new ArgumentException("asset cannot be empty. ", "asset"); } string args = $"asset={asset.ToUpper()}" + (status.HasValue ? $"&status={(int)status}" : "") + (startTime.HasValue ? $"&startTime={startTime.Value.GetUnixTimeStamp()}" : "") + (endTime.HasValue ? $"&endTime={endTime.Value.GetUnixTimeStamp()}" : "") + $"&recvWindow={recvWindow}"; DepositHistory result = await _apiClient.CallAsync <DepositHistory>(ApiMethod.POST, EndPoints.DepositHistory, true, args); return(result); }
public DepositHistory PerformDepositCalculation(SavingsDeposit savingsDeposit) { DepositHistory depositHistory = new DepositHistory(); decimal dailyCalculatedAmount = savingsDeposit.AccountBalance * savingsDeposit.YearlyInterestPercentage / DaysInYear / 100m; decimal profitTax = 0; if (dailyCalculatedAmount > 0) { profitTax = dailyCalculatedAmount * savingsDeposit.TaxPercentage / 100m; } depositHistory.SavingsDepositId = savingsDeposit.Id; depositHistory.ProfitBeforeTax = Math.Round(dailyCalculatedAmount, 2); depositHistory.ProfitTax = Math.Round(profitTax, 2); depositHistory.ProfitAfterTax = depositHistory.ProfitBeforeTax - depositHistory.ProfitTax; depositHistory.TotalProfitAfterTax = savingsDeposit.CurrentProfitAfterTax + depositHistory.ProfitAfterTax; return(depositHistory); }
public void SaveDeposits(DepositHistory depositHistory) { var depositJson = depositHistory.DepositList.OrderBy(t => t.InsertTime).Select(JsonConvert.SerializeObject); File.WriteAllLines(GetFileName(_depositFileName), depositJson); }