private async void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                bool nameValid = false;
                if (string.IsNullOrEmpty(txtName.Text.ToString()))
                {
                    errorProvider1.SetError(txtName, "Name required");
                }
                else
                {
                    nameValid = true;
                    errorProvider1.SetError(txtName, "");
                }

                if (!nameValid)
                {
                    return;
                }

                EventAndAppointment curentEvent = new EventAndAppointment();

                curentEvent.Name        = txtName.Text;
                curentEvent.Description = txtDescription.Text;
                curentEvent.Date        = dtpEvent.Value;

                RecursiveItem recItem = null;
                if (repeatToggle.Checked)
                {
                    recItem = new RecursiveItem();
                    curentEvent.IsRepeat = true;
                    recItem.Type         = "Event";
                    recItem.Every        = int.Parse(cbRepeatValues.SelectedItem.ToString());
                    recItem.Option       = cbRepeatValuesType.SelectedItem.ToString();
                    if (recItem.Option.Equals("Month"))
                    {
                        recItem.Day = dtpRepeat.Value.Day;
                    }
                }
                else
                {
                    curentEvent.IsRepeat = false;
                }

                EventAndAppointmentModel eventAndAppointmentModel = new EventAndAppointmentModel(db);
                if (editingEventAndAppointment == null)
                {
                    if (repeatToggle.Checked)
                    {
                        RecursiveItemModel recursiveItemModel = new RecursiveItemModel(db);
                        await recursiveItemModel.SaveRecursiveItem(recItem);

                        curentEvent.RecursiveItemId = recItem.Id;
                    }
                    await eventAndAppointmentModel.SaveEventAndAppointment(curentEvent);

                    MessageBox.Show("Successfully created.");
                }
                else
                {
                    curentEvent.Id = editingEventAndAppointment.Id;
                    await eventAndAppointmentModel.UpdateEventAndAppointment(curentEvent);

                    MessageBox.Show("Successfully updated.");
                }

                eventAndAppointment.HandleClose();
                this.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("Failed to add event");
            }
        }
Beispiel #2
0
        private async void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                Transaction transaction     = new Transaction();
                double      amount          = -1;
                int         categoryId      = -1;
                int         contactId       = -1;
                string      transactionType = "Income";
                string      paymentType     = "";
                int         payeeId         = -1;
                string      description     = txtDescription.Text.Trim();
                DateTime    date            = dtpTransaction.Value;
                try
                {
                    amount = double.Parse(txtAmount.Text);
                    errorProvider1.SetError(txtAmount, "");
                }
                catch (Exception ex)
                {
                    errorProvider1.SetError(txtAmount, "Amount should be digits");
                }

                if (cbCategory.SelectedIndex > -1)
                {
                    categoryId = ((Category)cbCategory.SelectedItem).Id;
                    errorProvider1.SetError(cbCategory, "");
                }
                else
                {
                    errorProvider1.SetError(cbCategory, "Please select a category");
                }

                if (amount == -1 || categoryId == -1)
                {
                    lblError.Visible = true;
                    return;
                }
                else
                {
                    lblError.Visible = false;
                }

                if (rbExpense.Checked)
                {
                    transactionType = "Expense";
                }

                if (cbPaymentType.SelectedIndex > -1)
                {
                    paymentType = cbPaymentType.SelectedItem.ToString();
                }

                if (cbContacts.SelectedIndex > -1)
                {
                    contactId = ((ContactsSet)cbContacts.SelectedItem).Id;
                }

                currentTransaction             = new Transaction();
                currentTransaction.Type        = transactionType;
                currentTransaction.PaymentType = cbPaymentType.SelectedItem.ToString();
                currentTransaction.Date        = date;
                currentTransaction.Amount      = amount;
                currentTransaction.Description = description;
                currentTransaction.IsRepeat    = toggleButton.Checked;
                if (categoryId > -1)
                {
                    currentTransaction.CategoryId = categoryId;
                }
                if (contactId > -1)
                {
                    currentTransaction.ContactId = contactId;
                }


                TransactionModel transactionModel = new TransactionModel(db);
                RecursiveItem    recursiveItem    = null;
                if (toggleButton.Checked)
                {
                    recursiveItem        = new RecursiveItem();
                    recursiveItem.Type   = "Transaction";
                    recursiveItem.Every  = int.Parse(cbRepeatValues.SelectedItem.ToString());
                    recursiveItem.Option = cbRepeatValuesType.SelectedItem.ToString();
                    if (cbRepeatValuesType.SelectedItem.ToString().Equals("Month"))
                    {
                        recursiveItem.Day = dpRepeatDate.Value.Day;
                    }
                    else
                    {
                        recursiveItem.Day = null;
                    }
                    currentTransaction.IsRepeat = true;
                }
                else
                {
                    currentTransaction.IsRepeat = false;
                }
                if (currentEditingTransaction == null)
                {
                    if (recursiveItem != null)
                    {
                        RecursiveItemModel recursiveItemModel = new RecursiveItemModel(db);
                        await recursiveItemModel.SaveRecursiveItem(recursiveItem);
                    }

                    if (currentTransaction.IsRepeat)
                    {
                        currentTransaction.RecursiveItemId = recursiveItem.Id;
                    }
                    await transactionModel.SaveTransaction(currentTransaction);

                    MessageBox.Show("Successfully created.");
                }
                else
                {
                    currentTransaction.Id = currentEditingTransaction.Id;
                    if (currentEditingTransaction.RepeatData != null)
                    {
                        currentTransaction.RecursiveItemId = currentEditingTransaction.RepeatData.Id;
                    }

                    await transactionModel.UpdateTransaction(currentTransaction);

                    RecursiveItemModel recursiveItemModel = new RecursiveItemModel(db);
                    if (recursiveItem == null && currentEditingTransaction.RepeatData != null)
                    {
                        var recItem = await recursiveItemModel.GetRecursiveItem(currentEditingTransaction.RepeatData.Id);

                        recItem.IsActive = false;
                        await recursiveItemModel.UpdateRecursiveItem(recItem);
                    }
                    if (recursiveItem != null)
                    {
                        if (!recursiveItem.Option.Equals(currentEditingTransaction.RepeatData.Option) ||
                            recursiveItem.Every != currentEditingTransaction.RepeatData.Every ||
                            recursiveItem.Day != currentEditingTransaction.RepeatData.Day)
                        {
                            recursiveItem.Id = currentEditingTransaction.RepeatData.Id;
                            await recursiveItemModel.UpdateRecursiveItem(recursiveItem);
                        }
                    }
                    MessageBox.Show("Successfully updated.");
                }

                transactionsManagement.HandleFormClose <Transaction>(currentTransaction);
                this.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("Failed to add transaction");
            }
        }