product_visible_discount.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
  6. # $Id$
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp import SUPERUSER_ID
  23. from openerp.osv import fields, osv
  24. from openerp.tools.translate import _
  25. class product_pricelist(osv.osv):
  26. _inherit = 'product.pricelist'
  27. _columns ={
  28. 'visible_discount': fields.boolean('Visible Discount'),
  29. }
  30. _defaults = {
  31. 'visible_discount': True,
  32. }
  33. class sale_order_line(osv.osv):
  34. _inherit = "sale.order.line"
  35. def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
  36. uom=False, qty_uos=0, uos=False, name='', partner_id=False,
  37. lang=False, update_tax=True, date_order=False, packaging=False,
  38. fiscal_position=False, flag=False, context=None):
  39. def get_real_price_curency(res_dict, product_id, qty, uom, pricelist):
  40. """Retrieve the price before applying the pricelist"""
  41. item_obj = self.pool.get('product.pricelist.item')
  42. price_type_obj = self.pool.get('product.price.type')
  43. product_obj = self.pool.get('product.product')
  44. field_name = 'list_price'
  45. rule_id = res_dict.get(pricelist) and res_dict[pricelist][1] or False
  46. currency_id = None
  47. if rule_id:
  48. item_base = item_obj.read(cr, uid, [rule_id], ['base'])[0]['base']
  49. if item_base > 0:
  50. price_type = price_type_obj.browse(cr, uid, item_base)
  51. field_name = price_type.field
  52. currency_id = price_type.currency_id
  53. product = product_obj.browse(cr, uid, product_id, context)
  54. product_read = product_obj.read(cr, uid, [product_id], [field_name], context=context)[0]
  55. if not currency_id:
  56. currency_id = product.company_id.currency_id.id
  57. factor = 1.0
  58. if uom and uom != product.uom_id.id:
  59. # the unit price is in a different uom
  60. factor = self.pool['product.uom']._compute_qty(cr, uid, uom, 1.0, product.uom_id.id)
  61. return product_read[field_name] * factor, currency_id
  62. def get_real_price(res_dict, product_id, qty, uom, pricelist):
  63. return get_real_price_curency(res_dict, product_id, qty, uom, pricelist)[0]
  64. res=super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
  65. uom, qty_uos, uos, name, partner_id,
  66. lang, update_tax, date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
  67. context = {'lang': lang, 'partner_id': partner_id}
  68. result=res['value']
  69. pricelist_obj=self.pool.get('product.pricelist')
  70. product_obj = self.pool.get('product.product')
  71. account_tax_obj = self.pool.get('account.tax')
  72. if product and pricelist and self.pool.get('res.users').has_group(cr, uid, 'sale.group_discount_per_so_line'):
  73. if result.get('price_unit',False):
  74. price=result['price_unit']
  75. else:
  76. return res
  77. uom = result.get('product_uom', uom)
  78. product = product_obj.browse(cr, uid, product, context)
  79. pricelist_context = dict(context, uom=uom, date=date_order)
  80. list_price = pricelist_obj.price_rule_get(cr, uid, [pricelist],
  81. product.id, qty or 1.0, partner_id, context=pricelist_context)
  82. so_pricelist = pricelist_obj.browse(cr, uid, pricelist, context=context)
  83. new_list_price, currency_id = get_real_price_curency(list_price, product.id, qty, uom, pricelist)
  84. # The superuser is used by website_sale in order to create a sale order. We need to make
  85. # sure we only select the taxes related to the company of the partner. This should only
  86. # apply if the partner is linked to a company.
  87. if uid == SUPERUSER_ID and context.get('company_id'):
  88. taxes = product.taxes_id.filtered(lambda r: r.company_id.id == context['company_id'])
  89. else:
  90. taxes = product.taxes_id
  91. new_list_price = account_tax_obj._fix_tax_included_price(cr, uid, new_list_price, taxes, result.get('tax_id', []))
  92. if so_pricelist.visible_discount and list_price[pricelist][0] != 0 and new_list_price != 0:
  93. if product.company_id and so_pricelist.currency_id.id != product.company_id.currency_id.id:
  94. # new_list_price is in company's currency while price in pricelist currency
  95. ctx = context.copy()
  96. ctx['date'] = date_order
  97. new_list_price = self.pool['res.currency'].compute(cr, uid,
  98. currency_id.id, so_pricelist.currency_id.id,
  99. new_list_price, context=ctx)
  100. discount = (new_list_price - price) / new_list_price * 100
  101. if discount > 0:
  102. result['price_unit'] = new_list_price
  103. result['discount'] = discount
  104. else:
  105. result['discount'] = 0.0
  106. else:
  107. result['discount'] = 0.0
  108. else:
  109. result['discount'] = 0.0
  110. return res