journal_generator.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import pandas as pd
  2. from tkinter import messagebox
  3. class JournalGenerator:
  4. """日记账生成器"""
  5. def __init__(self):
  6. self.param_df = None
  7. self.account_info_df = None
  8. self.shouzhi_class_df = None
  9. def load_parameter_table(self, param_file_path):
  10. """加载参数表"""
  11. try:
  12. self.param_df = pd.read_excel(param_file_path, dtype=str)
  13. account_info_sheet = pd.read_excel(param_file_path, sheet_name="账户信息参数", dtype=str, header=1)
  14. self.account_info_df = account_info_sheet
  15. shouzhi_class_sheet = pd.read_excel(param_file_path, sheet_name="收支分类参数", dtype=str, header=1)
  16. self.shouzhi_class_df = shouzhi_class_sheet
  17. return True
  18. except Exception as e:
  19. messagebox.showerror("错误", f"加载参数表失败:{str(e)}")
  20. return False
  21. def generate_journal_data(self, company_name, bank_name, param_file_path, flow_df):
  22. """生成日记账数据"""
  23. if not self.load_parameter_table(param_file_path):
  24. return None
  25. try:
  26. bank_keyword_mapping = {
  27. "微众银行": "微众",
  28. "中国银行": "中国银行",
  29. "农业银行": "农业银行",
  30. "建设银行": "建设银行"
  31. }
  32. bank_keyword = bank_keyword_mapping.get(bank_name, bank_name)
  33. company_accounts = self.account_info_df[self.account_info_df["公司"] == company_name]
  34. account_info = None
  35. for _, account in company_accounts.iterrows():
  36. if bank_keyword in account["开户行"]:
  37. account_info = company_accounts[company_accounts.index == account.name]
  38. break
  39. if account_info is None or account_info.empty:
  40. messagebox.showerror("错误", f"未找到账户信息:{company_name} - {bank_name}")
  41. return None
  42. account_code = account_info["编号"].values[0]
  43. account_short_name = account_info["简称"].values[0]
  44. company_name_full = account_info["公司"].values[0]
  45. journal_data = []
  46. for _, row in flow_df.iterrows():
  47. transaction_time = row["交易时间"]
  48. opponent_name = str(row["对方户名"]).strip()
  49. summary = str(row["摘要"]).strip()
  50. income = pd.to_numeric(row["收入"], errors="coerce")
  51. expense = pd.to_numeric(row["支出"], errors="coerce")
  52. balance = str(row["余额"]).strip()
  53. if pd.isna(income):
  54. income = 0
  55. if pd.isna(expense):
  56. expense = 0
  57. if income == 0 and expense == 0:
  58. continue
  59. amount = income if income > 0 else expense
  60. shouzhi_type = "收入" if income > 0 else "支出"
  61. try:
  62. transaction_date = pd.to_datetime(transaction_time, errors="coerce")
  63. if pd.isna(transaction_date):
  64. continue
  65. formatted_date = f"{transaction_date.year}/{transaction_date.month}/{transaction_date.day}"
  66. month = str(transaction_date.month)
  67. except:
  68. continue
  69. opponent_bank = str(row.get("对方开户机构", "")).strip()
  70. flow_remark = str(row.get("备注", "")).strip()
  71. is_settlement = False
  72. if opponent_name in self.account_info_df["公司"].values:
  73. is_settlement = True
  74. if is_settlement:
  75. if income > 0:
  76. fund_level1 = "结算收入"
  77. fund_level2 = "结算收入"
  78. fund_level3 = "结算收入"
  79. else:
  80. fund_level1 = "结算支出"
  81. fund_level2 = "结算支出"
  82. fund_level3 = "结算支出"
  83. elif "会长提现" in flow_remark or "提现" in flow_remark:
  84. fund_level1 = "游戏业务"
  85. fund_level2 = "结算支出"
  86. fund_level3 = "会长提现支付"
  87. elif "手续费" in summary or "服务费" in summary:
  88. fund_level1 = "财务费用"
  89. fund_level2 = "财务费用"
  90. fund_level3 = "手续费"
  91. else:
  92. fund_level1 = ""
  93. fund_level2 = ""
  94. fund_level3 = ""
  95. if "手续费" in summary or "服务费" in summary:
  96. opponent_name = "手续费"
  97. remark = "手续费"
  98. elif "会长提现" in flow_remark or "提现" in flow_remark:
  99. remark = opponent_name + 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