|
|
@@ -10,7 +10,7 @@ from sqlalchemy.orm import Session
|
|
|
from app.core.delete_guard import assert_row_deletable_by_creator
|
|
|
from app.core.utils import generate_uuid
|
|
|
from app.crud.crm.product import get_product
|
|
|
-from app.models.crm_models import CrmQuoteProduct
|
|
|
+from app.models.crm_models import CrmProductBrand, CrmQuoteProduct
|
|
|
from app.services.crm_supplier_hazard import enrich_products_with_hazard_flags
|
|
|
|
|
|
_FMT = "%Y-%m-%d %H:%M:%S"
|
|
|
@@ -48,6 +48,52 @@ def calculate_subtotal(
|
|
|
return price * quantity * (Decimal("1") - disc / Decimal("100"))
|
|
|
|
|
|
|
|
|
+def normalize_line_brand(*, brand_mode, brand_id, brand_name, product_has_brands: bool):
|
|
|
+ if not product_has_brands:
|
|
|
+ return None, None, None
|
|
|
+ mode = brand_mode or "any"
|
|
|
+ if mode == "required":
|
|
|
+ if not brand_id or not brand_name:
|
|
|
+ raise ValueError("指定品牌时必须选择品牌")
|
|
|
+ return "required", brand_id, brand_name
|
|
|
+ if mode == "any":
|
|
|
+ return "any", None, None
|
|
|
+ raise ValueError("无效的 brand_mode")
|
|
|
+
|
|
|
+
|
|
|
+def product_has_brands(db: Session, product_id: str, tenant_no: str = None) -> bool:
|
|
|
+ if not product_id:
|
|
|
+ return False
|
|
|
+ q = db.query(CrmProductBrand.id).filter(
|
|
|
+ CrmProductBrand.product_id == product_id,
|
|
|
+ CrmProductBrand.del_flag == 0,
|
|
|
+ )
|
|
|
+ if tenant_no:
|
|
|
+ q = q.filter(CrmProductBrand.tenant_no == tenant_no)
|
|
|
+ return q.first() is not None
|
|
|
+
|
|
|
+
|
|
|
+def _resolve_line_brand_fields(
|
|
|
+ db: Session,
|
|
|
+ *,
|
|
|
+ tenant_no: str,
|
|
|
+ product_id: str,
|
|
|
+ data: dict,
|
|
|
+ defaults: tuple = (None, None, None),
|
|
|
+) -> tuple:
|
|
|
+ brand_mode = _get_field(data, "brand_mode", "brandMode")
|
|
|
+ brand_id = _get_field(data, "brand_id", "brandId")
|
|
|
+ brand_name = _get_field(data, "brand_name", "brandName")
|
|
|
+ if brand_mode is None and brand_id is None and brand_name is None:
|
|
|
+ brand_mode, brand_id, brand_name = defaults
|
|
|
+ return normalize_line_brand(
|
|
|
+ brand_mode=brand_mode,
|
|
|
+ brand_id=brand_id,
|
|
|
+ brand_name=brand_name,
|
|
|
+ product_has_brands=product_has_brands(db, product_id, tenant_no),
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
def _row_to_dict(row: CrmQuoteProduct) -> Dict[str, Any]:
|
|
|
"""ORM → camelCase(单点真相)"""
|
|
|
return {
|
|
|
@@ -55,6 +101,9 @@ def _row_to_dict(row: CrmQuoteProduct) -> Dict[str, Any]:
|
|
|
"quoteId": row.quote_id or "",
|
|
|
"productId": row.product_id or "",
|
|
|
"productName": row.product_name or "",
|
|
|
+ "brandMode": row.brand_mode,
|
|
|
+ "brandId": row.brand_id,
|
|
|
+ "brandName": row.brand_name,
|
|
|
"productNo": row.product_no or "",
|
|
|
"categoryId": row.category_id or "",
|
|
|
"categoryName": row.category_name or "",
|
|
|
@@ -174,6 +223,16 @@ def insert_quote_product(
|
|
|
if not entity.product_name:
|
|
|
raise ValueError("产品名称不能为空")
|
|
|
|
|
|
+ brand_mode, brand_id, brand_name = _resolve_line_brand_fields(
|
|
|
+ db,
|
|
|
+ tenant_no=tenant_no,
|
|
|
+ product_id=product_id,
|
|
|
+ data=data,
|
|
|
+ )
|
|
|
+ entity.brand_mode = brand_mode
|
|
|
+ entity.brand_id = brand_id
|
|
|
+ entity.brand_name = brand_name
|
|
|
+
|
|
|
db.add(entity)
|
|
|
db.flush()
|
|
|
return entity
|
|
|
@@ -225,6 +284,18 @@ def update_quote_product(
|
|
|
entity.tax_rate = _decimal_value(_get_field(data, "tax_rate", "taxRate"), entity.tax_rate)
|
|
|
|
|
|
entity.subtotal = calculate_subtotal(entity.price, entity.quantity, entity.discount)
|
|
|
+ brand_keys = ("brand_mode", "brandMode", "brand_id", "brandId", "brand_name", "brandName")
|
|
|
+ if any(k in data for k in brand_keys) or "product_id" in data or "productId" in data:
|
|
|
+ brand_mode, brand_id, brand_name = _resolve_line_brand_fields(
|
|
|
+ db,
|
|
|
+ tenant_no=entity.tenant_no,
|
|
|
+ product_id=entity.product_id or "",
|
|
|
+ data=data,
|
|
|
+ defaults=(entity.brand_mode, entity.brand_id, entity.brand_name),
|
|
|
+ )
|
|
|
+ entity.brand_mode = brand_mode
|
|
|
+ entity.brand_id = brand_id
|
|
|
+ entity.brand_name = brand_name
|
|
|
entity.update_by = user_id
|
|
|
entity.update_time = datetime.now()
|
|
|
db.flush()
|