private void cbDestinationPlan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            MoneyDataSet.PlannedTransactionsRow plan = cbDestinationPlan.SelectedItem as MoneyDataSet.PlannedTransactionsRow;

            // protection from old schema
            if (plan.TransactionTemplatesRow == null)
            {
                ErrorHelper.ShowErrorBox(ErrorHelper.Errors.PlanTemplateIsNull);
                return;
            }

            // cbDestinationAccount.DataSource = keeper.GetAccounts(plan.AccountTypeRow, plan.TransactionTemplatesRow.ExactDestinationAccountType);
            cbDestinationAccount.Items.Clear();
            foreach (var acc in keeper.GetAccounts(plan.AccountTypeRow, plan.TransactionTemplatesRow.ExactDestinationAccountType))
            {
                cbDestinationAccount.Items.Add(acc);
            }
            cbDestinationAccount.SelectedIndex = 0;

            cbDestinationAccount_SelectionChangeCommitted(null, null);

            if (!plan.IsAggregated)
            {
                MoneyDataSet.AccountsRow account = cbDestinationAccount.SelectedItem as MoneyDataSet.AccountsRow;
                numDestinationAmount.Value = (decimal)(plan.Amount * plan.CurrenciesRow.ExchangeRate / account.CurrenciesRow.ExchangeRate);
            }
            else
            {
                numDestinationAmount.Value = 0;
            }
            numDestinationAmount.Select(0, Int32.MaxValue);
        }
Example #2
0
 public PlanEditForm(MoneyDataSet.TransactionTemplatesRow template,
                     MoneyDataSet.PlannedTransactionsRow sourcePlan = null, MoneyDataSet.PlannedTransactionsRow destinationPlan = null, bool isCopy = false)
 {
     InitializeComponent();
     this.keeper          = MoneyDataKeeper.Instance;
     this.template        = template;
     this.sourcePlan      = sourcePlan;
     this.destinationPlan = destinationPlan;
     this.isCopy          = isCopy;
 }
