/// <summary> /// If it is a new expense, set by default: /// 1. Today as Transaction date. /// 2. Draft as status /// 3. First available category /// 4. First available currency /// </summary> public void SetDefaultsValues() { if (Expense.Id == Guid.Empty && Expense.CanEdit()) { // Default transaction date to today Expense.msdyn_TransactionDate_utc = Expense.msdyn_TransactionDate_utc ?? DateTime.Today; // Default status to Draft Expense.msdyn_ExpenseStatus = Expense.msdyn_ExpenseStatus ?? new OptionSetValue((int)msdyn_expense_msdyn_expensestatus.Draft); // Default category to the first available one if (Expense.msdyn_ExpenseCategory == null) { msdyn_expensecategory expenseCategory = this.GetDefaultData <msdyn_expensecategory>(); if (expenseCategory != null) { EntityReference newReference = new EntityReference(expenseCategory.LogicalName, expenseCategory.Id); newReference.Name = expenseCategory.Preview; Expense.msdyn_ExpenseCategory = newReference; } } // Default transaction currency to the first available one. if (Expense.TransactionCurrency == null || Expense.TransactionCurrency.Id == Guid.Empty) { TransactionCurrency transactionCurrency = this.GetDefaultData <TransactionCurrency>(); if (transactionCurrency != null) { Expense.TransactionCurrency = transactionCurrency; } } } }
/// <summary> /// Search in the ReferenceData dictionary the object for given type and key /// and added to selected Expense /// </summary> /// <param name="key">selected item from UI</param> public async Task <bool> OnCategorySelected(string selectedItemKey, int selectedItemIndex) { msdyn_expensecategory selectedCategory = this.GetObjectByName <msdyn_expensecategory>(selectedItemKey); if (selectedCategory != null) { if (Expense.ExpenseCategory != null && selectedCategory.Id == Expense.ExpenseCategory.Id) { // It is not a change return(true); } else if (this.SetCategoryHandler != null) { // Allow all the behaviors to react based on new category return(await this.SetCategoryHandler(selectedCategory)); } else { Expense.ExpenseCategory = selectedCategory; return(true); } } else { return(false); } }
/// <summary> /// Check the behaviors of the new category, if there is data that needs to be deleted, /// show a warning message to the user before change category. /// </summary> /// <param name="newExpenseCategory">expense category selected by the user</param> /// <param name="warningMessage">If there is data to delete, message with details of the data to be deleted</param> /// <returns>true if the change of category was successful</returns> public async Task <bool> SetCategory(msdyn_expensecategory newExpenseCategory, string warningMessage = null) { bool allowSetCategory = true; if (!string.IsNullOrEmpty(warningMessage)) { // Confirm with user the change of category string message = string.Format(AppResources.ChangeExpenseTypeWarning, warningMessage); allowSetCategory = await MessageCenter.ShowDialog(message, null, null); } if (allowSetCategory) { this.ViewModel.Expense.ExpenseCategory = newExpenseCategory; // Change category and update new view this.ExtendedExpenseBehavior = ExpenseViewFactory.DecorateExpenseView(this.ExtendedExpenseBehavior); await this.ExtendedExpenseBehavior.CreateContent(); } return(allowSetCategory); }
/// <summary> /// Delegate to call change category /// </summary> /// <param name="newExpenseCategory"></param> /// <param name="warningMessage"></param> /// <returns></returns> public virtual async Task <bool> SetCategory(msdyn_expensecategory newExpenseCategory, string warningMessage = null) { return(await this.ExpenseView.SetCategory(newExpenseCategory, warningMessage)); }