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.

59 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 openerp 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('line_ids.account_id.internal_type', 'line_ids.balance')
  18. def _compute_move_type(self):
  19. sequence = (
  20. ("liquidity", lambda balance: "liquidity"),
  21. ("payable", lambda balance: ('payable' if balance < 0
  22. else 'payable_refund')),
  23. ("receivable", lambda balance: ('receivable' if balance > 0
  24. else 'receivable_refund')),
  25. (False, lambda balance: "other"),
  26. )
  27. chunked_self = (
  28. self[i:i + models.PREFETCH_MAX]
  29. for i in range(0, len(self), models.PREFETCH_MAX)
  30. )
  31. for chunk in chunked_self:
  32. move_ids = set(chunk.ids)
  33. for internal_type, criteria in sequence:
  34. if not move_ids:
  35. break
  36. # Find balances of the expected type for this move
  37. domain = [
  38. ("move_id", "in", list(move_ids)),
  39. ]
  40. if internal_type:
  41. domain += [
  42. ("account_id.internal_type", "=", internal_type),
  43. ]
  44. balances = self.env["account.move.line"].read_group(
  45. domain=domain,
  46. fields=["move_id", "balance"],
  47. groupby=["move_id"],
  48. )
  49. for balance in balances:
  50. move = self.browse(balance["move_id"][0])
  51. # Discard the move for next searches
  52. move_ids.discard(move.id)
  53. # Update its type
  54. move.move_type = criteria(balance["balance"])