-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (131 loc) · 5.7 KB
/
main.py
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
169
170
171
172
173
174
175
176
177
178
179
from flask import Flask
from flask import render_template, request, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from resources.emp_payroll import Payroll
# import the configs
from config.configs import DevelopmentConfig, ProductionConfig, Config
app = Flask(__name__)
app.config.from_object(DevelopmentConfig)
# instantiate SQLAlchemy
db = SQLAlchemy(app)
# import the models
from models.department import DepartmentModel
from models.employee import EmployeeModel
@app.before_first_request
def create_tables():
db.create_all()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/employees', methods=['GET','POST'])
def employees():
if request.method == 'POST':
name = request.form['full_name']
gender = request.form['gender']
email = request.form['email']
phone = request.form['phone']
dpt_id = request.form['department']
id_number = request.form['id_number']
kra_pin = request.form['kra_pin']
basic_salary = request.form['basic_salary']
benefits = request.form['benefits']
if EmployeeModel.id_number_exists(id_number):
# print("Already Exists")
flash('The record already exists','danger')
return redirect(url_for('employees'))
else:
emp = EmployeeModel(fullName=name, gender=gender, email=email, phoneNumber=phone,
department_id=dpt_id, idNumber=id_number, kraPin=kra_pin,
basicSalary=basic_salary, benefits=benefits)
emp.create_record()
# print("Success")
flash('Employee successfully added', 'success')
return redirect(url_for('employees'))
all_emps = EmployeeModel.fetch_all_emps()
all_depts = DepartmentModel.fetch_all_depts()
return render_template('employees.html', emps=all_emps, dpts=all_depts)
@app.route('/departments', methods=['GET', 'POST'])
def departments():
all_depts = DepartmentModel.fetch_all_depts()
if request.method == 'POST':
name = request.form['department']
print(name)
if DepartmentModel.check_dept_exists(name):
flash('Department already exists', 'danger')
else:
record = DepartmentModel(name=name)
record.create()
flash('Department successfully added', 'success')
return redirect(url_for('departments'))
return render_template('departments.html', dpts=all_depts)
# page to display all employees in a particular department
@app.route('/departments/employees/<int:id>', methods=['GET', 'POST'])
def department_employees(id):
dept_emps = DepartmentModel.fetch_by_id(id)
emp = dept_emps.employee # fetch employees in a particular department fetched by id
return render_template('dept_emps.html', wafanyikazi=emp, dep=dept_emps)
@app.route('/employee/edit/<int:id>', methods=['POST'])
def edit_employees(id):
if request.method == 'POST':
name = request.form['full_name']
gender = request.form['gender']
email = request.form['email']
phone = request.form['phone']
dpt_id = request.form['department']
id_number = request.form['id_number']
kra_pin = request.form['kra_pin']
basic_salary = request.form['basic_salary']
benefits = request.form['benefits']
update = EmployeeModel.edit_employee_by_id(id=id, fullName=name, gender=gender, email=email, phoneNumber=phone,
idNumber=id_number, kraPin=kra_pin, basicSalary=basic_salary,
benefits=benefits,department_id=dpt_id)
if update:
print('update successful')
return redirect(url_for('employees'))
else:
print('record not found')
return redirect(url_for('employees'))
@app.route('/employee/delete/<int:id>', methods=['POST'])
def delete_employees(id):
delete = EmployeeModel.delete_employee_by_id(id)
if delete:
print('deleted successfully')
return redirect(url_for('employees'))
else:
print('unable to delete record at this time')
return redirect(url_for('employees'))
@app.route('/department/edit/<int:id>', methods=['POST'])
def edit_department(id):
if request.method == 'POST':
name = request.form['department']
update = DepartmentModel.edit_department_by_id(id=id, name=name)
if update:
flash('Update successful', 'success')
return redirect(url_for('departments'))
else:
flash('Unable to edit the department', 'danger')
return redirect(url_for('departments'))
@app.route('/generate_payroll/<int:id>', methods=['POST'])
def generate_payroll(id):
an_employee = EmployeeModel.fetch_emp_by_id(id)
if request.method == 'POST':
month = request.form['month']
basic = float(request.form['basic'])
benefit = float(request.form['benefit'])
pay = Payroll(basic_salary=basic, benefits=benefit)
gross = pay.calculate_gross_salary()
print('GROSS SALARY:', gross)
nssf = pay.calculate_nssf()
print('NSSF CONTRIBUTION:', nssf)
# the income after pension deduction = grossSalary - nssfContribution
taxable_income = pay.calculate_taxable_income()
print('Income after pension deduction(taxable income', taxable_income)
# tax on the taxable income
paye = pay.calculate_payee()
print('PAYE(Tax on taxable income):', paye)
personal_relief = pay.personal_relief
print('Personal relief:', personal_relief)
# some missing code here...... to be continued
return 'Payroll generated'
if __name__ == '__main__':
app.run(debug=True)