/ExpenseTracker/ExpenseTracker/ExpenseForm.cs

# · C# · 332 lines · 296 code · 36 blank · 0 comment · 41 complexity · 608c82a1163a49f8acc401c62502b06d MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10. using ExpenseTracker.Entity;
  11. using ExpenseTracker.Controller;
  12. namespace ExpenseTracker
  13. {
  14. public partial class ExpenseForm : BaseForm
  15. {
  16. public delegate void ExpenseFormSaved();
  17. public event ExpenseFormSaved OnExpenseFormSaved;
  18. private ExpenseTracker.Controller.CategoryController _categoryController;
  19. private ExpenseTracker.Controller.ExpenseController _expenseController;
  20. private ExpenseTracker.Entity.Expense _expense;
  21. public ExpenseForm(ExpenseTracker.Entity.Account account) : base(account)
  22. {
  23. _categoryController = new CategoryController(account);
  24. _expenseController = new ExpenseController(account);
  25. _expense = new Expense();
  26. InitializeComponent();
  27. }
  28. protected override void OnLoad(EventArgs e)
  29. {
  30. PopulateCategoryComboBox();
  31. PopulateForm();
  32. btnDelete.Enabled = (FormAction == FormAction.Create) ? false : true;
  33. base.OnLoad(e);
  34. }
  35. public ExpenseTracker.Entity.Expense Expense
  36. {
  37. get { return _expense; }
  38. set { _expense = value; }
  39. }
  40. protected void PopulateForm()
  41. {
  42. if (FormAction == FormAction.Update && Expense.ExpenseID > 0)
  43. {
  44. Expense = _expenseController.GetExpense(Expense.ExpenseID);
  45. for (int i = 0; i < cbCategory.Items.Count; i++)
  46. {
  47. Category category = cbCategory.Items[i] as Category;
  48. if (category.CategoryID == Expense.CategoryID)
  49. {
  50. cbCategory.SelectedItem = cbCategory.Items[i];
  51. break;
  52. }
  53. }
  54. dtpTransactionDate.Value = Expense.TransactionDate;
  55. txtItem.Text = Expense.Item;
  56. txtDescription.Text = Expense.Description;
  57. SetDefaultNumberValue(txtAmount, Expense.Amount);
  58. SetDefaultNumberValue(txtGST, Expense.GST);
  59. SetDefaultNumberValue(txtPST, Expense.PST);
  60. SetDefaultNumberValue(txtTotal, Expense.Total);
  61. chkIsDisabled.Checked = Expense.IsDisabled;
  62. }
  63. }
  64. protected void PopulateCategoryComboBox()
  65. {
  66. cbCategory.Items.Clear();
  67. List<Category> list = _categoryController.GetCategories(TransactionType.Expense);
  68. foreach (Category item in list)
  69. {
  70. cbCategory.Items.Add(item);
  71. }
  72. cbCategory.DisplayMember = "Title";
  73. cbCategory.ValueMember = "CategoryID";
  74. }
  75. protected bool ValidateForm()
  76. {
  77. bool valid = true;
  78. StringBuilder sb = new StringBuilder();
  79. if (cbCategory.SelectedItem == null)
  80. {
  81. sb.Append("- Select category." + Environment.NewLine);
  82. valid = false;
  83. }
  84. if (String.IsNullOrEmpty(txtItem.Text.Trim()))
  85. {
  86. sb.Append("- Enter item." + Environment.NewLine);
  87. valid = false;
  88. }
  89. SetDefaultNumberValue(txtAmount, 0.0F);
  90. SetDefaultNumberValue(txtGST, 0.0F);
  91. SetDefaultNumberValue(txtPST, 0.0F);
  92. SetDefaultNumberValue(txtTotal, 0.0F);
  93. ValidateNumberValue(txtAmount, "- Enter valid amount.", ref sb, ref valid);
  94. ValidateNumberValue(txtGST, "- Enter valid GST.", ref sb, ref valid);
  95. ValidateNumberValue(txtPST, "- Enter valid PST.", ref sb, ref valid);
  96. ValidateNumberValue(txtTotal, "- Enter valid total.", ref sb, ref valid);
  97. if (!valid)
  98. {
  99. MessageBox.Show("Error:" + Environment.NewLine + sb.ToString());
  100. }
  101. return valid;
  102. }
  103. protected void FormToObject()
  104. {
  105. if (FormAction == FormAction.Create)
  106. {
  107. Expense = new Expense();
  108. Expense.TransactionDate = dtpTransactionDate.Value;
  109. if (cbCategory.SelectedItem != null)
  110. {
  111. Category category = cbCategory.SelectedItem as Category;
  112. Expense.CategoryID = category.CategoryID;
  113. }
  114. Expense.Item = txtItem.Text.Trim();
  115. Expense.Description = txtDescription.Text.Trim();
  116. Expense.Amount = GetNumberValue(txtAmount, 0.0F);
  117. Expense.GST = GetNumberValue(txtGST, 0.0F);
  118. Expense.PST = GetNumberValue(txtPST, 0.0F);
  119. Expense.Total = GetNumberValue(txtTotal, 0.0F);
  120. Expense.IsDisabled = chkIsDisabled.Checked;
  121. }
  122. else if (FormAction == FormAction.Update)
  123. {
  124. Expense.TransactionDate = dtpTransactionDate.Value;
  125. if (cbCategory.SelectedItem != null)
  126. {
  127. Category category = cbCategory.SelectedItem as Category;
  128. Expense.CategoryID = category.CategoryID;
  129. }
  130. Expense.Item = txtItem.Text.Trim();
  131. Expense.Description = txtDescription.Text.Trim();
  132. Expense.Amount = GetNumberValue(txtAmount, 0.0F);
  133. Expense.GST = GetNumberValue(txtGST, 0.0F);
  134. Expense.PST = GetNumberValue(txtPST, 0.0F);
  135. Expense.Total = GetNumberValue(txtTotal, 0.0F);
  136. Expense.IsDisabled = chkIsDisabled.Checked;
  137. }
  138. }
  139. private void btnCancel_Click(object sender, EventArgs e)
  140. {
  141. this.Close();
  142. }
  143. private void btnSave_Click(object sender, EventArgs e)
  144. {
  145. if (ValidateForm())
  146. {
  147. FormToObject();
  148. if (FormAction == FormAction.Create)
  149. {
  150. _expenseController.InsertExpense(Expense);
  151. }
  152. else if (FormAction == FormAction.Update)
  153. {
  154. _expenseController.UpdateExpense(Expense);
  155. }
  156. if (OnExpenseFormSaved != null)
  157. {
  158. OnExpenseFormSaved();
  159. }
  160. this.Close();
  161. }
  162. }
  163. private void btnDelete_Click(object sender, EventArgs e)
  164. {
  165. if (MessageBox.Show("Are you sure to delete this expense?", "Delete Expense", MessageBoxButtons.YesNo) == DialogResult.Yes)
  166. {
  167. _expenseController.DeleteExpense(Expense.ExpenseID);
  168. if (OnExpenseFormSaved != null)
  169. {
  170. OnExpenseFormSaved();
  171. }
  172. this.Close();
  173. }
  174. }
  175. #region Leave event handlers
  176. private void txtAmount_Leave(object sender, EventArgs e)
  177. {
  178. float amount = GetNumberValue(txtAmount, 0.0F);
  179. float gst = amount * Configuration.Instance.GSTRate;
  180. float pst = amount * Configuration.Instance.PSTRate;
  181. float total = amount + gst + pst;
  182. txtGST.Text = gst.ToString("N2");
  183. txtPST.Text = pst.ToString("N2");
  184. txtTotal.Text = total.ToString("N2");
  185. SetDefaultNumberValue(txtAmount, 0.0F);
  186. }
  187. private void txtGST_Leave(object sender, EventArgs e)
  188. {
  189. float amount = GetNumberValue(txtAmount, 0.0F);
  190. float gst = GetNumberValue(txtGST, 0.0F);
  191. float pst = amount * Configuration.Instance.PSTRate;
  192. float total = amount + gst + pst;
  193. txtPST.Text = pst.ToString("N2");
  194. txtTotal.Text = total.ToString("N2");
  195. SetDefaultNumberValue(txtGST, 0.0F);
  196. }
  197. private void txtPST_Leave(object sender, EventArgs e)
  198. {
  199. float amount = GetNumberValue(txtAmount, 0.0F);
  200. float gst = GetNumberValue(txtGST, 0.0F);
  201. float pst = GetNumberValue(txtPST, 0.0F);
  202. float total = amount + gst + pst;
  203. txtTotal.Text = total.ToString("N2");
  204. SetDefaultNumberValue(txtPST, 0.0F);
  205. }
  206. private void txtTotal_Leave(object sender, EventArgs e)
  207. {
  208. SetDefaultNumberValue(txtTotal, 0.0F);
  209. }
  210. #endregion
  211. #region KeyPress event handlers
  212. private void txtAmount_KeyPress(object sender, KeyPressEventArgs e)
  213. {
  214. ValidateNumberValue(sender, e);
  215. TrySubmitForm(sender, e);
  216. }
  217. private void txtGST_KeyPress(object sender, KeyPressEventArgs e)
  218. {
  219. ValidateNumberValue(sender, e);
  220. TrySubmitForm(sender, e);
  221. }
  222. private void txtPST_KeyPress(object sender, KeyPressEventArgs e)
  223. {
  224. ValidateNumberValue(sender, e);
  225. TrySubmitForm(sender, e);
  226. }
  227. private void txtTotal_KeyPress(object sender, KeyPressEventArgs e)
  228. {
  229. ValidateNumberValue(sender, e);
  230. TrySubmitForm(sender, e);
  231. }
  232. private void txtItem_KeyPress(object sender, KeyPressEventArgs e)
  233. {
  234. TrySubmitForm(sender, e);
  235. }
  236. #endregion
  237. #region Number utilties
  238. private void SetDefaultNumberValue(TextBox textbox, float defaultValue)
  239. {
  240. if (String.IsNullOrEmpty(textbox.Text.Trim()))
  241. {
  242. textbox.Text = defaultValue.ToString("N2");
  243. }
  244. }
  245. private float GetNumberValue(TextBox textbox, float defaultValue)
  246. {
  247. float val = defaultValue;
  248. if (!String.IsNullOrEmpty(textbox.Text.Trim()))
  249. {
  250. if (float.TryParse(textbox.Text.Trim(), out val))
  251. {
  252. return val;
  253. }
  254. }
  255. return val;
  256. }
  257. private void ValidateNumberValue(object sender, KeyPressEventArgs e)
  258. {
  259. Match match = Regex.Match(e.KeyChar.ToString(), @"\d|\.");
  260. if (!match.Success)
  261. {
  262. e.Handled = true;
  263. }
  264. else
  265. {
  266. TextBox textbox = (TextBox)sender;
  267. string text = textbox.Text.Trim() + e.KeyChar.ToString();
  268. if (!String.IsNullOrEmpty(text))
  269. {
  270. float val;
  271. if (!float.TryParse(text, out val))
  272. {
  273. e.Handled = true;
  274. }
  275. }
  276. }
  277. }
  278. private void TrySubmitForm(object sender, KeyPressEventArgs e)
  279. {
  280. if (e.KeyChar == Convert.ToChar(Keys.Enter))
  281. {
  282. btnSave_Click(this, null);
  283. }
  284. }
  285. private void ValidateNumberValue(TextBox textbox, string message, ref StringBuilder sb, ref bool valid)
  286. {
  287. float val;
  288. if (!float.TryParse(textbox.Text.Trim(), out val))
  289. {
  290. sb.Append(message + Environment.NewLine);
  291. valid = false;
  292. }
  293. }
  294. #endregion
  295. }
  296. }