clinical_laboratory/lims_management/models/stock_lot.py
Luis Ernesto Portillo Zaldivar d240ba5de1 feat(#44): Add sample_type_product_id field to StockLot model
- Added sample_type_product_id Many2one field to reference sample type products
- Kept container_type field for backward compatibility (marked as legacy)
- Added @api.onchange method to synchronize fields
- Added get_container_name() method to retrieve container name from either field

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-14 20:44:43 -06:00

92 lines
3.0 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api
class StockLot(models.Model):
_inherit = 'stock.lot'
is_lab_sample = fields.Boolean(string='Is a Laboratory Sample')
patient_id = fields.Many2one(
'res.partner',
string='Patient',
domain="[('is_patient', '=', True)]"
)
request_id = fields.Many2one(
'sale.order',
string='Lab Request',
domain="[('is_lab_request', '=', True)]"
)
collection_date = fields.Datetime(string='Collection Date')
container_type = fields.Selection([
('serum_tube', 'Serum Tube'),
('edta_tube', 'EDTA Tube'),
('swab', 'Swab'),
('urine', 'Urine Container'),
('other', 'Other')
], string='Container Type (Legacy)', help='Deprecated field, use sample_type_product_id instead')
sample_type_product_id = fields.Many2one(
'product.template',
string='Tipo de Muestra',
domain="[('is_sample_type', '=', True)]",
help="Producto que representa el tipo de contenedor/muestra"
)
collector_id = fields.Many2one(
'res.users',
string='Collected by',
default=lambda self: self.env.user
)
state = fields.Selection([
('collected', 'Recolectada'),
('received', 'Recibida en Laboratorio'),
('in_process', 'En Proceso'),
('analyzed', 'Analizada'),
('stored', 'Almacenada'),
('disposed', 'Desechada')
], string='Estado', default='collected', tracking=True)
def action_receive(self):
self.write({'state': 'received'})
def action_start_analysis(self):
self.write({'state': 'in_process'})
def action_complete_analysis(self):
self.write({'state': 'analyzed'})
def action_store(self):
self.write({'state': 'stored'})
def action_dispose(self):
self.write({'state': 'disposed'})
@api.onchange('sample_type_product_id')
def _onchange_sample_type_product_id(self):
"""Synchronize container_type when sample_type_product_id changes"""
if self.sample_type_product_id:
# Try to map product name to legacy container type
product_name = self.sample_type_product_id.name.lower()
if 'suero' in product_name or 'serum' in product_name:
self.container_type = 'serum_tube'
elif 'edta' in product_name:
self.container_type = 'edta_tube'
elif 'hisopo' in product_name or 'swab' in product_name:
self.container_type = 'swab'
elif 'orina' in product_name or 'urine' in product_name:
self.container_type = 'urine'
else:
self.container_type = 'other'
def get_container_name(self):
"""Get container name from product or legacy field"""
if self.sample_type_product_id:
return self.sample_type_product_id.name
elif self.container_type:
return dict(self._fields['container_type'].selection).get(self.container_type)
return 'Unknown'