feat(settings): add GoMag API config, Oracle dropdowns, compact 2x2 layout
- Remove ID_GESTIUNE from config (unused) - Add GoMag API settings (key, shop, days_back, limit) to SQLite — editable without restart - sync_service reads GoMag settings from SQLite before download - gomag_client.download_orders accepts api_key/api_shop/limit overrides - New GET /api/settings/sectii and /api/settings/politici endpoints for Oracle dropdowns (nom_sectii.sectie, crm_politici_preturi.nume_lista_preturi) - id_pol, id_sectie, transport_id_pol, discount_id_pol now use select dropdowns - order_reader extracts discount_vat from GoMag JSON discounts[].vat - import_service uses GoMag discount_vat as primary, settings as fallback - settings.html redesigned to compact 2x2 grid (GoMag API | Import ROA / Transport | Discount) - settings.js v2: loadDropdowns() sequential before loadSettings() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -16,19 +16,27 @@ logger = logging.getLogger(__name__)
|
||||
async def download_orders(
|
||||
json_dir: str,
|
||||
days_back: int = None,
|
||||
api_key: str = None,
|
||||
api_shop: str = None,
|
||||
limit: int = None,
|
||||
log_fn: Callable[[str], None] = None,
|
||||
) -> dict:
|
||||
"""Download orders from GoMag API and save as JSON files.
|
||||
|
||||
Returns dict with keys: pages, total, files (list of saved file paths).
|
||||
If API keys are not configured, returns immediately with empty result.
|
||||
Optional api_key, api_shop, limit override config.settings values.
|
||||
"""
|
||||
def _log(msg: str):
|
||||
logger.info(msg)
|
||||
if log_fn:
|
||||
log_fn(msg)
|
||||
|
||||
if not settings.GOMAG_API_KEY or not settings.GOMAG_API_SHOP:
|
||||
effective_key = api_key or settings.GOMAG_API_KEY
|
||||
effective_shop = api_shop or settings.GOMAG_API_SHOP
|
||||
effective_limit = limit or settings.GOMAG_LIMIT
|
||||
|
||||
if not effective_key or not effective_shop:
|
||||
_log("GoMag API keys neconfigurați, skip download")
|
||||
return {"pages": 0, "total": 0, "files": []}
|
||||
|
||||
@@ -40,8 +48,8 @@ async def download_orders(
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
headers = {
|
||||
"Apikey": settings.GOMAG_API_KEY,
|
||||
"ApiShop": settings.GOMAG_API_SHOP,
|
||||
"Apikey": effective_key,
|
||||
"ApiShop": effective_shop,
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
@@ -57,7 +65,7 @@ async def download_orders(
|
||||
params = {
|
||||
"startDate": start_date,
|
||||
"page": page,
|
||||
"limit": settings.GOMAG_LIMIT,
|
||||
"limit": effective_limit,
|
||||
}
|
||||
try:
|
||||
response = await client.get(settings.GOMAG_API_URL, headers=headers, params=params)
|
||||
|
||||
@@ -92,7 +92,8 @@ def build_articles_json(items, order=None, settings=None) -> str:
|
||||
articles.append(article_dict)
|
||||
# Discount total with quantity -1 (positive price)
|
||||
if order.discount_total > 0 and discount_codmat:
|
||||
discount_vat = settings.get("discount_vat", "19")
|
||||
# Use GoMag JSON discount VAT if available, fallback to settings
|
||||
discount_vat = getattr(order, 'discount_vat', None) or settings.get("discount_vat", "19")
|
||||
article_dict = {
|
||||
"sku": discount_codmat,
|
||||
"quantity": "-1",
|
||||
|
||||
@@ -57,6 +57,7 @@ class OrderData:
|
||||
total: float = 0.0
|
||||
delivery_cost: float = 0.0
|
||||
discount_total: float = 0.0
|
||||
discount_vat: Optional[str] = None
|
||||
payment_name: str = ""
|
||||
delivery_name: str = ""
|
||||
source_file: str = ""
|
||||
@@ -160,11 +161,14 @@ def _parse_order(order_id: str, data: dict, source_file: str) -> OrderData:
|
||||
# Parse delivery cost
|
||||
delivery_cost = float(delivery.get("total", 0) or 0) if isinstance(delivery, dict) else 0.0
|
||||
|
||||
# Parse discount total (sum of all discount values)
|
||||
# Parse discount total (sum of all discount values) and VAT from first discount item
|
||||
discount_total = 0.0
|
||||
discount_vat = None
|
||||
for d in data.get("discounts", []):
|
||||
if isinstance(d, dict):
|
||||
discount_total += float(d.get("value", 0) or 0)
|
||||
if discount_vat is None and d.get("vat") is not None:
|
||||
discount_vat = str(d["vat"])
|
||||
|
||||
return OrderData(
|
||||
id=str(data.get("id", order_id)),
|
||||
@@ -178,6 +182,7 @@ def _parse_order(order_id: str, data: dict, source_file: str) -> OrderData:
|
||||
total=float(data.get("total", 0) or 0),
|
||||
delivery_cost=delivery_cost,
|
||||
discount_total=discount_total,
|
||||
discount_vat=discount_vat,
|
||||
payment_name=str(payment.get("name", "")) if isinstance(payment, dict) else "",
|
||||
delivery_name=str(delivery.get("name", "")) if isinstance(delivery, dict) else "",
|
||||
source_file=source_file
|
||||
|
||||
@@ -129,8 +129,18 @@ async def run_sync(id_pol: int = None, id_sectie: int = None, run_id: str = None
|
||||
# Phase 0: Download orders from GoMag API
|
||||
_update_progress("downloading", "Descărcare comenzi din GoMag API...")
|
||||
_log_line(run_id, "Descărcare comenzi din GoMag API...")
|
||||
# Read GoMag settings from SQLite (override config defaults)
|
||||
dl_settings = await sqlite_service.get_app_settings()
|
||||
gomag_key = dl_settings.get("gomag_api_key") or None
|
||||
gomag_shop = dl_settings.get("gomag_api_shop") or None
|
||||
gomag_days_str = dl_settings.get("gomag_order_days_back")
|
||||
gomag_days = int(gomag_days_str) if gomag_days_str else None
|
||||
gomag_limit_str = dl_settings.get("gomag_limit")
|
||||
gomag_limit = int(gomag_limit_str) if gomag_limit_str else None
|
||||
dl_result = await gomag_client.download_orders(
|
||||
json_dir, log_fn=lambda msg: _log_line(run_id, msg)
|
||||
json_dir, log_fn=lambda msg: _log_line(run_id, msg),
|
||||
api_key=gomag_key, api_shop=gomag_shop,
|
||||
days_back=gomag_days, limit=gomag_limit,
|
||||
)
|
||||
if dl_result["files"]:
|
||||
_log_line(run_id, f"GoMag: {dl_result['total']} comenzi în {dl_result['pages']} pagini → {len(dl_result['files'])} fișiere")
|
||||
|
||||
Reference in New Issue
Block a user