time_recorder_format_main.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. from fast_excel_to_bill.config_for_custom_bills import OUTPUT_CUSTOMER, OUTPUT_MONTH, OUTPUT_YEAR
  2. from tool_lib.read_table import Read_Table
  3. from tool_lib.create_excel_tables import CreateTable
  4. import numpy as np
  5. def add_sum_to_dict(work_time_dict, income_per_hour):
  6. for key in work_time_dict.keys():
  7. work_time_dict[key]['Betrag'] = int(work_time_dict[key]['Arbeitszeit']*income_per_hour)
  8. return work_time_dict
  9. def save_overtime_in_excel(overtime_dict, overtime_table_path):
  10. # Try to read existing Table
  11. try:
  12. read_table_object = Read_Table(overtime_table_path)
  13. table_object = read_table_object.table_to_dict()
  14. overtime_dict['Überstunden'] += table_object['Überstunden']
  15. overtime_dict['KW'] += table_object['KW']
  16. except:
  17. pass
  18. table_tuple = []
  19. xlsx = CreateTable(overtime_dict,
  20. path=overtime_table_path)
  21. writer = xlsx.create_writer()
  22. xlsx.export_to_excel(writer)
  23. # Add to Table
  24. def round_worktime_for_week(work_time_dict,overtime_table_path, round_time = 300):
  25. overtime_dict ={'Überstunden':[], 'KW': []}
  26. for key in work_time_dict.keys():
  27. overtime = work_time_dict[key]['Arbeitszeit'] % round_time # default round to 5 hours
  28. work_time_dict[key]['Arbeitszeit'] = (work_time_dict[key]['Arbeitszeit']-overtime)/ 60
  29. overtime_dict['Überstunden'] += [overtime]
  30. overtime_dict['KW'] += [key]
  31. save_overtime_in_excel(overtime_dict,overtime_table_path)
  32. return work_time_dict
  33. def get_task_out_of_tree(work_time_dict):
  34. for key in work_time_dict.keys():
  35. work_time_dict[key]['Task'] = ['- ' + last_branch[last_branch.rfind('/')+1:] for last_branch in work_time_dict[key]['Task']]
  36. return work_time_dict
  37. def compress_data_by_key_list(*compressable_lists, key_list):
  38. '''
  39. function: a list containing different Values, if a Value is more than one time
  40. in the list. Then this list an all list of the same size are compressed, for the values of the
  41. list_of_same_size they are put in tuple.
  42. '''
  43. if len(compressable_lists) > 1:
  44. for list_ in compressable_lists:
  45. past_values = []
  46. pos_bias = 0
  47. for pos, value in enumerate(key_list):
  48. if value in past_values:
  49. pos = pos - pos_bias
  50. if isinstance(list_[pos - pos_bias], int):
  51. list_[pos - 1] += list_[pos]
  52. elif isinstance(list_[pos - pos_bias], str):
  53. list_[pos - 1] = [list_[pos - 1], list_[pos]]
  54. elif isinstance(list_[pos - pos_bias], list):
  55. list_[pos - 1] = list_[pos - 1].append(list_[pos])
  56. list_.remove(list_[pos])
  57. pos_bias += 1
  58. else:
  59. past_values += [value]
  60. else:
  61. compressable_lists = compressable_lists[0]
  62. if len(compressable_lists) > len(key_list):
  63. compressable_lists = compressable_lists[:len(key_list)]
  64. elif len(compressable_lists) < len(key_list):
  65. assert (False, 'Compressable list length should be greater equal then the length of the key_list')
  66. past_values = []
  67. pos_bias = 0
  68. for pos, value in enumerate(key_list):
  69. if value in past_values:
  70. pos = pos - pos_bias
  71. if isinstance(compressable_lists[pos - 1], int):
  72. compressable_lists[pos - 1] += compressable_lists[pos]
  73. elif isinstance(compressable_lists[pos - 1], str):
  74. compressable_lists[pos - 1] = [compressable_lists[pos - 1], compressable_lists[pos]]
  75. elif isinstance(compressable_lists[pos - 1], list):
  76. compressable_lists[pos - 1] += [compressable_lists[pos]]
  77. compressable_lists.remove(compressable_lists[pos])
  78. pos_bias += 1
  79. else:
  80. past_values += [value]
  81. if isinstance(compressable_lists[pos - pos_bias], str):
  82. compressable_lists[pos-pos_bias] = [compressable_lists[pos-pos_bias]]
  83. return compressable_lists
  84. def main_data_collection(pathes, list_of_keys=['Arbeitszeit', 'Task']):
  85. week_keys_to_list_of_keys_dict = {}
  86. if isinstance(pathes, list):
  87. for c, path in enumerate(pathes):
  88. # Read Data
  89. read_table_object = Read_Table(path)
  90. table_object = read_table_object.table_to_dict()
  91. # Filter Data
  92. customer_filter_value_indices = []
  93. for task_value_idx in range(len(table_object['Task'])):
  94. task_value = table_object['Task'][task_value_idx]
  95. if task_value.startswith('root/' + OUTPUT_CUSTOMER):
  96. customer_filter_value_indices.append(task_value_idx)
  97. if OUTPUT_MONTH > 9:
  98. date_filter = str(OUTPUT_MONTH) + '/' + str(OUTPUT_YEAR)
  99. else:
  100. date_filter = '0' + str(OUTPUT_MONTH) + '/' + str(OUTPUT_YEAR)
  101. date_filter_value_indices = []
  102. for date_value_idx in range(len(table_object['Datum'])):
  103. date_value = table_object['Datum'][date_value_idx]
  104. if date_value.endswith(date_filter):
  105. date_filter_value_indices.append(date_value_idx)
  106. customer_and_date_filtered_indices = list(set(customer_filter_value_indices) & set(date_filter_value_indices))
  107. customer_and_date_filtered_indices.sort()
  108. for key in table_object:
  109. table_object[key] = [table_object[key][idx] for idx in customer_and_date_filtered_indices]
  110. # Calendar Week
  111. calendar_weeks_list = table_object['Kalenderwoche']
  112. sortet_calendar_weeks = list(set(calendar_weeks_list))
  113. sortet_calendar_weeks.sort()
  114. # Add new Weeks to dict
  115. for week in sortet_calendar_weeks:
  116. if week in week_keys_to_list_of_keys_dict:
  117. pass
  118. else:
  119. week_keys_to_list_of_keys_dict[week] = {}
  120. # Get variable key dict with values of table
  121. key_dict = {}
  122. for key in list_of_keys:
  123. key_dict[key] = table_object[key]
  124. # Compress Data by Calendar week
  125. for key in key_dict.keys():
  126. all_weeks = compress_data_by_key_list(key_dict[key], key_list=calendar_weeks_list)
  127. if key == 'Task':
  128. for idx in range(len(all_weeks)):
  129. all_weeks[idx] = list(set(all_weeks[idx]))
  130. for week_indx, week in enumerate(sortet_calendar_weeks):
  131. if c == 0:
  132. week_keys_to_list_of_keys_dict[week][key] = all_weeks[week_indx]
  133. else:
  134. if isinstance(all_weeks[week_indx], int):
  135. week_keys_to_list_of_keys_dict[week][key] += all_weeks[week_indx]
  136. elif isinstance(all_weeks[week_indx], list):
  137. week_keys_to_list_of_keys_dict[week][key] += all_weeks[week_indx] # Change to Append if needed
  138. else:
  139. # Read Data
  140. read_table_object = Read_Table(pathes)
  141. table_object = read_table_object.table_to_dict()
  142. # Calendar Week
  143. calendar_weeks_list = table_object['Kalenderwoche']
  144. sortet_calendar_weeks = list(set(calendar_weeks_list))
  145. sortet_calendar_weeks.sort()
  146. # Filter Data
  147. customer_filter_value_indices = []
  148. for task_value_idx in range(len(table_object['Task'])):
  149. task_value = table_object['Task'][task_value_idx]
  150. if task_value.startswith('root/' + OUTPUT_CUSTOMER):
  151. customer_filter_value_indices.append(task_value_idx)
  152. if OUTPUT_MONTH > 9:
  153. date_filter = str(OUTPUT_MONTH) + '/' + str(OUTPUT_YEAR)
  154. else:
  155. date_filter = '0' + str(OUTPUT_MONTH) + '/' + str(OUTPUT_YEAR)
  156. date_filter_value_indices = []
  157. for date_value_idx in range(len(table_object['Datum'])):
  158. date_value = table_object['Datum'][date_value_idx]
  159. if date_value.endswith(date_filter):
  160. date_filter_value_indices.append(date_value_idx)
  161. customer_and_date_filtered_indices = list(set(customer_filter_value_indices) & set(date_filter_value_indices))
  162. for key in table_object:
  163. table_object[key] = [table_object[key][idx] for idx in customer_and_date_filtered_indices]
  164. # Add new Weeks to dict
  165. for week in sortet_calendar_weeks:
  166. if week in week_keys_to_list_of_keys_dict:
  167. pass
  168. else:
  169. week_keys_to_list_of_keys_dict[week] = {}
  170. # Get variable key dict with values of table
  171. key_dict = {}
  172. for key in list_of_keys:
  173. key_dict[key] = table_object[key]
  174. # Compress Data by Calendar week
  175. compressed_lists = []
  176. for key in key_dict.keys():
  177. all_weeks = compress_data_by_key_list(key_dict[key], key_list=calendar_weeks_list)
  178. if key == 'Task':
  179. for idx in range(len(all_weeks)):
  180. all_weeks[idx] = list(set(all_weeks[idx]))
  181. for week_indx, week in enumerate(sortet_calendar_weeks):
  182. week_keys_to_list_of_keys_dict[week][key] = all_weeks[week_indx]
  183. return week_keys_to_list_of_keys_dict
  184. if __name__ == '__main__':
  185. table_object = main_data_collection([TABLE_PATH, TABLE_PATH])