FinanceTableViewModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using FinancialManagement.Models;
  2. using FinancialManagement.Views;
  3. using Microsoft.CodeAnalysis.CSharp.Syntax;
  4. using Microsoft.EntityFrameworkCore;
  5. using ReactiveUI;
  6. using SkiaSharp;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace FinancialManagement.ViewModels
  13. {
  14. public class FinanceTableViewModel : ReactiveObject
  15. {
  16. ShmelevTeamContext ppContext;
  17. List<FinanceTableView> finances = new List<FinanceTableView>();
  18. List<BudgetTableView> budgets = new List<BudgetTableView>();
  19. private string categoryFinTitle;
  20. private string categoryTitle;
  21. private decimal income;
  22. private decimal expenditure;
  23. private double result;
  24. int indexFinance;
  25. public int IndexFinance { get => indexFinance; set => indexFinance = value;}
  26. public List<FinanceTableView> Finance { get => finances; set => this.RaiseAndSetIfChanged(ref finances, value); }
  27. public List<BudgetTableView> Budget { get => budgets; set => this.RaiseAndSetIfChanged(ref budgets, value); }
  28. public Finance ItemFinance { get => ppContext.Finances.First(it => it.FinanceId == finances[IndexFinance].FinanceId); }
  29. public string CategoryFinTitle { get => categoryFinTitle; set => categoryFinTitle = value; }
  30. public string CategoryTitle { get => categoryTitle; set => categoryTitle = value; }
  31. public decimal Income { get => income; set => income = value; }
  32. public decimal Expenditure { get => expenditure; set => expenditure = value; }
  33. public double Result { get => result; set => result = value; }
  34. public string LoadCategoryFinTitle(int CategoryFinId)
  35. {
  36. using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
  37. {
  38. var financeCategoryFin = ppContexts.Finances.
  39. Where(f => f.CategoryFinId == CategoryFinId)
  40. .Select(f => f.CategoryFin.FinTitle)
  41. .FirstOrDefault();
  42. CategoryFinTitle = financeCategoryFin;
  43. }
  44. return CategoryFinTitle;
  45. }
  46. public string LoadCategoryTitle(int CategoryId)
  47. {
  48. using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
  49. {
  50. var financeCategory = ppContexts.Finances.
  51. Where(f => f.CategoryId == CategoryId)
  52. .Select(f => f.Category.Title)
  53. .FirstOrDefault();
  54. CategoryTitle = financeCategory;
  55. }
  56. return CategoryTitle;
  57. }
  58. public decimal GetToIncome(User currentUser)
  59. {
  60. using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
  61. {
  62. int CategoryId = 1;
  63. var IncomeCategory = ppContexts.Finances.
  64. Where(f => f.CategoryFinId == CategoryId && f.UserId == currentUser.UserId)
  65. .Select(f => f.Price)
  66. .Sum();
  67. Income = IncomeCategory;
  68. }
  69. return Income;
  70. }
  71. public decimal GetToExpenditure(User currentUser)
  72. {
  73. using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
  74. {
  75. int CategoryId = 2;
  76. var ExpenditureCategory = ppContexts.Finances.
  77. Where(f => f.CategoryFinId == CategoryId && f.UserId == currentUser.UserId)
  78. .Select(f => f.Price)
  79. .Sum();
  80. Expenditure = ExpenditureCategory;
  81. }
  82. return Expenditure;
  83. }
  84. public double GetToResult(User currentUser)
  85. {
  86. Result = Convert.ToDouble(GetToIncome(currentUser) - GetToExpenditure(currentUser));
  87. return Result;
  88. }
  89. public FinanceTableViewModel(User currentUser, ShmelevTeamContext ppContext)
  90. {
  91. this.ppContext = ppContext;
  92. List<Finance> financesList = currentUser.Finances.ToList();
  93. financesList.ForEach(it =>
  94. {
  95. FinanceTableView item = new FinanceTableView();
  96. item.FinanceId = it.FinanceId;
  97. item.DataRecording = it.DataRecording;
  98. item.CategoryFinanceTitle = LoadCategoryFinTitle(it.CategoryFinId);
  99. item.Title = it.Title;
  100. item.CategoryTitle = LoadCategoryTitle(it.CategoryId);
  101. item.Price = it.Price;
  102. finances.Add(item);
  103. });
  104. BudgetTableView item = null;
  105. budgets.Add(item);
  106. financesList.ForEach(it =>
  107. {
  108. BudgetTableView item = new BudgetTableView();
  109. item.Income = GetToIncome(currentUser);
  110. item.Expenditure = GetToExpenditure(currentUser);
  111. item.Result = GetToResult(currentUser);
  112. budgets[0] = item;
  113. });
  114. }
  115. }
  116. public class BudgetTableView
  117. {
  118. public decimal Income { get; set; }
  119. public decimal Expenditure { get; set; }
  120. public double Result { get; set; }
  121. }
  122. public class FinanceTableView
  123. {
  124. public int FinanceId { get; set; }
  125. public DateTime? DataRecording { get; set; }
  126. public string CategoryFinanceTitle { get; set; } = null!;
  127. public string Title { get; set; } = null!;
  128. public string CategoryTitle { get; set; } = null!;
  129. public decimal Price { get; set; }
  130. }
  131. }