account_payment.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. """Classes defining the populate factory for Payments and related models."""
  3. from odoo import models, fields
  4. from odoo.tools import populate
  5. from dateutil.relativedelta import relativedelta
  6. import logging
  7. import math
  8. from functools import lru_cache
  9. _logger = logging.getLogger(__name__)
  10. class AccountPayment(models.Model):
  11. """Populate factory part for account.payment."""
  12. _inherit = "account.payment"
  13. _populate_sizes = {
  14. 'small': 100,
  15. 'medium': 5000,
  16. 'large': 50000,
  17. }
  18. _populate_dependencies = ['res.company', 'res.partner', 'account.journal']
  19. def _populate_factories(self):
  20. @lru_cache()
  21. def search_partner_ids(company_id):
  22. """Search all the partners that a company has access to.
  23. This method is cached, only one search is done per company_id.
  24. :param company_id (int): the company to search partners for.
  25. :return (list<int>): the ids of partner the company has access to.
  26. """
  27. return self.env['res.partner'].search([
  28. '|', ('company_id', '=', company_id), ('company_id', '=', False),
  29. ('id', 'in', self.env.registry.populated_models['res.partner']),
  30. ]).ids
  31. @lru_cache()
  32. def search_journal_ids(company_id):
  33. """Search all the journal of a certain type for a company.
  34. This method is cached, only one search is done per company_id.
  35. :param company_id (int): the company to search journals for.
  36. :return (list<int>): the ids of the bank and cash journals of a company
  37. """
  38. return self.env['account.journal'].search([
  39. ('company_id', '=', company_id),
  40. ('type', 'in', ('cash', 'bank')),
  41. ]).ids
  42. @lru_cache()
  43. def search_payment_method_line_ids(payment_type, journal):
  44. """Search all the payment methods of a certain type.
  45. This method is cached, only one search is done per type.
  46. :param payment_type (str): the type of payment method. Valid values are customer and supplier.
  47. :param journal (int): the journal of the payment method.
  48. :return list<int>: list of ids of payment methods of the selected type
  49. """
  50. need_bank_account = self._get_method_codes_needing_bank_account()
  51. other_blacklist = ['sdd', 'bacs_dd']
  52. return self.env['account.payment.method.line'].search([
  53. ('journal_id', '=', journal),
  54. ('payment_method_id.payment_type', '=', payment_type),
  55. ('code', 'not in', need_bank_account + other_blacklist),
  56. ]).ids
  57. def get_partner(random, values, **kwargs):
  58. """Get a random partner depending on the company and the partner_type.
  59. The first 3/5 of the available partners are used as customer
  60. The last 3/5 of the available partners are used as suppliers
  61. It means 1/5 is both customer/supplier
  62. -> Same proportions as in account.move
  63. :param random: seeded random number generator.
  64. :param values (dict): the values already selected for the record.
  65. :return (int): the id of the partner randomly selected.
  66. """
  67. partner_type = values['partner_type']
  68. company_id = values['company_id']
  69. partner_ids = search_partner_ids(company_id)
  70. if partner_type == 'customer':
  71. return random.choice(partner_ids[:math.ceil(len(partner_ids)/5*2)])
  72. else:
  73. return random.choice(partner_ids[math.floor(len(partner_ids)/5*2):])
  74. def get_journal(random, values, **kwargs):
  75. """Get a random bank or cash journal depending on the company.
  76. :param random: seeded random number generator.
  77. :param values (dict): the values already selected for the record.
  78. :return (int): the id of the journal randomly selected
  79. """
  80. return random.choice(search_journal_ids(values['company_id']))
  81. def get_payment_method_line(random, values, **kwargs):
  82. """Get the payment method depending on the payment type.
  83. :param random: seeded random number generator.
  84. :param values (dict): the values already selected for the record.
  85. """
  86. return random.choice(search_payment_method_line_ids(values['payment_type'], values['journal_id']))
  87. company_ids = self.env['res.company'].search([
  88. ('chart_template_id', '!=', False),
  89. ('id', 'in', self.env.registry.populated_models['res.company']),
  90. ])
  91. return [
  92. ('company_id', populate.cartesian(company_ids.ids)),
  93. ('payment_type', populate.cartesian(['inbound', 'outbound'])),
  94. ('partner_type', populate.cartesian(['customer', 'supplier'])),
  95. ('journal_id', populate.compute(get_journal)),
  96. ('payment_method_line_id', populate.compute(get_payment_method_line)),
  97. ('partner_id', populate.compute(get_partner)),
  98. ('amount', populate.randfloat(0, 1000)),
  99. ('date', populate.randdatetime(relative_before=relativedelta(years=-4))),
  100. ]
  101. def _populate(self, size):
  102. records = super()._populate(size)
  103. _logger.info('Validating Payments')
  104. records.move_id.filtered(lambda r: r.date < fields.Date.today()).action_post()
  105. return records