'Creates invoice related analytics and financial move lines'

@api.multi
def action_move_create(self):

    account_move = self.env['account.move']
    for inv in self:
        if (not inv.journal_id.sequence_id):
            raise UserError(_('Please    define    sequence    on    the    journal    related    to    this    invoice.'))
        if (not inv.invoice_line_ids):
            raise UserError(_('Please    create    some    invoice    lines.'))
        if inv.move_id:
            continue
        ctx = dict(self._context, lang=inv.partner_id.lang)
        if (not inv.date_invoice):
            inv.with_context(ctx).write({'date_invoice': fields.Date.context_today(self)})
        company_currency = inv.company_id.currency_id
        iml = inv.invoice_line_move_line_get()
        iml += inv.tax_line_move_line_get()
        diff_currency = (inv.currency_id != company_currency)
        (total, total_currency, iml) = inv.with_context(ctx).compute_invoice_totals(company_currency, iml)
        name = (inv.name or '/')
        if inv.payment_term_id:
            totlines = inv.with_context(ctx).payment_term_id.with_context(currency_id=company_currency.id).compute(total, inv.date_invoice)[0]
            res_amount_currency = total_currency
            ctx['date'] = (inv.date or inv.date_invoice)
            for (i, t) in enumerate(totlines):
                if (inv.currency_id != company_currency):
                    amount_currency = company_currency.with_context(ctx).compute(t[1], inv.currency_id)
                else:
                    amount_currency = False
                res_amount_currency -= (amount_currency or 0)
                if ((i + 1) == len(totlines)):
                    amount_currency += res_amount_currency
                iml.append({'type': 'dest', 'name': name, 'price': t[1], 'account_id': inv.account_id.id, 'date_maturity': t[0], 'amount_currency': (diff_currency and amount_currency), 'currency_id': (diff_currency and inv.currency_id.id), 'invoice_id': inv.id})
        else:
            iml.append({'type': 'dest', 'name': name, 'price': total, 'account_id': inv.account_id.id, 'date_maturity': inv.date_due, 'amount_currency': (diff_currency and total_currency), 'currency_id': (diff_currency and inv.currency_id.id), 'invoice_id': inv.id})
        part = self.env['res.partner']._find_accounting_partner(inv.partner_id)
        line = [(0, 0, self.line_get_convert(l, part.id)) for l in iml]
        line = inv.group_lines(iml, line)
        journal = inv.journal_id.with_context(ctx)
        line = inv.finalize_invoice_move_lines(line)
        date = (inv.date or inv.date_invoice)
        move_vals = {'ref': inv.reference, 'line_ids': line, 'journal_id': journal.id, 'date': date, 'narration': inv.comment}
        ctx['company_id'] = inv.company_id.id
        ctx['invoice'] = inv
        ctx_nolang = ctx.copy()
        ctx_nolang.pop('lang', None)
        move = account_move.with_context(ctx_nolang).create(move_vals)
        move.post()
        vals = {'move_id': move.id, 'date': date, 'move_name': move.name}
        inv.with_context(ctx).write(vals)
    return True