Example #3
0
        public static bool DeletePlannedTransaction(MoneyDataSet.PlannedTransactionsRow plan)
        {
            String message = plan.IsPairReferenceIDNull() ? String.Format(Resources.Labels.DeletePlannedTransactionFormat, plan.FullTitle) :
                             String.Format(Resources.Labels.DeletePairedPlannedTransactionFormat, plan.FullTitle);

            if (MessageBox.Show(message, Resources.Labels.DeletePlannedTransactionTitle, MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                MoneyDataKeeper.Instance.DeletePlannedTransaction(plan.ID);
                return(true);
            }
            return(false);
        }
Example #4
0
        public PlanViewForm(MoneyDataSet.PlannedTransactionsRow plan)
        {
            InitializeComponent();

            // locate source and destination plans
            if ((!plan.IsPairReferenceIDNull()) && (plan.PairReferenceID != 0))
            {
                foreach (MoneyDataSet.PlannedTransactionsRow p in
                         keeper.PlannedTransactions.Where(p => ((!p.IsPairReferenceIDNull()) &&
                                                                (p.PairReferenceID == plan.PairReferenceID))))
                {
                    // setting source and destination
                    if (p.TransactionTypeID.Equals(plan.TransactionTemplatesRow.SourceTransactionTypeID))
                    {
                        sourcePlan = p;
                    }
                    else if (p.TransactionTypeID.Equals(plan.TransactionTemplatesRow.DestinationTransactionTypeID))
                    {
                        destinationPlan = p;
                    }
                }

                if ((sourcePlan == null) || (destinationPlan == null))
                {
                    ErrorHelper.ShowErrorBox(ErrorHelper.Errors.InvalidTransaction);
                    return;
                }

                tbDestinationAccountType.Text = destinationPlan.AccountTypeRow.Title;
                tbDestinationAmount.Text      = destinationPlan.Amount.ToString(Consts.UI.CurrencyFormat,
                                                                                destinationPlan.CurrenciesRow.CurrencyCultureInfo);
            }
            else
            {
                sourcePlan = plan;
                // only one planned transaction, removing second column
                tlpTemplatePlan.Controls.Remove(gbDestination);
                tlpTemplatePlan.SetColumnSpan(gbSource, 2);
            }


            tbTitle.Text             = sourcePlan.FullTitle;
            tbSourceAccountType.Text = sourcePlan.AccountTypeRow.Title;
            tbSourceAmount.Text      = sourcePlan.Amount.ToString(Consts.UI.CurrencyFormat,
                                                                  sourcePlan.CurrenciesRow.CurrencyCultureInfo);
            tbDescription.Text = sourcePlan.Description;
            ttbTags.Tags       = keeper.GetPlannedTransactionTagStrings(sourcePlan);

            this.DialogResult = DialogResult.Cancel;
        }
Example #5
0
        private void tsbEditCopy_Click(object sender, EventArgs e)
        {
            if (dgvPlans.SelectedRows.Count == 1)
            {
                MoneyDataSet.PlannedTransactionsRow plan = dgvPlans.SelectedRows[0].Tag as MoneyDataSet.PlannedTransactionsRow;

                MoneyDataSet.PlannedTransactionsRow sourcePlan = null;
                MoneyDataSet.PlannedTransactionsRow destinationPlan = null;

                if (plan.TransactionTemplatesRow == null)
                {
                    ErrorHelper.ShowErrorBox(ErrorHelper.Errors.PlanWithoutTemplate);
                    Log.Write("Plan", plan);
                    return;
                }

                if (plan.TransactionTemplatesRow.HasDestinationAccount)
                {
                    sourcePlan = keeper.PlannedTransactions.SingleOrDefault(p => ((!p.IsPairReferenceIDNull()) &&
                        (p.PairReferenceID == plan.PairReferenceID) &&
                        (p.TransactionTypeID.Equals(plan.TransactionTemplatesRow.SourceTransactionTypeID))));

                    destinationPlan = keeper.PlannedTransactions.SingleOrDefault(p => ((!p.IsPairReferenceIDNull()) &&
                        (p.PairReferenceID == plan.PairReferenceID) &&
                        (p.TransactionTypeID.Equals(plan.TransactionTemplatesRow.DestinationTransactionTypeID))));
                }
                else
                {
                    sourcePlan = plan;
                }

                PlanEditForm form = new PlanEditForm(sourcePlan.TransactionTemplatesRow,
                    sourcePlan, destinationPlan, (sender as ToolStripButton).Equals(tsbCopy));

                if (form.ShowDialog(this) == DialogResult.OK)
                {
                    refreshPlans();
                    foreach (DataGridViewRow row in dgvPlans.Rows)
                    {
                        if (row.Tag == form.UpdatedPlan)
                        {
                            dgvPlans.FirstDisplayedCell = dgvPlans.CurrentCell = row.Cells[0];
                            break;
                        }
                    }
                }
            }
        }
        private void cbSourcePlan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            MoneyDataSet.PlannedTransactionsRow plan = cbSourcePlan.SelectedItem as MoneyDataSet.PlannedTransactionsRow;
            // protection from old schema
            if (plan.TransactionTemplatesRow == null)
            {
                ErrorHelper.ShowErrorBox(ErrorHelper.Errors.PlanTemplateIsNull);
                return;
            }

            // cbSourceAccount.DataSource = keeper.GetAccounts(plan.AccountTypeRow, plan.TransactionTemplatesRow.ExactSourceAccountType);
            cbSourceAccount.Items.Clear();
            foreach (var acc in keeper.GetAccounts(plan.AccountTypeRow, plan.TransactionTemplatesRow.ExactSourceAccountType))
            {
                cbSourceAccount.Items.Add(acc);
            }
            cbSourceAccount.SelectedIndex = 0;
            cbSourceAccount_SelectionChangeCommitted(null, null);

            MoneyDataSet.AccountsRow account = cbSourceAccount.SelectedItem as MoneyDataSet.AccountsRow;

            if (!plan.IsAggregated)
            {
                numSourceAmount.Value = (decimal)(plan.Amount * plan.CurrenciesRow.ExchangeRate / account.CurrenciesRow.ExchangeRate);
            }
            else
            {
                numSourceAmount.Value = 0;
            }

            numSourceAmount.Select(0, Int32.MaxValue);

            if ((tbTitle.Text.Equals(template.TransactionDefaultTitle)) || (sourcePlans.Count(p => (p.Title.Equals(tbTitle.Text))) > 0))
            {
                if (String.IsNullOrWhiteSpace(plan.Title))
                {
                    tbTitle.Text = template.Title;
                }
                else
                {
                    tbTitle.Text = plan.Title;
                }
            }
            ttbTags.Tags = keeper.GetPlannedTransactionTagStrings(plan);
        }
