edun_amortization.py
7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import streamlit as st
import pandas as pd
import datetime
import os
import sys
import traceback
# 添加当前目录到 sys.path 以便导入同级模块
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.append(current_dir)
import audit_calculation
import audit_deduction
import calc_month_share
def show():
st.title("E盾-分摊全流程")
# 年份选择
current_year = datetime.datetime.now().year
selected_year = st.number_input("选择年份", min_value=2000, max_value=2100, value=current_year, step=1)
st.write("---")
st.header("1.导出对外明细数据")
st.info(f"""
**功能说明**:
该功能将选定年份的内部审计数据(续费、设备、明细、应收单)同步至存储全部年份的数据表。
⚠️ **注意**:执行时会自动清除目标表中该年份的旧数据,请谨慎操作。
**涉及表信息**:
- **源表 (Source)**:
- `d_eshield_device_details_{{selected_year}}` (当年设备明细)
- `d_eshield_service_details_{{selected_year}}` (当年服务明细)
- **目标表 (Target)**:
- `d_eshield_renewal` (续费记录表)
- `t_eshield_device` (设备基础表)
- `t_eshield_device_detail` (设备明细整合表)
- `t_eshield_receivables` (应收单表)
""")
# 初始化 session_state
if 'confirm_export' not in st.session_state:
st.session_state.confirm_export = False
# 导出按钮
if st.button(f"并入 {selected_year} 年数据对外明细数据"):
st.session_state.confirm_export = True
if st.session_state.confirm_export:
st.warning(f"⚠️ 请确认:您即将导出 {selected_year} 年的数据。此操作将**删除**该年份已存在的旧数据并重新生成。")
col1, col2 = st.columns([1, 1])
with col1:
if st.button("确认执行", use_container_width=True):
st.session_state.confirm_export = False # 重置状态
st.info(f"开始处理 {selected_year} 年数据 (Batch: {selected_year})...")
try:
# 调用处理逻辑
# batch 默认为年份字符串
batch = str(selected_year)
# 显示加载状态
with st.spinner('正在执行数据清理和生成,请稍候...'):
results = audit_calculation.process_audit_calculation(selected_year, batch=batch)
st.success(f"{selected_year} 年数据处理完成!")
st.subheader("处理结果明细:")
st.write("以下数据已从源表插入到目标表(操作前已自动清理旧批次数据):")
# 使用 DataFrame 展示结果
if results:
df_results = pd.DataFrame(results)
st.table(df_results)
else:
st.info("没有返回处理结果")
except Exception as e:
st.error(f"处理过程中发生错误: {e}")
st.code(traceback.format_exc())
with col2:
if st.button("取消", use_container_width=True):
st.session_state.confirm_export = False
st.info("已取消操作")
st.write("---")
st.header("2.正向推导设备和服务明细数据")
st.info(f"""
**功能说明**:
该功能执行审计抵扣逻辑,正向推导设备和服务明细数据,生成 `t_eshield_device_detail_{selected_year}_output` 和 `t_eshield_service_detail_{selected_year}_output` 表。
1.后续审计会重点核对这两张表是否和之前提供的明细数据一致。
**涉及表信息**:
- **源表 (Source)**:
- `t_eshield_device`
- `t_eshield_device_detail`
- `t_eshield_receivables`
- `d_eshield_renewal`
- **目标表 (Target)**:
- `t_eshield_device_detail_{selected_year}_output` (设备抵扣结果表)
- `t_eshield_service_detail_{selected_year}_output` (服务抵扣结果表)
""")
if st.button(f"开始推导 {selected_year} 年设备和服务明细数据"):
try:
with st.spinner(f'正在执行 {selected_year} 年审计抵扣推导...'):
audit_deduction.process_audit_deduction(selected_year)
st.success(f"{selected_year} 年审计抵扣推导完成!")
except Exception as e:
st.error(f"处理出错: {e}")
st.code(traceback.format_exc())
st.write("---")
st.header("3.生成分摊数据")
st.info(f"""
**功能说明**:
该功能根据已有的明细数据,计算并生成最终的分摊报表(CSV格式)。
**涉及表信息**:
- **源表 (Source)**:
- `d_eshield_device_details_{{year}}` (涵盖 2015 ~ 当前年份的所有表)
- `d_eshield_service_details_{{year}}` (涵盖 2015 ~ 当前年份的所有表)
- **输出 (Output)**:
- CSV 文件 (例如:`门店_追踪器_按月分摊_{selected_year}年收入.csv`)
""")
if st.button(f"生成 {selected_year} 年分摊数据"):
try:
with st.spinner(f'正在生成 {selected_year} 年分摊数据...'):
# 1. 获取时间跨度描述
date_str = calc_month_share.get_settle_time_span(selected_year)
# 2. 调用生成逻辑
file_name = calc_month_share.calc_income_share('收入', date_str)
st.success(f"{selected_year} 年分摊数据生成完成!")
if file_name:
abs_path = os.path.abspath(file_name)
base_name = os.path.basename(abs_path)
st.write(f"生成的文件: **{base_name}**")
st.write(f"文件保存路径: {abs_path}")
if os.path.exists(abs_path):
with open(abs_path, "rb") as f:
st.download_button(
label="下载分摊报表",
data=f,
file_name=base_name,
mime="text/csv"
)
except Exception as e:
st.error(f"生成出错: {e}")
st.code(traceback.format_exc())
st.write("---")
st.header("4.同步数据到对外数据库表")
st.info(f"""
**功能说明**:
该功能将生成的内部表同步到对外数据库(glsx_data),供外部系统或报表查询使用。
同步表包括:
- 基础表:d_eshield_renewal, t_eshield_device, t_eshield_device_detail, t_eshield_receivables
- 年份表:d_eshield_device_details_{selected_year}, d_eshield_service_details_{selected_year}
**涉及表信息**:
- **源表 (Source)**:
- `d_eshield_renewal`
- `t_eshield_device`
- `t_eshield_device_detail`
- `t_eshield_receivables`
- `d_eshield_device_details_{selected_year}`
- `d_eshield_service_details_{selected_year}`
- **目标表 (Target)**:
- 对外数据库 (`glsx_data`) 中的同名表
""")
if st.button(f"同步 {selected_year} 年数据到对外数据库"):
try:
with st.spinner(f'正在同步 {selected_year} 年数据到对外数据库...'):
audit_calculation.sync_to_external_db(year=selected_year)
st.success(f"{selected_year} 年数据同步完成!")
except Exception as e:
st.error(f"同步出错: {e}")
st.code(traceback.format_exc())