'This function iterates recursively on the recordset given as parameter as long as it
can find a debit and a credit to reconcile together. It returns the recordset of the
account move lines that were not reconciled during the process.'

def auto_reconcile_lines(self):

    if (not self.ids):
        return self
    (sm_debit_move, sm_credit_move) = self._get_pair_to_reconcile()
    if ((not sm_credit_move) or (not sm_debit_move)):
        return self
    field = ((self[0].account_id.currency_id and 'amount_residual_currency') or 'amount_residual')
    if ((not sm_debit_move.debit) and (not sm_debit_move.credit)):
        field = 'amount_residual_currency'
    if (self[0].currency_id and all([(x.currency_id == self[0].currency_id) for x in self])):
        field = 'amount_residual_currency'
    if (self._context.get('skip_full_reconcile_check') == 'amount_currency_excluded'):
        field = 'amount_residual'
    elif (self._context.get('skip_full_reconcile_check') == 'amount_currency_only'):
        field = 'amount_residual_currency'
    amount_reconcile = min(sm_debit_move[field], (- sm_credit_move[field]))
    if (amount_reconcile == sm_debit_move[field]):
        self -= sm_debit_move
    if (amount_reconcile == (- sm_credit_move[field])):
        self -= sm_credit_move
    currency = False
    amount_reconcile_currency = 0
    if ((sm_debit_move.currency_id == sm_credit_move.currency_id) and sm_debit_move.currency_id.id):
        currency = sm_credit_move.currency_id.id
        amount_reconcile_currency = min(sm_debit_move.amount_residual_currency, (- sm_credit_move.amount_residual_currency))
    amount_reconcile = min(sm_debit_move.amount_residual, (- sm_credit_move.amount_residual))
    if (self._context.get('skip_full_reconcile_check') == 'amount_currency_excluded'):
        amount_reconcile_currency = 0.0
        currency = self._context.get('manual_full_reconcile_currency')
    elif (self._context.get('skip_full_reconcile_check') == 'amount_currency_only'):
        currency = self._context.get('manual_full_reconcile_currency')
    self.env['account.partial.reconcile'].create({'debit_move_id': sm_debit_move.id, 'credit_move_id': sm_credit_move.id, 'amount': amount_reconcile, 'amount_currency': amount_reconcile_currency, 'currency_id': currency})
    return self.auto_reconcile_lines()
