journal_generator.py 7.0 KB

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