-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormCreateTransaction.cs
168 lines (126 loc) · 3.72 KB
/
FormCreateTransaction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Linq;
using System.Windows.Forms;
using Trip_Budget_Tracker.Models.Services;
using Trip_Expense_Tracker;
using Trip_Expense_Tracker.Models.Dto;
using Trip_Expense_Tracker.Models.EFModels;
using Trip_Expense_Tracker.Models.Interfaces;
using Trip_Expense_Tracker.Models.Repositories;
using Trip_Expense_Tracker.Models.Services;
namespace Trip_Transaction_Tracker
{
public partial class FormCreateTransaction : Form,IDataContainer
{
private object dataGridView1;
public FormCreateTransaction()
{
InitializeComponent();
this.Load += FormCreateTransation_Load1;
this.buttonSave.Click += ButtonSave_Click;
InitializeComboBoxData();
}
private void InitializeComboBoxData()
{
var budgets = new BudgetService(new BudgetEFRepo());
var budgetList = budgets.Search("")
.ToList();
budgetList.Insert(0, new BudgetDto{ Budgno = null, Name = "" });
comboBox2.DataSource = budgetList;
comboBox2.DisplayMember = "ComboBoxDisplay";
comboBox2.ValueMember = "Budgno";
var expenses= new ExpenseService(new ExpenseEFRepo());
var expenseList = expenses.Search("")
.ToList();
expenseList.Insert(0, new ExpenseDto{Expeno = null, Name="" });
comboBox1.DataSource = expenseList;
comboBox1.DisplayMember = "ComboboxDisplay";
comboBox1.ValueMember = "Expeno";
}
private ITransactionRepository GetRepo()
{
return new TransactionEFRepo();
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(comboBox1.Text) && !string.IsNullOrWhiteSpace(comboBox2.Text))
{
MessageBox.Show("必須選擇一個消費項目或預算項目!");
return; // Don't proceed with saving if the validation fails
}
TransactionDto dto = new TransactionDto()
{
Amount = int.TryParse(textBox1.Text, out int value) ? value : 0,
ExpenseName = comboBox1.Text,
BudgetName = comboBox2.Text,
Remarks = textBox3.Text
};
if (DateTime.TryParse(textBox2.Text, out DateTime dateValue))
{
dto.Date = dateValue;
}
else
{
// Handle the case where parsing the date fails, e.g., provide a default value or show an error message.
// For now, I'll set it to DateTime.MinValue.
dto.Date = DateTime.Now;
}
dto.Expeno = MapExpenseNameToExpeno(comboBox1.Text);
dto.Budgno = MapBudgetNameToBudgno(comboBox2.Text);
int newData = new TransactionService(GetRepo()).Create(dto);
MessageBox.Show($"交易項目已更新!");
var container = this.Owner as IDataContainer;
if (container != null)
{
container.Display();
}
else
{
//MessageBox.Show("Owner 表單沒有實作IDateContainerur介面");
}
var container1 = this.Owner as IAmountContainer;
if (container1 != null)
{
container1.Amount();
}
this.DialogResult = DialogResult.OK;
}
private int? MapBudgetNameToBudgno(string text)
{
string[] parts = comboBox2.Text.Split(' ');
{
if (parts.Length >= 2 && int.TryParse(parts[0], out int selectedBudget))
{
return selectedBudget;
}
return null;
}
}
private int? MapExpenseNameToExpeno(string text)
{
string[] parts = comboBox1.Text.Split(' ');
{
if (parts.Length >= 2 && int.TryParse(parts[0], out int selectedExpeno))
{
return selectedExpeno;
}
return null;
}
}
private void FormCreateTransation_Load1(object sender, EventArgs e)
{
this.Text = "新增交易項目";
this.FormBorderStyle = FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
InitializeComboBoxData();
}
public void Display()
{
throw new NotImplementedException();
}
public void Balance(decimal totalAmount)
{
throw new NotImplementedException();
}
}
}