Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a69ef2993 | ||
|
|
04b6035d52 |
247
excel_writer.py
247
excel_writer.py
@@ -341,11 +341,10 @@ def write_salesperson_sheet(wb, person, dept, settle_month, D, G, month_data, ve
|
|||||||
|
|
||||||
def write_vehicle_tracking_sheet(wb, settle_month, G, master_vehicles, vehicle_payments, vehicle_info):
|
def write_vehicle_tracking_sheet(wb, settle_month, G, master_vehicles, vehicle_payments, vehicle_info):
|
||||||
ws = wb.create_sheet('车辆考核追踪')
|
ws = wb.create_sheet('车辆考核追踪')
|
||||||
from calc_engine import VEHICLE_TARGET_MAP
|
from calc_engine import VEHICLE_TARGET_MAP, DAYS
|
||||||
|
|
||||||
wrap_top = Alignment(wrap_text=True, vertical='top')
|
center_top = Alignment(horizontal='center', vertical='top', wrap_text=True)
|
||||||
center_top = Alignment(horizontal='center', vertical='top')
|
left_top = Alignment(vertical='top', wrap_text=True)
|
||||||
# 颜色
|
|
||||||
green_font = Font(color='006100')
|
green_font = Font(color='006100')
|
||||||
green_fill = PatternFill(start_color='C6EFCE', end_color='C6EFCE', fill_type='solid')
|
green_fill = PatternFill(start_color='C6EFCE', end_color='C6EFCE', fill_type='solid')
|
||||||
red_font = Font(color='9C0006')
|
red_font = Font(color='9C0006')
|
||||||
@@ -354,26 +353,22 @@ def write_vehicle_tracking_sheet(wb, settle_month, G, master_vehicles, vehicle_p
|
|||||||
blue_fill = PatternFill(start_color='DAEEF3', end_color='DAEEF3', fill_type='solid')
|
blue_fill = PatternFill(start_color='DAEEF3', end_color='DAEEF3', fill_type='solid')
|
||||||
gold_fill = PatternFill(start_color='FFF2CC', end_color='FFF2CC', fill_type='solid')
|
gold_fill = PatternFill(start_color='FFF2CC', end_color='FFF2CC', fill_type='solid')
|
||||||
|
|
||||||
# 表头
|
# 表头:基本信息 + 业务员 + 每月(应考核/实际/达标) + 累计 + 发放 + 奖金池
|
||||||
headers = ['车牌号','车架号','归属公司','车型','考核目标','月度奖励']
|
headers = ['车牌号','车架号','归属公司','车型','考核目标','月度奖励','业务员']
|
||||||
month_start_col = len(headers) # 月度考核开始列(0-indexed)
|
info_cols = 6 # 前6列是车辆信息(需要合并)
|
||||||
for m in range(1, settle_month+1):
|
for m in range(1, settle_month+1):
|
||||||
headers.append(f'{m}月考核明细')
|
headers += [f'{m}月应考核', f'{m}月实际', f'{m}月达标']
|
||||||
cum_start_col = len(headers)
|
|
||||||
if settle_month >= 2:
|
if settle_month >= 2:
|
||||||
headers += ['累计里程/目标','累计达标']
|
headers += ['累计应完成','累计实际','累计达标']
|
||||||
pay_start_col = len(headers)
|
headers += ['本月发放金额','发放类型','已发期数','已发金额','剩余期数']
|
||||||
headers += ['本月发放明细','已发期数','已发金额','剩余期数']
|
|
||||||
WH(ws, headers)
|
WH(ws, headers)
|
||||||
|
ws.freeze_panes = 'H2' # 冻结车辆信息+业务员列
|
||||||
|
|
||||||
# 冻结首行+首列
|
rn = 2
|
||||||
ws.freeze_panes = 'B2'
|
|
||||||
|
|
||||||
rn=2
|
|
||||||
for mv in master_vehicles:
|
for mv in master_vehicles:
|
||||||
plate=mv['车牌号']
|
plate = mv['车牌号']
|
||||||
info=vehicle_info.get(plate, {})
|
info = vehicle_info.get(plate, {})
|
||||||
pays=vehicle_payments.get(plate, [])
|
pays = vehicle_payments.get(plate, [])
|
||||||
|
|
||||||
target_name = info.get('考核目标','')
|
target_name = info.get('考核目标','')
|
||||||
monthly_bonus = info.get('月度奖励',0)
|
monthly_bonus = info.get('月度奖励',0)
|
||||||
@@ -384,102 +379,146 @@ def write_vehicle_tracking_sheet(wb, settle_month, G, master_vehicles, vehicle_p
|
|||||||
if mapped:
|
if mapped:
|
||||||
target_name, _, monthly_bonus = mapped
|
target_name, _, monthly_bonus = mapped
|
||||||
|
|
||||||
row=[plate,mv.get('车架号',''),mv.get('归属公司',''),mv.get('车型确定',''),
|
# 收集该车所有(业务员) - 跨月去重
|
||||||
target_name or '',monthly_bonus or '']
|
person_set = {}
|
||||||
|
|
||||||
cum_t=0; cum_a=0
|
|
||||||
month_qualified = {} # {月份: bool}
|
|
||||||
for m in range(1, settle_month+1):
|
for m in range(1, settle_month+1):
|
||||||
mgs=[(k,g) for k,g in G.get(m,{}).items() if k[0]==plate]
|
for k,g in G.get(m, {}).items():
|
||||||
if mgs:
|
if k[0] == plate:
|
||||||
lines = []
|
person_set[k[1]] = g['部门']
|
||||||
any_q = False
|
persons = sorted(person_set.keys())
|
||||||
for _,g in sorted(mgs, key=lambda x: x[0][1]):
|
if not persons:
|
||||||
t=g['应考核']; a=g['实际']
|
persons = [''] # 无考核记录也占一行
|
||||||
cum_t+=t; cum_a+=a
|
|
||||||
q = g['有达标']
|
start_rn = rn
|
||||||
if q: any_q = True
|
for pi, person in enumerate(persons):
|
||||||
sd = g['部门'].replace('业务','') if '业务' in g.get('部门','') else g.get('部门','')
|
sd = person_set.get(person,'').replace('业务','') if person else ''
|
||||||
lines.append(f"{sd}-{g['销售']}: {R(a,0)}/{R(t,0)} {'✓' if q else '✗'}")
|
person_label = f'{sd}-{person}' if person and sd else person
|
||||||
row.append('\n'.join(lines))
|
|
||||||
month_qualified[m] = any_q
|
# 车辆信息(只在第一行写,后面留空等合并)
|
||||||
|
if pi == 0:
|
||||||
|
row_base = [plate, mv.get('车架号',''), mv.get('归属公司',''),
|
||||||
|
mv.get('车型确定',''), target_name or '', monthly_bonus or '']
|
||||||
else:
|
else:
|
||||||
row.append('')
|
row_base = ['','','','','','']
|
||||||
month_qualified[m] = None # 无数据
|
|
||||||
|
|
||||||
cum_q = False
|
row = row_base + [person_label]
|
||||||
if settle_month>=2:
|
|
||||||
cum_q = cum_a>=cum_t and cum_t>0
|
|
||||||
row+=[f'{R(cum_a,0)}/{R(cum_t,0)}', '✓ 达标' if cum_q else '✗ 未达标']
|
|
||||||
|
|
||||||
# 本月发放
|
# 每月数据(按该业务员的group)
|
||||||
tp=[p for p in pays if p['结算月']==settle_month]
|
cum_t = 0; cum_a = 0
|
||||||
if tp:
|
for m in range(1, settle_month+1):
|
||||||
pay_lines = []
|
gm = G.get(m, {}).get((plate, person)) if person else None
|
||||||
for p in sorted(tp, key=lambda x: x['业务员']):
|
if gm:
|
||||||
pd = p.get('部门','').replace('业务','') if '业务' in p.get('部门','') else p.get('部门','')
|
# 逐条记录汇总(同人同月可能多条)
|
||||||
pay_lines.append(f"{pd}-{p['业务员']}: {R(p['金额'])}元 ({p['类型']})")
|
t_sum = gm['应考核']; a_sum = gm['实际']
|
||||||
row.append('\n'.join(pay_lines))
|
cum_t += t_sum; cum_a += a_sum
|
||||||
else:
|
# 每条记录的达标情况
|
||||||
row.append('')
|
rec_details = []
|
||||||
|
for rec in gm['recs']:
|
||||||
|
rq = rec['是否达标'] == '达标'
|
||||||
|
rec_details.append(f"{R(rec['实际行驶里程(km)'],0)}/{R(rec['应考核里程(km)'],0)}{'✓' if rq else '✗'}")
|
||||||
|
all_q = all(rec['是否达标'] == '达标' for rec in gm['recs'])
|
||||||
|
if len(gm['recs']) == 1:
|
||||||
|
detail = rec_details[0].split('✓')[0].split('✗')[0] # 只取数字
|
||||||
|
row += [R(t_sum), R(a_sum), '✓' if all_q else '✗']
|
||||||
|
else:
|
||||||
|
# 多条:显示汇总,但单元格内注明各条
|
||||||
|
row += [R(t_sum), R(a_sum), '\n'.join(rec_details)]
|
||||||
|
else:
|
||||||
|
row += ['', '', '']
|
||||||
|
|
||||||
pays_to_date=[p for p in pays if p['结算月']<=settle_month]
|
# 累计
|
||||||
tp_count=len(pays_to_date); tp_amt=sum(p['金额'] for p in pays_to_date)
|
if settle_month >= 2:
|
||||||
row+=[f'{tp_count}/12', R(tp_amt) if tp_amt>0 else 0, 12-tp_count]
|
if cum_t > 0:
|
||||||
|
cum_q = cum_a >= cum_t
|
||||||
|
row += [R(cum_t), R(cum_a), '✓' if cum_q else '✗']
|
||||||
|
else:
|
||||||
|
row += ['', '', '']
|
||||||
|
cum_q = False
|
||||||
|
else:
|
||||||
|
cum_q = False
|
||||||
|
|
||||||
WR(ws,rn,row)
|
# 本月发放(该业务员的)
|
||||||
|
tp = [p for p in pays if p['结算月'] == settle_month and p['业务员'] == person]
|
||||||
|
if tp:
|
||||||
|
amt = sum(p['金额'] for p in tp)
|
||||||
|
types = ', '.join(p['类型'] for p in tp)
|
||||||
|
row += [R(amt), types]
|
||||||
|
else:
|
||||||
|
row += [0, '']
|
||||||
|
|
||||||
# --- 美化 ---
|
# 奖金池(整车,只在第一行显示)
|
||||||
# 车辆基本信息列:浅灰底色
|
if pi == 0:
|
||||||
for ci in range(1, month_start_col+1):
|
pays_to_date = [p for p in pays if p['结算月'] <= settle_month]
|
||||||
ws.cell(row=rn, column=ci).fill = grey_fill
|
tp_count = len(pays_to_date); tp_amt = sum(p['金额'] for p in pays_to_date)
|
||||||
|
row += [f'{tp_count}/12', R(tp_amt) if tp_amt > 0 else 0, 12 - tp_count]
|
||||||
|
else:
|
||||||
|
row += ['', '', '']
|
||||||
|
|
||||||
# 月度考核列:达标绿底/未达标红底
|
WR(ws, rn, row)
|
||||||
for mi, m in enumerate(range(1, settle_month+1)):
|
|
||||||
col = month_start_col + mi + 1
|
|
||||||
cell = ws.cell(row=rn, column=col)
|
|
||||||
cell.alignment = wrap_top
|
|
||||||
if month_qualified.get(m) == True:
|
|
||||||
cell.fill = green_fill; cell.font = green_font
|
|
||||||
elif month_qualified.get(m) == False:
|
|
||||||
cell.fill = red_fill; cell.font = red_font
|
|
||||||
|
|
||||||
# 累计列
|
# 美化
|
||||||
if settle_month >= 2:
|
# 车辆信息列灰底
|
||||||
cum_cell = ws.cell(row=rn, column=cum_start_col+2) # 累计达标列
|
for ci in range(1, info_cols + 1):
|
||||||
if cum_q:
|
ws.cell(row=rn, column=ci).fill = grey_fill
|
||||||
cum_cell.fill = green_fill; cum_cell.font = Font(bold=True, color='006100')
|
|
||||||
elif cum_t > 0:
|
|
||||||
cum_cell.fill = red_fill; cum_cell.font = Font(bold=True, color='9C0006')
|
|
||||||
|
|
||||||
# 发放列:有发放金底
|
# 月度达标列着色
|
||||||
pay_cell = ws.cell(row=rn, column=pay_start_col+1)
|
for m in range(1, settle_month + 1):
|
||||||
pay_cell.alignment = wrap_top
|
col_base = info_cols + 1 + (m - 1) * 3 # 业务员列后面
|
||||||
if tp:
|
qual_col = col_base + 3 # 达标列
|
||||||
pay_cell.fill = gold_fill
|
cell = ws.cell(row=rn, column=qual_col)
|
||||||
|
cell.alignment = center_top
|
||||||
|
val = cell.value
|
||||||
|
if val and '✓' in str(val) and '✗' not in str(val):
|
||||||
|
cell.fill = green_fill; cell.font = green_font
|
||||||
|
elif val and '✗' in str(val):
|
||||||
|
cell.fill = red_fill; cell.font = red_font
|
||||||
|
|
||||||
# 奖金池:已发期数列
|
# 累计达标着色
|
||||||
period_cell = ws.cell(row=rn, column=pay_start_col+2)
|
if settle_month >= 2:
|
||||||
period_cell.alignment = center_top
|
cum_qual_col = info_cols + 1 + settle_month * 3 + 3 # 累计达标列
|
||||||
if tp_count > 0:
|
cell = ws.cell(row=rn, column=cum_qual_col)
|
||||||
period_cell.fill = blue_fill; period_cell.font = Font(bold=True)
|
if cell.value == '✓':
|
||||||
|
cell.fill = green_fill; cell.font = Font(bold=True, color='006100')
|
||||||
|
elif cell.value == '✗':
|
||||||
|
cell.fill = red_fill; cell.font = Font(bold=True, color='9C0006')
|
||||||
|
|
||||||
rn+=1
|
# 发放金底
|
||||||
|
pay_col = info_cols + 1 + settle_month * 3 + (3 if settle_month >= 2 else 0) + 1
|
||||||
|
if tp:
|
||||||
|
ws.cell(row=rn, column=pay_col).fill = gold_fill
|
||||||
|
|
||||||
|
# 奖金池蓝底
|
||||||
|
if pi == 0 and tp_count > 0:
|
||||||
|
pool_col = pay_col + 2
|
||||||
|
ws.cell(row=rn, column=pool_col).fill = blue_fill
|
||||||
|
ws.cell(row=rn, column=pool_col).font = Font(bold=True)
|
||||||
|
|
||||||
|
rn += 1
|
||||||
|
|
||||||
|
# 合并车辆信息列(如果多人)
|
||||||
|
if len(persons) > 1:
|
||||||
|
for ci in range(1, info_cols + 1):
|
||||||
|
ws.merge_cells(start_row=start_rn, start_column=ci,
|
||||||
|
end_row=start_rn + len(persons) - 1, end_column=ci)
|
||||||
|
ws.cell(row=start_rn, column=ci).alignment = Alignment(vertical='center', wrap_text=True)
|
||||||
|
# 奖金池列也合并
|
||||||
|
for offset in [2, 3, 4]: # 已发期数/已发金额/剩余期数
|
||||||
|
pool_col = pay_col + offset
|
||||||
|
ws.merge_cells(start_row=start_rn, start_column=pool_col,
|
||||||
|
end_row=start_rn + len(persons) - 1, end_column=pool_col)
|
||||||
|
|
||||||
# 列宽
|
# 列宽
|
||||||
col_widths = {'A':12,'B':20,'C':20,'D':12,'E':20,'F':8}
|
col_widths = {'A':12,'B':18,'C':18,'D':10,'E':18,'F':8,'G':14}
|
||||||
for col_letter, w in col_widths.items():
|
for cl, w in col_widths.items():
|
||||||
ws.column_dimensions[col_letter].width = w
|
ws.column_dimensions[cl].width = w
|
||||||
# 月度考核列宽
|
# 月度列
|
||||||
for mi in range(settle_month):
|
start = ord('H')
|
||||||
col_letter = chr(ord('G') + mi)
|
for m in range(settle_month):
|
||||||
ws.column_dimensions[col_letter].width = 30
|
for i in range(3):
|
||||||
# 剩余列自动
|
cl = chr(start + m*3 + i)
|
||||||
remaining_start = ord('G') + settle_month
|
if cl <= 'Z': ws.column_dimensions[cl].width = 12
|
||||||
for i in range(8):
|
# 剩余列
|
||||||
cl = chr(remaining_start + i)
|
rem = start + settle_month * 3
|
||||||
if cl <= 'Z':
|
for i in range(10):
|
||||||
ws.column_dimensions[cl].width = 16
|
cl = chr(rem + i)
|
||||||
|
if cl <= 'Z': ws.column_dimensions[cl].width = 14
|
||||||
# 自动筛选
|
|
||||||
ws.auto_filter.ref = f"A1:{chr(ord('A')+len(headers)-1)}1"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user