/
AddLoan.xaml.cs
162 lines (140 loc) · 6.21 KB
/
AddLoan.xaml.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
using DMSIPayroll.Model;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace DMSIPayroll
{
/// <summary>
/// Interaction logic for AddLoan.xaml
/// </summary>
public partial class AddLoan : MetroWindow
{
public int mode;
public int empid;
public int loanid;
public AddLoan()
{
InitializeComponent();
}
private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
try
{
using (var db = new DMSIPayrollEntities())
{
cbLoanType.ItemsSource = db.LoanTypes.OrderBy(m => m.Description).ToList();
cbLoanType.DisplayMemberPath = "Description";
cbLoanType.SelectedValuePath = "LoanTypeID";
cbPeriod.ItemsSource = db.Periods.OrderBy(m => m.PeriodDescription).ToList();
cbPeriod.DisplayMemberPath = "PeriodDescription";
cbPeriod.SelectedValuePath = "PeriodID";
var emp = db.Employees.Where(m => m.EmployeeID == empid).FirstOrDefault();
tbEmployee.Text = (emp.LastName + ", " + emp.FirstName + " " + emp.MiddleName).ToUpper();
}
}
catch (Exception)
{
MessageBox.Show("Something went wrong.", "System Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
if (mode == 2)
{
this.Title = "UPDATE LOAN";
GetLoan();
}
}
private void GetLoan()
{
try
{
using (var db = new DMSIPayrollEntities())
{
var loan = db.Loans.Where(m => m.LoanID == loanid).FirstOrDefault();
tbAmortization.Text = loan.Amortization.ToString("G29");
tbPrincipalAmount.Text = loan.PrincipalAmount.ToString("G29");
tbTotalLoanAmount.Text = loan.TotalLoanAmount.ToString("G29");
tbTotalPayment.Text = loan.TotalPayment.ToString("G29");
cbLoanType.SelectedValue = loan.LoanTypeID;
cbPeriod.SelectedValue = loan.PeriodID;
dpStartDate.SelectedDate = loan.StDate;
dpToDate.SelectedDate = loan.ToDate;
}
}
catch (Exception)
{
MessageBox.Show("Something went wrong.", "System Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
try
{
using (var db = new DMSIPayrollEntities())
{
if (tbAmortization.Text == "" || tbPrincipalAmount.Text == "" || tbTotalLoanAmount.Text == "" || tbTotalPayment.Text == "" || cbLoanType.SelectedItem == null || cbPeriod.SelectedItem == null || dpStartDate.SelectedDate == null ||dpToDate.SelectedDate == null )
{
MessageBox.Show("Required fields cannot be empty.", "System Warning!", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (mode == 1)
{
Loan loan = new Loan();
loan.EmployeeID = empid;
loan.Amortization = Decimal.Parse(tbAmortization.Text);
loan.PrincipalAmount = Decimal.Parse(tbPrincipalAmount.Text);
loan.TotalLoanAmount = Decimal.Parse(tbTotalLoanAmount.Text);
loan.TotalPayment = Decimal.Parse(tbTotalPayment.Text);
loan.StDate = dpStartDate.SelectedDate.Value;
loan.ToDate = dpToDate.SelectedDate.Value;
loan.LoanTypeID = Convert.ToInt32(cbLoanType.SelectedValue);
loan.PeriodID = Convert.ToInt32(cbPeriod.SelectedValue);
db.Loans.Add(loan);
db.SaveChanges();
MessageBox.Show("Add Succesful", "System Succes!", MessageBoxButton.OK, MessageBoxImage.Information);
clear();
}
else if (mode == 2)
{
var loan = db.Loans.Where(m => m.LoanID == loanid).FirstOrDefault();
loan.EmployeeID = empid;
loan.Amortization = Decimal.Parse(tbAmortization.Text);
loan.PrincipalAmount = Decimal.Parse(tbPrincipalAmount.Text);
loan.TotalLoanAmount = Decimal.Parse(tbTotalLoanAmount.Text);
loan.TotalPayment = Decimal.Parse(tbTotalPayment.Text);
loan.StDate = dpStartDate.SelectedDate.Value;
loan.ToDate = dpToDate.SelectedDate.Value;
loan.LoanTypeID = Convert.ToInt32(cbLoanType.SelectedValue);
loan.PeriodID = Convert.ToInt32(cbPeriod.SelectedValue);
db.SaveChanges();
MessageBox.Show("Update Succesful", "System Succes!", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception)
{
MessageBox.Show("Something went wrong.", "System Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void clear()
{
tbAmortization.Text = "";
tbPrincipalAmount.Text = "";
tbTotalLoanAmount.Text = "";
tbTotalPayment.Text = "";
cbLoanType.Text = "";
cbPeriod.Text = "";
dpStartDate.SelectedDate = null;
dpToDate.SelectedDate = null;
}
}
}