Jelajahi Sumber

fix(pl): treat required brand as preference in brand qty validation

Allow multi-brand feedback/archive rows when plan brandMode is required; only enforce sum equals actual qty.

Co-authored-by: Cursor <cursoragent@cursor.com>
zlwu 3 bulan lalu
induk
melakukan
156e4b63e5

+ 1 - 8
backend/app/services/pl_brand_archive.py

@@ -18,14 +18,7 @@ def validate_brand_qty_rows(
     actual_qty = Decimal(str(actual_qty))
     if not brand_mode:
         return
-    positive = [r for r in rows if Decimal(str(r.get("qty") or 0)) > 0]
-    if brand_mode == "required":
-        if len(positive) != 1 or positive[0].get("brandId") != required_brand_id:
-            raise ValueError("指定品牌时只能填写该品牌数量")
-        if Decimal(str(positive[0]["qty"])) != actual_qty:
-            raise ValueError("指定品牌数量必须等于归档实际数量")
-        return
-    if brand_mode == "any":
+    if brand_mode in ("required", "any"):
         total = sum((Decimal(str(r.get("qty") or 0)) for r in rows), Decimal("0"))
         if total != actual_qty:
             raise ValueError("各品牌数量合计必须等于归档实际数量")

+ 27 - 3
backend/tests/test_pl_brand_archive.py

@@ -5,13 +5,37 @@ import pytest
 from app.services.pl_brand_archive import validate_brand_qty_rows
 
 
-def test_required_allows_only_that_brand():
-    with pytest.raises(ValueError, match="指定品牌"):
+def test_required_allows_other_brand_when_sum_matches():
+    validate_brand_qty_rows(
+        brand_mode="required",
+        required_brand_id="b1",
+        actual_qty=Decimal("10"),
+        rows=[{"brandId": "b2", "brandName": "红四方", "qty": Decimal("10")}],
+    )
+
+
+def test_required_allows_multi_brand_when_sum_matches():
+    validate_brand_qty_rows(
+        brand_mode="required",
+        required_brand_id="b1",
+        actual_qty=Decimal("10"),
+        rows=[
+            {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")},
+            {"brandId": "b2", "brandName": "红四方", "qty": Decimal("4")},
+        ],
+    )
+
+
+def test_required_sum_must_match():
+    with pytest.raises(ValueError, match="合计"):
         validate_brand_qty_rows(
             brand_mode="required",
             required_brand_id="b1",
             actual_qty=Decimal("10"),
-            rows=[{"brandId": "b2", "brandName": "红四方", "qty": Decimal("10")}],
+            rows=[
+                {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")},
+                {"brandId": "b2", "brandName": "红四方", "qty": Decimal("3")},
+            ],
         )