~vauxoo/addons-vauxoo/8.0-import_tax_tariff-dev-yani-rev-2

« back to all changes in this revision

Viewing changes to price_structure/model/sale.py

  • Committer: Jose Morales
  • Date: 2013-03-20 19:12:38 UTC
  • mto: This revision was merged to the branch mainline in revision 519.
  • Revision ID: jose@vauxoo.com-20130320191238-h1ndo5du24m6ler9
 
[ADD] To have a price structure management was created this module
      [ADD] Added price management directly for products and categories in the form view
      [ADD] Added price selection on sale order line to set price for sale line, it filter for product in the line 
      [ADD] Added a validation to not sell products at a price lower or higher that indicated in the margin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- encoding: utf-8 -*-
 
3
###########################################################################
 
4
#    Module Writen to OpenERP, Open Source Management Solution
 
5
#    Copyright (C) OpenERP Venezuela (<http://openerp.com.ve>).
 
6
#    All Rights Reserved
 
7
###############Credits######################################################
 
8
#    Coded by: Vauxoo C.A.           
 
9
#    Planified by: Nhomar Hernandez
 
10
#    Audited by: Vauxoo C.A.
 
11
#############################################################################
 
12
#    This program is free software: you can redistribute it and/or modify
 
13
#    it under the terms of the GNU Affero General Public License as published by
 
14
#    the Free Software Foundation, either version 3 of the License, or
 
15
#    (at your option) any later version.
 
16
#
 
17
#    This program is distributed in the hope that it will be useful,
 
18
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
#    GNU Affero General Public License for more details.
 
21
#
 
22
#    You should have received a copy of the GNU Affero General Public License
 
23
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
24
################################################################################
 
25
 
 
26
from osv import fields, osv
 
27
import tools
 
28
from tools.translate import _
 
29
from tools import config
 
30
import netsvc
 
31
import decimal_precision as dp
 
32
 
 
33
 
 
34
class sale_order_line(osv.osv):
 
35
    
 
36
    def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,        
 
37
            uom=False, qty_uos=0, uos=False, name='', partner_id=False,
 
38
            lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False,context=None):
 
39
        '''
 
40
        Overridden the method of product line sales, to replace the unit price calculation and selection of the cost structure 
 
41
        that handles the product, and later to filter the prices for the product selected
 
42
        '''
 
43
            
 
44
        if context is None:
 
45
            context ={}
 
46
        price_obj = self.pool.get('product.pricelist')
 
47
        product_obj = self.pool.get('product.product')
 
48
        product_brw = product and product_obj.browse(cr,uid,product,context=context)
 
49
        res = super(sale_order_line,self).product_id_change(cr, uid, ids, pricelist, product, qty=qty,
 
50
            uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
 
51
            lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag,context=context)
 
52
        if context.get('price_change',False):
 
53
            price = price_obj.price_get(cr, uid, [context.get('price_change',False)], product, qty, context=context)
 
54
            res.get('value',{}).update({'price_unit': round(price.get(context.get('price_change',False)),2)})
 
55
        res.get('value',False) and product_brw and product_brw.categ_id and res.get('value',False).update({'categ_id':product_brw.categ_id.id })
 
56
        res.get('value',False) and 'price_unit' in res.get('value',False)  and res['value'].pop('price_unit') 
 
57
        return res
 
58
    
 
59
    
 
60
    def price_unit(self,cr,uid,ids,price_list,product_id,qty,context=None):
 
61
        '''
 
62
        Calculating the amount of model _compute_price method product.uom
 
63
        '''
 
64
        if context is None:
 
65
            context = {}
 
66
        res = {'value':{}}
 
67
        if price_list and product_id and qty:
 
68
            price_obj = self.pool.get('product.pricelist')
 
69
            price = price_obj.price_get(cr, uid, [price_list], product_id, qty, context=context)
 
70
            res['value'].update({'price_unit': round(price.get(price_list),2)})
 
71
        return res
 
72
    #
 
73
    _inherit = 'sale.order.line'
 
74
    _columns = {
 
75
        'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True),
 
76
        'price_list_ids':fields.many2one('product.pricelist','Select Price'),
 
77
        'cost_structure_id':fields.many2one('cost.structure','Cost Structure'),
 
78
        'categ_id':fields.many2one('product.category','Category',help='Category by product selected'),
 
79
    
 
80
    }
 
81
    
 
82
sale_order_line()
 
83
 
 
84
class sale_order(osv.osv):
 
85
   
 
86
   
 
87
    
 
88
    _inherit = 'sale.order'
 
89
    
 
90
        
 
91
    def _price_status(self, cr, uid, ids, field_name, arg, context=None):
 
