account_invoice.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Onestein (<http://www.onestein.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. class AccountInvoice(models.Model):
  6. _inherit = 'account.invoice'
  7. pricelist_id = fields.Many2one(
  8. 'product.pricelist',
  9. 'Pricelist',
  10. help="Pricelist for current invoice.")
  11. @api.multi
  12. def onchange_partner_id(self, type, partner_id, date_invoice=False,
  13. payment_term=False, partner_bank_id=False,
  14. company_id=False):
  15. res = super(AccountInvoice, self).onchange_partner_id(
  16. type, partner_id, date_invoice=False,
  17. payment_term=False, partner_bank_id=False,
  18. company_id=False)
  19. if type in ['out_invoice', 'out_refund']:
  20. res['value'].update({
  21. 'pricelist_id': None,
  22. })
  23. if partner_id:
  24. partner = self.env['res.partner'].browse(partner_id)
  25. if partner.property_product_pricelist:
  26. res['value'].update({
  27. 'pricelist_id': partner.property_product_pricelist.id,
  28. 'currency_id': partner.property_product_pricelist.currency_id.id
  29. })
  30. if type in ['in_invoice', 'in_refund']:
  31. res['value'].update({
  32. 'pricelist_id': None,
  33. })
  34. if partner_id:
  35. partner = self.env['res.partner'].browse(partner_id)
  36. if partner.property_product_pricelist_purchase:
  37. res['value'].update({
  38. 'pricelist_id': partner.property_product_pricelist_purchase.id,
  39. 'currency_id': partner.property_product_pricelist.currency_id.id
  40. })
  41. return res
  42. @api.one
  43. @api.onchange('pricelist_id')
  44. def onchange_pricelist_id(self):
  45. self.currency_id = self.pricelist_id.currency_id.id