Inheritance: IEntityBase
        private void SaveAction(object e)
        {
            Validate();

            if (IsValid)
            {
                if (!_isEdit)
                {
                    MoneyBox newMoneyBox = new MoneyBox()
                    {
                        Title = Title,
                        YearBudget = YearBudget,
                        MonthBudget = MonthBudget
                    };

                    _moneyBoxService.Insert(newMoneyBox, false, false);

                    newMoneyBox.PrimaryCurrency = _currenciesService.Get(SelectedPrimaryCurrency.Id);

                    _moneyBoxService.Update(newMoneyBox, true);

                    ReportingPeriod yearPeriod = new ReportingPeriod()
                    {
                        Type = ReportingPeriodType.Year,
                        Period = DateTime.Now,
                        Budget = newMoneyBox.YearBudget,
                    };

                    _reportingPeriodsService.Insert(yearPeriod, false, false);

                    yearPeriod.MoneyBox = _moneyBoxService.Get(newMoneyBox.Id);

                    yearPeriod.ParentPeriod = null;

                    _reportingPeriodsService.Update(yearPeriod, true);

                    ReportingPeriod monthPeriod = new ReportingPeriod()
                    {
                        Type = ReportingPeriodType.Month,
                        Period = DateTime.Now,
                        Budget = newMoneyBox.MonthBudget,
                    };

                    _reportingPeriodsService.Insert(monthPeriod, false, false);

                    monthPeriod.MoneyBox = _moneyBoxService.Get(newMoneyBox.Id);

                    monthPeriod.ParentPeriod = _reportingPeriodsService.Get(yearPeriod.Id);

                    _reportingPeriodsService.Update(monthPeriod, true);

                }
                else
                {
                    _moneyBox.Title = Title;
                    _moneyBox.YearBudget = YearBudget;
                    _moneyBox.MonthBudget = MonthBudget;

                    _moneyBoxService.Insert(_moneyBox, true, false);

                    _moneyBox.PrimaryCurrency = _currenciesService.Get(SelectedPrimaryCurrency.Id);

                    _moneyBoxService.Update(_moneyBox, true);
                }

                _navigationService.ClearNavigationData("MoneyBoxId");
                _navigationService.Navigate(typeof(MoneyBoxesPage));
            }
        }
