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.

38 lines
1.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Carlos Dauden - Tecnativa <carlos.dauden@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp import api, fields, models
  5. class AccountAnalyticAccount(models.Model):
  6. _inherit = 'account.analytic.account'
  7. mandate_id = fields.Many2one(
  8. comodel_name='account.banking.mandate',
  9. ondelete='restrict',
  10. string='Direct Debit Mandate',
  11. help="If mandate required in payment method and not set mandate, "
  12. "invoice takes the first valid mandate"
  13. )
  14. mandate_required = fields.Boolean(
  15. related='payment_mode_id.payment_method_id.mandate_required',
  16. readonly=True)
  17. commercial_partner_id = fields.Many2one(
  18. related='partner_id.commercial_partner_id',
  19. readonly=True,
  20. string='Commercial Entity',
  21. )
  22. @api.multi
  23. def _prepare_invoice(self):
  24. invoice_vals = super(AccountAnalyticAccount, self)._prepare_invoice()
  25. if self.mandate_id:
  26. invoice_vals['mandate_id'] = self.mandate_id.id
  27. elif self.payment_mode_id.payment_method_id.mandate_required:
  28. mandate = self.env['account.banking.mandate'].search([
  29. ('partner_id', '=', self.partner_id.commercial_partner_id.id),
  30. ('state', '=', 'valid'),
  31. ], limit=1)
  32. invoice_vals['mandate_id'] = mandate.id
  33. return invoice_vals