import frappe from frappe import _ @frappe.whitelist() def get_qa_ready_serials(limit=50): """Get Serial Nos ready for QA (Erasure Complete state).""" return frappe.get_all('Serial No', filters={'r2_status': 'Erasure Complete'}, fields=['name', 'item_code', 'item_name', 'pallet', 'cosmetic_grade'], limit=limit, order_by='creation asc' ) @frappe.whitelist() def create_qa_from_serial(serial_no): """Create QA inspection for a Serial No.""" if not frappe.db.exists('Serial No', serial_no): return {'error': 'Serial No not found'} serial = frappe.get_doc('Serial No', serial_no) # Check if already has active inspection existing = frappe.db.get_value('R2 Device Inspection', {'serial_no': serial_no, 'docstatus': ['!=', 2]}, 'name') if existing: return {'error': f'Inspection {existing} already exists for this serial'} insp = frappe.get_doc({ 'doctype': 'R2 Device Inspection', 'serial_no': serial_no, 'item_code': serial.item_code, 'inspection_date': frappe.utils.today(), 'inspector': frappe.session.user, 'cosmetic_grade': serial.cosmetic_grade, 'screen_condition': serial.screen_condition, 'keyboard_condition': serial.keyboard_condition, 'case_condition': serial.case_condition }) insp.insert(ignore_permissions=True) return {'success': True, 'inspection': insp.name}