account_invoice_line.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, api
  5. class AccountInvoiceLine(models.Model):
  6. _inherit = 'account.invoice.line'
  7. @api.multi
  8. def product_id_change(
  9. self, product, uom_id, qty=0, name='', type='out_invoice',
  10. partner_id=False, fposition_id=False, price_unit=False,
  11. currency_id=False, company_id=None):
  12. res = super(AccountInvoiceLine, self).product_id_change(
  13. product, uom_id, qty=qty, name=name, type=type,
  14. partner_id=partner_id, fposition_id=fposition_id,
  15. price_unit=price_unit, currency_id=currency_id,
  16. company_id=company_id)
  17. if not res or not res['value']:
  18. return res
  19. if not partner_id or not product:
  20. return res
  21. if currency_id:
  22. currency = self.env['res.currency'].browse(currency_id)
  23. rounded_price_unit = currency.round(res['value']['price_unit'])
  24. if 'price_unit' not in res['value']\
  25. or not res['value']['price_unit']\
  26. or 'uos_id' in res['value']\
  27. or 'quantity' in res['value']\
  28. or rounded_price_unit == price_unit:
  29. product_data = self.env['product.product'].browse(product)
  30. price_unit = product_data.lst_price
  31. partner = self.env['res.partner'].browse(partner_id)
  32. pricelist = self._context.get('pricelist_id', False) or \
  33. (partner.property_product_pricelist and
  34. partner.property_product_pricelist.id) \
  35. or None
  36. if pricelist:
  37. pricelist = self.env['product.pricelist'].browse(pricelist)
  38. if 'uos_id' in res['value'] and res['value']['uos_id'] and res['value']['uos_id'] != product_data.uom_id.id:
  39. pricedict = pricelist.with_context(uom=res['value']['uos_id']).price_get(product, qty, partner_id)
  40. else:
  41. pricedict = pricelist.price_get(product, qty, partner_id)
  42. price_unit = pricedict[pricelist.id]
  43. price_unit = pricelist.currency_id.compute(
  44. price_unit, currency, round=True)
  45. price_unit = currency.round(price_unit)
  46. res['value']['price_unit'] = price_unit
  47. return res