# -*- coding: utf-8 -*- from odoo import models, fields, api from odoo.exceptions import ValidationError class SampleRejectionWizard(models.TransientModel): _name = 'lims.sample.rejection.wizard' _description = 'Wizard para Rechazo de Muestras' sample_id = fields.Many2one( 'stock.lot', string='Muestra', required=True, readonly=True, domain=[('is_lab_sample', '=', True)] ) rejection_reason_id = fields.Many2one( 'lims.rejection.reason', string='Motivo de Rechazo', required=True, domain=[('active', '=', True)] ) rejection_notes = fields.Text( string='Notas Adicionales', help="Información adicional sobre el rechazo" ) requires_new_sample = fields.Boolean( string='Requiere Nueva Muestra', related='rejection_reason_id.requires_new_sample', readonly=True ) create_new_sample = fields.Boolean( string='Crear Nueva Solicitud', help="Crear automáticamente una nueva solicitud de muestra" ) @api.model def default_get(self, fields): res = super(SampleRejectionWizard, self).default_get(fields) active_id = self.env.context.get('active_id') if active_id: sample = self.env['stock.lot'].browse(active_id) res['sample_id'] = sample.id return res @api.onchange('rejection_reason_id') def _onchange_rejection_reason_id(self): if self.rejection_reason_id and self.rejection_reason_id.requires_new_sample: self.create_new_sample = True def action_reject_sample(self): """Reject the sample with the provided reason""" self.ensure_one() if not self.sample_id: raise ValidationError('No se ha seleccionado ninguna muestra') if self.sample_id.state == 'completed': raise ValidationError('No se puede rechazar una muestra ya completada') # Update sample with rejection information self.sample_id.write({ 'rejection_reason_id': self.rejection_reason_id.id, 'rejection_notes': self.rejection_notes }) # Call the rejection method on the sample with explicit resample creation preference self.sample_id.action_reject(create_resample=self.create_new_sample) return {'type': 'ir.actions.act_window_close'} def _create_new_sample_request(self): """Create a new sample request based on the rejected one""" original_order = self.sample_id.request_id # Create a note in the original order original_order.message_post( body=f'Se solicitará una nueva muestra debido al rechazo. Motivo: {self.rejection_reason_id.name}', subject='Nueva Muestra Solicitada', message_type='notification' ) # Here you could implement logic to create a new sale.order # or a specific request for a new sample # For now, we'll just add a note return True