~camptocamp/sale-financial/7.0-port-sale_markup

« back to all changes in this revision

Viewing changes to sale_floor_price/floor_sale.py

  • Committer: Alexandre Fayolle @ camptocamp
  • Date: 2012-05-29 07:06:32 UTC
  • Revision ID: alexandre.fayolle@camptocamp.com-20120529070632-1ahmpjkrwz4o7my9
[MERGE] from bzr+ssh://bazaar.launchpad.net/+branch/c2c-addons/trunk/
(lp:c2c-addons/6.1  rev 28.2.1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Copyright (c) 2011 Camptocamp SA (http://www.camptocamp.com)
 
5
#    All Right Reserved
 
6
#
 
7
#    Author : Joel Grand-Guillaume (Camptocamp)
 
8
#
 
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.
 
13
#
 
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.
 
18
#
 
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/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from osv.orm import Model
 
25
from tools.translate import _
 
26
 
 
27
class SaleOrderLine(Model):
 
28
    _inherit = 'sale.order.line'
 
29
 
 
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):
 
35
            return True
 
36
        return False
 
37
 
 
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))
 
42
 
 
43
    def _compute_lowest_price(self, cr, uid, floor_price, discount):
 
44
        if discount == 100.0:
 
45
            res = 0.0
 
46
        else:
 
47
            res = floor_price / (1-(discount / 100.0))
 
48
        return res
 
49
 
 
50
    def product_id_change(self, cr, uid, ids, *args, **kwargs):
 
51
        '''
 
52
        Overload method:
 
53
            - Empty the discount when changing.
 
54
        '''
 
55
        res = super(SaleOrderLine, self).product_id_change(cr, uid, ids, *args, **kwargs)
 
56
        res['value']['discount'] = 0.0
 
57
        return res
 
58
 
 
59
 
 
60
    def onchange_price_unit(self, cr, uid, ids, price_unit, product_id, discount, product_uom,
 
61
                            pricelist, **kwargs):
 
62
        '''
 
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
 
65
        we leave it empty...
 
66
        '''
 
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,
 
70
                                                             pricelist, **kwargs)
 
71
        self._check_floor_price(cr, uid, res, price_unit, product_id, discount, override_unit_price)
 
72
        return res
 
73
 
 
74
 
 
75
    def onchange_discount(self, cr, uid, ids, price_unit, product_id, discount, product_uom, pricelist, **kwargs):
 
76
        '''
 
77
        If discount change, check that final price is not < floor_price_limit of related product
 
78
        '''
 
79
        res = super(SaleOrderLine, self).onchange_discount(cr, uid, ids, price_unit, product_id,
 
80
                                                           discount, product_uom, pricelist)
 
81
 
 
82
        self._check_floor_price(cr, uid, res, price_unit, product_id, discount)
 
83
 
 
84
 
 
85
    def _check_floor_price(self, cr, uid, result, price_unit, product_id, discount, override_unit_price=True):
 
86
        """
 
87
        result is a partially filled result dictionary, modified in place
 
88
        """
 
89
        if 'value' not in result:
 
90
            result['value'] = {}
 
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)
 
97
                else:
 
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.") 
 
106
 
 
107
                warning = {'title': _('Floor price reached !'),
 
108
                           'message': warn_msg % substs}
 
109
                result['warning'] = warning
 
110
                result['domain'] = {}