main.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from fast_excel_to_bill.bill_doc.docx_builder import DOCFileBuilder
  2. from fast_excel_to_bill import config_for_custom_bills
  3. from tool_lib import datetime_functions
  4. from datetime import datetime
  5. from fast_excel_to_bill.transform_excel_data_to_bill_data.time_recorder_format_main import main_data_collection, \
  6. round_worktime_for_week, add_sum_to_dict, get_task_out_of_tree
  7. payment_request_range = config_for_custom_bills.payment_request_range
  8. year = int(datetime_functions.datetime_to_str(datetime_functions.get_current_date(), '%Y'))
  9. current_month_year = datetime_functions.datetime_to_str('current', '%m_%Y')
  10. docx_output_path = config_for_custom_bills.docx_output_dir_path + r'\bill_{}.docx'.format(current_month_year)
  11. def get_excel_data():
  12. # Get Data which is necessary for the docx
  13. bill_data_dict = main_data_collection(config_for_custom_bills.employee_worktime_table_pathes, config_for_custom_bills.LIST_OF_KEYS)
  14. # transform data to fomat
  15. bill_data_dict, over_time = round_worktime_for_week(bill_data_dict, config_for_custom_bills.ROUND_TIME)
  16. bill_data_dict = add_sum_to_dict(bill_data_dict, config_for_custom_bills.income_per_hour)
  17. bill_data_dict = get_task_out_of_tree(bill_data_dict)
  18. return bill_data_dict
  19. def set_current_dates(builder_obj):
  20. current_date = datetime_functions.datetime_to_str(datetime_functions.get_current_date(), '%d.%m.%Y')
  21. builder_obj.replace_dates_string_in_standard_text(current_date)
  22. builder_obj.replace_dates_string_in_all_tables(current_date)
  23. return builder_obj
  24. def set_Leistungszeitraum(builder_obj):
  25. last_month = datetime_functions.get_current_month_number() - 1
  26. month_range = datetime_functions.get_count_of_days_for_specific_month(year=year, month_number=last_month)
  27. if last_month > 9:
  28. start_date = '01.' + str(last_month) + '.' + str(year)
  29. else:
  30. start_date = '01.' + '0' + str(last_month) + '.' + str(year)
  31. end_date = datetime_functions.add_days_to_date(datetime.strptime(start_date, '%d.%m.%Y'), month_range - 1)
  32. end_date = datetime_functions.datetime_to_str(end_date, '%d.%m.%Y')
  33. builder_obj.replace_Leistungszeitraum(start_date, end_date)
  34. return builder_obj
  35. def fill_table(builder_obj, key_list, data_list):
  36. table = builder_obj.search_table_with_key_columns(key_list)
  37. builder_obj.replace_rows(table, data_list)
  38. builder_obj.delete_empty_rows_in_table(table, config_for_custom_bills.NUMBER_OF_COLUMNS_FIRST_TABLE)
  39. return builder_obj
  40. def main():
  41. # Get Data
  42. bill_data_dict = get_excel_data()
  43. # Create a doc file which is based on a docx template
  44. builder_obj = DOCFileBuilder(config_for_custom_bills.DOCX_INPUT_PATH, docx_output_path)
  45. # Set Date of Template
  46. builder_obj = set_current_dates(builder_obj)
  47. # Set Leistungszeitraum
  48. builder_obj = set_Leistungszeitraum(builder_obj)
  49. # Table KW/Time/ sum
  50. data_list = []
  51. for key in bill_data_dict.keys():
  52. data_list += [(key, bill_data_dict[key][config_for_custom_bills.LIST_OF_KEYS[0]], bill_data_dict[key]['Betrag'])]
  53. builder_obj = fill_table(builder_obj, config_for_custom_bills.FIRST_TABLE_KEYS, data_list)
  54. # Table KW and Task
  55. data_list = []
  56. for key in bill_data_dict.keys():
  57. data_list += [(key, bill_data_dict[key][config_for_custom_bills.LIST_OF_KEYS[1]])]
  58. builder_obj = fill_table(builder_obj, config_for_custom_bills.SECOND_TABLE_KEYS, data_list)
  59. # Replace Placeholders
  60. netto = sum([bill_data_dict[key]['Betrag'] for key in bill_data_dict.keys()])
  61. tax = round(netto * 0.19, 2)
  62. abs_amount = netto + tax
  63. payment_request_date = datetime_functions.add_days_to_date(datetime_functions.get_current_date(), payment_request_range)
  64. payment_request_date = datetime_functions.datetime_to_str(payment_request_date, '%d.%m.%Y')
  65. place_holder_replacement_list = [netto, tax, abs_amount, payment_request_date]
  66. place_holders = config_for_custom_bills.list_of_place_holders
  67. for count, place_holder in enumerate(place_holders):
  68. builder_obj.replace_custom_placeholder_in_doc(place_holder, str(place_holder_replacement_list[count]))
  69. builder_obj.save_docx()
  70. if __name__ == '__main__':
  71. main()