bemade-addons/helpdesk_sale_order_ai/models/sale_order.py
mathis c5904a51d7 [REF] Replace openwebui_connector with centralized openwebui_base module
This commit represents a significant architectural improvement:

- Completely replaced openwebui_connector with a more robust openwebui_base module
- Centralized OpenWebUI API integration for better maintainability
- Redesigned helpdesk_sale_order_ai to use the new openwebui_base module
- Added support for multiple OpenWebUI providers and models
- Improved error handling and response parsing
- Added proper template management with openwebui_prompt_template
- Fixed KeyError issues with safe template substitution
- Streamlined API client initialization and usage
2025-07-28 13:27:29 -04:00

85 lines
3.3 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api
import logging
_logger = logging.getLogger(__name__)
class SaleOrder(models.Model):
_inherit = 'sale.order'
# Add missing_product_count field to avoid view errors
# This is needed because some views expect this field
missing_product_count = fields.Integer(
string='Missing Products Count',
default=0,
help='Number of products that could not be found in the database'
)
# Add has_missing_products field to avoid view errors
has_missing_products = fields.Boolean(
string='Has Missing Products',
default=False,
help='Whether this sale order has products that could not be found in the database'
)
# Add ticket_id field to link sale orders to helpdesk tickets
ticket_id = fields.Many2one(
'helpdesk.ticket',
string='Helpdesk Ticket',
readonly=True,
copy=False,
help='Helpdesk ticket from which this sale order was created'
)
def action_confirm(self):
"""Override to delete missing products message when sale order is confirmed"""
# Call the original method first
result = super(SaleOrder, self).action_confirm()
# Delete missing products message if exists
if self.has_missing_products:
self._delete_missing_products_message()
# Reset missing products flags
self.write({
'missing_product_count': 0,
'has_missing_products': False
})
return result
def _delete_missing_products_message(self):
"""Delete the missing products message from the chatter"""
# Find the message with the unique identifier
domain = [
('res_id', '=', self.id),
('model', '=', 'sale.order'),
('body', 'ilike', '%<!-- MISSING_PRODUCTS_MESSAGE -->%'),
]
messages = self.env['mail.message'].sudo().search(domain)
if messages:
_logger.info(f"Found {len(messages)} missing products message(s) for sale order {self.id} with IDs: {messages.ids}")
try:
# Make sure we're actually deleting the message
message_ids = messages.ids # Store IDs before deletion
messages.sudo().unlink()
_logger.info(f"Successfully deleted missing products message(s) with IDs: {message_ids} for sale order {self.id}")
except Exception as e:
_logger.error(f"Error deleting missing products message: {e}")
else:
_logger.warning(f"No missing products message found for sale order {self.id} - check HTML comment marker")
# Debug: Try a broader search to see if there are any messages at all
all_messages = self.env['mail.message'].sudo().search([
('res_id', '=', self.id),
('model', '=', 'sale.order'),
], limit=5)
if all_messages:
_logger.info(f"Found {len(all_messages)} recent messages for this sale order. First message body sample: {all_messages[0].body[:100] if all_messages[0].body else 'Empty'}...")
else:
_logger.info(f"No messages found at all for sale order {self.id}")