import odoo def test_sample_generation(cr): env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {}) print("=== TESTING AUTOMATIC SAMPLE GENERATION ===\n") # Create test patient patient = env['res.partner'].create({ 'name': 'Test Patient for Validation', 'is_patient': True, 'patient_identifier': 'P-TEST01' }) print(f"Created patient: {patient.name}") # Create test doctor doctor = env['res.partner'].create({ 'name': 'Dr. Test Validation', 'is_doctor': True, 'doctor_license': 'L-TEST01' }) print(f"Created doctor: {doctor.name}") # Get or create sample types sample_types = {} # EDTA Tube edta = env['product.template'].search([('name', 'like', 'EDTA'), ('is_sample_type', '=', True)], limit=1) if not edta: edta = env['product.template'].create({ 'name': 'Test EDTA Tube', 'is_sample_type': True, 'type': 'consu' }) sample_types['edta'] = edta # Serum Tube serum = env['product.template'].search([('name', 'like', 'Suero'), ('is_sample_type', '=', True)], limit=1) if not serum: serum = env['product.template'].create({ 'name': 'Test Serum Tube', 'is_sample_type': True, 'type': 'consu' }) sample_types['serum'] = serum # Create test analyses analyses = [] # Analysis 1 - requires EDTA analysis1 = env['product.template'].create({ 'name': 'Test Hemograma', 'is_analysis': True, 'type': 'service', 'required_sample_type_id': edta.id, 'sample_volume_ml': 3.0 }) analyses.append(analysis1) print(f"Created analysis: {analysis1.name} (requires {edta.name}, {analysis1.sample_volume_ml} ml)") # Analysis 2 - also requires EDTA analysis2 = env['product.template'].create({ 'name': 'Test HbA1c', 'is_analysis': True, 'type': 'service', 'required_sample_type_id': edta.id, 'sample_volume_ml': 2.0 }) analyses.append(analysis2) print(f"Created analysis: {analysis2.name} (requires {edta.name}, {analysis2.sample_volume_ml} ml)") # Analysis 3 - requires Serum analysis3 = env['product.template'].create({ 'name': 'Test Glucose', 'is_analysis': True, 'type': 'service', 'required_sample_type_id': serum.id, 'sample_volume_ml': 1.0 }) analyses.append(analysis3) print(f"Created analysis: {analysis3.name} (requires {serum.name}, {analysis3.sample_volume_ml} ml)") # Analysis 4 - no sample type defined analysis4 = env['product.template'].create({ 'name': 'Test Special Analysis', 'is_analysis': True, 'type': 'service' }) analyses.append(analysis4) print(f"Created analysis: {analysis4.name} (no sample type defined)") # Create lab order print("\n--- Creating Lab Order ---") order = env['sale.order'].create({ 'partner_id': patient.id, 'doctor_id': doctor.id, 'is_lab_request': True, 'order_line': [(0, 0, { 'product_id': a.product_variant_id.id, 'product_uom_qty': 1 }) for a in analyses] }) print(f"Created order: {order.name}") print(f"Order lines: {len(order.order_line)}") # Confirm order - this should trigger automatic sample generation print("\n--- Confirming Order (triggering sample generation) ---") order.action_confirm() print(f"Order state: {order.state}") # Check generated samples print(f"\n--- Generated Samples: {len(order.generated_sample_ids)} ---") for sample in order.generated_sample_ids: print(f"\nSample: {sample.name}") print(f" Barcode: {sample.barcode}") print(f" Sample Type: {sample.sample_type_product_id.name if sample.sample_type_product_id else 'None'}") print(f" Total Volume: {sample.volume_ml} ml") print(f" Analyses: {sample.analysis_names}") print(f" State: {sample.state}") # Check messages print("\n--- Order Messages ---") messages = order.message_ids.filtered(lambda m: m.body and m.message_type == 'notification') for msg in messages[:5]: print(f"Message: {msg.body[:200]}...") print("\n=== TEST COMPLETED ===") return order if __name__ == '__main__': db_name = 'lims_demo' registry = odoo.registry(db_name) with registry.cursor() as cr: test_sample_generation(cr) cr.commit()