main.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. DOCX_OUTPUT_PATH = config_for_custom_bills.docx_output_dir_path / 'bill_{}.docx'.format(CURRENT_MONTH_YEAR)
  12. def get_excel_data():
  13. # Get Data which is necessary for the docx
  14. bill_data_dict = main_data_collection(config_for_custom_bills.employee_worktime_table_pathes, config_for_custom_bills.LIST_OF_KEYS)
  15. # transform data to format
  16. bill_data_dict = round_worktime_for_week(bill_data_dict, config_for_custom_bills.SAVE_OVERTIME_IN_TABLE,
  17. config_for_custom_bills.ROUND_TIME)
  18. bill_data_dict = add_sum_to_dict(bill_data_dict, config_for_custom_bills.income_per_hour)
  19. bill_data_dict = get_task_out_of_tree(bill_data_dict)
  20. return bill_data_dict
  21. def set_current_dates(builder_obj):
  22. current_date = datetime_functions.datetime_to_str(datetime_functions.get_current_date(), '%d.%m.%Y')
  23. builder_obj.replace_dates_string_in_standard_text(current_date)
  24. builder_obj.replace_dates_string_in_all_tables(current_date)
  25. return builder_obj
  26. def set_Leistungszeitraum(builder_obj, month: int, year: int):
  27. if not 0 < month <= 12:
  28. last_month = datetime_functions.get_current_month_number() - 1
  29. else:
  30. last_month = month
  31. if year < 0:
  32. year = CURRENT_YEAR
  33. month_range = datetime_functions.get_count_of_days_for_specific_month(year=year, month_number=last_month)
  34. if last_month > 9:
  35. start_date = '01.' + str(last_month) + '.' + str(year)
  36. else:
  37. start_date = '01.' + '0' + str(last_month) + '.' + str(year)
  38. end_date = datetime_functions.add_days_to_date(datetime.strptime(start_date, '%d.%m.%Y'), month_range - 1)
  39. end_date = datetime_functions.datetime_to_str(end_date, '%d.%m.%Y')
  40. builder_obj.replace_Leistungszeitraum(start_date, end_date)
  41. return builder_obj
  42. def set_rechnungsnummer():
  43. pass
  44. def fill_table(builder_obj, key_list, data_list):
  45. table = builder_obj.search_table_with_key_columns(key_list)
  46. builder_obj.replace_rows(table, data_list)
  47. builder_obj.delete_empty_rows_in_table(table, config_for_custom_bills.NUMBER_OF_COLUMNS_FIRST_TABLE)
  48. return builder_obj
  49. def main():
  50. # Get Data
  51. bill_data_dict = get_excel_data()
  52. # Create a doc file which is based on a docx template
  53. builder_obj = DOCFileBuilder(config_for_custom_bills.DOCX_INPUT_PATH, DOCX_OUTPUT_PATH)
  54. # Set Date of Template
  55. builder_obj = set_current_dates(builder_obj)
  56. # Set Leistungszeitraum
  57. builder_obj = set_Leistungszeitraum(builder_obj, OUTPUT_MONTH, OUTPUT_YEAR)
  58. # Table KW/Time/ sum
  59. data_list = []
  60. for key in bill_data_dict.keys():
  61. data_list += [(key, bill_data_dict[key][config_for_custom_bills.LIST_OF_KEYS[0]], bill_data_dict[key]['Betrag'])]
  62. builder_obj = fill_table(builder_obj, config_for_custom_bills.FIRST_TABLE_KEYS, data_list)
  63. # Table KW and Task
  64. data_list = []
  65. for key in bill_data_dict.keys():
  66. data_list += [(key, bill_data_dict[key][config_for_custom_bills.LIST_OF_KEYS[1]])]
  67. builder_obj = fill_table(builder_obj, config_for_custom_bills.SECOND_TABLE_KEYS, data_list)
  68. # Replace Placeholders
  69. netto = sum([bill_data_dict[key]['Betrag'] for key in bill_data_dict.keys()])
  70. tax = round(netto * 0.19, 2)
  71. abs_amount = netto + tax
  72. payment_request_date = datetime_functions.add_days_to_date(datetime_functions.get_current_date(), PAYMENT_REQUEST_RANGE)
  73. payment_request_date = datetime_functions.datetime_to_str(payment_request_date, '%d.%m.%Y')
  74. place_holder_replacement_list = [netto, tax, abs_amount, payment_request_date]
  75. place_holders = config_for_custom_bills.list_of_place_holders
  76. for count, place_holder in enumerate(place_holders):
  77. builder_obj.replace_custom_placeholder_in_doc(place_holder, str(place_holder_replacement_list[count]))
  78. builder_obj.save_docx()
  79. if __name__ == '__main__':
  80. main()