コード例 #1
0
        private void SaveTransaction()
        {
            if (ValidateEntry())
            {
                Model.Transaction transaction = new Model.Transaction();

                transaction.Date       = dtpDate.Value;
                transaction.Type       = transactionType.ToString();
                transaction.CategoryId = new Guid(lblCategoryId.Text);
                transaction.AccountId  = new Guid(lblAccountId.Text);
                transaction.Amount     = decimal.Parse(txtAmount.Text.Replace(".", string.Empty));
                transaction.Notes      = txtNotes.Text;

                string errMsg = string.Empty;
                try
                {
                    if (formMode == FormMode.AddNew)
                    {
                        errMsg = "Gagal menyimpan transaksi!";

                        if (transactionType == TransactionType.Income.ToString())
                        {
                            transaction.Description = "Pendapatan dari " + cboCategory.Text + " disimpan di " + cboAccount.Text;
                        }
                        else if (transactionType == TransactionType.Expense.ToString())
                        {
                            transaction.Description = "Pengeluaran untuk " + cboCategory.Text + " dibayar dengan " + cboAccount.Text;
                        }

                        if (transactionRepository.IsTransactionExist(dtpDate.Value, this.transactionType,
                                                                     new Guid(lblCategoryId.Text), new Guid(lblAccountId.Text)))
                        {
                            MessageBox.Show("Transaksi sudah ada", "Perhatian", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            transactionRepository.Save(transaction);

                            if (this.isSaveAndNew)
                            {
                                ClearForm();
                            }
                            else
                            {
                                this.Close();
                            }
                        }
                    }
                    else if (formMode == FormMode.Edit)
                    {
                        errMsg         = "Gagal mengubah transaksi!";
                        transaction.ID = new Guid(lblID.Text);
                        transactionRepository.Update(transaction);

                        this.Close();
                    }

                    frmMain.LoadTransactionByType(frmMain.CboFilterText);
                    frmMain.DisableEditDelete();
                    frmMain.DrawChart(frmMain.CboChart);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(errMsg, "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }