# -*- coding: utf-8 -*- """起薪/调薪项目核对:项目取值、固定项总额、岗位名称校验。""" from __future__ import annotations from typing import List, Optional from .. import config from ..data.wage_api import WageApi #: 固定参数项(不计入固定项总额) NO_TOTAL_TYPES = ("6", "8") def to_number(value) -> Optional[float]: s = config.text(value).replace(",", "") if not s: return None try: return float(s) except ValueError: return None def _prefill_map(base_items: List[dict]) -> dict: result = {} for item in base_items or []: for key in (config.text(item.get("itemId")), config.text(item.get("itemName"))): if key: result.setdefault(key, item) return result def _cities_in(text: str) -> List[str]: """文本里出现的城市(标准名形如「服务-金色印象-长沙接待员」「金色印象-轻养师(重庆)」)。""" normalized = config.norm(text) return [city for city in config.CITIES if config.norm(city) in normalized] def _area_issue(names: List[str], brand: str, city: str) -> str: """核对工资标准的品牌/区域与员工是否一致;一致返回空串。""" if not names: return "" if brand: hit_brand = [n for n in names if config.norm(brand) in config.norm(n)] other = [b for b in config.BRANDS if b != brand and any(config.norm(b) in config.norm(n) for n in names)] if not hit_brand: return f"工资标准均不含品牌「{brand}」" if other: return f"工资标准区域含其它品牌「{'/'.join(other)}」,与员工「{brand}」不一致" cities = sorted({c for n in names for c in _cities_in(n)}) if not cities: return "" if not city: return f"工资标准区域为「{'/'.join(cities)}」,无法确定员工区域" if city not in cities: return f"工资标准区域为「{'/'.join(cities)}」,与员工区域「{city}」不一致" return "" def build_plan(api: WageApi, row: dict, account_id: str, *, same_account: bool, base_info_id: str = "", brand: str = "", city: str = "", fix_items: Optional[dict] = None) -> dict: """按前端口径生成工资项明细与固定项总额。 Args: fix_items: 按标准表修正的固定项,如 ``{"绩效奖金": 1000.0}`` (仅湖畔管理岗绩效奖金、印象见习主管/见习领班岗位补贴)。 Returns: {"items": [...], "total": float, "post_in_standard": bool, "standard_names": [...], "issues": [...], "warnings": [...], "corrections": [...], "prefilled": bool} """ staff_id = config.text(row.get("base_id")) stru_id = config.text(row.get("STRU_ID")) post_name = config.text(row.get("POST_ORG_NAME")) title = config.text(row.get("POST_LEVEL_NAME")) account_items = api.account_items(account_id, staff_id, stru_id) standards = api.standards(staff_id, account_id, stru_id) base_items = api.base_items(base_info_id) if same_account and base_info_id else [] prefill = _prefill_map(base_items) items: List[dict] = [] issues: List[str] = [] warnings: List[str] = [] corrections: List[str] = [] standard_names = [] total = 0.0 std_by_item: dict = {} for std in standards or []: key = config.text(std.get("itemId")) std_by_item.setdefault(key, []).append(std) name = config.text(std.get("standardName")) if name: standard_names.append(name) for raw in account_items: item_id = config.text(raw.get("itemId")) name = config.text(raw.get("itemName")) item_type = config.text(raw.get("itemType")) cal_type = config.text(raw.get("calculateType")) pre = prefill.get(item_id) or prefill.get(name) matches = std_by_item.get(item_id) or [] standard_id, standard_name, value, source = "", "", "", "" if pre: value = config.text(pre.get("itemValue")) standard_id = config.text(pre.get("standardId")) source = "沿用原值" elif len(matches) == 1: value = config.text(matches[0].get("sum")) standard_id = config.text(matches[0].get("standardId")) standard_name = config.text(matches[0].get("standardName")) source = "工资标准" elif len(matches) > 1: source = "多标准" if item_type in ("1", "5"): issues.append(f"「{name}」匹配到 {len(matches)} 个工资标准,需人工选择") else: source = "未匹配" # 固定项(itemType=1)平台默认 0、人工手填;按标准项(itemType=5)缺标准时下拉为「暂无薪资标准(0)」 if item_type == "5": warnings.append(f"「{name}」未匹配到工资标准,按 0 处理") number = to_number(value) if item_type in ("1", "5") and not value: # 平台弹窗:固定项默认 0(required),标准项无匹配时下拉值为 0 value = "0" number = 0.0 # 按标准表修正固定项(只改指定项目,其余保持原值) if fix_items and item_type == "1": for key, target in fix_items.items(): if key in config.norm(name) and abs((number or 0.0) - float(target)) > 0.01: original = config.text(value) or "0" value = f"{float(target):.2f}" number = float(target) source = "标准修正" corrections.append(f"{name} {original} → {value}(按标准修正)") break if item_type not in NO_TOTAL_TYPES and number is not None: total += -number if cal_type in ("2", "4") else number items.append({ "accountItemId": config.text(raw.get("accountItemId")), "itemId": item_id, "itemName": name, "itemType": item_type, "calType": cal_type, "standardId": standard_id, "standardName": standard_name, "preValue": config.text(pre.get("itemValue")) if pre else "", "value": value, "source": source, }) post_hit = bool(post_name) and any( config.norm(post_name) in config.norm(n) for n in standard_names) if not standard_names: issues.append("未取到工资标准,无法核对岗位名称") elif not post_hit: issues.append(f"工资标准名称均不含岗位名称「{post_name or '空'}」") area_issue = _area_issue(standard_names, brand, city) if area_issue: issues.append(area_issue) return { "items": items, "total": round(total, 2), "post_in_standard": post_hit, "standard_names": standard_names, "issues": issues, "warnings": warnings, "corrections": corrections, "prefilled": bool(prefill), "title": title, } def part_groups(items: List[dict]) -> dict: """按项目名归集固定项金额:岗位补贴 / 绩效奖金 / 星级奖金 / 其余薪资小计。""" groups = {"岗位补贴": 0.0, "绩效奖金": 0.0, "星级奖金": 0.0, "薪资小计": 0.0} for item in items: if item["itemType"] in NO_TOTAL_TYPES: continue number = to_number(item["value"]) or 0.0 if item["calType"] in ("2", "4"): number = -number name = config.norm(item["itemName"]) if "岗位补贴" in name: groups["岗位补贴"] += number elif "绩效奖金" in name: groups["绩效奖金"] += number elif "星级奖金" in name: groups["星级奖金"] += number else: groups["薪资小计"] += number return {key: round(value, 2) for key, value in groups.items()} def diff_parts(items: List[dict], expected: dict) -> List[str]: """分项核对:expected 形如 {"岗位补贴": 1000.0, "薪资小计": 4000.0}。""" groups = part_groups(items) return [f"{key} {groups[key]:.2f} ≠ 标准 {float(target):.2f}" for key, target in expected.items() if abs(groups[key] - float(target)) > 0.01]