l10n_latam_payment_mass_transfer.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. from odoo import models, api, fields, _
  3. from odoo.exceptions import UserError
  4. class L10nLatamPaymentMassTransfer(models.TransientModel):
  5. _name = 'l10n_latam.payment.mass.transfer'
  6. _description = 'Checks Mass Transfers'
  7. payment_date = fields.Date(
  8. string="Payment Date",
  9. required=True,
  10. default=fields.Date.context_today,
  11. )
  12. destination_journal_id = fields.Many2one(
  13. comodel_name='account.journal',
  14. string='Destination Journal',
  15. domain="[('type', 'in', ('bank', 'cash')), ('company_id', '=', company_id), ('id', '!=', journal_id)]",
  16. )
  17. communication = fields.Char(
  18. string="Memo",
  19. )
  20. journal_id = fields.Many2one(
  21. 'account.journal',
  22. compute='_compute_journal_company'
  23. )
  24. company_id = fields.Many2one(
  25. 'res.company',
  26. compute="_compute_journal_company"
  27. )
  28. check_ids = fields.Many2many(
  29. 'account.payment',
  30. )
  31. @api.depends('check_ids')
  32. def _compute_journal_company(self):
  33. # use ._origin because if not a NewId for the checks is used and the returned
  34. # value for l10n_latam_check_current_journal_id is wrong
  35. journal = self.check_ids._origin.mapped("l10n_latam_check_current_journal_id")
  36. if len(journal) != 1 or not journal.inbound_payment_method_line_ids.filtered(
  37. lambda x: x.code == 'in_third_party_checks'):
  38. raise UserError(_("All selected checks must be on the same journal and on hand"))
  39. self.journal_id = journal
  40. self.company_id = journal.company_id.id
  41. @api.model
  42. def default_get(self, fields_list):
  43. res = super().default_get(fields_list)
  44. if 'check_ids' in fields_list and 'check_ids' not in res:
  45. if self._context.get('active_model') != 'account.payment':
  46. raise UserError(_("The register payment wizard should only be called on account.payment records."))
  47. checks = self.env['account.payment'].browse(self._context.get('active_ids', []))
  48. if checks.filtered(lambda x: x.payment_method_line_id.code != 'new_third_party_checks'):
  49. raise 'You have select some payments that are not checks. Please call this action from the Third Party Checks menu'
  50. elif not all(check.state == 'posted' for check in checks):
  51. raise UserError(_("All the selected checks must be posted"))
  52. res['check_ids'] = checks.ids
  53. return res
  54. def _create_payments(self):
  55. """ This is nedeed because we would like to create a payment of type internal transfer for each check with the
  56. counterpart journal and then, when posting a second payment will be created automatically """
  57. self.ensure_one()
  58. checks = self.check_ids.filtered(lambda x: x.payment_method_line_id.code == 'new_third_party_checks')
  59. payment_vals_list = []
  60. pay_method_line = self.journal_id._get_available_payment_method_lines('outbound').filtered(
  61. lambda x: x.code == 'out_third_party_checks')
  62. for check in checks:
  63. payment_vals_list.append({
  64. 'date': self.payment_date,
  65. 'l10n_latam_check_id': check.id,
  66. 'amount': check.amount,
  67. 'payment_type': 'outbound',
  68. 'ref': self.communication,
  69. 'journal_id': self.journal_id.id,
  70. 'currency_id': check.currency_id.id,
  71. 'is_internal_transfer': True,
  72. 'payment_method_line_id': pay_method_line.id,
  73. 'destination_journal_id': self.destination_journal_id.id,
  74. })
  75. payments = self.env['account.payment'].create(payment_vals_list)
  76. payments.action_post()
  77. return payments
  78. def action_create_payments(self):
  79. payments = self._create_payments()
  80. action = {
  81. 'name': _('Payments'),
  82. 'type': 'ir.actions.act_window',
  83. 'res_model': 'account.payment',
  84. 'context': {'create': False},
  85. }
  86. if len(payments) == 1:
  87. action.update({
  88. 'view_mode': 'form',
  89. 'res_id': payments.id,
  90. })
  91. else:
  92. action.update({
  93. 'view_mode': 'tree,form',
  94. 'domain': [('id', 'in', payments.ids)],
  95. })
  96. return action