calc_month_share.py
14.1 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from MysqlUtility import MySQL, paddstr, paddnum
import datetime
from dateutil.relativedelta import relativedelta
import logging
g_pro_biz_dbcfg = {'host': '192.168.5.2', 'port': 3306, 'user': 'biz_glsx_data', 'passwd': 'unEKczDc',
'db': 'glsx_biz_data', 'charset': 'utf8'}
'''
g_pro_biz_dbcfg = {'host': '192.168.5.25', 'port': 3310, 'user': 'biz_device_detail', 'passwd': 'M6khz3CQ',
'db': 'device_detail',
'charset': 'utf8'}
'''
def get_months(begin, end):
begin_year, end_year = begin.year, end.year
begin_month, end_month = begin.month, end.month
if begin_year == end_year:
months = end_month - begin_month
else:
months = (end_year - begin_year) * 12 + end_month - begin_month
return months + 1
def calc_gap_month_of_year(year, pk_start, pk_end):
yearStart = datetime.datetime.strptime(f"{year}-01-01", '%Y-%m-%d')
yearEnd = datetime.datetime.strptime(f"{year}-12-31", '%Y-%m-%d')
gap_month = 0
if pk_start < yearStart < pk_end:
calcStartTime = yearStart
elif yearStart <= pk_start <= yearEnd:
calcStartTime = pk_start
else:
calcStartTime = None
if pk_end < yearStart:
calcEndTime = None
elif yearStart <= pk_end <= yearEnd:
calcEndTime = pk_end
else:
calcEndTime = yearEnd
if calcStartTime is not None and calcEndTime is not None:
gap_month = get_months(calcStartTime, calcEndTime)
return gap_month
def calc_remain_year(year, start_share_month, end_share_month, price, remainTotal, amount):
"""
计算每年余额
:param year:
:return:
"""
gap_months = calc_gap_month_of_year(year, start_share_month, end_share_month)
if gap_months > 0:
share = gap_months * price #
remainShare = remainTotal - share #
else:
share = 0
remainShare = 0
if remainShare > 1.0:
remainTotal = remainShare
else:
remainTotal = amount
remainShare = 0
return share, remainShare, remainTotal
def calc_income_share(calcType='收入'):
"""
计算收入的分摊
:return:
"""
tblName = "dj_device_details"
tblName = 'dj_2021_settle_details'
fileName = calcType + ".csv"
fileHandle = open(file=fileName, mode='w', encoding="utf-8")
bizConn = MySQL(g_pro_biz_dbcfg)
if calcType == '收入':
# and settle_merchant='重庆卡达能汽车用品有限公司'
sql = f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end," \
f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month," \
f"DATE_FORMAT(active_time,'%Y-%m') as active_month,COUNT(1) as count," \
f"sum(IFNULL(pk_price,0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0)) as amount, channel," \
f"case when `return`=1 then '退货' else '正常' end " \
f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 " \
f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),DATE_FORMAT(active_time,'%Y-%m'),channel,`return` " \
f"order by settle_merchant,settle_month"
elif calcType == '成本':
sql = f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end," \
f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month," \
f"DATE_FORMAT(active_time,'%Y-%m') as active_month,COUNT(1) as count," \
f"sum(IFNULL(hw_cost,0) + IFNULL(install_cost, 0) ) as amount, channel, " \
f"case when `return`=1 then '退货' else '正常' end " \
f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 " \
f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),DATE_FORMAT(active_time,'%Y-%m'),channel,`return` " \
f"order by settle_merchant,settle_month"
if not bizConn.query(sql):
return
result = bizConn.fetchAllRows()
# datetime_2017_end = datetime.datetime.strptime("2017-12-31", '%Y-%m-%d')
headName = f"客户名称,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入,"\
f"2017年摊销金额,2017年末累计摊销余额,2018年摊销金额,"\
f"2018年末累计摊销余额,2019年摊销金额,2019年末累计摊销余额,2020年摊销金额,2020年末累计摊销余额,2021年摊销金额,"\
f"2021年末累计摊销余额,2022年摊销金额,2022年末累计摊销余额,产品分类,激活开始月份,激活结束月份,是否退货,研发开始分摊月份 \n"
fileHandle.write(headName)
for row in result:
settleMer = row[0]
devName = row[1]
period = row[2]
if period == 0 or period is None:
period = 12
settle_start_month = row[3]
settle_end_month = datetime.datetime.strptime(settle_start_month, '%Y-%m') \
+ relativedelta(months=period) - relativedelta(days=1)
active_month = row[4]
total = row[5]
amount = row[6]
channel = row[7]
returnFlag = row[8]
if active_month is None:
start_share = '2025-12'
active_start_month = ''
active_end_month = ''
else:
start_share = active_month
active_start_month = datetime.datetime.strptime(start_share, '%Y-%m')
active_end_month = active_start_month + relativedelta(months=period) - relativedelta(days=1)
# 代销从结算时间开始
if (channel == '代销' or channel == '广汇') and (active_start_month == '' or active_start_month < settle_end_month):
start_share = settle_start_month
#
elif (channel == '直销' and settle_start_month >= '2022-01') and (active_start_month == '' or active_start_month < settle_end_month):
start_share = settle_start_month
start_share_month = datetime.datetime.strptime(start_share, '%Y-%m')
end_share_month = start_share_month + relativedelta(months=period) - relativedelta(days=1)
price = amount / total
share_per_month = amount / period #share_per_month: 每年每月摊销多少
'''
gap_2018_months = get_months(start_share_month, datetime_2017_end)
if gap_2018_months > 0:
remainShare2018 = amount - gap_2018_months * share_per_month #2018年末摊销余额
else:
remainShare2018 = 0
if remainShare2018 > 0:
remainTotal = remainShare2018
else:
remainTotal = amount
'''
remainTotal = amount
share_2017, remainShare2017, remainTotal = calc_remain_year(2017, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2018, remainShare2018, remainTotal = calc_remain_year(2018, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2019, remainShare2019, remainTotal = calc_remain_year(2019, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2020, remainShare2020, remainTotal = calc_remain_year(2020, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2021, remainShare2021, remainTotal = calc_remain_year(2021, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2022, remainShare2022, remainTotal = calc_remain_year(2022, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
lineName = f"{settleMer},{channel},{period},{total},{settle_start_month},{amount},{settle_start_month},"\
f"{settle_end_month},{price},{share_per_month},{share_2017},{remainShare2017},{share_2018},"\
f"{remainShare2018},{share_2019},{remainShare2019},{share_2020},{remainShare2020},"\
f"{share_2021},{remainShare2021},{share_2022},{remainShare2022},{devName},"\
f"{active_start_month},{active_end_month},{returnFlag},{start_share_month}\n"
fileHandle.write(lineName)
fileHandle.close()
def calc_return_share(calcType):
"""
退货
:return:
"""
tblName = 'dj_2021_settle_details_return'
fileName = calcType + ".csv"
fileHandle = open(file=fileName, mode='w', encoding="utf-8")
bizConn = MySQL(g_pro_biz_dbcfg)
if calcType == '退货成本':
sql = f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end," \
f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month,COUNT(1) as count," \
f"sum(IFNULL(hw_cost,0) + IFNULL(install_cost, 0) ) as amount, channel " \
f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 " \
f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),channel " \
f"order by settle_merchant,settle_month"
elif calcType == '退货收入':
sql = f"select settle_merchant,case when device_type=8 then '追踪器' when device_type=6 then '记录仪' when device_type=12501 then '车充' end," \
f"pk_period,DATE_FORMAT(settle_time, '%Y-%m') as settle_month,COUNT(1) as count," \
f"sum(IFNULL(pk_price, 0) + IFNULL(hw_price, 0) + IFNULL(install_price, 0)) as amount, channel " \
f"from {tblName} where settle_time < '2023-01-01 00:00:00' and device_type = 8 " \
f"GROUP BY settle_merchant,device_type,pk_period,DATE_FORMAT(settle_time, '%Y-%m'),channel " \
f"order by settle_merchant,settle_month"
if not bizConn.query(sql):
return
result = bizConn.fetchAllRows()
headName = f"客户名称,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入,"\
f"2017年摊销金额,2017年末累计摊销余额,2018年摊销金额,"\
f"2018年末累计摊销余额,2019年摊销金额,2019年末累计摊销余额,2020年摊销金额,2020年末累计摊销余额,2021年摊销金额,"\
f"2021年末累计摊销余额,2022年摊销金额,2022年末累计摊销余额,产品分类 \n"
fileHandle.write(headName)
for row in result:
settleMer = row[0]
devName = row[1]
period = row[2]
if period == 0 or period is None:
period = 12
settle_start_month = row[3]
settle_end_month = datetime.datetime.strptime(settle_start_month, '%Y-%m') \
+ relativedelta(months=period) - relativedelta(days=1)
total = row[4]
amount = row[5]
channel = row[6]
start_share_month = datetime.datetime.strptime(settle_start_month, '%Y-%m')
end_share_month = settle_end_month
price = amount / total
share_per_month = amount / period
remainTotal = amount
share_2017, remainShare2017, remainTotal = calc_remain_year(2017, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2018, remainShare2018, remainTotal = calc_remain_year(2018, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2019, remainShare2019, remainTotal = calc_remain_year(2019, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2020, remainShare2020, remainTotal = calc_remain_year(2020, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2021, remainShare2021, remainTotal = calc_remain_year(2021, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
share_2022, remainShare2022, remainTotal = calc_remain_year(2022, start_share_month,
end_share_month, share_per_month, remainTotal,
amount)
lineName = f"{settleMer},{channel},{period},{total},{settle_start_month},{amount},{settle_start_month},"\
f"{settle_end_month},{price},{share_per_month},{share_2017},{remainShare2017},{share_2018},"\
f"{remainShare2018},{share_2019},{remainShare2019},{share_2020},{remainShare2020},"\
f"{share_2021},{remainShare2021},{share_2022},{remainShare2022},{devName}\n"
fileHandle.write(lineName)
fileHandle.close()
'''
计算分摊数据
'''
if __name__ == "__main__":
calc_income_share(calcType='收入')
calc_income_share(calcType='成本')
calc_return_share('退货收入')
calc_return_share('退货成本')