diff --git a/VERSION b/VERSION index f33f3c6..94f8697 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.58 \ No newline at end of file +1.3.59 \ No newline at end of file diff --git a/app/customers/backend/router.py b/app/customers/backend/router.py index 39a8c50..53d4390 100644 --- a/app/customers/backend/router.py +++ b/app/customers/backend/router.py @@ -327,6 +327,52 @@ async def link_economic_customer(customer_id: int, link_request: dict): raise HTTPException(status_code=500, detail=str(e)) +@router.get("/customers/{customer_id}/search-economic") +async def search_economic_for_customer(customer_id: int, query: Optional[str] = None): + """Search e-conomic for matching customers by name""" + try: + from app.services.economic_service import EconomicService + + # Get customer + customer = execute_query_single( + "SELECT id, name FROM customers WHERE id = %s", + (customer_id,)) + + if not customer: + raise HTTPException(status_code=404, detail="Customer not found") + + # Use provided query or customer name + search_query = query or customer['name'] + + # Search in e-conomic + economic = EconomicService() + results = await economic.search_customer_by_name(search_query) + + return { + "status": "success", + "customer_id": customer_id, + "customer_name": customer['name'], + "search_query": search_query, + "results": [ + { + "customerNumber": r.get('customerNumber'), + "name": r.get('name'), + "corporateIdentificationNumber": r.get('corporateIdentificationNumber'), + "email": r.get('email'), + "city": r.get('city') + } + for r in results + ], + "count": len(results) + } + + except HTTPException: + raise + except Exception as e: + logger.error(f"❌ Failed to search e-conomic for customer {customer_id}: {e}") + raise HTTPException(status_code=500, detail=str(e)) + + @router.post("/customers/{customer_id}/subscriptions/lock") async def lock_customer_subscriptions(customer_id: int, lock_request: dict): """Lock/unlock subscriptions for customer in local DB - BMC Låst status controlled in vTiger""" diff --git a/app/services/economic_service.py b/app/services/economic_service.py index 0b99acd..a9fbd86 100644 --- a/app/services/economic_service.py +++ b/app/services/economic_service.py @@ -197,6 +197,36 @@ class EconomicService: logger.error(f"❌ Error searching customer by CVR: {e}") return None + async def search_customer_by_name(self, name: str) -> List[Dict]: + """ + Search for customers by name (partial match) + + Args: + name: Customer name to search for + + Returns: + List of matching customer records + """ + try: + async with aiohttp.ClientSession() as session: + async with session.get( + f"{self.api_url}/customers", + params={"filter": f"name$like:*{name}*"}, + headers=self._get_headers() + ) as response: + if response.status == 200: + data = await response.json() + customers = data.get('collection', []) + logger.info(f"✅ Found {len(customers)} customers matching '{name}'") + return customers + else: + error = await response.text() + logger.error(f"❌ Name search failed: {response.status} - {error}") + return [] + except Exception as e: + logger.error(f"❌ Error searching customer by name: {e}") + return [] + # ========== SUPPLIER/VENDOR MANAGEMENT ========== async def search_supplier_by_name(self, supplier_name: str) -> Optional[Dict]: