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>
This commit is contained in:
Luis Ernesto Portillo Zaldivar 2025-07-14 20:44:43 -06:00
parent dde56b907e
commit d240ba5de1
5 changed files with 34 additions and 2 deletions

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from odoo import models, fields
from odoo import models, fields, api
class StockLot(models.Model):
_inherit = 'stock.lot'
@ -26,7 +26,14 @@ class StockLot(models.Model):
('swab', 'Swab'),
('urine', 'Urine Container'),
('other', 'Other')
], string='Container Type')
], 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',
@ -57,3 +64,28 @@ class StockLot(models.Model):
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'