You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import models, fields, api
  5. class AccountMove(models.Model):
  6. _inherit = 'account.move'
  7. move_type = fields.Selection(
  8. string="Move type", selection=[
  9. ('other', 'Other'),
  10. ('liquidity', 'Liquidity'),
  11. ('receivable', 'Receivable'),
  12. ('receivable_refund', 'Receivable refund'),
  13. ('payable', 'Payable'),
  14. ('payable_refund', 'Payable refund'),
  15. ], compute='_compute_move_type', store=True, readonly=True)
  16. @api.multi
  17. @api.depends(
  18. 'line_ids.account_id.internal_type', 'line_ids.balance',
  19. 'line_ids.account_id.user_type_id.type', 'line_ids.invoice_id.type'
  20. )
  21. def _compute_move_type(self):
  22. refund_types = ('in_refund', 'out_refund')
  23. def _is_refund(line_ids, internal_type):
  24. """Check whether all the lines of type `internal_type`
  25. come from a refund."""
  26. line_ids = line_ids.filtered(
  27. lambda x: x.account_id.internal_type == internal_type)
  28. line_types = line_ids.mapped('invoice_id.type')
  29. if len(line_types) == 1:
  30. res = line_types[0] in refund_types
  31. else:
  32. # Lines are linked to invoices of different types,
  33. # or to no invoice at all.
  34. # If their summed balance is negative, this is a refund.
  35. res = sum(line_ids.mapped('balance')) < 0
  36. return res
  37. for move in self:
  38. move_lines = move.line_ids
  39. internal_types = move_lines.mapped('account_id.internal_type')
  40. if 'liquidity' in internal_types:
  41. move.move_type = 'liquidity'
  42. elif 'payable' in internal_types:
  43. is_refund = _is_refund(move_lines, 'payable')
  44. move.move_type = (
  45. 'payable' if not is_refund else 'payable_refund')
  46. elif 'receivable' in internal_types:
  47. is_refund = _is_refund(move_lines, 'receivable')
  48. move.move_type = (
  49. 'receivable' if not is_refund else 'receivable_refund')
  50. else:
  51. move.move_type = 'other'