diff --git a/backend/app/crud/crm/contract_product.py b/backend/app/crud/crm/contract_product.py index 01e6df110..50752c5ce 100644 --- a/backend/app/crud/crm/contract_product.py +++ b/backend/app/crud/crm/contract_product.py @@ -447,8 +447,14 @@ def _assert_contract_line_unreferenced(db, tenant_no, line_id): def _referenced_line_identity(data: dict): - """产品、品牌、型号、单位决定被引用明细的身份;包装规格单独校验。""" - return _product_identity(data)[:-1] + """产品、品牌、型号、单位决定被引用明细的身份;包装规格单独校验。 + + 未指定具体品牌时,历史空值和编辑页提交的 any(不限品牌)是同一身份。 + """ + product_id, brand_mode, brand_id, model, unit = _product_identity(data)[:-1] + if not brand_id and brand_mode in ("", "any"): + brand_mode = "" + return (product_id, brand_mode, brand_id, model, unit) def _package_specs_used_by_active_plans(db, tenant_no, line_id): diff --git a/backend/tests/test_crm_missing_identity_patch.py b/backend/tests/test_crm_missing_identity_patch.py index 6f6c75f22..d2c59562c 100644 --- a/backend/tests/test_crm_missing_identity_patch.py +++ b/backend/tests/test_crm_missing_identity_patch.py @@ -309,6 +309,34 @@ def test_referenced_line_still_cannot_change_product_identity_or_be_removed(db): assert old.del_flag == 0 +def test_referenced_blank_brand_can_save_as_unrestricted_and_add_line(db): + """编辑页把历史空品牌提交为不限品牌时,不算更换身份,可以同时新增明细。""" + old = _referenced_contract(db) + old.brand_mode = None + old.brand_id = None + old.brand_name = None + db.flush() + sync(db, 'contract', [ + {'id': 'old', 'brandMode': 'any', 'brandId': None, 'brandName': None}, + {'productId': 'lye30', 'productName': '30%液碱', 'model': '30%', 'unit': '吨', 'quantity': 0, 'packageSpec': '槽罐车'}, + ]) + assert old.del_flag == 0 and old.id == 'old' and old.product_id == 'sku' + assert old.brand_mode == 'any' and old.brand_id is None + added = db.query(CrmContractProduct).filter_by(product_id='lye30', del_flag=0).one() + assert added.model == '30%' and added.unit == '吨' + + +def test_referenced_blank_brand_still_cannot_become_specific_brand(db): + old = _referenced_contract(db) + old.brand_mode = None + old.brand_id = None + old.brand_name = None + db.flush() + with pytest.raises(ValueError, match='不能删除或更换产品身份'): + sync(db, 'contract', [{'id': 'old', 'brandMode': 'required', 'brandId': 'hong', 'brandName': '红四方'}]) + assert old.brand_mode is None and old.brand_id is None and old.del_flag == 0 + + @pytest.mark.parametrize('follow_status,delivery_flag', [('cancelled', 0), ('arranged', 1)]) def test_inactive_plan_reference_does_not_lock_package_spec(db, follow_status, delivery_flag): old = _referenced_contract(db, follow_status=follow_status, delivery_flag=delivery_flag)