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.

42 lines
1.5 KiB

  1. from odoo import api, fields, models
  2. class ContractContract(models.Model):
  3. _inherit = 'contract.contract'
  4. payment_mode_id = fields.Many2one(
  5. comodel_name='account.payment.mode',
  6. string='Payment Mode',
  7. domain=[('payment_type', '=', 'inbound')],
  8. index=True,
  9. )
  10. @api.onchange('partner_id')
  11. def on_change_partner_id(self):
  12. self.payment_mode_id = self.partner_id.customer_payment_mode_id.id
  13. @api.multi
  14. def _prepare_invoice(self, date_invoice, journal=None):
  15. invoice_vals = super(ContractContract, self)._prepare_invoice(
  16. date_invoice=date_invoice, journal=journal)
  17. if self.payment_mode_id:
  18. invoice_vals['payment_mode_id'] = self.payment_mode_id.id
  19. invoice = self.env['account.invoice'].new(invoice_vals)
  20. invoice._onchange_payment_mode_id()
  21. invoice_vals = invoice._convert_to_write(invoice._cache)
  22. return invoice_vals
  23. @api.model
  24. def _finalize_invoice_creation(self, invoices):
  25. """
  26. This override preserves the payment mode when calling the partner's
  27. onchange.
  28. """
  29. payment_modes_by_invoice = {}
  30. for invoice in invoices:
  31. payment_modes_by_invoice[invoice] = invoice.payment_mode_id
  32. res = super(ContractContract, self)._finalize_invoice_creation(
  33. invoices)
  34. for invoice in invoices:
  35. invoice.payment_mode_id = payment_modes_by_invoice.get(invoice)
  36. return res