/// <summary>
        /// Gets the budget amount for category.
        /// </summary>
        /// <param name="budgetItem">The budget item.</param>
        /// <param name="detailsCondition">The details condition.</param>
        /// <returns></returns>
        public decimal GetBudgetAmountForCategory(BudgetItem budgetItem, DetailsCondition detailsCondition)
        {
            var query = AccountBookDataContext.BudgetItems.Where(p => p.BudgetTargetId == budgetItem.AssociatedCategory.Id)
                .ToList().Sum(p => p.GetMoney());

            return query.GetValueOrDefault();
        }
 /// <summary>
 /// Counts for last settle amount for category. The value is represent in global currency rate by the default currency type.
 /// </summary>
 /// <param name="budgetItem">The budget item.</param>
 /// <returns></returns>
 public decimal CountForLastSettleAmountForCategory(BudgetItem budgetItem, SearchingScope scope = SearchingScope.LastMonth)
 {
     DetailsCondition dc = new DetailsCondition();
     dc.SearchingScope = scope;
     return CountSumOfBudgetItems(budgetItem, dc);
 }
        /// <summary>
        /// Counts the sum of budget items.
        /// </summary>
        /// <param name="budgetItem">The budget item.</param>
        /// <param name="dc">The dc.</param>
        /// <returns></returns>
        public decimal CountSumOfBudgetItems(BudgetItem budgetItem, DetailsCondition dc)
        {
            var query = AccountBookDataContext.AccountItems.AsQueryable();

            query = query.Where(p => p.CreateTime.Date >= dc.StartDate.Value.Date
                && p.CreateTime.Date <= dc.EndDate.Value.Date);

            if (budgetItem.BudgetItemType == BudgetType.ParentCategory)
            {
                var childIds = AccountBookDataContext.Categories
                    .Where(p => p.ParentCategoryId == budgetItem.AssociatedCategory.Id)
                    .Select(p => p.Id)
                        .ToList();

                query = query.Where(p => childIds.Contains(p.CategoryId));
            }
            else
            {
                query = query.Where(p => p.CategoryId == budgetItem.AssociatedCategory.Id);
            }

            var result = query.AsEnumerable().Sum(p => p.GetMoney());

            return result.GetValueOrDefault();
        }
        /// <summary>
        /// Updatings the associated categories for current edit instance.
        /// </summary>
        /// <param name="category">The category.</param>
        public void UpdatingAssociatedCategoriesForCurrentEditInstance(params Category[] categories)
        {
            var currentCategories = CurrentEditOrViewProject.BudgetItems.Select(p => p.AssociatedCategory.Id).ToList();

            var targetNews = categories.Where(p => !currentCategories.Contains(p.Id)).ToList();

            if (targetNews.Count == 0)
                return;

            if (CurrentEditOrViewProject.Id == Guid.Empty)
            {
                foreach (var item in targetNews)
                {
                    BugetItemsForAdd.Add(new BudgetItem()
                    {
                        Amount = 0.0m,
                        ProjectId = GuidForAdd,
                        BudgetItemType = item.IsParent ? BudgetType.ParentCategory : BudgetType.Category,
                        AssociatedCategory = item,
                    });
                }
            }
            else
            {
                foreach (var item in targetNews)
                {
                    var budgetItem = new BudgetItem()
                    {
                        Amount = 0.0m,
                        BudgetProject = CurrentEditOrViewProject,
                        BudgetItemType = item.IsParent ? BudgetType.ParentCategory : BudgetType.Category,
                        AssociatedCategory = item,
                    };

                    BugetItemsForAdd.Add(budgetItem);
                    BugetItemsForEditToAdd.Add(budgetItem);
                    CurrentEditOrViewProject.BudgetItems.Add(budgetItem);
                }

                if (BugetItemsForEditToAdd.Count > 0)
                {
                    AccountBookDataContext.BudgetItems.InsertAllOnSubmit(BugetItemsForEditToAdd);
                    AccountBookDataContext.SubmitChanges();

                    BugetItemsForEditToAdd.Clear();
                    CurrentEditOrViewProject.AssociatedBudgetItemsSummary = CurrentEditOrViewProject.GetNotesForProject(BugetItemsForAdd);
                }
            }

            UpdateCurrentMonthBudgetSummary();
        }
 private void detach_ToDo(BudgetItem toDo)
 {
     this.OnNotifyPropertyChanging("BudgetItem");
     toDo.BudgetProject = null;
 }
        private void LoadStasticsInfo(Category category)
        {
            int count = 0;
            DetailsCondition dc = new DetailsCondition
            {
                SearchingScope = SearchingScope.CurrentMonth
            };


            decimal val = ViewModelLocator.CategoryViewModel.CountStatistic(category, dc, delegate(int p)
            {
                count = p;
            });


            AccountItem ai = new AccountItem
            {
                SecondInfo = this.currentMonthName,
                ThirdInfo = AppResources.StatisticsInfoFormatterForCategory.FormatWith(new object[] { count, string.Empty, "{0}{1}".FormatWith(new object[] { this.symbol, val.ToMoneyF2() }) })
            };
            count = 0;

            DetailsCondition condition2 = new DetailsCondition
            {
                SearchingScope = SearchingScope.LastMonth
            };

            val = ViewModelLocator.CategoryViewModel.CountStatistic(category, condition2, delegate(int p)
            {
                count = p;
            });
            AccountItem lastMonthai = new AccountItem
            {
                SecondInfo = this.lastMonthName,
                ThirdInfo = AppResources.StatisticsInfoFormatterForCategory.FormatWith(new object[] { count, string.Empty, "{0}{1}".FormatWith(new object[] { this.symbol, val.ToMoneyF2() }) })
            };
            base.Dispatcher.BeginInvoke(delegate
            {
                this.CurrentMonthStaticsInfoPanel.DataContext = ai;
                this.LastMonthStaticsInfoPanel.DataContext = lastMonthai;
                this.hasLoadStatisticsInfo = true;
                this.WorkDone();
            });

            BudgetItem budgetItem = new BudgetItem
            {
                AssociatedCategory = category,
                BudgetType = category.CategoryType
            };
            DetailsCondition detailsCondition = new DetailsCondition
            {
                SearchingScope = SearchingScope.CurrentMonth
            };
            decimal monthlyBudgetAmount = ViewModelLocator.BudgetProjectViewModel.GetBudgetAmountForCategory(budgetItem, detailsCondition);
            base.Dispatcher.BeginInvoke(delegate
            {
                this.BudgetBlock.Text = "{0}{1}".FormatWith(new object[] { this.symbol, monthlyBudgetAmount.ToMoneyF2() });
            });
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
            {
                budgetItem = BudgetItemGetter();
                this.LastScopeInfoTitleBlock.Text = AppResources.BudgetForLastMonthCertainExpenseTitle.FormatWith(budgetItem.AssociatedCategory.CategoryInfo);

                this.FilterType.DataContext = budgetItem;

                this.KeyNameBlock.Text = AppResources.BudgetForCertain.FormatWith(this.GetNavigatingParameter("currentScope"));
                this.FilterType.SelectedIndex = budgetItem.SettleTypeIndex;
                this.KeyNameResultBox.Text = budgetItem.Amount.ToMoneyF2();
            }
        }
 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
 {
     base.OnNavigatedFrom(e);
     this.budgetItem = null;
 }