123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using FinancialManagement.Models;
- using FinancialManagement.Views;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- using Microsoft.EntityFrameworkCore;
- using ReactiveUI;
- using SkiaSharp;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace FinancialManagement.ViewModels
- {
- public class FinanceTableViewModel : ReactiveObject
- {
- ShmelevTeamContext ppContext;
- List<FinanceTableView> finances = new List<FinanceTableView>();
- List<BudgetTableView> budgets = new List<BudgetTableView>();
- private string categoryFinTitle;
- private string categoryTitle;
- private decimal income;
- private decimal expenditure;
- private double result;
- int indexFinance;
- public int IndexFinance { get => indexFinance; set => indexFinance = value;}
- public List<FinanceTableView> Finance { get => finances; set => this.RaiseAndSetIfChanged(ref finances, value); }
- public List<BudgetTableView> Budget { get => budgets; set => this.RaiseAndSetIfChanged(ref budgets, value); }
- public Finance ItemFinance { get => ppContext.Finances.First(it => it.FinanceId == finances[IndexFinance].FinanceId); }
- public string CategoryFinTitle { get => categoryFinTitle; set => categoryFinTitle = value; }
- public string CategoryTitle { get => categoryTitle; set => categoryTitle = value; }
- public decimal Income { get => income; set => income = value; }
- public decimal Expenditure { get => expenditure; set => expenditure = value; }
- public double Result { get => result; set => result = value; }
- public string LoadCategoryFinTitle(int CategoryFinId)
- {
- using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
- {
- var financeCategoryFin = ppContexts.Finances.
- Where(f => f.CategoryFinId == CategoryFinId)
- .Select(f => f.CategoryFin.FinTitle)
- .FirstOrDefault();
- CategoryFinTitle = financeCategoryFin;
- }
- return CategoryFinTitle;
- }
- public string LoadCategoryTitle(int CategoryId)
- {
- using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
- {
- var financeCategory = ppContexts.Finances.
- Where(f => f.CategoryId == CategoryId)
- .Select(f => f.Category.Title)
- .FirstOrDefault();
- CategoryTitle = financeCategory;
- }
- return CategoryTitle;
- }
- public decimal GetToIncome(User currentUser)
- {
- using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
- {
- int CategoryId = 1;
- var IncomeCategory = ppContexts.Finances.
- Where(f => f.CategoryFinId == CategoryId && f.UserId == currentUser.UserId)
- .Select(f => f.Price)
- .Sum();
- Income = IncomeCategory;
- }
- return Income;
- }
- public decimal GetToExpenditure(User currentUser)
- {
- using (ShmelevTeamContext ppContexts = new ShmelevTeamContext())
- {
- int CategoryId = 2;
- var ExpenditureCategory = ppContexts.Finances.
- Where(f => f.CategoryFinId == CategoryId && f.UserId == currentUser.UserId)
- .Select(f => f.Price)
- .Sum();
- Expenditure = ExpenditureCategory;
- }
- return Expenditure;
- }
- public double GetToResult(User currentUser)
- {
- Result = Convert.ToDouble(GetToIncome(currentUser) - GetToExpenditure(currentUser));
- return Result;
- }
- public FinanceTableViewModel(User currentUser, ShmelevTeamContext ppContext)
- {
- this.ppContext = ppContext;
- List<Finance> financesList = currentUser.Finances.ToList();
- financesList.ForEach(it =>
- {
- FinanceTableView item = new FinanceTableView();
- item.FinanceId = it.FinanceId;
- item.DataRecording = it.DataRecording;
- item.CategoryFinanceTitle = LoadCategoryFinTitle(it.CategoryFinId);
- item.Title = it.Title;
- item.CategoryTitle = LoadCategoryTitle(it.CategoryId);
- item.Price = it.Price;
- finances.Add(item);
- });
- BudgetTableView item = null;
- budgets.Add(item);
- financesList.ForEach(it =>
- {
- BudgetTableView item = new BudgetTableView();
- item.Income = GetToIncome(currentUser);
- item.Expenditure = GetToExpenditure(currentUser);
- item.Result = GetToResult(currentUser);
- budgets[0] = item;
- });
-
- }
- }
- public class BudgetTableView
- {
- public decimal Income { get; set; }
- public decimal Expenditure { get; set; }
- public double Result { get; set; }
- }
- public class FinanceTableView
- {
- public int FinanceId { get; set; }
- public DateTime? DataRecording { get; set; }
- public string CategoryFinanceTitle { get; set; } = null!;
- public string Title { get; set; } = null!;
- public string CategoryTitle { get; set; } = null!;
- public decimal Price { get; set; }
- }
- }
|