journal_generator.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import pandas as pd
  2. from tkinter import messagebox
  3. from fund_classification_rules import get_classification_rules
  4. class JournalGenerator:
  5. """日记账生成器"""
  6. def __init__(self):
  7. self.param_df = None
  8. self.account_info_df = None
  9. self.shouzhi_class_df = None
  10. self.classification_rules = get_classification_rules()
  11. def load_parameter_table(self, param_file_path):
  12. """加载参数表"""
  13. try:
  14. self.param_df = pd.read_excel(param_file_path, dtype=str)
  15. account_info_sheet = pd.read_excel(param_file_path, sheet_name="账户信息参数", dtype=str, header=1)
  16. self.account_info_df = account_info_sheet
  17. shouzhi_class_sheet = pd.read_excel(param_file_path, sheet_name="收支分类参数", dtype=str, header=1)
  18. self.shouzhi_class_df = shouzhi_class_sheet
  19. return True
  20. except Exception as e:
  21. messagebox.showerror("错误", f"加载参数表失败:{str(e)}")
  22. return False
  23. def generate_journal_data(self, company_name, bank_name, param_file_path, flow_df):
  24. """生成日记账数据"""
  25. if not self.load_parameter_table(param_file_path):
  26. return None
  27. try:
  28. bank_keyword_mapping = {
  29. "微众银行": "微众",
  30. "中国银行": "中国银行",
  31. "农业银行": "农业银行",
  32. "建设银行": "建设银行"
  33. }
  34. bank_keyword = bank_keyword_mapping.get(bank_name, bank_name)
  35. company_accounts = self.account_info_df[self.account_info_df["公司"] == company_name]
  36. account_info = None
  37. for _, account in company_accounts.iterrows():
  38. if bank_keyword in account["开户行"]:
  39. account_info = company_accounts[company_accounts.index == account.name]
  40. break
  41. if account_info is None or account_info.empty:
  42. messagebox.showerror("错误", f"未找到账户信息:{company_name} - {bank_name}")
  43. return None
  44. account_code = account_info["编号"].values[0]
  45. account_short_name = account_info["简称"].values[0]
  46. company_name_full = account_info["公司"].values[0]
  47. journal_data = []
  48. for _, row in flow_df.iterrows():
  49. transaction_time = row["交易时间"]
  50. opponent_name = str(row["对方户名"]).strip()
  51. summary = str(row["摘要"]).strip()
  52. income = pd.to_numeric(row["收入"], errors="coerce")
  53. expense = pd.to_numeric(row["支出"], errors="coerce")
  54. balance = str(row["余额"]).strip()
  55. if pd.isna(income):
  56. income = 0
  57. if pd.isna(expense):
  58. expense = 0
  59. if income == 0 and expense == 0:
  60. continue
  61. amount = income if income > 0 else expense
  62. shouzhi_type = "收入" if income > 0 else "支出"
  63. try:
  64. transaction_date = pd.to_datetime(transaction_time, errors="coerce")
  65. if pd.isna(transaction_date):
  66. continue
  67. formatted_date = f"{transaction_date.year}/{transaction_date.month}/{transaction_date.day}"
  68. month = str(transaction_date.month)
  69. except:
  70. continue
  71. opponent_bank = str(row.get("对方开户机构", "")).strip()
  72. flow_remark = str(row.get("备注", "")).strip()
  73. is_settlement = False
  74. if opponent_name in self.account_info_df["公司"].values:
  75. is_settlement = True
  76. context = {
  77. "is_settlement": is_settlement,
  78. "income": income,
  79. "opponent_name": opponent_name,
  80. "summary": summary,
  81. "flow_remark": flow_remark
  82. }
  83. fund_level1 = ""
  84. fund_level2 = ""
  85. fund_level3 = ""
  86. for rule in self.classification_rules:
  87. if rule.match(context):
  88. result = rule.apply(context)
  89. fund_level1 = result["level1"]
  90. fund_level2 = result["level2"]
  91. fund_level3 = result["level3"]
  92. break
  93. if "手续费" in summary or "服务费" in summary:
  94. opponent_name = "手续费"
  95. remark = "手续费"
  96. elif "会长提现" in flow_remark or "提现" in flow_remark:
  97. remark = opponent_name + flow_remark
  98. elif flow_remark is not None and flow_remark.strip() != "" and flow_remark != "nan":
  99. remark = flow_remark
  100. else:
  101. remark = opponent_name + opponent_bank
  102. journal_row = {
  103. "编号": account_code,
  104. "简称": account_short_name,
  105. "企业": company_name_full,
  106. "日期": formatted_date,
  107. "月份": month,
  108. "收支": shouzhi_type,
  109. "资金分类-1级": fund_level1,
  110. "资金分类-2级": fund_level2,
  111. "资金分类-3级": fund_level3,
  112. "对手户": opponent_name,
  113. "备注": remark,
  114. "发生额": amount,
  115. "余额": balance,
  116. "备注.1": ""
  117. }
  118. journal_data.append(journal_row)
  119. if not journal_data:
  120. messagebox.showwarning("警告", "未生成任何日记账数据")
  121. return None
  122. journal_df = pd.DataFrame(journal_data)
  123. column_order = ["编号", "简称", "企业", "日期", "月份", "收支", "资金分类-1级",
  124. "资金分类-2级", "资金分类-3级", "对手户", "备注", "发生额", "余额", "备注.1"]
  125. journal_df = journal_df[column_order]
  126. return journal_df
  127. except Exception as e:
  128. messagebox.showerror("错误", f"生成日记账数据失败:{str(e)}")
  129. return None
  130. def save_journal(self, journal_df, save_path):
  131. """保存日记账"""
  132. try:
  133. journal_df.to_excel(save_path, index=False)
  134. return True
  135. except Exception as e:
  136. messagebox.showerror("错误", f"保存日记账失败:{str(e)}")
  137. return False