main.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 fast_excel_to_bill.config_for_custom_bills import OUTPUT_MONTH, OUTPUT_YEAR
  4. from tool_lib import datetime_functions
  5. from datetime import datetime
  6. from fast_excel_to_bill.transform_excel_data_to_bill_data.time_recorder_format_main import main_data_collection, \
  7. round_worktime_for_week, add_sum_to_dict, get_task_out_of_tree
  8. PAYMENT_REQUEST_RANGE = config_for_custom_bills.payment_request_range
  9. CURRENT_YEAR = int(datetime_functions.datetime_to_str(datetime_functions.get_current_date(), '%Y'))
  10. CURRENT_MONTH_YEAR = datetime_functions.datetime_to_str('current', '%m_%Y')
  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 format
  15. bill_data_dict = round_worktime_for_week(bill_data_dict, config_for_custom_bills.SAVE_OVERTIME_IN_TABLE,
  16. config_for_custom_bills.ROUND_TIME)
  17. bill_data_dict = add_sum_to_dict(bill_data_dict, config_for_custom_bills.income_per_hour)
  18. bill_data_dict = get_task_out_of_tree(bill_data_dict)
  19. return bill_data_dict
  20. def set_current_dates(builder_obj):
  21. current_date = datetime_functions.datetime_to_str(datetime_functions.get_current_date(), '%d.%m.%Y')
  22. builder_obj.replace_dates_string_in_standard_text(current_date)
  23. builder_obj.replace_dates_string_in_all_tables(current_date)
  24. return builder_obj
  25. def set_Leistungszeitraum(builder_obj, month: int, year: int):
  26. if year < 0:
  27. year = CURRENT_YEAR
  28. month_range = datetime_functions.get_count_of_days_for_specific_month(year=year, month_number=month)
  29. if month > 9:
  30. start_date = '01.' + str(month) + '.' + str(year)
  31. else:
  32. start_date = '01.' + '0' + str(month) + '.' + str(year)
  33. end_date = datetime_functions.add_days_to_date(datetime.strptime(start_date, '%d.%m.%Y'), month_range - 1)
  34. end_date = datetime_functions.datetime_to_str(end_date, '%d.%m.%Y')
  35. builder_obj.replace_Leistungszeitraum(start_date, end_date)
  36. return builder_obj
  37. def set_Rechnungsnummer(builder_obj, month, year):
  38. month_str = str(month)
  39. if month <= 9:
  40. month_str = '0' + month_str
  41. builder_obj.replace_Rechnungsnummer(month_str, str(year))
  42. return builder_obj
  43. def get_selected_month():
  44. if not 0 < OUTPUT_MONTH <= 12:
  45. return datetime_functions.get_current_month_number() - 1
  46. else:
  47. return OUTPUT_MONTH
  48. def get_selected_year():
  49. if 0 > OUTPUT_YEAR:
  50. return CURRENT_YEAR
  51. else:
  52. return OUTPUT_YEAR
  53. def month_to_string(month: int):
  54. if month <= 9:
  55. return '0' + str(month)
  56. else:
  57. return str(month)
  58. def set_rechnungsnummer():
  59. pass
  60. def fill_table(builder_obj, key_list, data_list):
  61. table = builder_obj.search_table_with_key_columns(key_list)
  62. builder_obj.replace_rows(table, data_list)
  63. builder_obj.delete_empty_rows_in_table(table, config_for_custom_bills.NUMBER_OF_COLUMNS_FIRST_TABLE)
  64. return builder_obj
  65. def main():
  66. # Get Data
  67. bill_data_dict = get_excel_data()
  68. docx_output_path = config_for_custom_bills.docx_output_dir_path / 'bill_{}.docx'.format(
  69. month_to_string(get_selected_month()) + '_' + str(get_selected_year()))
  70. # Create a doc file which is based on a docx template
  71. builder_obj = DOCFileBuilder(config_for_custom_bills.DOCX_INPUT_PATH, docx_output_path)
  72. # Set Date of Template
  73. builder_obj = set_current_dates(builder_obj)
  74. # Set Leistungszeitraum
  75. builder_obj = set_Leistungszeitraum(builder_obj, get_selected_month(), get_selected_year())
  76. # Set Rechnungsnummer
  77. builder_obj = set_Rechnungsnummer(builder_obj, get_selected_month(), get_selected_year())
  78. # Table KW/Time/ sum
  79. data_list = []
  80. for key in bill_data_dict.keys():
  81. data_list += [(key, bill_data_dict[key][config_for_custom_bills.LIST_OF_KEYS[0]], bill_data_dict[key]['Betrag'])]
  82. builder_obj = fill_table(builder_obj, config_for_custom_bills.FIRST_TABLE_KEYS, data_list)
  83. # Table KW and Task
  84. data_list = []
  85. for key in bill_data_dict.keys():
  86. data_list += [(key, bill_data_dict[key][config_for_custom_bills.LIST_OF_KEYS[1]])]
  87. builder_obj = fill_table(builder_obj, config_for_custom_bills.SECOND_TABLE_KEYS, data_list)
  88. # Replace Placeholders
  89. netto = sum([bill_data_dict[key]['Betrag'] for key in bill_data_dict.keys()])
  90. tax = round(netto * 0.19, 2)
  91. abs_amount = netto + tax
  92. payment_request_date = datetime_functions.add_days_to_date(datetime_functions.get_current_date(), PAYMENT_REQUEST_RANGE)
  93. payment_request_date = datetime_functions.datetime_to_str(payment_request_date, '%d.%m.%Y')
  94. place_holder_replacement_list = [netto, tax, abs_amount, payment_request_date]
  95. place_holders = config_for_custom_bills.list_of_place_holders
  96. for count, place_holder in enumerate(place_holders):
  97. builder_obj.replace_custom_placeholder_in_doc(place_holder, str(place_holder_replacement_list[count]))
  98. builder_obj.save_docx()
  99. if __name__ == '__main__':
  100. main()