Exemplo n.º 2
0
        public void Initialize()
        {
            List<Category> categories = new List<Category>()
            {
                new Category()
                {
                    Title = "Salary",
                    Type = TransactionType.Income,
                    Image = "/Assets/Icons/Light/appbar.currency.dollar.light.png"
                },
                new Category()
                {
                    Title = "Gifts",
                    Type = TransactionType.Income,
                    Image = "/Assets/Icons/Light/appbar.gift.png"
                },
                new Category()
                {
                    Title = "Nightlife",
                    Type = TransactionType.Expence,
                    Image = "/Assets/Icons/Light/appbar.drinks.beer.png"
                },
                new Category()
                {
                    Title = "Food",
                    Type = TransactionType.Expence,
                    Image = "/Assets/Icons/Light/appbar.food.silverware.png"
                },
                new Category()
                {
                    Title = "Electricity",
                    Type = TransactionType.Expence,
                    Image = "/Assets/Icons/Light/appbar.lightbulb.png"
                },
                new Category()
                {
                    Title = "Gifts",
                    Type = TransactionType.Expence,
                    Image = "/Assets/Icons/Light/appbar.gift.png"
                }
            };

            _categoryService.InsertAll(categories, false, false);

            Account account = new Account()
            {
                Username = "******",
                Password = "******",
                Email = "*****@*****.**"
            };

            _accountService.Insert(account, false, false);

            List<Currency> currencies = new List<Currency>()
            {
                new Currency()
                {
                    Title = "United States Dollar",
                    Abbreviation = CurrencyType.USD,
                    Symbol = "$",
                    Image = "/Assets/Icons/Light/appbar.currency.dollar.light.png"
                },
                new Currency()
                {
                    Title = "European Euro",
                    Abbreviation = CurrencyType.EUR,
                    Symbol = "€",
                    Image = "/Assets/Icons/Light/appbar.currency.euro.png"
                },
                new Currency()
                {
                    Title = "Great Britain Pound",
                    Abbreviation = CurrencyType.GBP,
                    Symbol = "£",
                    Image = "/Assets/Icons/Light/appbar.currency.pound.png"
                },
                new Currency()
                {
                    Title = "Ukrainian Hryvna",
                    Abbreviation = CurrencyType.UAH,
                    Symbol = "₴",
                    Image = "/Assets/Icons/Light/appbar.currency.grivna.png"
                },
                new Currency()
                {
                    Title = "Russian Ruble",
                    Abbreviation = CurrencyType.RUB,
                    Symbol = "₽",
                    Image = "/Assets/Icons/Light/appbar.currency.rubles.png"
                }
            };

            _currencyService.InsertAll(currencies, false, false);

            List<CurrencyExchange> currencyExchanges = new List<CurrencyExchange>();

            foreach (Currency from in currencies)
            {
                foreach (Currency to in currencies.Where(c => c.Id != from.Id))
                {
                    currencyExchanges.Add(new CurrencyExchange()
                    {
                        From = from.Abbreviation.ToString(),
                        To = to.Abbreviation.ToString()
                    });
                }
            }

            _currencyExchangeService.InsertAll(currencyExchanges, false, false);

            MoneyBox euroMoneyBox = new MoneyBox()
            {
                Title = "Euro Money Box",
                YearBudget = 80000M,
                MonthBudget = 7000M
            };

            MoneyBox dollarMoneyBox = new MoneyBox()
            {
                Title = "Dollar Money Box",
                YearBudget = 90000M,
                MonthBudget = 7500M
            };

            _moneyBoxesService.Insert(euroMoneyBox, false, false);

            _moneyBoxesService.Insert(dollarMoneyBox, false, false);

            euroMoneyBox.PrimaryCurrency = _currencyService.Get(2);

            _moneyBoxesService.Update(euroMoneyBox, true);

            dollarMoneyBox.PrimaryCurrency = _currencyService.Get(1);

            _moneyBoxesService.Update(dollarMoneyBox, true);

            ReportingPeriod euroMoneyBox2016Period = new ReportingPeriod()
            {
                Type = ReportingPeriodType.Year,
                Period = DateTime.Now,
                Income = 12000M,
                Expence = 6000M,
                Budget = 80000M,
            };

            _reportingPeriodsService.Insert(euroMoneyBox2016Period, false, false);

            euroMoneyBox2016Period.MoneyBox = _moneyBoxesService.Get(1);

            euroMoneyBox2016Period.ParentPeriod = null;

            _reportingPeriodsService.Update(euroMoneyBox2016Period, true);

            ReportingPeriod euroMoneyBoxMarch2016Period = new ReportingPeriod()
            {
                Type = ReportingPeriodType.Month,
                Period = DateTime.Now.AddMonths(-1),
                Income = 10000M,
                Expence = 5000M,
                Budget = 7000M,
            };

            _reportingPeriodsService.Insert(euroMoneyBoxMarch2016Period, false, false);

            euroMoneyBoxMarch2016Period.MoneyBox = _moneyBoxesService.Get(1);

            euroMoneyBoxMarch2016Period.ParentPeriod = _reportingPeriodsService.Get(1);

            _reportingPeriodsService.Update(euroMoneyBoxMarch2016Period, true);

            ReportingPeriod dollarMoneyBox2016Period = new ReportingPeriod()
            {
                Type = ReportingPeriodType.Year,
                Period = DateTime.Now,
                Income = 13000M,
                Expence = 10000M,
                Budget = 80000M,
            };

            _reportingPeriodsService.Insert(dollarMoneyBox2016Period, false, false);

            dollarMoneyBox2016Period.MoneyBox = _moneyBoxesService.Get(2);

            dollarMoneyBox2016Period.ParentPeriod = null;

            _reportingPeriodsService.Update(dollarMoneyBox2016Period, true);

            ReportingPeriod dollarMoneyBoxMarch2016Period = new ReportingPeriod()
            {
                Type = ReportingPeriodType.Month,
                Period = DateTime.Now.AddMonths(-1),
                Income = 7000M,
                Expence = 8000M,
                Budget = 5000M,
            };

            _reportingPeriodsService.Insert(dollarMoneyBoxMarch2016Period, false, false);

            dollarMoneyBoxMarch2016Period.MoneyBox = _moneyBoxesService.Get(2);

            dollarMoneyBoxMarch2016Period.ParentPeriod = _reportingPeriodsService.Get(3);

            _reportingPeriodsService.Update(dollarMoneyBoxMarch2016Period, true);

            Setting setting = new Setting()
            {
                TransferBalance = true,
                PeriodStartDay = 15
            };

            _settingsService.Insert(setting, false, false);
        }
        private void InitIfEdit()
        {
            object moneyBoxId = _navigationService.GetNavigationData("MoneyBoxId");

            if (moneyBoxId != null)
            {
                _isEdit = true;

                _moneyBox = _moneyBoxService.Get((int)moneyBoxId);

                PageTitle = "Edit Box";
                Title = _moneyBox.Title;
                YearBudget = _moneyBox.YearBudget;
                MonthBudget = _moneyBox.MonthBudget;
                SelectedPrimaryCurrency = Currencies.Where(x => x.Id == _moneyBox.PrimaryCurrency.Id).Single();
            }
            else
            {
                PageTitle = "New Box";
                Title = "";
                YearBudget = 0M;
                MonthBudget = 0M;
            }
        }
        private void AcceptAction(object e)
        {
            if (IsCalculatorOpened)
            {
                IsCalculatorOpened = false;
                IsCalculatorClosed = true;

                Amount = Result;
            }
            else
            {
                Validate();

                if (IsValid)
                {
                    object moneyBoxId = _navigationService.GetNavigationData("MoneyBoxId");

                    MoneyBox moneyBox = new MoneyBox();

                    if (moneyBoxId != null)
                        moneyBox = _moneyBoxesService.Get((int)moneyBoxId, true);

                    Transaction transaction = new Transaction()
                    {
                        Value = Amount,
                        ToPrimaryCoeff = _currencyRatesService.GetExchangeCoeff(
                            SelectedCurrency.Abbreviation.ToString(),
                            moneyBox.PrimaryCurrency.Abbreviation.ToString()),
                        Note = Notes,
                        Created = CreationDate,
                        Type = (TransactionType)Enum.Parse(typeof(TransactionType), TransactionType.ToString()),
                    };

                    _transactionService.Insert(transaction, false, false);

                    transaction.Category = _categoriesService.Get(SelectedCategory.Id);

                    transaction.Currency = _currenciesService.Get(SelectedCurrency.Id);

                    ReportingPeriod period = _reportingPeriodsService.GetAll()
                        .Where(x => x.Period.Month == CreationDate.Month &&
                            x.Period.Year == CreationDate.Year &&
                            x.Type == ReportingPeriodType.Month &&
                            x.MoneyBoxId == moneyBox.Id)
                            .SingleOrDefault();

                    ReportingPeriod parentPeriod = period.ParentPeriod;

                    decimal ratedAmount = moneyBox.PrimaryCurrency.Id.Equals(SelectedCurrency.Id) ? Amount :
                            _currencyRatesService.Exchange(
                                SelectedCurrency.Abbreviation.ToString(),
                                moneyBox.PrimaryCurrency.Abbreviation.ToString(),
                                Amount);

                    if (TransactionType == 0)
                    {
                        period.Income = period.Income + ratedAmount;
                        parentPeriod.Income = parentPeriod.Income + ratedAmount;
                    }
                    else
                    {
                        period.Expence = period.Expence + ratedAmount;
                        parentPeriod.Expence = parentPeriod.Expence + ratedAmount;
                    }

                    _reportingPeriodsService.Insert(period, true, false);

                    _reportingPeriodsService.Insert(parentPeriod, true, false);

                    transaction.ReportingPeriod = period;

                    _transactionService.Update(transaction, true);

                    _navigationService.Navigate(typeof(MoneyBoxPage));
                }
            }
        }