clinical_laboratory/lims_management/models/sale_order.py
Luis Ernesto Portillo Zaldivar 4be56fc9f7 fix(#32): Spanish translations and workflow fixes
- Fixed missing action_collect method for pending_collection state
- Updated all model field labels to Spanish
- Updated view labels and strings to Spanish
- Fixed readonly conditions for pending_collection state
- Added barcode and new fields to stock.lot views
- Updated sale.order embedded view with correct button
- Added 5-minute timeout note to CLAUDE.md
- Removed problematic demo sale.order XML records
- Updated test script location guidance in CLAUDE.md
- Marked all acceptance criteria as completed in plan
2025-07-14 23:46:31 -06:00

166 lines
6.2 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api, _
from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)
class SaleOrder(models.Model):
_inherit = 'sale.order'
is_lab_request = fields.Boolean(
string="Es Orden de Laboratorio",
default=False,
copy=False,
help="Campo técnico para identificar si la orden de venta es una solicitud de laboratorio."
)
doctor_id = fields.Many2one(
'res.partner',
string="Médico Referente",
domain="[('is_doctor', '=', True)]",
help="El médico que refirió al paciente para esta solicitud de laboratorio."
)
generated_sample_ids = fields.Many2many(
'stock.lot',
'sale_order_stock_lot_rel',
'order_id',
'lot_id',
string='Muestras Generadas',
domain="[('is_lab_sample', '=', True)]",
readonly=True,
help="Muestras de laboratorio generadas automáticamente cuando se confirmó esta orden"
)
def action_confirm(self):
"""Override to generate laboratory samples automatically"""
res = super(SaleOrder, self).action_confirm()
# Generate samples only for laboratory requests
for order in self.filtered('is_lab_request'):
try:
order._generate_lab_samples()
except Exception as e:
_logger.error(f"Error generating samples for order {order.name}: {str(e)}")
# Continue with order confirmation even if sample generation fails
# But notify the user
order.message_post(
body=_("Error al generar muestras automáticamente: %s. "
"Por favor, genere las muestras manualmente.") % str(e),
message_type='notification'
)
return res
def _generate_lab_samples(self):
"""Generate laboratory samples based on the analyses in the order"""
self.ensure_one()
_logger.info(f"Generating laboratory samples for order {self.name}")
# Group analyses by sample type
sample_groups = self._group_analyses_by_sample_type()
if not sample_groups:
_logger.warning(f"No analyses with sample types found in order {self.name}")
return
# Create samples for each group
created_samples = self.env['stock.lot']
for sample_type_id, group_data in sample_groups.items():
sample = self._create_sample_for_group(group_data)
if sample:
created_samples |= sample
# Link created samples to the order
if created_samples:
self.generated_sample_ids = [(6, 0, created_samples.ids)]
_logger.info(f"Created {len(created_samples)} samples for order {self.name}")
# Post message with created samples
sample_list = "<ul>"
for sample in created_samples:
sample_list += f"<li>{sample.name} - {sample.sample_type_product_id.name}</li>"
sample_list += "</ul>"
self.message_post(
body=_("Muestras generadas automáticamente: %s") % sample_list,
message_type='notification'
)
def _group_analyses_by_sample_type(self):
"""Group order lines by required sample type"""
groups = {}
for line in self.order_line:
product = line.product_id
# Skip non-analysis products
if not product.is_analysis:
continue
# Check if analysis has a required sample type
if not product.required_sample_type_id:
_logger.warning(
f"Analysis {product.name} has no required sample type defined"
)
# Post warning message
self.message_post(
body=_("Advertencia: El análisis '%s' no tiene tipo de muestra definido") % product.name,
message_type='notification'
)
continue
sample_type = product.required_sample_type_id
# Initialize group if not exists
if sample_type.id not in groups:
groups[sample_type.id] = {
'sample_type': sample_type,
'lines': [],
'total_volume': 0.0,
'analyses': []
}
# Add line to group
groups[sample_type.id]['lines'].append(line)
groups[sample_type.id]['analyses'].append(product.name)
groups[sample_type.id]['total_volume'] += (product.sample_volume_ml or 0.0) * line.product_uom_qty
return groups
def _create_sample_for_group(self, group_data):
"""Create a single sample for a group of analyses"""
try:
sample_type = group_data['sample_type']
# Prepare sample values
vals = {
'product_id': sample_type.product_variant_id.id,
'patient_id': self.partner_id.id,
'doctor_id': self.doctor_id.id if self.doctor_id else False,
'origin': self.name,
'sample_type_product_id': sample_type.id,
'volume_ml': group_data['total_volume'],
'is_lab_sample': True,
'state': 'pending_collection',
'analysis_names': ', '.join(group_data['analyses'][:3]) +
('...' if len(group_data['analyses']) > 3 else '')
}
# Create the sample
sample = self.env['stock.lot'].create(vals)
_logger.info(
f"Created sample {sample.name} for {len(group_data['analyses'])} analyses"
)
return sample
except Exception as e:
_logger.error(f"Error creating sample: {str(e)}")
raise UserError(
_("Error al crear muestra para %s: %s") % (sample_type.name, str(e))
)