account_move.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. from openerp.tools import float_round, DEFAULT_SERVER_DATE_FORMAT
  4. from dateutil.parser import parse
  5. from dateutil.relativedelta import relativedelta as rd
  6. def create_invoice_move_lines(invoice_ids, paid_amount, date_today):
  7. assert len(invoice_ids) == 1
  8. invoice = r.env['account.invoice'].browse(invoice_ids)
  9. is_purchase = False
  10. scoped_context = dict(r.context, lang=invoice.partner_id.lang)
  11. invoice_move_lines = invoice._get_analytic_lines()
  12. decimal_precision = r.env['decimal.precision'].precision_get('Account')
  13. compute_taxes = r.env['account.invoice.tax'].compute(invoice.with_context(lang=invoice.partner_id.lang))
  14. invoice.check_tax_lines(compute_taxes)
  15. invoice._recompute_tax_amount()
  16. invoice_move_lines += r.env['account.invoice.tax'].move_line_get(invoice.id)
  17. total, total_currency, invoice_move_lines = invoice.with_context(scoped_context).compute_invoice_totals(invoice.company_id.currency_id, invoice.reference, invoice_move_lines)
  18. if total < 0:
  19. total = total * -1
  20. is_purchase = True
  21. paid_amount = float_round(paid_amount, precision_digits=decimal_precision)
  22. paid_percentage = abs(paid_amount / float_round(total, decimal_precision))
  23. distributed_percentage = -(paid_percentage / len(invoice.payment_term.line_ids))
  24. payment_lines = []
  25. for line in invoice.payment_term.line_ids:
  26. date_due = (parse(date_today) + rd(days=line.days + line.days2)).strftime(DEFAULT_SERVER_DATE_FORMAT)
  27. if paid_percentage and paid_percentage < 1.0:
  28. payment_lines.append([date_today, paid_percentage])
  29. paid_percentage = paid_amount = 0
  30. if date_due == date_today and line.value_amount:
  31. distributed_percentage = -((payment_lines[0][1] - line.value_amount) / (len(invoice.payment_term.line_ids) - 1))
  32. continue
  33. if line.value != 'balance':
  34. payment_lines.append([date_due, line.value_amount + distributed_percentage])
  35. continue
  36. payment_lines.append([date_due, line.value_amount])
  37. for payment_line in payment_lines:
  38. current_price = round(total * payment_line[1], decimal_precision)
  39. if current_price < 0.0:
  40. continue
  41. paid_amount = paid_amount + current_price
  42. price = current_price if payment_line[1] else round(total - paid_amount, decimal_precision) or total
  43. if is_purchase:
  44. price = price * -1
  45. invoice_move_lines.append({
  46. 'type': 'dest',
  47. 'name': '/',
  48. 'price': price,
  49. 'account_id': invoice.account_id.id,
  50. 'date_maturity': payment_line[0],
  51. 'amount_currency': invoice.company_id.currency_id.compute(payment_line[1], invoice.currency_id) if invoice.currency_id != invoice.company_id.currency_id else False,
  52. 'currency_id': invoice.currency_id != invoice.company_id.currency_id and invoice.currency_id.id,
  53. 'ref': invoice.type in ('in_invoice', 'in_refund') and invoice.reference or invoice.number
  54. })
  55. payment_lines = []
  56. return invoice_move_lines
  57. def create_account_move(invoice_ids, invoice_move_lines):
  58. assert len(invoice_ids) == 1
  59. invoice = r.env['account.invoice'].browse(invoice_ids)
  60. accounting_partner = r.env['res.partner']._find_accounting_partner(invoice.partner_id)
  61. move_line_values = [(0, 0, invoice.line_get_convert(line, accounting_partner.id, invoice.date_invoice)) for line in invoice_move_lines]
  62. move_line_values = invoice.group_lines(invoice_move_lines, move_line_values)
  63. move_line_values = invoice.finalize_invoice_move_lines(move_line_values)
  64. ctx = dict(r.context, lang=invoice.partner_id.lang, company_id=invoice.company_id.id)
  65. period = invoice.period_id
  66. if not period:
  67. period = period.with_context(ctx).find(invoice.date_invoice)[:1]
  68. if period:
  69. for line in move_line_values:
  70. line[2]['period_id'] = period.id
  71. ctx['invoice'] = invoice
  72. ctx_nolang = ctx.copy()
  73. ctx_nolang.pop('lang', None)
  74. account_move = r.env['account.move'].with_context(ctx_nolang).create({
  75. 'ref': invoice.reference or invoice.name,
  76. 'line_id': move_line_values,
  77. 'journal_id': invoice.journal_id.with_context(r.context, lang=invoice.partner_id.lang).id,
  78. 'date': invoice.date_invoice,
  79. 'narration': invoice.comment,
  80. 'company_id': invoice.company_id.id,
  81. 'period_id': period.id
  82. })
  83. invoice.with_context(ctx).write({
  84. 'move_id': account_move.id,
  85. 'period_id': account_move.period_id.id,
  86. 'move_name': account_move.name,
  87. })
  88. account_move.post()
  89. return account_move