feat(crm): snapshot brand mode on quote and contract lines
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
bcfadd23d9
commit
ba98108659
4 files changed
+93
-2
No files matched your search
@@ -662,7 +662,10 @@ def create_from_contract(
|
||||
product_id=cp.product_id,
|
||||
product_no=cp.product_no,
|
||||
product_name=cp.product_name,
|
||||
model=cp.specification,
|
||||
brand_mode=cp.brand_mode,
|
||||
brand_id=cp.brand_id,
|
||||
brand_name=cp.brand_name,
|
||||
model=cp.model,
|
||||
unit=cp.unit or "吨",
|
||||
is_explosive=is_explosive,
|
||||
is_toxic=is_toxic,
|
||||
|
||||
@@ -1686,6 +1686,9 @@ def _quote_products_to_contract_products(quote_products: List[dict]) -> List[dic
|
||||
{
|
||||
"productId": item.get("productId") or item.get("product_id"),
|
||||
"productName": item.get("productName") or item.get("product_name"),
|
||||
"brandMode": item.get("brandMode") or item.get("brand_mode"),
|
||||
"brandId": item.get("brandId") or item.get("brand_id"),
|
||||
"brandName": item.get("brandName") or item.get("brand_name"),
|
||||
"productNo": item.get("productNo") or item.get("product_no"),
|
||||
"categoryId": item.get("categoryId") or item.get("category_id"),
|
||||
"categoryName": item.get("categoryName") or item.get("category_name"),
|
||||
|
||||
@@ -10,6 +10,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.utils import generate_uuid
|
||||
from app.crud.crm.product import get_product
|
||||
from app.crud.crm.quote_product import _resolve_line_brand_fields
|
||||
from app.models.crm_models import CrmContractProduct, CrmContractProductPriceTier
|
||||
from app.services.crm_supplier_hazard import enrich_products_with_hazard_flags
|
||||
|
||||
@@ -23,6 +24,9 @@ def _row_to_dict(row: CrmContractProduct) -> Dict[str, Any]:
|
||||
"contractId": row.contract_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 "",
|
||||
@@ -240,6 +244,16 @@ def insert_contract_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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user