feat(pricing): kit/pachet pricing with price list lookup, replace procent_pret
- Oracle PL/SQL: kit pricing logic with Mode A (distributed discount) and
Mode B (separate discount line), dual policy support, PRETURI_CU_TVA flag
- Eliminate procent_pret from entire stack (Oracle, Python, JS, HTML)
- New settings: kit_pricing_mode, kit_discount_codmat, price_sync_enabled
- Settings UI: cards for Kit Pricing and Price Sync configuration
- Mappings UI: kit badges with lazy-loaded component prices from price list
- Price sync from orders: auto-update ROA prices when web prices differ
- Catalog price sync: new service to sync all GoMag product prices to ROA
- Kit component price validation: pre-check prices before import
- New endpoint GET /api/mappings/{sku}/prices for component price display
- New endpoints POST /api/price-sync/start, GET status, GET history
- DDL script 07_drop_procent_pret.sql (run after deploy confirmation)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ from pydantic import BaseModel, validator
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import io
|
||||
import asyncio
|
||||
|
||||
from ..services import mapping_service, sqlite_service
|
||||
|
||||
@@ -19,7 +20,6 @@ class MappingCreate(BaseModel):
|
||||
sku: str
|
||||
codmat: str
|
||||
cantitate_roa: float = 1
|
||||
procent_pret: float = 100
|
||||
|
||||
@validator('sku', 'codmat')
|
||||
def not_empty(cls, v):
|
||||
@@ -29,14 +29,12 @@ class MappingCreate(BaseModel):
|
||||
|
||||
class MappingUpdate(BaseModel):
|
||||
cantitate_roa: Optional[float] = None
|
||||
procent_pret: Optional[float] = None
|
||||
activ: Optional[int] = None
|
||||
|
||||
class MappingEdit(BaseModel):
|
||||
new_sku: str
|
||||
new_codmat: str
|
||||
cantitate_roa: float = 1
|
||||
procent_pret: float = 100
|
||||
|
||||
@validator('new_sku', 'new_codmat')
|
||||
def not_empty(cls, v):
|
||||
@@ -47,7 +45,6 @@ class MappingEdit(BaseModel):
|
||||
class MappingLine(BaseModel):
|
||||
codmat: str
|
||||
cantitate_roa: float = 1
|
||||
procent_pret: float = 100
|
||||
|
||||
class MappingBatchCreate(BaseModel):
|
||||
sku: str
|
||||
@@ -63,11 +60,10 @@ async def mappings_page(request: Request):
|
||||
@router.get("/api/mappings")
|
||||
async def list_mappings(search: str = "", page: int = 1, per_page: int = 50,
|
||||
sort_by: str = "sku", sort_dir: str = "asc",
|
||||
show_deleted: bool = False, pct_filter: str = None):
|
||||
show_deleted: bool = False):
|
||||
result = mapping_service.get_mappings(search=search, page=page, per_page=per_page,
|
||||
sort_by=sort_by, sort_dir=sort_dir,
|
||||
show_deleted=show_deleted,
|
||||
pct_filter=pct_filter)
|
||||
show_deleted=show_deleted)
|
||||
# Merge product names from web_products (R4)
|
||||
skus = list({m["sku"] for m in result.get("mappings", [])})
|
||||
product_names = await sqlite_service.get_web_products_batch(skus)
|
||||
@@ -75,13 +71,13 @@ async def list_mappings(search: str = "", page: int = 1, per_page: int = 50,
|
||||
m["product_name"] = product_names.get(m["sku"], "")
|
||||
# Ensure counts key is always present
|
||||
if "counts" not in result:
|
||||
result["counts"] = {"total": 0, "complete": 0, "incomplete": 0}
|
||||
result["counts"] = {"total": 0}
|
||||
return result
|
||||
|
||||
@router.post("/api/mappings")
|
||||
async def create_mapping(data: MappingCreate):
|
||||
try:
|
||||
result = mapping_service.create_mapping(data.sku, data.codmat, data.cantitate_roa, data.procent_pret)
|
||||
result = mapping_service.create_mapping(data.sku, data.codmat, data.cantitate_roa)
|
||||
# Mark SKU as resolved in missing_skus tracking
|
||||
await sqlite_service.resolve_missing_sku(data.sku)
|
||||
return {"success": True, **result}
|
||||
@@ -97,7 +93,7 @@ async def create_mapping(data: MappingCreate):
|
||||
@router.put("/api/mappings/{sku}/{codmat}")
|
||||
def update_mapping(sku: str, codmat: str, data: MappingUpdate):
|
||||
try:
|
||||
updated = mapping_service.update_mapping(sku, codmat, data.cantitate_roa, data.procent_pret, data.activ)
|
||||
updated = mapping_service.update_mapping(sku, codmat, data.cantitate_roa, data.activ)
|
||||
return {"success": updated}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
@@ -106,7 +102,7 @@ def update_mapping(sku: str, codmat: str, data: MappingUpdate):
|
||||
def edit_mapping(sku: str, codmat: str, data: MappingEdit):
|
||||
try:
|
||||
result = mapping_service.edit_mapping(sku, codmat, data.new_sku, data.new_codmat,
|
||||
data.cantitate_roa, data.procent_pret)
|
||||
data.cantitate_roa)
|
||||
return {"success": result}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
@@ -133,16 +129,10 @@ async def create_batch_mapping(data: MappingBatchCreate):
|
||||
if not data.mappings:
|
||||
return {"success": False, "error": "No mappings provided"}
|
||||
|
||||
# Validate procent_pret sums to 100 for multi-line sets
|
||||
if len(data.mappings) > 1:
|
||||
total_pct = sum(m.procent_pret for m in data.mappings)
|
||||
if abs(total_pct - 100) > 0.01:
|
||||
return {"success": False, "error": f"Procent pret trebuie sa fie 100% (actual: {total_pct}%)"}
|
||||
|
||||
try:
|
||||
results = []
|
||||
for m in data.mappings:
|
||||
r = mapping_service.create_mapping(data.sku, m.codmat, m.cantitate_roa, m.procent_pret, auto_restore=data.auto_restore)
|
||||
r = mapping_service.create_mapping(data.sku, m.codmat, m.cantitate_roa, auto_restore=data.auto_restore)
|
||||
results.append(r)
|
||||
# Mark SKU as resolved in missing_skus tracking
|
||||
await sqlite_service.resolve_missing_sku(data.sku)
|
||||
@@ -151,6 +141,23 @@ async def create_batch_mapping(data: MappingBatchCreate):
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
@router.get("/api/mappings/{sku}/prices")
|
||||
async def get_mapping_prices(sku: str):
|
||||
"""Get component prices from crm_politici_pret_art for a kit SKU."""
|
||||
app_settings = await sqlite_service.get_app_settings()
|
||||
id_pol = int(app_settings.get("id_pol") or 0) or None
|
||||
id_pol_productie = int(app_settings.get("id_pol_productie") or 0) or None
|
||||
if not id_pol:
|
||||
return {"error": "Politica de pret nu este configurata", "prices": []}
|
||||
try:
|
||||
prices = await asyncio.to_thread(
|
||||
mapping_service.get_component_prices, sku, id_pol, id_pol_productie
|
||||
)
|
||||
return {"prices": prices}
|
||||
except Exception as e:
|
||||
return {"error": str(e), "prices": []}
|
||||
|
||||
|
||||
@router.post("/api/mappings/import-csv")
|
||||
async def import_csv(file: UploadFile = File(...)):
|
||||
content = await file.read()
|
||||
|
||||
Reference in New Issue
Block a user