Facebook
From Harrison, 1 Day ago, written in Python.
Embed
Download Paste or View Raw
Hits: 20
  1. @api.multi
  2.     def action_move_create(self):
  3.         """ Creates invoice related analytics and financial move lines """
  4.         account_move = self.env['account.move']
  5.  
  6.         for inv in self:
  7.             if not inv.journal_id.sequence_id:
  8.                 raise UserError(_('Please define sequence on the journal related to this invoice.'))
  9.             if not inv.invoice_line_ids:
  10.                 raise UserError(_('Please create some invoice lines.'))
  11.             if inv.move_id:
  12.                 continue
  13.  
  14.             ctx = dict(self._context, lang=inv.partner_id.lang)
  15.  
  16.             if not inv.date_invoice:
  17.                 inv.with_context(ctx).write({'date_invoice': fields.Date.context_today(self)})
  18.             if not inv.date_due:
  19.                 inv.with_context(ctx).write({'date_due': inv.date_invoice})
  20.             company_currency = inv.company_id.currency_id
  21.  
  22.             # create move lines (one per invoice line + eventual taxes and analytic lines)
  23.             iml = inv.invoice_line_move_line_get()
  24.             iml += inv.tax_line_move_line_get()
  25.  
  26.             diff_currency = inv.currency_id != company_currency
  27.             # create one move line for the total and possibly adjust the other lines amount
  28.             total, total_currency, iml = inv.with_context(ctx).compute_invoice_totals(company_currency, iml)
  29.  
  30.             name = inv.name or '/'
  31.             if inv.payment_term_id:
  32.                 totlines = inv.with_context(ctx).payment_term_id.with_context(currency_id=company_currency.id).compute(total, inv.date_invoice)[0]
  33.                 res_amount_currency = total_currency
  34.                 ctx['date'] = inv._get_currency_rate_date()
  35.                 for i, t in enumerate(totlines):
  36.                     if inv.currency_id != company_currency:
  37.                         amount_currency = company_currency.with_context(ctx).compute(t[1], inv.currency_id)
  38.                     else:
  39.                         amount_currency = False
  40.  
  41.                     # last line: add the diff
  42.                     res_amount_currency -= amount_currency or 0
  43.                     if i + 1 == len(totlines):
  44.                         amount_currency += res_amount_currency
  45.  
  46.                     iml.append({
  47.                         'type': 'dest',
  48.                         'name': name,
  49.                         'price': t[1],
  50.                         'account_id': inv.account_id.id,
  51.                         'date_maturity': t[0],
  52.                         'amount_currency': diff_currency and amount_currency,
  53.                         'currency_id': diff_currency and inv.currency_id.id,
  54.                         'invoice_id': inv.id
  55.                     })
  56.             else:
  57.                 iml.append({
  58.                     'type': 'dest',
  59.                     'name': name,
  60.                     'price': total,
  61.                     'account_id': inv.account_id.id,
  62.                     'date_maturity': inv.date_due,
  63.                     'amount_currency': diff_currency and total_currency,
  64.                     'currency_id': diff_currency and inv.currency_id.id,
  65.                     'invoice_id': inv.id
  66.                 })
  67.             part = self.env['res.partner']._find_accounting_partner(inv.partner_id)
  68.             line = [(0, 0, self.line_get_convert(l, part.id)) for l in iml]
  69.             line = inv.group_lines(iml, line)
  70.  
  71.             journal = inv.journal_id.with_context(ctx)
  72.             line = inv.finalize_invoice_move_lines(line)
  73.  
  74.             date = inv.date or inv.date_invoice
  75.             move_vals = {
  76.                 'ref': inv.reference,
  77.                 'line_ids': line,
  78.                 'journal_id': journal.id,
  79.                 'date': date,
  80.                 'narration': inv.comment,
  81.             }
  82.             ctx['company_id'] = inv.company_id.id
  83.             ctx['invoice'] = inv
  84.             ctx_nolang = ctx.copy()
  85.             ctx_nolang.pop('lang', None)
  86.             move = account_move.with_context(ctx_nolang).create(move_vals)
  87.             # Pass invoice in context in method post: used if you want to get the same
  88.             # account move reference when creating the same invoice after a cancelled one:
  89.  
  90.             # Pasar factura en contexto en m├ętodo post: se usa si desea obtener el mismo
  91.             # referencia de movimiento de cuenta al crear la misma factura despu├ęs de una cancelada:
  92.             move.post()
  93.             # make the invoice point to that move
  94.             vals = {
  95.                 'move_id': move.id,
  96.                 'date': date,
  97.                 'move_name': move.name,
  98.             }
  99.             inv.with_context(ctx).write(vals)
  100.         return True