import pandas as pd
from collections import defaultdict

fp = "/Users/liuxinyuan/Desktop/hermas输入-工作台数据库/2024和2025历史数据/2025全年订单.xlsx"
df = pd.read_excel(fp, sheet_name='Sheet1', dtype=str)

# 口径：大客户1区域（= 现在刘子研接手的盘子），非赠品，价税合计>0
sub = df[(df['销售员'] == '大客户1')]
sub = sub.copy()
sub['_amt'] = pd.to_numeric(sub['价税合计'], errors='coerce').fillna(0)
sub['_gift'] = sub['是否赠品'].fillna('')
# 非赠品
sub = sub[sub['_gift'] != '是']

monthly = defaultdict(float)
cust_month = defaultdict(lambda: defaultdict(float))
for _, r in sub.iterrows():
    m = str(r['合同月份']).strip()
    if not m.startswith('2025'):
        continue
    c = str(r['客户']).strip()
    a = float(r['_amt'])
    monthly[m] += a
    cust_month[c][m] += a

print("=== 2025年 大客户1区域（=刘子研盘子）月度销售额 ===")
print(f"{'月份':<10}{'销售额(元)':>16}{'万元':>10}")
for m in sorted(monthly):
    v = monthly[m]
    print(f"{m:<10}{v:>16,.0f}{v/1e4:>10.2f}")

print(f"\n>>> 2025年7月: {monthly.get('2025-07',0):,.0f} 元 = {monthly.get('2025-07',0)/1e4:.2f}万")
print(f">>> 2025年8月: {monthly.get('2025-08',0):,.0f} 元 = {monthly.get('2025-08',0)/1e4:.2f}万")

# 1-8月合计
h1_8 = sum(monthly.get(f'2025-{i:02d}',0) for i in range(1,9))
print(f">>> 2025年1-8月合计: {h1_8:,.0f} 元 = {h1_8/1e4:.2f}万")

# 各客户 1-8月明细
print("\n=== 2025年 大客户1 各客户 1-8月明细（元）===")
months = [f'2025-{i:02d}' for i in range(1,9)]
customers = sorted(cust_month.keys(), key=lambda c: -sum(cust_month[c].get(m,0) for m in months))
print(f"{'客户':<32}" + "".join(f"{m[5:]}月".rjust(11) for m in months) + "1-8月合计".rjust(12))
for c in customers:
    tot = sum(cust_month[c].get(m,0) for m in months)
    if tot == 0:
        continue
    row = "".join(f"{cust_month[c].get(m,0):,.0f}".rjust(11) for m in months)
    print(f"{c:<32}{row}{tot:>12,.0f}")
