@@ -342,33 +342,15 @@ def _create_response_lines(
342342            and  verifactu_response ["RespuestaLinea" ]
343343            or  []
344344        )
345-         models  =  self .env ["verifactu.mixin" ]._get_verifactu_reference_models ()
346345        for  verifactu_response_line  in  verifactu_response_lines :
347346            invoice_num  =  verifactu_response_line ["IDFactura" ]["NumSerieFactura" ]
348-             document  =  False 
349-             for  model  in  models :
350-                 document  =  self .env [model ].search (
351-                     [
352-                         ("name" , "=" , invoice_num ),
353-                         ("id" , "in" , self .mapped ("document_id" )),
354-                     ],
355-                     limit = 1 ,
356-                 )
357-                 if  document :
358-                     break 
359- 
360-             # Skip if document not found 
361-             if  not  document :
362-                 continue 
363- 
364-             # Find the verifactu.invoice entry for this document 
365-             verifactu_invoice_entry  =  document .last_verifactu_invoice_entry_id 
366- 
367-             # Skip if no verifactu invoice entry found - this should not happen 
368-             # in normal flow but can happen in tests with mocked responses 
369-             if  not  verifactu_invoice_entry :
347+             matching_entries  =  self .filtered (
348+                 lambda  r : r .document_name  ==  invoice_num 
349+             ).sorted (lambda  x : x .create_date , reverse = True )
350+             if  not  matching_entries :
370351                continue 
371- 
352+             verifactu_invoice_entry  =  matching_entries [0 ]  # Assume one match 
353+             document  =  verifactu_invoice_entry .document 
372354            previous_response_line  =  document .last_verifactu_response_line_id 
373355            send_state  =  VERIFACTU_STATE_MAPPING [
374356                verifactu_response_line ["EstadoRegistro" ]
0 commit comments