calc_month_share_yangll.py
19.7 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
from MysqlUtility import MySQL, paddstr, paddnum
import datetime
from dateutil.relativedelta import relativedelta
import logging
from init.base_create_table import BaseCreateTable
from init.StringBuilder import StringBuilder
from init.Utils import *
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'}
"""
# 设置全局参数
base_table = "temp_ft_table"
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, price, device_id):
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
my_fentan_detail_map = {}
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)
# 将分摊明细写入分摊表中
# insertDataToFt(device_id, calcStartTime, calcEndTime, price)
# 建立分摊map 在内存中处理分摊明细
buildFenTanDetailMap(calcStartTime, calcEndTime, price, my_fentan_detail_map)
return gap_month, my_fentan_detail_map
def buildFenTanDetailMap(calcStartTime, calcEndTime, price, my_fentan_detail_map):
calcStartTime = calcStartTime.replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)
for i in range(12):
# 添加一个月的日期
calculate_time = add_one_month(calcStartTime, i)
if calcStartTime <= calculate_time < calcEndTime:
fentan_detail_time = calculate_time.strftime("%Y-%m-%d")
my_fentan_detail_map[fentan_detail_time] = price
return my_fentan_detail_map
def buildApportionment(device_id, calcStartTime, calcEndTime, price):
# 将业务日期开始时间设置为 1号 方便比较
calcStartTime = calcStartTime.replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)
sb = StringBuilder()
for i in range(12):
# 添加一个月的日期
calculate_time = add_one_month(calcStartTime, i)
if calcStartTime <= calculate_time < calcEndTime:
sb.append(f"({device_id},'{calculate_time}',{price}),")
return str(sb)[0 : len(str(sb)) - 1]
def insertDataToFt(device_id, calcStartTime, calcEndTime, price):
"""
calcStartTime 开始分摊时间
calcEndTime 结束分摊时间
price 每月分摊金额
"""
values = buildApportionment(device_id, calcStartTime, calcEndTime, price)
bizConn = MySQL(g_pro_biz_dbcfg)
sql01 = f"""
INSERT INTO {base_table} (device_id,apportionment_date,apportionment_price) values {values};
"""
bizConn.insert(sql01)
def getMonthPriceByYear(my_fentan_detail_map, year):
sb = StringBuilder()
yearStart = datetime.datetime.strptime(f"{year}-01-01", "%Y-%m-%d").date()
yearEnd = datetime.datetime.strptime(f"{year}-12-31", "%Y-%m-%d").date()
d_str = f"{year}-01-01"
first_month = datetime.datetime.strptime(d_str, "%Y-%m-%d").date()
for i in range(12):
# 添加一个月的日期
calculate_time = add_one_month(first_month, i)
if yearStart <= calculate_time < yearEnd:
fentan_detail_time = calculate_time.strftime("%Y-%m-%d")
# print("fentan_detail_time==", fentan_detail_time)
price = my_fentan_detail_map.get(fentan_detail_time, 0)
sb.append(str(price) + ",")
return str(sb)
def calc_remain_year(
year, start_share_month, end_share_month, price, remainTotal, amount, device_id
):
"""
price 每月分摊金额
计算每年余额
:param year:
:return:
"""
gap_months, my_fentan_detail_map = calc_gap_month_of_year(
year, start_share_month, end_share_month, price, device_id
)
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, my_fentan_detail_map
def calc_income_share(calcType="收入"):
"""
计算收入的分摊
:return:
"""
# tblName = "dj_device_details"
tblName = "dj_2021_settle_details"
fileName = calcType + "_01.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 ,id "
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 ,id "
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"客户名称,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入,"
year_list = [2017, 2018, 2019, 2020, 2021, 2022]
for year in year_list:
headName += f"{year}年摊销金额,{year}年末累计摊销余额,"
headName += f"{year}-01月分摊,{year}-02月分摊,{year}-03月分摊,{year}-04月分摊,{year}-05月分摊,{year}-06月分摊,{year}-07月分摊,{year}-08月分摊,{year}-09月分摊,{year}-10月分摊,{year}-11月分摊,{year}-12月分摊,"
headName += f"产品分类,激活开始月份,激活结束月份,是否退货,研发开始分摊月份"
headName += "\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]
device_id = row[9]
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,
my_fentan_detail_map_2017,
) = calc_remain_year(
2017,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2018,
remainShare2018,
remainTotal,
my_fentan_detail_map_2018,
) = calc_remain_year(
2018,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2019,
remainShare2019,
remainTotal,
my_fentan_detail_map_2019,
) = calc_remain_year(
2019,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2020,
remainShare2020,
remainTotal,
my_fentan_detail_map_2020,
) = calc_remain_year(
2020,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2021,
remainShare2021,
remainTotal,
my_fentan_detail_map_2021,
) = calc_remain_year(
2021,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2022,
remainShare2022,
remainTotal,
my_fentan_detail_map_2022,
) = calc_remain_year(
2022,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
lineName = f"{settleMer},{channel},{period},{total},{settle_start_month},{amount},{settle_start_month},{settle_end_month},{price},{share_per_month},"
lineName += f"{share_2017},{remainShare2017},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2017, 2017)
lineName += f"{share_2018},{remainShare2018},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2018, 2018)
lineName += f"{share_2019},{remainShare2019},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2019, 2019)
lineName += f"{share_2020},{remainShare2020},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2020, 2020)
lineName += f"{share_2021},{remainShare2021},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2021, 2021)
lineName += f"{share_2022},{remainShare2022},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2022, 2022)
lineName += f"{devName},{active_start_month},{active_end_month},{returnFlag},{start_share_month}"
lineName += "\n"
fileHandle.write(lineName)
fileHandle.close()
def calc_return_share(calcType):
"""
退货
:return:
"""
tblName = "dj_2021_settle_details_return"
fileName = calcType + "_01.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 ,id "
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 ,id "
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"客户名称,渠道,服务期限,数量,结算月份,不含税收入,开始计费月份,结束计费月份,不含税单价,月摊收入,"
year_list = [2017, 2018, 2019, 2020, 2021, 2022]
for year in year_list:
headName += f"{year}年摊销金额,{year}年末累计摊销余额,"
headName += f"{year}-01月分摊,{year}-02月分摊,{year}-03月分摊,{year}-04月分摊,{year}-05月分摊,{year}-06月分摊,{year}-07月分摊,{year}-08月分摊,{year}-09月分摊,{year}-10月分摊,{year}-11月分摊,{year}-12月分摊,"
headName += f"产品分类"
headName += "\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]
device_id = row[7]
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,
my_fentan_detail_map_2017,
) = calc_remain_year(
2017,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2018,
remainShare2018,
remainTotal,
my_fentan_detail_map_2018,
) = calc_remain_year(
2018,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2019,
remainShare2019,
remainTotal,
my_fentan_detail_map_2019,
) = calc_remain_year(
2019,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2020,
remainShare2020,
remainTotal,
my_fentan_detail_map_2020,
) = calc_remain_year(
2020,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2021,
remainShare2021,
remainTotal,
my_fentan_detail_map_2021,
) = calc_remain_year(
2021,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
(
share_2022,
remainShare2022,
remainTotal,
my_fentan_detail_map_2022,
) = calc_remain_year(
2022,
start_share_month,
end_share_month,
share_per_month,
remainTotal,
amount,
device_id,
)
# 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"
# )
lineName = f"{settleMer},{channel},{period},{total},{settle_start_month},{amount},{settle_start_month},{settle_end_month},{price},{share_per_month},"
lineName += f"{share_2017},{remainShare2017},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2017, 2017)
lineName += f"{share_2018},{remainShare2018},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2018, 2018)
lineName += f"{share_2019},{remainShare2019},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2019, 2019)
lineName += f"{share_2020},{remainShare2020},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2020, 2020)
lineName += f"{share_2021},{remainShare2021},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2021, 2021)
lineName += f"{share_2022},{remainShare2022},"
lineName += getMonthPriceByYear(my_fentan_detail_map_2022, 2022)
lineName += f"{devName}"
lineName += "\n"
fileHandle.write(lineName)
fileHandle.close()
"""
计算分摊数据
"""
if __name__ == "__main__":
# BaseCreateTable(base_table).create_table()
calc_income_share(calcType="收入")
calc_income_share(calcType="成本")
calc_return_share("退货收入")
calc_return_share("退货成本")
print("end===")