diff --git a/lims_management/models/__pycache__/stock_lot.cpython-312.pyc b/lims_management/models/__pycache__/stock_lot.cpython-312.pyc index 94794ea..120db73 100644 Binary files a/lims_management/models/__pycache__/stock_lot.cpython-312.pyc and b/lims_management/models/__pycache__/stock_lot.cpython-312.pyc differ diff --git a/lims_management/models/stock_lot.py b/lims_management/models/stock_lot.py index 0cd362a..dfe9ef6 100644 --- a/lims_management/models/stock_lot.py +++ b/lims_management/models/stock_lot.py @@ -456,13 +456,18 @@ class StockLot(models.Model): """Create a new sample as a resample of the current one""" self.ensure_one() - # Check if there's already an active resample - active_resamples = self.child_sample_ids.filtered( + # Determine the parent sample for the new resample + # If current sample is already a resample, use its parent + # Otherwise, use the current sample as parent + parent_for_resample = self.parent_sample_id if self.parent_sample_id else self + + # Check if there's already an active resample for the parent + active_resamples = parent_for_resample.child_sample_ids.filtered( lambda s: s.state not in ['rejected', 'cancelled', 'disposed'] ) if active_resamples: - raise UserError(_('Esta muestra ya tiene una re-muestra activa (%s). No se puede crear otra hasta que se procese o rechace la existente.') % - ', '.join(active_resamples.mapped('name'))) + raise UserError(_('La muestra %s ya tiene una re-muestra activa (%s). No se puede crear otra hasta que se procese o rechace la existente.') % + (parent_for_resample.name, ', '.join(active_resamples.mapped('name')))) # Get configuration IrConfig = self.env['ir.config_parameter'].sudo() @@ -472,7 +477,7 @@ class StockLot(models.Model): max_attempts = int(IrConfig.get_param('lims_management.max_resample_attempts', '3')) # Find the original sample (root of the resample chain) - original_sample = self + original_sample = parent_for_resample while original_sample.parent_sample_id: original_sample = original_sample.parent_sample_id @@ -483,12 +488,12 @@ class StockLot(models.Model): if max_attempts > 0 and total_resamples >= max_attempts: raise UserError(_('Se ha alcanzado el número máximo de re-muestreos (%d) para esta cadena de muestras.') % max_attempts) - # Calculate resample number for naming - resample_number = self.resample_count + 1 + # Calculate resample number for naming (based on parent's resample count) + resample_number = len(parent_for_resample.child_sample_ids) + 1 # Prepare values for new sample vals = { - 'name': f"{prefix}{self.name}-{resample_number}", + 'name': f"{prefix}{parent_for_resample.name}-{resample_number}", 'product_id': self.product_id.id, 'patient_id': self.patient_id.id, 'doctor_id': self.doctor_id.id, @@ -498,23 +503,31 @@ class StockLot(models.Model): 'is_lab_sample': True, 'state': initial_state, 'analysis_names': self.analysis_names, - 'parent_sample_id': self.id, + 'parent_sample_id': parent_for_resample.id, # Always use the determined parent 'request_id': self.request_id.id if self.request_id else False, } # Create the resample resample = self.create(vals) - # Post message in both samples + # Post message in all relevant samples self.message_post( body=_('Re-muestra creada: %s') % resample.name, subject='Re-muestreo', message_type='notification' ) + if self != parent_for_resample: + # If we're creating from a resample, also notify the parent + parent_for_resample.message_post( + body=_('Nueva re-muestra creada: %s (debido al rechazo de %s)') % (resample.name, self.name), + subject='Re-muestreo', + message_type='notification' + ) + resample.message_post( - body=_('Esta es una re-muestra de: %s
Motivo: %s') % - (self.name, self.rejection_reason_id.name if self.rejection_reason_id else 'No especificado'), + body=_('Esta es una re-muestra de: %s
Creada debido al rechazo de: %s
Motivo: %s') % + (parent_for_resample.name, self.name, self.rejection_reason_id.name if self.rejection_reason_id else 'No especificado'), subject='Re-muestra creada', message_type='notification' )