Example #7
0
        private void tsbImplement_Click(object sender, EventArgs e)
        {
            if (dgvPlans.SelectedRows.Count == 1)
            {
                MoneyDataSet.PlannedTransactionsRow plan = dgvPlans.SelectedRows[0].Tag as MoneyDataSet.PlannedTransactionsRow;

                TransactionEditForm form = new TransactionEditForm(plan.TransactionTemplatesRow, plan);

                if (form.ShowDialog(this) == DialogResult.OK)
                {
                    refreshPlans();

                    foreach (DataGridViewRow row in dgvPlans.Rows)
                    {
                        if (row.Tag == plan)
                        {
                            dgvPlans.FirstDisplayedCell = dgvPlans.CurrentCell = row.Cells[0];
                            break;
                        }
                    }
                }
            }
        }
        public TransactionEditForm(MoneyDataSet.TransactionTemplatesRow template, MoneyDataSet.PlannedTransactionsRow plan = null)
        {
            InitializeComponent();
            this.keeper     = MoneyDataKeeper.Instance;
            this.template   = template;
            this.sourcePlan = plan;

            sourceAccounts = keeper.GetAccounts(template.AccountTypesRowByFK_AccountTypes_Source_TransactionTemplates,
                                                template.ExactSourceAccountType);

            if (template.HasDestinationAccount)
            {
                if (sourcePlan != null)
                {
                    int pairID = sourcePlan.PairReferenceID;
                    sourcePlan = null;

                    // trying to find source and destination plans
                    foreach (MoneyDataSet.PlannedTransactionsRow pairedPlan in keeper.PlannedTransactions.Where(p =>
                                                                                                                ((!p.IsPairReferenceIDNull()) && (p.PairReferenceID != 0) && (p.PairReferenceID == pairID))))
                    {
                        if (pairedPlan.TransactionTypeID.Equals(template.SourceTransactionTypeID))
                        {
                            sourcePlan = pairedPlan;
                        }
                        if (pairedPlan.TransactionTypeID.Equals(template.DestinationTransactionTypeID))
                        {
                            destinationPlan = pairedPlan;
                        }
                    }
                }

                destinationAccounts = keeper.GetAccounts(template.AccountTypesRowByFK_AccountTypes_Destination_TransactionTemplates,
                                                         template.ExactDestinationAccountType);
            }
        }
Example #9
0
        private void openSearchResult(bool editMode = false)
        {
            object tag = dgvSearchResults.CurrentRow.Tag;

            if (tag is MoneyDataSet.AccountsRow)
            {
                MoneyDataSet.AccountsRow account = tag as MoneyDataSet.AccountsRow;
                if (editMode)
                {
                    AccountEditForm form = new AccountEditForm(account.AccountTypesRow.IsDebit, account: account);
                    if (form.ShowDialog() != DialogResult.Cancel)
                    {
                        updateTagCloud();
                        dgvSearchResults.Rows.Clear();
                    }
                }
                else
                {
                    AccountViewForm form = new AccountViewForm(account);
                    if (form.ShowDialog() != DialogResult.Cancel)
                    {
                        updateTagCloud();
                        dgvSearchResults.Rows.Clear();
                    }
                }
            }
            else if (tag is MoneyDataSet.TransactionsRow)
            {
                MoneyDataSet.TransactionsRow transaction = tag as MoneyDataSet.TransactionsRow;
                TransactionViewForm          form        = new TransactionViewForm(transaction);
                if (form.ShowDialog() != DialogResult.Cancel)
                {
                    dgvSearchResults.Rows.Remove(dgvSearchResults.CurrentRow);
                }
            }
            else if (tag is MoneyDataSet.PlannedTransactionsRow)
            {
                MoneyDataSet.PlannedTransactionsRow plan = tag as MoneyDataSet.PlannedTransactionsRow;
                if (editMode)
                {
                    MoneyDataSet.PlannedTransactionsRow sourcePlan      = null;
                    MoneyDataSet.PlannedTransactionsRow destinationPlan = null;

                    if (plan.TransactionTemplatesRow == null)
                    {
                        ErrorHelper.ShowErrorBox(ErrorHelper.Errors.PlanWithoutTemplate);
                        Log.Write("Plan", plan);
                        return;
                    }

                    if (plan.TransactionTemplatesRow.HasDestinationAccount)
                    {
                        sourcePlan = keeper.PlannedTransactions.SingleOrDefault(p => ((!p.IsPairReferenceIDNull()) &&
                                                                                      (p.PairReferenceID == plan.PairReferenceID) &&
                                                                                      (p.TransactionTypeID.Equals(plan.TransactionTemplatesRow.SourceTransactionTypeID))));

                        destinationPlan = keeper.PlannedTransactions.SingleOrDefault(p => ((!p.IsPairReferenceIDNull()) &&
                                                                                           (p.PairReferenceID == plan.PairReferenceID) &&
                                                                                           (p.TransactionTypeID.Equals(plan.TransactionTemplatesRow.DestinationTransactionTypeID))));
                    }
                    else
                    {
                        sourcePlan = plan;
                    }

                    PlanEditForm form = new PlanEditForm(sourcePlan.TransactionTemplatesRow,
                                                         sourcePlan, destinationPlan);

                    if (form.ShowDialog(this) != DialogResult.Cancel)
                    {
                        updateTagCloud();
                        dgvSearchResults.Rows.Clear();
                    }
                }
                else
                {
                    PlanViewForm form = new PlanViewForm(plan);
                    if (form.ShowDialog(this) != DialogResult.Cancel)
                    {
                        updateTagCloud();
                        dgvSearchResults.Rows.Clear();
                    }
                }
            }
            else
            {
                ErrorHelper.ShowErrorBox(ErrorHelper.Errors.UnknownSearchResult);
                Log.Write("Found in search", tag);
            }

            //updateTagCloud();
            //dgvSearchResults.Rows.Clear();
        }
