示例#1
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();
        }