account_invoice.py 4.4 KB

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