Example #10
0
        private void PlanFromTemplateForm_Load(object sender, EventArgs e)
        {
            // some pre-checks
            if (template == null)
            {
                ErrorHelper.ShowErrorBox(ErrorHelper.Errors.PlanTemplateIsNull);
                this.DialogResult = DialogResult.Cancel;
                this.Close();
                return;
            }

            if ((sourcePlan != null) && (template.HasDestinationAccount) && (destinationPlan == null))
            {
                ErrorHelper.ShowErrorBox(ErrorHelper.Errors.PlanDestinationAccountIsNull);
                Log.Write("Source plan", sourcePlan);
                Log.Write("Template", template);
                this.DialogResult = DialogResult.Cancel;
                this.Close();
                return;
            }

            this.Text = String.Format(Resources.Labels.PlannedTemplateFormat, template.Title);
            lblTransactionMessage.Text = template.Message;

            tbTitle.AutoCompleteCustomSource.AddRange(keeper.GetTextHistory(String.Format(Consts.Keeper.TransactionTitleHistoryIDFormat, template.ID)));

            if (sourcePlan != null)
            {
                if (isCopy)
                {
                    tbTitle.Text = String.Format(Resources.Labels.CopyFormat, sourcePlan.Title);
                }
                else
                {
                    tbTitle.Text = sourcePlan.Title;
                }

                cbIsAggregated.Checked = sourcePlan.IsAggregated;
            }
            else
            {
                tbTitle.Text = template.TransactionDefaultTitle;
            }
            ttbTags.SetAvailableTags(keeper.Tags);


            cbSourceAccountType.DisplayMember = keeper.DataSet.AccountTypes.TitleColumn.ColumnName;
            cbSourceAccountType.ValueMember   = keeper.DataSet.AccountTypes.IDColumn.ColumnName;
            // cbSourceAccountType.DataSource = keeper.GetAccountTypes(template.AccountTypesRowByFK_AccountTypes_Source_TransactionTemplates,
            //    template.ExactSourceAccountType);
            foreach (var accountType in keeper.GetAccountTypes(template.AccountTypesRowByFK_AccountTypes_Source_TransactionTemplates, template.ExactSourceAccountType))
            {
                cbSourceAccountType.Items.Add(accountType);
            }
            cbSourceAccountType.SelectedIndex = 0;

            cbSourceCurrency.DisplayMember = keeper.DataSet.Currencies.TitleColumn.ColumnName;
            cbSourceCurrency.ValueMember   = keeper.DataSet.Currencies.IDColumn.ColumnName;
            //cbSourceCurrency.DataSource = keeper.Currencies;
            cbDestinationCurrency.DisplayMember = keeper.DataSet.Currencies.TitleColumn.ColumnName;
            cbDestinationCurrency.ValueMember   = keeper.DataSet.Currencies.IDColumn.ColumnName;
            //cbDestinationCurrency.DataSource = keeper.Currencies;

            foreach (MoneyDataSet.CurrenciesRow currency in keeper.Currencies)
            {
                cbSourceCurrency.Items.Add(currency);
                cbDestinationCurrency.Items.Add(currency);
            }
            cbSourceCurrency.SelectedIndex      = 0;
            cbDestinationCurrency.SelectedIndex = 0;

            cbRecurrency.DisplayMember = keeper.DataSet.Recurrencies.TitleColumn.ColumnName;
            cbRecurrency.ValueMember   = keeper.DataSet.Recurrencies.IDColumn.ColumnName;
            // cbRecurrency.DataSource = keeper.Recurrencies;
            foreach (var recurrency in keeper.Recurrencies)
            {
                cbRecurrency.Items.Add(recurrency);
            }
            cbRecurrency.SelectedIndex = 0;

            cbRecurrency_SelectionChangeCommitted(null, null);

            if (template.HasDestinationAccount)
            {
                cbDestinationAccountType.DisplayMember = keeper.DataSet.AccountTypes.TitleColumn.ColumnName;
                cbDestinationAccountType.ValueMember   = keeper.DataSet.AccountTypes.IDColumn.ColumnName;
                //cbDestinationAccountType.DataSource = keeper.GetAccountTypes(template.AccountTypesRowByFK_AccountTypes_Destination_TransactionTemplates,
                //    template.ExactDestinationAccountType);
                foreach (var accountType in keeper.GetAccountTypes(template.AccountTypesRowByFK_AccountTypes_Destination_TransactionTemplates, template.ExactDestinationAccountType))
                {
                    cbDestinationAccountType.Items.Add(accountType);
                }
                cbDestinationAccountType.SelectedIndex = 0;

                if (template.IsAmountIdentical)
                {
                    numDestinationAmount.Enabled = false;
                }
                updateDestinationAmount();
            }
            else
            {
                tlpTemplatePlan.Controls.Remove(gbDestination);
                tlpTemplatePlan.SetColumnSpan(gbSource, 2);

                // renaming Source to Account
                gbSource.Text = Resources.Labels.AccountAmountGroupBoxLabel;
            }

            if (sourcePlan != null)
            {
                cbSourceAccountType.SelectedItem = sourcePlan.AccountTypeRow;
                cbSourceCurrency.SelectedItem    = sourcePlan.CurrenciesRow;
                cbRecurrency.SelectedItem        = sourcePlan.RecurrenciesRow;
                ttbTags.Tags          = keeper.GetPlannedTransactionTagStrings(sourcePlan);
                numSourceAmount.Value = (decimal)sourcePlan.Amount;

                if (sourcePlan.IsStartTimeNull())
                {
                    dtpStartDate.Checked = false;
                }
                else
                {
                    dtpStartDate.Checked = true;
                    dtpStartDate.Value   = sourcePlan.StartTime;
                }
                dtpStartDate_ValueChanged(null, null);

                if (destinationPlan != null)
                {
                    cbDestinationAccountType.SelectedItem = destinationPlan.AccountTypeRow;
                    cbDestinationCurrency.SelectedItem    = destinationPlan.CurrenciesRow;
                    numDestinationAmount.Value            = (decimal)destinationPlan.Amount;
                }
            }
            else
            {
                dtpStartDate.Checked = true;
            }

            if ((sourcePlan != null) && (!sourcePlan.RecurrencyID.Equals(MoneyDataSet.IDs.Recurrencies.None)) && (!sourcePlan.IsEndTimeNull()))
            {
                dtpEndDate.Value   = sourcePlan.EndTime;
                dtpEndDate.Enabled = true;
                lblEndDate.Enabled = true;
            }
            else
            {
                // default end date - next month, unchecked
                dtpEndDate.Value   = DateTime.Now.AddMonths(1);
                dtpEndDate.Checked = false;
                lblEndDate.Enabled = false;
            }

            if (isCopy)
            {
                // clearing info, so submit will create new entries
                sourcePlan      = null;
                destinationPlan = null;
            }
            numSourceAmount.Select(0, Int32.MaxValue);
            numDestinationAmount.Select(0, Int32.MaxValue);
        }
