'Create a writeoff move for the account.move.lines in self. If debit/credit is not specified in vals,
the writeoff amount will be computed as the sum of amount_residual of the given recordset.
:param vals: dict containing values suitable fot account_move_line.create(). The data in vals will
be processed to create bot writeoff acount.move.line and their enclosing account.move.'

def _create_writeoff(self, vals):

    if (('account_id' not in vals) or ('journal_id' not in vals)):
        raise UserError(_('It    is    mandatory    to    specify    an    account    and    a    journal    to    create    a    write-off.'))
    if (('debit' in vals) ^ ('credit' in vals)):
        raise UserError(_('Either    pass    both    debit    and    credit    or    none.'))
    if ('date' not in vals):
        vals['date'] = (self._context.get('date_p') or time.strftime('%Y-%m-%d'))
    if ('name' not in vals):
        vals['name'] = (self._context.get('comment') or _('Write-Off'))
    if ('analytic_account_id' not in vals):
        vals['analytic_account_id'] = self.env.context.get('analytic_id', False)
    if (('credit' not in vals) and ('debit' not in vals)):
        amount = sum([r.amount_residual for r in self])
        vals['credit'] = (((amount > 0) and amount) or 0.0)
        vals['debit'] = (((amount < 0) and abs(amount)) or 0.0)
    vals['partner_id'] = self.env['res.partner']._find_accounting_partner(self[0].partner_id).id
    company_currency = self[0].account_id.company_id.currency_id
    writeoff_currency = (self[0].currency_id or company_currency)
    if ((not (self._context.get('skip_full_reconcile_check') == 'amount_currency_excluded')) and ('amount_currency' not in vals) and (writeoff_currency != company_currency)):
        vals['currency_id'] = writeoff_currency.id
        sign = (1 if (vals['debit'] > 0) else (-1))
        vals['amount_currency'] = (sign * abs(sum([r.amount_residual_currency for r in self])))
    first_line_dict = vals.copy()
    first_line_dict['account_id'] = self[0].account_id.id
    if ('analytic_account_id' in first_line_dict):
        del first_line_dict['analytic_account_id']
    if ('tax_ids' in first_line_dict):
        tax_ids = []
        for tax_id in vals['tax_ids']:
            tax_ids.append(tax_id[1])
        amount = (first_line_dict['credit'] - first_line_dict['debit'])
        amount_tax = self.env['account.tax'].browse(tax_ids).compute_all(amount)['total_included']
        first_line_dict['credit'] = (((amount_tax > 0) and amount_tax) or 0.0)
        first_line_dict['debit'] = (((amount_tax < 0) and abs(amount_tax)) or 0.0)
        del first_line_dict['tax_ids']
    second_line_dict = vals.copy()
    (second_line_dict['debit'], second_line_dict['credit']) = (second_line_dict['credit'], second_line_dict['debit'])
    if ('amount_currency' in vals):
        second_line_dict['amount_currency'] = (- second_line_dict['amount_currency'])
    writeoff_move = self.env['account.move'].with_context(apply_taxes=True).create({'journal_id': vals['journal_id'], 'date': vals['date'], 'state': 'draft', 'line_ids': [(0, 0, first_line_dict), (0, 0, second_line_dict)]})
    writeoff_move.post()
    return writeoff_move.line_ids.filtered((lambda r: (r.account_id == self[0].account_id)))
