feat(#71): Mejorar script de creación de órdenes de laboratorio - 2 órdenes por paciente con manejo de errores
This commit is contained in:
parent
1ff44b1654
commit
53eada8432
|
@ -113,35 +113,79 @@ def process_order_tests(env, order):
|
||||||
product_tmpl = test.product_id.product_tmpl_id
|
product_tmpl = test.product_id.product_tmpl_id
|
||||||
for param_link in product_tmpl.parameter_ids:
|
for param_link in product_tmpl.parameter_ids:
|
||||||
param = param_link.parameter_id
|
param = param_link.parameter_id
|
||||||
result = env['lims.result'].create({
|
|
||||||
|
# Prepare result data with values
|
||||||
|
result_data = {
|
||||||
'test_id': test.id,
|
'test_id': test.id,
|
||||||
'parameter_id': param.id,
|
'parameter_id': param.id,
|
||||||
})
|
}
|
||||||
# Immediately set a value to avoid validation errors
|
|
||||||
if param.value_type == 'numeric':
|
# Set value based on parameter type
|
||||||
# Generar valor que a veces esté fuera de rango
|
try:
|
||||||
if random.random() < 0.3: # 30% de valores críticos
|
if param.value_type == 'numeric':
|
||||||
# Obtener rangos normales del parámetro
|
# Generar valor que a veces esté fuera de rango
|
||||||
normal_min = param_link.normal_min if hasattr(param_link, 'normal_min') and param_link.normal_min else 10
|
if random.random() < 0.3: # 30% de valores críticos
|
||||||
normal_max = param_link.normal_max if hasattr(param_link, 'normal_max') and param_link.normal_max else 100
|
# Obtener rangos normales del parámetro
|
||||||
|
normal_min = param_link.normal_min if hasattr(param_link, 'normal_min') and param_link.normal_min else 10
|
||||||
# Decidir si será alto o bajo
|
normal_max = param_link.normal_max if hasattr(param_link, 'normal_max') and param_link.normal_max else 100
|
||||||
if random.random() < 0.5:
|
|
||||||
# Valor alto
|
# Decidir si será alto o bajo
|
||||||
value = round(random.uniform(normal_max * 1.2, normal_max * 1.5), 2)
|
if random.random() < 0.5:
|
||||||
|
# Valor alto
|
||||||
|
value = round(random.uniform(normal_max * 1.2, normal_max * 1.5), 2)
|
||||||
|
else:
|
||||||
|
# Valor bajo
|
||||||
|
value = round(random.uniform(normal_min * 0.5, normal_min * 0.8), 2)
|
||||||
|
result_data['value_numeric'] = value
|
||||||
else:
|
else:
|
||||||
# Valor bajo
|
result_data['value_numeric'] = 50.0
|
||||||
value = round(random.uniform(normal_min * 0.5, normal_min * 0.8), 2)
|
elif param.value_type == 'text':
|
||||||
result.value_numeric = value
|
# Handle different text parameters appropriately
|
||||||
else:
|
param_lower = param.name.lower()
|
||||||
result.value_numeric = 50.0
|
if 'cultivo' in param_lower:
|
||||||
elif param.value_type == 'text':
|
result_data['value_text'] = "No se observa crecimiento bacteriano"
|
||||||
result.value_text = "Normal"
|
elif 'observacion' in param_lower:
|
||||||
elif param.value_type == 'boolean':
|
result_data['value_text'] = "Sin observaciones particulares"
|
||||||
result.value_boolean = False
|
elif 'color' in param_lower:
|
||||||
elif param.value_type == 'selection' and param.selection_options:
|
result_data['value_text'] = "Amarillo claro"
|
||||||
options = param.selection_options.split(',')
|
elif 'aspecto' in param_lower:
|
||||||
result.value_selection = options[0].strip()
|
result_data['value_text'] = "Transparente"
|
||||||
|
elif 'olor' in param_lower:
|
||||||
|
result_data['value_text'] = "Sui generis"
|
||||||
|
else:
|
||||||
|
result_data['value_text'] = "Normal"
|
||||||
|
elif param.value_type == 'boolean':
|
||||||
|
result_data['value_boolean'] = False
|
||||||
|
elif param.value_type == 'selection':
|
||||||
|
if param.selection_values:
|
||||||
|
options = param.selection_values.split(',')
|
||||||
|
# For pregnancy tests, randomly assign positive/negative
|
||||||
|
if 'beta-hcg' in param.name.lower() or 'embarazo' in param.name.lower():
|
||||||
|
# 30% chance of positive for pregnant patients, 5% for others
|
||||||
|
is_positive = random.random() < (0.3 if hasattr(test, 'patient_id') and test.patient_id.is_pregnant else 0.05)
|
||||||
|
result_data['value_selection'] = 'Positivo' if is_positive else 'Negativo'
|
||||||
|
else:
|
||||||
|
result_data['value_selection'] = options[0].strip()
|
||||||
|
else:
|
||||||
|
# No selection values defined, use default
|
||||||
|
if 'embarazo' in param.name.lower():
|
||||||
|
result_data['value_selection'] = 'Negativo'
|
||||||
|
else:
|
||||||
|
result_data['value_selection'] = 'Normal'
|
||||||
|
except Exception as e:
|
||||||
|
print(f" - Error preparing value for parameter {param.name}: {str(e)}")
|
||||||
|
# Set a default value to avoid validation errors
|
||||||
|
if param.value_type == 'numeric':
|
||||||
|
result_data['value_numeric'] = 50.0
|
||||||
|
elif param.value_type == 'text':
|
||||||
|
result_data['value_text'] = "Pendiente"
|
||||||
|
elif param.value_type == 'boolean':
|
||||||
|
result_data['value_boolean'] = False
|
||||||
|
elif param.value_type == 'selection':
|
||||||
|
result_data['value_selection'] = "Normal"
|
||||||
|
|
||||||
|
# Create result with values
|
||||||
|
result = env['lims.result'].create(result_data)
|
||||||
print(f" - Created {len(product_tmpl.parameter_ids)} result fields")
|
print(f" - Created {len(product_tmpl.parameter_ids)} result fields")
|
||||||
|
|
||||||
# Evaluar resultados críticos y agregar notas
|
# Evaluar resultados críticos y agregar notas
|
||||||
|
@ -209,65 +253,123 @@ def create_lab_requests(cr):
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
# Get all available analysis products
|
||||||
# Get patients and doctors - using search instead of ref to be more robust
|
all_analyses = env['product.template'].search([('is_analysis', '=', True)])
|
||||||
patient1 = env['res.partner'].search([('patient_identifier', '=', 'P-A87B01'), ('is_patient', '=', True)], limit=1)
|
|
||||||
patient2 = env['res.partner'].search([('patient_identifier', '=', 'P-C45D02'), ('is_patient', '=', True)], limit=1)
|
# Find or create pregnancy test
|
||||||
doctor1 = env['res.partner'].search([('doctor_license', '=', 'L-98765'), ('is_doctor', '=', True)], limit=1)
|
pregnancy_test = env['product.template'].search([('name', '=', 'Prueba de Embarazo'), ('is_analysis', '=', True)], limit=1)
|
||||||
|
if not pregnancy_test:
|
||||||
|
# Create pregnancy test if it doesn't exist
|
||||||
|
pregnancy_test = env['product.template'].create({
|
||||||
|
'name': 'Prueba de Embarazo',
|
||||||
|
'is_analysis': True,
|
||||||
|
'analysis_type': 'immunology',
|
||||||
|
'list_price': 15.00,
|
||||||
|
'standard_price': 8.00,
|
||||||
|
'type': 'service',
|
||||||
|
'categ_id': env.ref('lims_management.lims_category_immunology').id if env.ref('lims_management.lims_category_immunology', False) else env['product.category'].search([], limit=1).id,
|
||||||
|
})
|
||||||
|
print("Created Pregnancy Test product")
|
||||||
|
|
||||||
if not patient1:
|
# Create parameter for pregnancy test
|
||||||
print("Warning: Patient 1 not found, skipping lab requests creation")
|
preg_param = env['lims.analysis.parameter'].search([('name', '=', 'Beta-hCG')], limit=1)
|
||||||
return
|
if not preg_param:
|
||||||
|
preg_param = env['lims.analysis.parameter'].create({
|
||||||
# Get analysis products - using search instead of ref
|
'name': 'Beta-hCG',
|
||||||
hemograma = env['product.template'].search([('name', '=', 'Hemograma Completo'), ('is_analysis', '=', True)], limit=1)
|
'code': 'BHCG',
|
||||||
perfil_lipidico = env['product.template'].search([('name', '=', 'Perfil Lipídico'), ('is_analysis', '=', True)], limit=1)
|
'value_type': 'selection',
|
||||||
glucosa = env['product.template'].search([('name', '=', 'Glucosa en Sangre'), ('is_analysis', '=', True)], limit=1)
|
'selection_values': 'Positivo,Negativo,Indeterminado',
|
||||||
urocultivo = env['product.template'].search([('name', '=', 'Urocultivo'), ('is_analysis', '=', True)], limit=1)
|
'description': 'Hormona gonadotropina coriónica humana beta'
|
||||||
|
})
|
||||||
|
|
||||||
# Create Lab Request 1 - Multiple analyses with same sample type
|
# Link parameter to pregnancy test
|
||||||
if patient1 and hemograma and perfil_lipidico:
|
env['product.template.parameter'].create({
|
||||||
order1 = env['sale.order'].create({
|
'product_tmpl_id': pregnancy_test.id,
|
||||||
'partner_id': patient1.id,
|
'parameter_id': preg_param.id,
|
||||||
'doctor_id': doctor1.id if doctor1 else False,
|
'sequence': 10,
|
||||||
'is_lab_request': True,
|
'required': True
|
||||||
'order_line': [
|
})
|
||||||
(0, 0, {'product_id': hemograma.product_variant_id.id, 'product_uom_qty': 1}),
|
|
||||||
(0, 0, {'product_id': perfil_lipidico.product_variant_id.id, 'product_uom_qty': 1})
|
# Separate analyses for different purposes
|
||||||
]
|
routine_analyses = [a for a in all_analyses if a.name not in ['Prueba de Embarazo']]
|
||||||
})
|
|
||||||
print(f"Created Lab Order 1: {order1.name}")
|
# Get all patients
|
||||||
|
all_patients = env['res.partner'].search([('is_patient', '=', True)], order='id')
|
||||||
# Confirm the order to test automatic sample generation
|
|
||||||
order1.action_confirm()
|
# Get available doctors
|
||||||
print(f"Confirmed Lab Order 1. Generated samples: {len(order1.generated_sample_ids)}")
|
doctors = env['res.partner'].search([('is_doctor', '=', True)])
|
||||||
|
|
||||||
# Process tests for order1
|
print(f"\n=== Starting creation of lab orders for {len(all_patients)} patients ===")
|
||||||
process_order_tests(env, order1)
|
print(f"Available analyses: {len(all_analyses)}")
|
||||||
|
print(f"Available doctors: {len(doctors)}")
|
||||||
# Create Lab Request 2 - Different sample types
|
|
||||||
if patient2 and glucosa and urocultivo:
|
orders_created = 0
|
||||||
order2 = env['sale.order'].create({
|
failed_orders = []
|
||||||
'partner_id': patient2.id,
|
|
||||||
'is_lab_request': True,
|
for idx, patient in enumerate(all_patients):
|
||||||
'order_line': [
|
print(f"\n--- Processing patient {idx+1}/{len(all_patients)}: {patient.name} ---")
|
||||||
(0, 0, {'product_id': glucosa.product_variant_id.id, 'product_uom_qty': 1}),
|
|
||||||
(0, 0, {'product_id': urocultivo.product_variant_id.id, 'product_uom_qty': 1})
|
# Randomly assign a doctor
|
||||||
]
|
doctor = random.choice(doctors) if doctors else False
|
||||||
})
|
|
||||||
print(f"Created Lab Order 2: {order2.name}")
|
# Create 2 orders per patient
|
||||||
|
for order_num in range(1, 3):
|
||||||
# Confirm to test automatic sample generation with different types
|
try:
|
||||||
order2.action_confirm()
|
order_lines = []
|
||||||
print(f"Confirmed Lab Order 2. Generated samples: {len(order2.generated_sample_ids)}")
|
|
||||||
|
# For pregnant patients, include pregnancy test in one of the orders
|
||||||
# Process tests for order2
|
if patient.is_pregnant and order_num == 1:
|
||||||
process_order_tests(env, order2)
|
order_lines.append((0, 0, {
|
||||||
|
'product_id': pregnancy_test.product_variant_id.id,
|
||||||
except Exception as e:
|
'product_uom_qty': 1
|
||||||
print(f"Error creating lab requests: {str(e)}")
|
}))
|
||||||
import traceback
|
print(f" - Added pregnancy test for pregnant patient")
|
||||||
traceback.print_exc()
|
|
||||||
|
# Select random analyses (minimum 2 per order)
|
||||||
|
num_analyses = random.randint(2, 4)
|
||||||
|
selected_analyses = random.sample(routine_analyses, min(num_analyses, len(routine_analyses)))
|
||||||
|
|
||||||
|
for analysis in selected_analyses:
|
||||||
|
order_lines.append((0, 0, {
|
||||||
|
'product_id': analysis.product_variant_id.id,
|
||||||
|
'product_uom_qty': 1
|
||||||
|
}))
|
||||||
|
|
||||||
|
# Create the order
|
||||||
|
order = env['sale.order'].create({
|
||||||
|
'partner_id': patient.id,
|
||||||
|
'doctor_id': doctor.id if doctor else False,
|
||||||
|
'is_lab_request': True,
|
||||||
|
'order_line': order_lines
|
||||||
|
})
|
||||||
|
|
||||||
|
print(f" Order {order_num}: Created {order.name} with {len(order_lines)} analyses")
|
||||||
|
|
||||||
|
# Confirm the order
|
||||||
|
order.action_confirm()
|
||||||
|
print(f" Order {order_num}: Confirmed. Generated samples: {len(order.generated_sample_ids)}")
|
||||||
|
|
||||||
|
# Process tests
|
||||||
|
process_order_tests(env, order)
|
||||||
|
|
||||||
|
orders_created += 1
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"Patient: {patient.name}, Order {order_num}, Error: {str(e)}"
|
||||||
|
failed_orders.append(error_msg)
|
||||||
|
print(f" ERROR creating order {order_num} for {patient.name}: {str(e)}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
# Final summary
|
||||||
|
print("\n=== SUMMARY ===")
|
||||||
|
print(f"Total orders created: {orders_created}")
|
||||||
|
print(f"Failed orders: {len(failed_orders)}")
|
||||||
|
|
||||||
|
if failed_orders:
|
||||||
|
print("\n=== FAILED ORDERS ===")
|
||||||
|
for idx, error in enumerate(failed_orders, 1):
|
||||||
|
print(f"{idx}. {error}")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
db_name = 'lims_demo'
|
db_name = 'lims_demo'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user