92
        '''
 
93
        Check That the products sold are not sold at a price less than or greater than the price rago allocated in the product. 
 
94
        Failure to comply with this will print a message informing the product that is not complying with this requirement
 
95
        '''
 
96
        if context is None:
 
97
            context = {}
 
98
        if not ids:
 
99
            return {}
 
100
        res = {}
 
101
        product = []
 
102
        context.update({'query':False})
 
103
        pricelist_obj = self.pool.get('product.pricelist')
 
104
        for order in len(ids) == 1 and self.browse(cr,uid,ids,context=context) or []:
 
105
            for line in order.order_line:
 
106
                price_compute = line.product_id and [ pricelist_obj.price_get(cr, uid, [i.price_list_id and i.price_list_id.id ],
 
107
                                                      line.product_id.id, line.product_uom_qty, context=context).get(i.price_list_id.id) for i in line.product_id.price_list_item_ids or line.product_id.category_item_ids]
 
108
                
 
109
                property_cost_structure = line and line.product_id and line.product_id.property_cost_structure and line.product_id.property_cost_structure.id or False
 
110
                if property_cost_structure and len(price_compute) == len([i for i in price_compute if round(line.price_unit,2) < round(i,2)]):
 
111
                    product.append(u'Intenta vender el producto %s a un precio menor al estimado para su venta'%line.product_id.name)
 
112
                    res[order.id] = {'status_bool':True}
 
113
                
 
114
                elif property_cost_structure and len(price_compute) == len([i for i in price_compute if round(line.price_unit,2) > round(i,2)]):
 
115
                    product.append(u'Intenta vender el producto %s a un precio mayor al estimado para su venta'%line.product_id.name)
 
116
                    res[order.id] = {'status_bool':True}
 
117
                
 
118
                
 
119
                elif not property_cost_structure:
 
120
                    product.append(u'El producto %s no tiene una estructura de costo'%line.product_id.name)
 
121
                    res[order.id] = {'status_bool':True}
 
122
            
 
123
            if product:
 
124
                res[order.id] = '\n'.join(product)            
 
125
            else:
 
126
                res[order.id] = {'status_bool':False}
 
127
                product = []
 
128
                res[order.id] = '\n'.join(product)  
 
129
                
 
130
        return res
 
131
        
 
132
        
 
133
    _columns = {
 
134
    
 
135
        'status_price':fields.function(_price_status, method=True,type="text", store=True, string='Status Price'),
 
136
        'status_bool':fields.function(_price_status, method=True,type="boolean", string='Status Price'),
 
137
        
 
138
    }
 
139
    
 
140
    _defaults = {
 
141
    'status_bool':False
 
142
    
 
143
    
 
144
    }
 
145
    
 
146
    def price_unit_confirm(self,cr,uid,ids,context=None):
 
147
        '''
 
148
        Workflow condition does not allow the sale process if at least one product is being sold in the price range set out in its cost structure
 
149
        '''
 
150
        if context is None:
 
151
            context = {}
 
152
        product = []
 
153
        context.update({'query':False})
 
154
        sale_brw = self.browse(cr,uid,ids and ids[0],context=context)
 
155
        pricelist_obj = self.pool.get('product.pricelist')
 
156
        for line in len(ids) == 1 and sale_brw.order_line or []:
 
157
            property_cost_structure = line and line.product_id and line.product_id.property_cost_structure and line.product_id.property_cost_structure.id or False
 
158
            price_compute = line.product_id and [ pricelist_obj.price_get(cr, uid, [i.price_list_id and i.price_list_id.id ],
 
159
                                                      line.product_id.id, line.product_uom_qty, context=context).get(i.price_list_id.id) for i in line.product_id.price_list_item_ids or line.product_id.category_item_ids]
 
160
                            
 
161
            if property_cost_structure and len(price_compute) == len([i for i in price_compute if round(line.price_unit,2) < round(i,2)]):
 
162
                product.append(u'Intenta vender el producto %s a un precio menor al estimado para su venta'%line.product_id.name)
 
163
        
 
164
            elif property_cost_structure and len(price_compute) == len([i for i in price_compute if round(line.price_unit,2) > round(i,2)]):
 
165
                product.append(u'Intenta vender el producto %s a un precio mayor al estimado para su venta'%line.product_id.name)
 
166
 
 
167
 
 
168
            elif not property_cost_structure:
 
169
                product.append(u'The product %s has not a cost structure'%line.product_id.name)
 
170
                    
 
171
        if len(product) > 0:
 
172
            raise osv.except_osv(_('Error'), _('\n'.join(product)))
 
173
       
 
174
        return True
 
175
    
 
176
    
 
177
sale_order()
 
178
 
 
179