1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Copyright (c) 2011 Camptocamp SA (http://www.camptocamp.com)
7
# Author : Joel Grand-Guillaume (Camptocamp)
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU Affero General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
##############################################################################
24
from osv.orm import Model
25
from tools.translate import _
27
class SaleOrderLine(Model):
28
_inherit = 'sale.order.line'
30
def _reach_floor_price(self, cr, uid, floor_price, discount, price_unit):
31
sell_price = price_unit * (1 - (discount or 0.0) / 100.0)
32
precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Sale Price')
33
sell_price = round(sell_price, precision)
34
if (sell_price < floor_price):
38
def _compute_lowest_discount(self, cr, uid, floor_price, price_unit):
39
diff = (floor_price - price_unit)
40
disc = diff / price_unit
41
return abs(round(disc*100, 2))
43
def _compute_lowest_price(self, cr, uid, floor_price, discount):
47
res = floor_price / (1-(discount / 100.0))
50
def product_id_change(self, cr, uid, ids, *args, **kwargs):
53
- Empty the discount when changing.
55
res = super(SaleOrderLine, self).product_id_change(cr, uid, ids, *args, **kwargs)
56
res['value']['discount'] = 0.0
60
def onchange_price_unit(self, cr, uid, ids, price_unit, product_id, discount, product_uom,
63
If price unit change, check that it is not < floor_price_limit of related product.
64
If override_unit_price is True, we put in price_unit the min possible value, otherwise
67
override_unit_price = kwargs.pop('override_unit_price', True)
68
res = super(SaleOrderLine, self).onchange_price_unit(cr, uid, ids, price_unit,
69
product_id, discount, product_uom,
71
self._check_floor_price(cr, uid, res, price_unit, product_id, discount, override_unit_price)
75
def onchange_discount(self, cr, uid, ids, price_unit, product_id, discount, product_uom, pricelist, **kwargs):
77
If discount change, check that final price is not < floor_price_limit of related product
79
res = super(SaleOrderLine, self).onchange_discount(cr, uid, ids, price_unit, product_id,
80
discount, product_uom, pricelist)
82
self._check_floor_price(cr, uid, res, price_unit, product_id, discount)
85
def _check_floor_price(self, cr, uid, result, price_unit, product_id, discount, override_unit_price=True):
87
result is a partially filled result dictionary, modified in place
89
if 'value' not in result:
91
if product_id and price_unit > 0.0:
92
product_obj = self.pool.get('product.product')
93
prod = product_obj.browse(cr, uid, product_id)
94
if self._reach_floor_price(cr, uid, prod.floor_price_limit, discount, price_unit):
95
if override_unit_price:
96
result['value']['price_unit'] = self._compute_lowest_price(cr, uid, prod.floor_price_limit, discount)
98
result['value']['price_unit'] = price_unit
99
substs = {'price_unit':price_unit,
100
'discount': discount,
101
'floor_price': prod.floor_price_limit,
102
'min_price': result['value']['price_unit']}
103
warn_msg = _("You selected a unit price of %(price_unit)d.- with %(discount).2f discount.\n"
104
"The floor price has been set to %(floor_price)d.-,"
105
"so the mininum allowed value is %(min_price)d.")
107
warning = {'title': _('Floor price reached !'),
108
'message': warn_msg % substs}
109
result['warning'] = warning
110
result['domain'] = {}