Example #11
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            tbTitle.Text = tbTitle.Text.Trim();

            DateTime?startDate = null;
            DateTime?endDate   = null;

            MoneyDataSet.RecurrenciesRow recurrency = cbRecurrency.SelectedItem as MoneyDataSet.RecurrenciesRow;
            if (dtpStartDate.Checked)
            {
                startDate = dtpStartDate.Value;
            }
            else
            {
                recurrency = keeper.Recurrencies.SingleOrDefault(r => (r.ID.Equals(MoneyDataSet.IDs.Recurrencies.None)));
            }

            if ((dtpEndDate.Enabled) && (dtpEndDate.Checked))
            {
                endDate = dtpEndDate.Value;
            }

            MoneyDataSet.PlannedTransactionsRow preCreateSource =
                keeper.PreCreatePlannedTransaction(template.TransactionTypesRowByFK_TransactionTypes_Source_TransactionTemplates,
                                                   tbTitle.Text, tbDescription.Text, startDate, cbSourceAccountType.SelectedItem as MoneyDataSet.AccountTypesRow,
                                                   (double)numSourceAmount.Value, cbSourceCurrency.SelectedItem as MoneyDataSet.CurrenciesRow,
                                                   recurrency, endDate, cbIsAggregated.Checked, template);

            MoneyDataSet.PlannedTransactionsRow preCreateDestination = null;

            ValidationResult resultSource      = keeper.Validate(plan: preCreateSource);
            ValidationResult resultDestination = new ValidationResult(success: true, preventAction: false, message: String.Empty);

            if (template.HasDestinationAccount)
            {
                preCreateDestination = keeper.PreCreatePlannedTransaction(template.TransactionTypesRowByFK_TransactionTypes_Destination_TransactionTemplates,
                                                                          tbTitle.Text, tbDescription.Text, startDate, cbDestinationAccountType.SelectedItem as MoneyDataSet.AccountTypesRow,
                                                                          (double)numDestinationAmount.Value, cbDestinationCurrency.SelectedItem as MoneyDataSet.CurrenciesRow,
                                                                          recurrency, endDate, cbIsAggregated.Checked, template, preCreateSource.PairReferenceID);

                resultDestination = keeper.Validate(plan: preCreateDestination);
            }

            if (!((resultSource.Success) && (resultDestination.Success)))
            {
                String message = String.Empty;

                if (String.IsNullOrWhiteSpace(resultSource.Message))
                {
                    message = Resources.Labels.TransactionDestinationValidation + resultDestination.Message;
                }
                else if (String.IsNullOrWhiteSpace(resultDestination.Message))
                {
                    if (template.HasDestinationAccount)
                    {
                        message = Resources.Labels.TransactionSourceValidation + Environment.NewLine;
                    }
                    message += resultSource.Message;
                }
                else
                {
                    message = String.Join(Environment.NewLine, new String[] { Resources.Labels.TransactionSourceValidation,
                                                                              resultSource.Message, String.Empty, Resources.Labels.TransactionDestinationValidation,
                                                                              resultDestination.Message });
                }

                if ((resultSource.PreventAction) || (resultDestination.PreventAction))
                {
                    MessageBox.Show(String.Format(Resources.Labels.TransactionValidationErrorsFoundFormat, message),
                                    Resources.Labels.TransactionValidationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }
                else
                {
                    if (MessageBox.Show(String.Format(Resources.Labels.TransactionValidationWarningsFoundFormat, message),
                                        Resources.Labels.TransactionValidationTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }

            if (sourcePlan != null)
            {
                // updating existing plans
                selectedPlan = keeper.UpdatePlannedTransaction(sourcePlan.ID, preCreateSource.Title, preCreateSource.Description, startDate, preCreateSource.AccountTypeRow,
                                                               preCreateSource.Amount, preCreateSource.CurrenciesRow, preCreateSource.RecurrenciesRow, endDate, cbIsAggregated.Checked, ttbTags.Tags, template);

                if (template.HasDestinationAccount)
                {
                    keeper.UpdatePlannedTransaction(destinationPlan.ID, preCreateDestination.Title, preCreateDestination.Description, startDate,
                                                    preCreateDestination.AccountTypeRow, preCreateDestination.Amount, preCreateDestination.CurrenciesRow, preCreateDestination.RecurrenciesRow, endDate,
                                                    cbIsAggregated.Checked, ttbTags.Tags, template);
                }
            }
            else
            {
                // creating new plans
                selectedPlan = keeper.CreatePlannedTransaction(preCreateSource, ttbTags.Tags);
                if (template.HasDestinationAccount)
                {
                    keeper.CreatePlannedTransaction(preCreateDestination, ttbTags.Tags);
                }
            }
            keeper.AddTextHistory(String.Format(Consts.Keeper.TransactionTitleHistoryIDFormat, template.ID), tbTitle.Text);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            tbTitle.Text = tbTitle.Text.Trim();

            MoneyDataSet.AccountsRow sourceAccount = cbSourceAccount.SelectedItem as MoneyDataSet.AccountsRow;
            double sourceAmount = (double)numSourceAmount.Value;

            MoneyDataSet.PlannedTransactionsRow sourcePlan = null;
            if (cbSourceImplementsPlan.Checked)
            {
                sourcePlan = cbSourcePlan.SelectedItem as MoneyDataSet.PlannedTransactionsRow;
            }

            MoneyDataSet.TransactionsRow preCreateSource =
                keeper.PreCreateTransaction(template.TransactionTypesRowByFK_TransactionTypes_Source_TransactionTemplates,
                                            tbTitle.Text, tbDescription.Text, dtpTransactionDate.Value, sourceAccount, sourceAmount,
                                            plan: sourcePlan, template: template);

            MoneyDataSet.TransactionsRow preCreateDestination = null;

            ValidationResult resultSource      = keeper.Validate(transaction: preCreateSource);
            ValidationResult resultDestination = new ValidationResult(success: true, preventAction: false, message: String.Empty);

            if (template.HasDestinationAccount)
            {
                MoneyDataSet.AccountsRow destinationAccount = cbDestinationAccount.SelectedItem as MoneyDataSet.AccountsRow;
                double destinationAmount = (double)numDestinationAmount.Value;

                MoneyDataSet.PlannedTransactionsRow destinationPlan = null;
                if (cbDestinationImplementsPlan.Checked)
                {
                    destinationPlan = cbDestinationPlan.SelectedItem as MoneyDataSet.PlannedTransactionsRow;
                }

                preCreateDestination = keeper.PreCreateTransaction(template.TransactionTypesRowByFK_TransactionTypes_Destination_TransactionTemplates,
                                                                   tbTitle.Text, tbDescription.Text, dtpTransactionDate.Value, destinationAccount, destinationAmount,
                                                                   plan: destinationPlan, template: template, pairReference: preCreateSource.ID);

                resultDestination = keeper.Validate(transaction: preCreateDestination);
            }

            if (!((resultSource.Success) && (resultDestination.Success)))
            {
                StringBuilder message = new StringBuilder();

                if (String.IsNullOrWhiteSpace(resultSource.Message))
                {
                    message.AppendLine(Resources.Labels.TransactionDestinationValidation);
                    message.Append(resultDestination.Message);
                }
                else if (String.IsNullOrWhiteSpace(resultDestination.Message))
                {
                    if (template.HasDestinationAccount)
                    {
                        message.AppendLine(Resources.Labels.TransactionSourceValidation);
                    }
                    message.Append(resultSource.Message);
                }
                else
                {
                    message.Append(String.Join(Environment.NewLine, new String[] { Resources.Labels.TransactionSourceValidation,
                                                                                   resultSource.Message, String.Empty, Resources.Labels.TransactionDestinationValidation,
                                                                                   resultDestination.Message }));
                }

                if ((resultSource.PreventAction) || (resultDestination.PreventAction))
                {
                    MessageBox.Show(String.Format(Resources.Labels.TransactionValidationErrorsFoundFormat, message.ToString()),
                                    Resources.Labels.TransactionValidationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }
                else
                {
                    if (MessageBox.Show(String.Format(Resources.Labels.TransactionValidationWarningsFoundFormat, message.ToString()),
                                        Resources.Labels.TransactionValidationTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }

            transaction = keeper.CreateTransaction(preCreateSource, ttbTags.Tags);
            if (template.HasDestinationAccount)
            {
                keeper.CreateTransaction(preCreateDestination, ttbTags.Tags);
            }

            keeper.AddTextHistory(String.Format(Consts.Keeper.TransactionTitleHistoryIDFormat, template.ID), tbTitle.Text);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }