2025-10-30 21:08:07 +01:00
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Procurement Service Client for Inter-Service Communication
|
|
|
|
|
Provides API client for procurement operations and internal transfers
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import structlog
|
2025-11-30 09:12:40 +01:00
|
|
|
from typing import Dict, Any, List, Optional
|
2025-10-30 21:08:07 +01:00
|
|
|
from datetime import date
|
|
|
|
|
from shared.clients.base_service_client import BaseServiceClient
|
|
|
|
|
from shared.config.base import BaseServiceSettings
|
|
|
|
|
|
|
|
|
|
logger = structlog.get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProcurementServiceClient(BaseServiceClient):
|
2025-11-30 09:12:40 +01:00
|
|
|
"""Client for communicating with the Procurement Service"""
|
2025-10-30 21:08:07 +01:00
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
def __init__(self, config: BaseServiceSettings, service_name: str = "unknown"):
|
|
|
|
|
super().__init__(service_name, config)
|
|
|
|
|
self.service_base_url = config.PROCUREMENT_SERVICE_URL
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
def get_service_base_path(self) -> str:
|
|
|
|
|
return "/api/v1"
|
|
|
|
|
|
|
|
|
|
# ================================================================
|
2025-11-30 09:12:40 +01:00
|
|
|
# PURCHASE ORDER ENDPOINTS
|
2025-10-30 21:08:07 +01:00
|
|
|
# ================================================================
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def create_purchase_order(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-11-30 09:12:40 +01:00
|
|
|
order_data: Dict[str, Any]
|
2025-10-30 21:08:07 +01:00
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Create a new purchase order
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tenant_id: Tenant ID
|
2025-11-30 09:12:40 +01:00
|
|
|
order_data: Purchase order data
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Created purchase order
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.post(
|
|
|
|
|
"procurement/purchase-orders",
|
|
|
|
|
data=order_data,
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Created purchase order",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
po_number=response.get("po_number"))
|
2025-10-30 21:08:07 +01:00
|
|
|
return response
|
|
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error creating purchase order",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
error=str(e))
|
2025-10-30 21:08:07 +01:00
|
|
|
return None
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_purchase_order(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-11-30 09:12:40 +01:00
|
|
|
po_id: str
|
2025-10-30 21:08:07 +01:00
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get a specific purchase order
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tenant_id: Tenant ID
|
2025-11-30 09:12:40 +01:00
|
|
|
po_id: Purchase order ID
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
Purchase order details
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.get(
|
|
|
|
|
f"procurement/purchase-orders/{po_id}",
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved purchase order",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
po_id=po_id)
|
2025-10-30 21:08:07 +01:00
|
|
|
return response
|
|
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting purchase order",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
po_id=po_id,
|
|
|
|
|
error=str(e))
|
2025-10-30 21:08:07 +01:00
|
|
|
return None
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def update_purchase_order_status(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-11-30 09:12:40 +01:00
|
|
|
po_id: str,
|
|
|
|
|
new_status: str,
|
|
|
|
|
user_id: str
|
2025-10-30 21:08:07 +01:00
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Update purchase order status
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tenant_id: Tenant ID
|
2025-11-30 09:12:40 +01:00
|
|
|
po_id: Purchase order ID
|
|
|
|
|
new_status: New status
|
|
|
|
|
user_id: User ID performing update
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
Updated purchase order
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.put(
|
|
|
|
|
f"procurement/purchase-orders/{po_id}/status",
|
|
|
|
|
data={
|
|
|
|
|
"status": new_status,
|
|
|
|
|
"updated_by_user_id": user_id
|
|
|
|
|
},
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Updated purchase order status",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
po_id=po_id,
|
|
|
|
|
new_status=new_status)
|
2025-10-30 21:08:07 +01:00
|
|
|
return response
|
|
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error updating purchase order status",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
po_id=po_id,
|
|
|
|
|
new_status=new_status,
|
|
|
|
|
error=str(e))
|
2025-10-30 21:08:07 +01:00
|
|
|
return None
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_pending_purchase_orders(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-12-05 20:07:01 +01:00
|
|
|
limit: int = 50,
|
|
|
|
|
enrich_supplier: bool = True
|
2025-10-30 21:08:07 +01:00
|
|
|
) -> Optional[List[Dict[str, Any]]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get pending purchase orders
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tenant_id: Tenant ID
|
2025-11-30 09:12:40 +01:00
|
|
|
limit: Maximum number of results
|
2025-12-05 20:07:01 +01:00
|
|
|
enrich_supplier: Whether to include supplier details (default: True)
|
|
|
|
|
Set to False for faster queries when supplier data will be fetched separately
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
List of pending purchase orders
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.get(
|
|
|
|
|
"procurement/purchase-orders",
|
2025-12-05 20:07:01 +01:00
|
|
|
params={
|
|
|
|
|
"status": "pending_approval",
|
|
|
|
|
"limit": limit,
|
|
|
|
|
"enrich_supplier": enrich_supplier
|
|
|
|
|
},
|
2025-11-30 09:12:40 +01:00
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
2025-12-05 20:07:01 +01:00
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved pending purchase orders",
|
|
|
|
|
tenant_id=tenant_id,
|
2025-12-05 20:07:01 +01:00
|
|
|
count=len(response),
|
|
|
|
|
enriched=enrich_supplier)
|
2025-11-30 09:12:40 +01:00
|
|
|
return response if response else []
|
2025-10-30 21:08:07 +01:00
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting pending purchase orders",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
error=str(e))
|
|
|
|
|
return []
|
2025-10-30 21:08:07 +01:00
|
|
|
|
2025-12-05 20:07:01 +01:00
|
|
|
async def get_purchase_orders_by_supplier(
|
|
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
|
|
|
|
supplier_id: str,
|
|
|
|
|
date_from: Optional[date] = None,
|
|
|
|
|
date_to: Optional[date] = None,
|
|
|
|
|
status: Optional[str] = None,
|
|
|
|
|
limit: int = 100
|
|
|
|
|
) -> Optional[List[Dict[str, Any]]]:
|
|
|
|
|
"""
|
|
|
|
|
Get purchase orders for a specific supplier
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tenant_id: Tenant ID
|
|
|
|
|
supplier_id: Supplier ID to filter by
|
|
|
|
|
date_from: Start date for filtering
|
|
|
|
|
date_to: End date for filtering
|
|
|
|
|
status: Status filter (e.g., 'approved', 'delivered')
|
|
|
|
|
limit: Maximum number of results
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
List of purchase orders with items
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
params = {
|
|
|
|
|
"supplier_id": supplier_id,
|
|
|
|
|
"limit": limit
|
|
|
|
|
}
|
|
|
|
|
if date_from:
|
|
|
|
|
params["date_from"] = date_from.isoformat()
|
|
|
|
|
if date_to:
|
|
|
|
|
params["date_to"] = date_to.isoformat()
|
|
|
|
|
if status:
|
|
|
|
|
params["status"] = status
|
|
|
|
|
|
|
|
|
|
response = await self.get(
|
|
|
|
|
"procurement/purchase-orders",
|
|
|
|
|
params=params,
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved purchase orders by supplier",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
supplier_id=supplier_id,
|
|
|
|
|
count=len(response))
|
|
|
|
|
return response if response else []
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("Error getting purchase orders by supplier",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
supplier_id=supplier_id,
|
|
|
|
|
error=str(e))
|
|
|
|
|
return []
|
|
|
|
|
|
2025-10-30 21:08:07 +01:00
|
|
|
# ================================================================
|
2025-11-30 09:12:40 +01:00
|
|
|
# INTERNAL TRANSFER ENDPOINTS (NEW FOR ENTERPRISE TIER)
|
2025-10-30 21:08:07 +01:00
|
|
|
# ================================================================
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def create_internal_purchase_order(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
2025-11-30 09:12:40 +01:00
|
|
|
parent_tenant_id: str,
|
|
|
|
|
child_tenant_id: str,
|
|
|
|
|
items: List[Dict[str, Any]],
|
|
|
|
|
delivery_date: date,
|
|
|
|
|
notes: Optional[str] = None
|
2025-10-30 21:08:07 +01:00
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Create an internal purchase order from parent to child tenant
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
2025-11-30 09:12:40 +01:00
|
|
|
parent_tenant_id: Parent tenant ID (supplier)
|
|
|
|
|
child_tenant_id: Child tenant ID (buyer)
|
|
|
|
|
items: List of items with product_id, quantity, unit_of_measure
|
|
|
|
|
delivery_date: When child needs delivery
|
|
|
|
|
notes: Optional notes for the transfer
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
Created internal purchase order
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.post(
|
|
|
|
|
"procurement/internal-transfers",
|
|
|
|
|
data={
|
|
|
|
|
"destination_tenant_id": child_tenant_id,
|
|
|
|
|
"items": items,
|
|
|
|
|
"delivery_date": delivery_date.isoformat(),
|
|
|
|
|
"notes": notes
|
|
|
|
|
},
|
|
|
|
|
tenant_id=parent_tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Created internal purchase order",
|
|
|
|
|
parent_tenant_id=parent_tenant_id,
|
|
|
|
|
child_tenant_id=child_tenant_id,
|
|
|
|
|
po_number=response.get("po_number"))
|
2025-10-30 21:08:07 +01:00
|
|
|
return response
|
|
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error creating internal purchase order",
|
|
|
|
|
parent_tenant_id=parent_tenant_id,
|
|
|
|
|
child_tenant_id=child_tenant_id,
|
|
|
|
|
error=str(e))
|
2025-10-30 21:08:07 +01:00
|
|
|
return None
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_approved_internal_purchase_orders(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
2025-11-30 09:12:40 +01:00
|
|
|
parent_tenant_id: str,
|
|
|
|
|
target_date: Optional[date] = None,
|
|
|
|
|
status: Optional[str] = "approved"
|
2025-10-30 21:08:07 +01:00
|
|
|
) -> Optional[List[Dict[str, Any]]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get approved internal purchase orders for parent tenant
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
2025-11-30 09:12:40 +01:00
|
|
|
parent_tenant_id: Parent tenant ID
|
|
|
|
|
target_date: Optional target date to filter
|
|
|
|
|
status: Status filter (default: approved)
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
List of approved internal purchase orders
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
params = {"status": status}
|
|
|
|
|
if target_date:
|
|
|
|
|
params["target_date"] = target_date.isoformat()
|
|
|
|
|
|
|
|
|
|
response = await self.get(
|
|
|
|
|
"procurement/internal-transfers",
|
|
|
|
|
params=params,
|
|
|
|
|
tenant_id=parent_tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved internal purchase orders",
|
|
|
|
|
parent_tenant_id=parent_tenant_id,
|
|
|
|
|
count=len(response))
|
|
|
|
|
return response if response else []
|
2025-10-30 21:08:07 +01:00
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting internal purchase orders",
|
|
|
|
|
parent_tenant_id=parent_tenant_id,
|
|
|
|
|
error=str(e))
|
|
|
|
|
return []
|
2025-10-30 21:08:07 +01:00
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def approve_internal_purchase_order(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
2025-11-30 09:12:40 +01:00
|
|
|
parent_tenant_id: str,
|
|
|
|
|
po_id: str,
|
|
|
|
|
approved_by_user_id: str
|
2025-10-30 21:08:07 +01:00
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Approve an internal purchase order
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
2025-11-30 09:12:40 +01:00
|
|
|
parent_tenant_id: Parent tenant ID
|
|
|
|
|
po_id: Purchase order ID to approve
|
|
|
|
|
approved_by_user_id: User ID performing approval
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
Updated purchase order
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.post(
|
|
|
|
|
f"procurement/internal-transfers/{po_id}/approve",
|
|
|
|
|
data={
|
|
|
|
|
"approved_by_user_id": approved_by_user_id
|
|
|
|
|
},
|
|
|
|
|
tenant_id=parent_tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Approved internal purchase order",
|
|
|
|
|
parent_tenant_id=parent_tenant_id,
|
|
|
|
|
po_id=po_id)
|
2025-10-30 21:08:07 +01:00
|
|
|
return response
|
|
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error approving internal purchase order",
|
|
|
|
|
parent_tenant_id=parent_tenant_id,
|
|
|
|
|
po_id=po_id,
|
|
|
|
|
error=str(e))
|
2025-10-30 21:08:07 +01:00
|
|
|
return None
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_internal_transfer_history(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-11-30 09:12:40 +01:00
|
|
|
parent_tenant_id: Optional[str] = None,
|
|
|
|
|
child_tenant_id: Optional[str] = None,
|
|
|
|
|
start_date: Optional[date] = None,
|
|
|
|
|
end_date: Optional[date] = None
|
|
|
|
|
) -> Optional[List[Dict[str, Any]]]:
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get internal transfer history with optional filtering
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
2025-11-30 09:12:40 +01:00
|
|
|
tenant_id: Tenant ID (either parent or child)
|
|
|
|
|
parent_tenant_id: Filter by specific parent tenant
|
|
|
|
|
child_tenant_id: Filter by specific child tenant
|
|
|
|
|
start_date: Filter by start date
|
|
|
|
|
end_date: Filter by end date
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
List of internal transfer records
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
params = {}
|
|
|
|
|
if parent_tenant_id:
|
|
|
|
|
params["parent_tenant_id"] = parent_tenant_id
|
|
|
|
|
if child_tenant_id:
|
|
|
|
|
params["child_tenant_id"] = child_tenant_id
|
|
|
|
|
if start_date:
|
|
|
|
|
params["start_date"] = start_date.isoformat()
|
|
|
|
|
if end_date:
|
|
|
|
|
params["end_date"] = end_date.isoformat()
|
2025-10-30 21:08:07 +01:00
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.get(
|
|
|
|
|
"procurement/internal-transfers/history",
|
|
|
|
|
params=params,
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved internal transfer history",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
count=len(response))
|
|
|
|
|
return response if response else []
|
2025-10-30 21:08:07 +01:00
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting internal transfer history",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
error=str(e))
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
# ================================================================
|
|
|
|
|
# PROCUREMENT PLAN ENDPOINTS
|
|
|
|
|
# ================================================================
|
2025-10-30 21:08:07 +01:00
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_procurement_plan(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-11-30 09:12:40 +01:00
|
|
|
plan_id: str
|
|
|
|
|
) -> Optional[Dict[str, Any]]:
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get a specific procurement plan
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tenant_id: Tenant ID
|
2025-11-30 09:12:40 +01:00
|
|
|
plan_id: Procurement plan ID
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
Procurement plan details
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.get(
|
|
|
|
|
f"procurement/plans/{plan_id}",
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved procurement plan",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
plan_id=plan_id)
|
2025-10-30 21:08:07 +01:00
|
|
|
return response
|
|
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting procurement plan",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
plan_id=plan_id,
|
|
|
|
|
error=str(e))
|
2025-10-30 21:08:07 +01:00
|
|
|
return None
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_procurement_plans(
|
2025-10-30 21:08:07 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-11-30 09:12:40 +01:00
|
|
|
date_from: Optional[date] = None,
|
|
|
|
|
date_to: Optional[date] = None,
|
|
|
|
|
status: Optional[str] = None
|
|
|
|
|
) -> Optional[List[Dict[str, Any]]]:
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get procurement plans with optional filtering
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
tenant_id: Tenant ID
|
2025-11-30 09:12:40 +01:00
|
|
|
date_from: Start date for filtering
|
|
|
|
|
date_to: End date for filtering
|
|
|
|
|
status: Status filter
|
2025-10-30 21:08:07 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
List of procurement plan dictionaries
|
2025-10-30 21:08:07 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
params = {}
|
2025-11-30 09:12:40 +01:00
|
|
|
if date_from:
|
|
|
|
|
params["date_from"] = date_from.isoformat()
|
|
|
|
|
if date_to:
|
|
|
|
|
params["date_to"] = date_to.isoformat()
|
|
|
|
|
if status:
|
|
|
|
|
params["status"] = status
|
2025-10-30 21:08:07 +01:00
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.get(
|
|
|
|
|
"procurement/plans",
|
|
|
|
|
params=params,
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved procurement plans",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
count=len(response))
|
|
|
|
|
return response if response else []
|
2025-10-30 21:08:07 +01:00
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting procurement plans",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
error=str(e))
|
|
|
|
|
return []
|
2025-11-05 13:34:56 +01:00
|
|
|
|
|
|
|
|
# ================================================================
|
2025-11-30 09:12:40 +01:00
|
|
|
# SUPPLIER ENDPOINTS
|
2025-11-05 13:34:56 +01:00
|
|
|
# ================================================================
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_suppliers(
|
2025-11-05 13:34:56 +01:00
|
|
|
self,
|
2025-11-30 09:12:40 +01:00
|
|
|
tenant_id: str
|
|
|
|
|
) -> Optional[List[Dict[str, Any]]]:
|
2025-11-05 13:34:56 +01:00
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get suppliers for a tenant
|
2025-11-05 13:34:56 +01:00
|
|
|
|
|
|
|
|
Args:
|
2025-11-30 09:12:40 +01:00
|
|
|
tenant_id: Tenant ID
|
2025-11-05 13:34:56 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
List of supplier dictionaries
|
2025-11-05 13:34:56 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.get(
|
|
|
|
|
"procurement/suppliers",
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved suppliers",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
count=len(response))
|
|
|
|
|
return response if response else []
|
2025-11-05 13:34:56 +01:00
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting suppliers",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
error=str(e))
|
|
|
|
|
return []
|
2025-11-05 13:34:56 +01:00
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def get_supplier(
|
2025-11-05 13:34:56 +01:00
|
|
|
self,
|
|
|
|
|
tenant_id: str,
|
2025-11-30 09:12:40 +01:00
|
|
|
supplier_id: str
|
2025-11-05 13:34:56 +01:00
|
|
|
) -> Optional[Dict[str, Any]]:
|
|
|
|
|
"""
|
2025-11-30 09:12:40 +01:00
|
|
|
Get specific supplier details
|
2025-11-05 13:34:56 +01:00
|
|
|
|
|
|
|
|
Args:
|
2025-11-30 09:12:40 +01:00
|
|
|
tenant_id: Tenant ID
|
|
|
|
|
supplier_id: Supplier ID
|
2025-11-05 13:34:56 +01:00
|
|
|
|
|
|
|
|
Returns:
|
2025-11-30 09:12:40 +01:00
|
|
|
Supplier details
|
2025-11-05 13:34:56 +01:00
|
|
|
"""
|
|
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
response = await self.get(
|
|
|
|
|
f"procurement/suppliers/{supplier_id}",
|
|
|
|
|
tenant_id=tenant_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
logger.info("Retrieved supplier details",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
supplier_id=supplier_id)
|
|
|
|
|
return response
|
2025-11-05 13:34:56 +01:00
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Error getting supplier details",
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
supplier_id=supplier_id,
|
|
|
|
|
error=str(e))
|
2025-11-05 13:34:56 +01:00
|
|
|
return None
|
2025-11-07 22:12:21 +00:00
|
|
|
|
|
|
|
|
# ================================================================
|
2025-11-30 09:12:40 +01:00
|
|
|
# UTILITIES
|
2025-11-07 22:12:21 +00:00
|
|
|
# ================================================================
|
|
|
|
|
|
2025-11-30 09:12:40 +01:00
|
|
|
async def health_check(self) -> bool:
|
|
|
|
|
"""Check if procurement service is healthy"""
|
2025-11-07 22:12:21 +00:00
|
|
|
try:
|
2025-11-30 09:12:40 +01:00
|
|
|
# Use base health check method
|
|
|
|
|
response = await self.get("health")
|
|
|
|
|
return response is not None
|
2025-11-07 22:12:21 +00:00
|
|
|
except Exception as e:
|
2025-11-30 09:12:40 +01:00
|
|
|
logger.error("Procurement service health check failed", error=str(e))
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Factory function for dependency injection
|
|
|
|
|
def create_procurement_client(config: BaseServiceSettings, service_name: str = "unknown") -> ProcurementServiceClient:
|
|
|
|
|
"""Create procurement service client instance"""
|
|
|
|
|
return ProcurementServiceClient(config, service_name)
|