This repository has been archived by the owner on May 13, 2018. It is now read-only.
/
Salary.py
164 lines (135 loc) · 6.02 KB
/
Salary.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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import traceback
from Employee import *
from Position import *
from Table import *
class Salary:
monthes = {
1 : "January",
2 : "February",
3 : "March",
4 : "April",
5 : "May",
6 : "June",
7 : "July",
8 : "August",
9 : "September",
10 : "October",
11 : "November",
12 : "December"
}
def __init__(self):
self.employees_info = Employee().get_all_db_data()
self.positions_info = Position().get_all_db_data()
self.tables_info = Table().get_all_db_data()
def calculate_salary_for_all(self):
try:
employee_wages = {}
for position in self.positions_info:
employee_wages[position["name"]] = position["salaryPerHour"]
employees = {}
for employee in self.employees_info:
employees[employee["id"]] = employee
salaries = []
# підраховуємо заробітні плати та формуємо відповідні структури даних
for table in self.tables_info:
year = table["year"]
month = self.monthes[table["month"]]
employee_id = table["employeeId"]
current_employee = employees[employee_id]
employee_name = current_employee["name"]
position = current_employee["post"]
wage_per_hour = employee_wages[position]
employee_rate = current_employee["rate"]
daily_hours = table["hours"].split(" ")
sum_of_hours = 0
amount_of_simple_days = 0
amount_of_vacation_days = 0
amount_of_hospital_days = 0
for day_hours in daily_hours:
if day_hours.isnumeric():
try:
sum_of_hours += int(day_hours)
amount_of_simple_days += 1
except Exception:
traceback.format_exc()
if day_hours == "v":
amount_of_vacation_days += 1
if day_hours == "h":
amount_of_hospital_days += 1
sum = 0.0
# зарплата за звичайний день
sum += sum_of_hours * wage_per_hour
if sum_of_hours > employee_rate:
sum_of_hours = employee_rate
# зарплата за відпустку
sum += amount_of_vacation_days * (employee_rate / len(daily_hours)) * (wage_per_hour * 0.8)
# зарплата за лікарняний
sum += amount_of_hospital_days * (sum_of_hours / amount_of_simple_days) * wage_per_hour
compiled_salary = (year, month, employee_name, position, str(sum) + " $")
salaries.append(compiled_salary)
return tuple(salaries)
except Exception as e:
traceback.format_exc()
print("Trouble with calculate_salary_for_all: " + e.args[0])
def represent_as_date_dictionary(self, raw_tuple):
"""
:param raw_tuple: tuple з "сирими" данними про зарплати робітників
:return: date_dictionary: дані у форматі словника [year][month][employee, position,salary]
"""
try:
date_dictionary = {}
# переписуємо дані отримані з функції calculate_salary_for_all у відповідній формі, вказаній вище
for item in raw_tuple:
year = item[0]
date_dictionary[year] = {}
for item in raw_tuple:
month = item[1]
year = item[0]
date_dictionary[year][month] = []
for item in raw_tuple:
year = item[0]
month = item[1]
date_dictionary[year][month]\
.append({"employee" : item[2], "position" : item[3], "salary" : item[4]})
return date_dictionary
except Exception as e:
print("Troubles with represent_as_employee_dictionary: " + e.args[0])
traceback.format_exc()
def represent_as_employee_dictionary(self, raw_tuple):
"""
:param raw_tuple: tuple з "сирими" данними про зарплати робітників
:return: employee_dictionary: дані у форматі словника [employee][year][month][salary, position]
"""
try:
employees_info = {}
for employee in self.employees_info:
employees_info[employee["id"]] = employee
employee_dictionary = {}
# переписуємо дані отримані з функції calculate_salary_for_all у відповідній формі, вказаній вище
for item in raw_tuple:
employee = item[2]
employee_dictionary[employee] = {}
for item in raw_tuple:
employee = item[2]
year = item[0]
employee_dictionary[employee][year] = {}
for item in raw_tuple:
employee = item[2]
year = item[0]
month = item[1]
employee_dictionary[employee][year][month] = []
for item in raw_tuple:
employee = item[2]
year = item[0]
month = item[1]
position = item[3]
sum = item[4]
employee_dictionary[employee][year][month]\
.append({"position" : position, "sum" : sum})
print(employee_dictionary.keys())
return employee_dictionary
except Exception as e:
traceback.format_exc()
print("Troubles with represent_as_employee_dictionary: " + e.args[0])