account_invoice.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request
  3. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, float_round
  4. from dateutil.parser import parse
  5. from dateutil.relativedelta import relativedelta as rd
  6. _MODEL = 'account.invoice'
  7. def create_invoice(sale_order_id, currency_id, user_id, date_today, picking_done=True):
  8. sale_order = request.env['sale.order'].browse(sale_order_id)
  9. invoice_id = sale_order.action_invoice_create()
  10. invoice = request.env[_MODEL].browse(invoice_id)
  11. for picking in sale_order.picking_ids:
  12. picking.force_assign()
  13. if picking_done:
  14. picking.action_done()
  15. date_due = parse(date_today) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2)))
  16. invoice.write({
  17. 'currency_id': currency_id,
  18. 'date_invoice': date_today,
  19. 'date_due': date_due.strftime(DEFAULT_SERVER_DATE_FORMAT),
  20. 'user_id': user_id,
  21. 'state': 'open'
  22. })
  23. return invoice
  24. def create_invoice_move_lines(invoice_id, paid_amount, date_today):
  25. invoice = request.env[_MODEL].browse(invoice_id)
  26. context = dict(request.context, lang=invoice.partner_id.lang)
  27. invoice_move_lines = invoice._get_analytic_lines()
  28. decimal_precision = request.env['decimal.precision'].precision_get('Account')
  29. compute_taxes = request.env['account.invoice.tax'].compute(invoice.with_context(context))
  30. invoice.check_tax_lines(compute_taxes)
  31. invoice._recompute_tax_amount()
  32. invoice_move_lines += request.env['account.invoice.tax'].move_line_get(invoice.id)
  33. total, total_currency, invoice_move_lines = invoice.with_context(context).compute_invoice_totals(invoice.company_id.currency_id, invoice.reference, invoice_move_lines)
  34. paid_amount = float_round(paid_amount, precision_digits=decimal_precision)
  35. paid_percentage = paid_amount / float_round(total_currency, precision_digits=decimal_precision)
  36. distributed_percentage = -(paid_percentage / len(invoice.payment_term.line_ids))
  37. diff_currency = invoice.currency_id != invoice.company_id.currency_id
  38. if diff_currency:
  39. paid_amount = invoice.currency_id.compute(paid_amount, invoice.company_id.currency_id)
  40. payment_lines = []
  41. for line in invoice.payment_term.line_ids:
  42. date_due = (parse(date_today) + rd(days=line.days + line.days2)).strftime(DEFAULT_SERVER_DATE_FORMAT)
  43. if paid_percentage and paid_percentage < 1.0:
  44. payment_lines.append([date_today, paid_percentage])
  45. paid_percentage = paid_amount = 0
  46. if date_due == date_today and line.value_amount:
  47. distributed_percentage = -((payment_lines[0][1] - line.value_amount) / (len(invoice.payment_term.line_ids) - 1))
  48. continue
  49. if line.value != 'balance':
  50. payment_lines.append([date_due, line.value_amount + distributed_percentage])
  51. continue
  52. payment_lines.append([date_due, line.value_amount])
  53. for payment_line in payment_lines:
  54. current_price = float_round(total * payment_line[1], precision_digits=decimal_precision)
  55. if current_price < 0.0:
  56. continue
  57. paid_amount += current_price
  58. # Compute move line price
  59. if payment_line[1]:
  60. price = current_price
  61. else:
  62. price = float_round(total - paid_amount, precision_digits=decimal_precision) or total
  63. # Compute move line amount in currency
  64. if diff_currency:
  65. amount_currency = invoice.company_id.currency_id.with_context(context).compute(price, invoice.currency_id)
  66. else:
  67. amount_currency = False
  68. # Create invoice move live value
  69. invoice_move_lines.append({
  70. 'type': 'dest',
  71. 'name': '/',
  72. 'price': price,
  73. 'account_id': invoice.account_id.id,
  74. 'date_maturity': payment_line[0],
  75. 'amount_currency': amount_currency,
  76. 'currency_id': diff_currency and invoice.currency_id.id,
  77. 'ref': invoice.reference
  78. })
  79. payment_lines = []
  80. return invoice_move_lines
  81. def number_invoice(invoice_id):
  82. request.env[_MODEL].browse(invoice_id).action_number()
  83. def close_invoice(invoice_id):
  84. invoice = request.env[_MODEL].browse(invoice_id)
  85. if invoice.residual == 0:
  86. invoice.write({
  87. 'state': 'paid'
  88. })