-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode_based_queries.py
More file actions
436 lines (366 loc) · 17.4 KB
/
code_based_queries.py
File metadata and controls
436 lines (366 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python3
"""
Code-Based Query Library
This module contains proven, working code patterns for common Odoo queries.
These are adapted from your existing CRUD Autogen Agent patterns.
"""
import re
import time
import logging
from typing import Dict, List, Any, Optional
from dynamic_query_engine import QueryResult
logger = logging.getLogger(__name__)
class CodeBasedQueryLibrary:
"""Library of proven, working query functions"""
def __init__(self, odoo_client=None):
self.odoo_client = odoo_client
self.query_functions = {}
self._register_queries()
def _register_queries(self):
"""Register proven working code patterns"""
# Pattern 1: Finding Open invoices for a Customer
self.query_functions['customer_open_invoices'] = {
'patterns': [
r'(?:open|unpaid|outstanding)\s+(?:invoices?|bills?)\s+for\s+(?:customer|client|partner)\s+(.+)',
r'show.*(?:invoices?|bills?).*for.*(.+)',
r'(?:invoices?|bills?).*(?:customer|client).*(.+)',
],
'function': self.get_customer_open_invoices,
'description': 'Get open/unpaid invoices for a specific customer'
}
# Pattern 2: Finding Warehouses for a product
self.query_functions['product_warehouses'] = {
'patterns': [
r'(?:warehouses?|locations?)\s+for\s+(.+)',
r'where.*(?:is|located).*(.+)',
r'(?:find|show).*(?:warehouses?|locations?).*(.+)',
],
'function': self.get_product_warehouses,
'description': 'Find warehouses/locations where a product is stored'
}
# Pattern 3: Stock quantity for a product in warehouse
self.query_functions['product_stock_quantity'] = {
'patterns': [
r'(?:stock|inventory|quantity)\s+(?:of|for)\s+(.+)',
r'how\s+(?:much|many).*(.+).*(?:in\s+stock|available)',
r'(?:quantity|stock\s+level).*(.+)',
],
'function': self.get_product_stock_quantity,
'description': 'Get stock quantity for a product'
}
# Additional common patterns
self.query_functions['customer_all_invoices'] = {
'patterns': [
r'(?:all\s+)?(?:invoices?|bills?)\s+for\s+(?:customer|client|partner)\s+(.+)',
r'(?:customer|client)\s+(.+)\s+(?:invoices?|bills?)',
],
'function': self.get_customer_all_invoices,
'description': 'Get all invoices for a specific customer'
}
self.query_functions['product_details'] = {
'patterns': [
r'(?:details|info|information)\s+(?:about|for)\s+(?:product\s+)?(.+)',
r'(?:show|find)\s+(?:product\s+)?(.+)(?:\s+details)?',
],
'function': self.get_product_details,
'description': 'Get detailed information about a product'
}
def match_query(self, user_query: str) -> Optional[Dict[str, Any]]:
"""Try to match user query against registered patterns"""
query_lower = user_query.lower().strip()
for query_name, config in self.query_functions.items():
for pattern in config['patterns']:
match = re.search(pattern, query_lower, re.IGNORECASE)
if match:
# Extract parameters from the match
params = {'entity_name': match.group(1).strip()}
logger.info(f"Matched query '{user_query}' to pattern '{query_name}'")
return {
'function': config['function'],
'params': params,
'description': config['description'],
'pattern_name': query_name
}
return None
def execute_matched_query(self, match_result: Dict[str, Any]) -> QueryResult:
"""Execute a matched query function"""
try:
start_time = time.time()
function = match_result['function']
params = match_result['params']
# Execute the function
result_data = function(**params)
execution_time = time.time() - start_time
if isinstance(result_data, dict) and 'error' in result_data:
return QueryResult(
success=False,
error=result_data['error'],
method='code_based',
execution_time=execution_time
)
# Determine count
count = 0
if isinstance(result_data, dict):
if 'data' in result_data:
count = len(result_data['data']) if isinstance(result_data['data'], list) else 1
data = result_data
else:
count = 1
data = result_data
elif isinstance(result_data, list):
count = len(result_data)
data = {'data': result_data, 'count': count}
else:
count = 1
data = result_data
return QueryResult(
success=True,
data=data,
count=count,
method='code_based',
execution_time=execution_time
)
except Exception as e:
logger.error(f"Error executing code-based query: {str(e)}")
return QueryResult(
success=False,
error=f"Query execution failed: {str(e)}",
method='code_based'
)
# ==================== PROVEN QUERY FUNCTIONS ====================
# These are adapted from your working CRUD Autogen Agent code
def get_customer_open_invoices(self, entity_name: str) -> Dict[str, Any]:
"""
Get open/unpaid invoices for a customer
Adapted from your working code
"""
try:
if not self.odoo_client:
return {'error': 'Odoo client not available'}
# Find the partner ID for the customer
partner_search = self.odoo_client.env['res.partner'].search([
('name', 'ilike', entity_name)
])
if not partner_search:
return {'error': f'Customer "{entity_name}" not found'}
partner_id = partner_search[0]
# Search for open invoices for the partner (your proven logic)
invoices = self.odoo_client.env['account.move'].search_read([
('partner_id', '=', partner_id),
('move_type', '=', 'out_invoice'),
('payment_state', 'in', ['not_paid', 'partial'])
], [
'name', 'invoice_date', 'amount_total', 'currency_id', 'payment_state', 'state'
])
# Format the results
formatted_invoices = []
for invoice in invoices:
formatted_invoices.append({
'invoice_number': invoice['name'],
'invoice_date': invoice['invoice_date'],
'total_amount': invoice['amount_total'],
'currency': invoice['currency_id'][1] if invoice['currency_id'] else 'USD',
'payment_state': invoice['payment_state'],
'state': invoice['state']
})
return {
'success': True,
'customer_name': entity_name,
'data': formatted_invoices,
'count': len(formatted_invoices),
'total_amount': sum(inv['total_amount'] for inv in formatted_invoices)
}
except Exception as e:
logger.error(f"Error getting customer open invoices: {str(e)}")
return {'error': f'Failed to retrieve invoices: {str(e)}'}
def get_product_warehouses(self, entity_name: str) -> Dict[str, Any]:
"""
Find warehouses where a product is located
Adapted from your working code
"""
try:
if not self.odoo_client:
return {'error': 'Odoo client not available'}
# Find the product ID
product_search = self.odoo_client.env['product.product'].search([
('name', 'ilike', entity_name)
])
if not product_search:
return {'error': f'Product "{entity_name}" not found'}
product_id = product_search[0]
# Find stock quant records for the product (your proven logic)
quants = self.odoo_client.env['stock.quant'].search_read([
('product_id', '=', product_id),
('quantity', '>', 0) # Only locations with stock
], ['location_id', 'quantity'])
if not quants:
return {
'success': True,
'product_name': entity_name,
'data': [],
'message': f'Product "{entity_name}" is not available in any warehouse'
}
# Get warehouse information for each location
warehouses = {}
for quant in quants:
location_id = quant['location_id'][0]
# Get the warehouse for the location
location = self.odoo_client.env['stock.location'].browse(location_id)
if location.warehouse_id:
warehouse_name = location.warehouse_id.name
if warehouse_name not in warehouses:
warehouses[warehouse_name] = {
'warehouse_name': warehouse_name,
'total_quantity': 0,
'locations': []
}
warehouses[warehouse_name]['total_quantity'] += quant['quantity']
warehouses[warehouse_name]['locations'].append({
'location_name': quant['location_id'][1],
'quantity': quant['quantity']
})
warehouse_list = list(warehouses.values())
return {
'success': True,
'product_name': entity_name,
'data': warehouse_list,
'count': len(warehouse_list),
'total_quantity': sum(w['total_quantity'] for w in warehouse_list)
}
except Exception as e:
logger.error(f"Error getting product warehouses: {str(e)}")
return {'error': f'Failed to retrieve warehouse information: {str(e)}'}
def get_product_stock_quantity(self, entity_name: str) -> Dict[str, Any]:
"""
Get stock quantity for a product across all locations
"""
try:
if not self.odoo_client:
return {'error': 'Odoo client not available'}
# Find the product ID
product_search = self.odoo_client.env['product.product'].search([
('name', 'ilike', entity_name)
])
if not product_search:
return {'error': f'Product "{entity_name}" not found'}
product_id = product_search[0]
# Get stock quantities
quants = self.odoo_client.env['stock.quant'].search_read([
('product_id', '=', product_id)
], ['location_id', 'quantity', 'reserved_quantity'])
total_quantity = 0
available_quantity = 0
locations = []
for quant in quants:
total_quantity += quant['quantity']
available_quantity += (quant['quantity'] - quant['reserved_quantity'])
if quant['quantity'] > 0: # Only show locations with stock
locations.append({
'location': quant['location_id'][1],
'quantity': quant['quantity'],
'reserved': quant['reserved_quantity'],
'available': quant['quantity'] - quant['reserved_quantity']
})
return {
'success': True,
'product_name': entity_name,
'total_quantity': total_quantity,
'available_quantity': available_quantity,
'reserved_quantity': total_quantity - available_quantity,
'locations': locations,
'count': len(locations)
}
except Exception as e:
logger.error(f"Error getting product stock: {str(e)}")
return {'error': f'Failed to retrieve stock information: {str(e)}'}
def get_customer_all_invoices(self, entity_name: str) -> Dict[str, Any]:
"""Get all invoices (paid and unpaid) for a customer"""
try:
if not self.odoo_client:
return {'error': 'Odoo client not available'}
# Find the partner ID
partner_search = self.odoo_client.env['res.partner'].search([
('name', 'ilike', entity_name)
])
if not partner_search:
return {'error': f'Customer "{entity_name}" not found'}
partner_id = partner_search[0]
# Get all customer invoices
invoices = self.odoo_client.env['account.move'].search_read([
('partner_id', '=', partner_id),
('move_type', '=', 'out_invoice')
], [
'name', 'invoice_date', 'amount_total', 'currency_id', 'payment_state', 'state'
], order='invoice_date desc')
# Categorize invoices
paid_invoices = []
unpaid_invoices = []
total_paid = 0
total_unpaid = 0
for invoice in invoices:
invoice_data = {
'invoice_number': invoice['name'],
'invoice_date': invoice['invoice_date'],
'total_amount': invoice['amount_total'],
'currency': invoice['currency_id'][1] if invoice['currency_id'] else 'USD',
'payment_state': invoice['payment_state'],
'state': invoice['state']
}
if invoice['payment_state'] == 'paid':
paid_invoices.append(invoice_data)
total_paid += invoice['amount_total']
else:
unpaid_invoices.append(invoice_data)
total_unpaid += invoice['amount_total']
return {
'success': True,
'customer_name': entity_name,
'all_invoices': invoices,
'paid_invoices': paid_invoices,
'unpaid_invoices': unpaid_invoices,
'total_invoices': len(invoices),
'total_paid': total_paid,
'total_unpaid': total_unpaid,
'count': len(invoices)
}
except Exception as e:
logger.error(f"Error getting customer all invoices: {str(e)}")
return {'error': f'Failed to retrieve invoices: {str(e)}'}
def get_product_details(self, entity_name: str) -> Dict[str, Any]:
"""Get detailed information about a product"""
try:
if not self.odoo_client:
return {'error': 'Odoo client not available'}
# Find the product
product_search = self.odoo_client.env['product.template'].search([
('name', 'ilike', entity_name)
])
if not product_search:
return {'error': f'Product "{entity_name}" not found'}
product = self.odoo_client.env['product.template'].browse(product_search[0])
# Get stock information
variants = product.product_variant_ids
total_stock = sum(variant.qty_available for variant in variants)
product_data = {
'name': product.name,
'default_code': product.default_code,
'list_price': product.list_price,
'cost_price': product.standard_price,
'category': product.categ_id.name if product.categ_id else 'No Category',
'sale_ok': product.sale_ok,
'purchase_ok': product.purchase_ok,
'type': product.type,
'uom': product.uom_id.name if product.uom_id else 'Unit',
'description': product.description_sale or 'No description',
'qty_available': total_stock,
'variants_count': len(variants)
}
return {
'success': True,
'product_name': entity_name,
'data': product_data,
'count': 1
}
except Exception as e:
logger.error(f"Error getting product details: {str(e)}")
return {'error': f'Failed to retrieve product details: {str(e)}'}