account_move.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request
  3. _MODEL = 'account.move'
  4. '''
  5. Create account move
  6. '''
  7. def create_account_move(invoice_id, invoice_move_lines):
  8. invoice = request.env['account.invoice'].browse(invoice_id)
  9. accounting_partner = request.env['res.partner']._find_accounting_partner(invoice.partner_id)
  10. move_line_values = [(0, 0, invoice.line_get_convert(line, accounting_partner.id, invoice.date_invoice)) for line in invoice_move_lines]
  11. move_line_values = invoice.group_lines(invoice_move_lines, move_line_values)
  12. move_line_values = invoice.finalize_invoice_move_lines(move_line_values)
  13. ctx = dict(request.context, lang=invoice.partner_id.lang, company_id=invoice.company_id.id)
  14. period = invoice.period_id
  15. if not period:
  16. period = period.with_context(ctx).find(invoice.date_invoice)[:1]
  17. if period:
  18. for line in move_line_values:
  19. line[2]['period_id'] = period.id
  20. ctx['invoice'] = invoice
  21. ctx_nolang = ctx.copy()
  22. ctx_nolang.pop('lang', None)
  23. account_move = request.env[_MODEL].with_context(ctx_nolang).create({
  24. 'ref': invoice.reference or invoice.name,
  25. 'line_id': move_line_values,
  26. 'journal_id': invoice.journal_id.id,
  27. 'date': invoice.date_invoice,
  28. 'narration': invoice.comment,
  29. 'company_id': invoice.company_id.id,
  30. 'period_id': period.id
  31. })
  32. invoice.with_context(ctx).write({
  33. 'move_id': account_move.id,
  34. 'period_id': account_move.period_id.id,
  35. 'move_name': account_move.name,
  36. })
  37. account_move.post()
  38. return account_move