~unifield-team/unifield-wm/wm-2418-rw-fix

« back to all changes in this revision

Viewing changes to unifield_setup/installer/sales_price.py

  • Committer: pierre-marie
  • Date: 2012-07-25 14:13:53 UTC
  • mfrom: (1038 unifield-wm)
  • mto: This revision was merged to the branch mainline in revision 1060.
  • Revision ID: pierre-marie@pierre-marie-laptop-20120725141353-9iwjdr1kltbei90e
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2011 TeMPO Consulting, MSF
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
20
##############################################################################
 
21
 
 
22
from osv import osv
 
23
from osv import fields
 
24
 
 
25
from tools.translate import _
 
26
 
 
27
 
 
28
class sale_price_setup(osv.osv_memory):
 
29
    _name = 'sale.price.setup'
 
30
    _inherit = 'res.config'
 
31
    
 
32
    _columns = {
 
33
        'sale_price': fields.float(digits=(16,2), string='Fields price percentage', required=True,
 
34
                                   help='This percentage will be applied on field price from product form view.'),
 
35
    }
 
36
    
 
37
    def _check_sale_price_negative_value(self, cr, uid, ids, context=None):
 
38
        '''
 
39
        Check if the entered value is more than 0.00%
 
40
        '''
 
41
        for price in self.browse(cr, uid, ids, context=context):
 
42
            if price < 0.00:
 
43
                return False
 
44
        
 
45
        return True
 
46
    
 
47
    _constraints = [
 
48
        (_check_sale_price_negative_value, 'You cannot have a negative field price percentage !', ['sale_price']),
 
49
    ]
 
50
    
 
51
    def sale_price_change(self, cr, uid, ids, sale_price, context=None):
 
52
        '''
 
53
        Check if the entered value is more than 0.00%
 
54
        '''
 
55
        res = {}
 
56
        
 
57
        if sale_price < 0.00:
 
58
            res.update({'value': {'sale_price': 0.00},
 
59
                        'warning': {'title': 'Wrong value !',
 
60
                                    'message': 'You cannot have a negative field price percentage !'}})
 
61
        
 
62
        return res
 
63
    
 
64
    def default_get(self, cr, uid, fields, context=None):
 
65
        '''
 
66
        Display the default value for sale price
 
67
        '''
 
68
        setup_id = self.pool.get('unifield.setup.configuration').get_config(cr, uid)
 
69
        
 
70
        res = super(sale_price_setup, self).default_get(cr, uid, fields, context=context)
 
71
        
 
72
        res['sale_price'] = setup_id.sale_price
 
73
        
 
74
        return res
 
75
    
 
76
    def execute(self, cr, uid, ids, context=None):
 
77
        '''
 
78
        Fill the delivery process field in company
 
79
        '''
 
80
        assert len(ids) == 1, "We should only get one object from the form"
 
81
        payload = self.browse(cr, uid, ids[0], context=context)
 
82
        
 
83
        setup_obj = self.pool.get('unifield.setup.configuration')
 
84
        pricelist_obj = self.pool.get('product.pricelist')
 
85
        version_obj = self.pool.get('product.pricelist.version')
 
86
        item_obj = self.pool.get('product.pricelist.item')
 
87
        
 
88
        setup_id = setup_obj.get_config(cr, uid)
 
89
            
 
90
        # Update all sale pricelists
 
91
        pricelist_ids = pricelist_obj.search(cr, uid, [('type', '=', 'sale')], context=context)
 
92
        version_ids = version_obj.search(cr, uid, [('pricelist_id', 'in', pricelist_ids)], context=context)
 
93
        item_ids = item_obj.search(cr, uid, [('price_version_id', 'in', version_ids)], context=context)
 
94
        item_obj.write(cr, uid, item_ids, {'price_discount': payload.sale_price/100}, context=context)
 
95
    
 
96
        setup_obj.write(cr, uid, [setup_id.id], {'sale_price': payload.sale_price}, context=context)
 
97
        
 
98
sale_price_setup()
 
99
 
 
100
 
 
101
class product_pricelist_item(osv.osv):
 
102
    _name = 'product.pricelist.item'
 
103
    _inherit = 'product.pricelist.item'
 
104
    
 
105
    def create(self, cr, uid, vals, context=None):
 
106
        '''
 
107
        if the item is related to a sale price list, get the Unifield
 
108
        configuration value for price_discount
 
109
        '''
 
110
        setup_id = self.pool.get('unifield.setup.configuration').get_config(cr, uid)
 
111
        version_obj = self.pool.get('product.pricelist.version')
 
112
        
 
113
        if 'price_version_id' in vals:
 
114
            price_type = version_obj.browse(cr, uid, vals['price_version_id'], context=context).pricelist_id.type
 
115
            if price_type == 'sale':
 
116
                # Get the price from Unifield configuration
 
117
                if setup_id:
 
118
                    price_discount = setup_id.sale_price
 
119
                    vals.update({'price_discount': price_discount/100})
 
120
        
 
121
        return super(product_pricelist_item, self).create(cr, uid, vals, context=context)
 
122
    
 
123
product_pricelist_item()
 
 
b'\\ No newline at end of file'