Merge lp:~akretion-team/openobject-addons/trunk_addons_ref_product_available into lp:openobject-addons

Proposed by Benoit Guillot - http://www.akretion.com
Status: Needs review
Proposed branch: lp:~akretion-team/openobject-addons/trunk_addons_ref_product_available
Merge into: lp:openobject-addons
Diff against target: 185 lines (+90/-59)
1 file modified
stock/product.py (+90/-59)
To merge this branch: bzr merge lp:~akretion-team/openobject-addons/trunk_addons_ref_product_available
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+135609@code.launchpad.net

Description of the change

Hello,

This merge proposal refactors the method get_product_available() in the module stock.

It adds methods to prepare the where params (_get_where_params()) and the query (_get_query()) of the sql request which finds whether product is available or not.

It allows us to reuse these methods for a custom fields for example.

To post a comment you must log in.

Unmerged revisions

8078. By Benoit Guillot - http://www.akretion.com

[REF] ref method get_product_available by adding methods to prepare the params and the query of the sql request which finds the availability of the products

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'stock/product.py'
2--- stock/product.py 2012-11-02 17:34:07 +0000
3+++ stock/product.py 2012-11-22 08:49:24 +0000
4@@ -195,10 +195,16 @@
5 if (context.get('active_id', False)) and (context.get('active_model') == 'stock.location'):
6 return _('Products: ')+self.pool.get('stock.location').browse(cr, user, context['active_id'], context).name
7 return res
8+
9+ def _get_where_params(self, cr, uid, ids, context=None):
10+ """Prepare the list of tuples used in the sql request to get the
11+ availability of the products. This method may be overridden
12+ to implement custom calculation of the availability of the
13+ products (making sure to call super() to establish a clean
14+ extension chain).
15
16- def get_product_available(self, cr, uid, ids, context=None):
17- """ Finds whether product is available or not in particular warehouse.
18- @return: Dictionary of values
19+ :param list(int) ids: list of product ids to get availability
20+ :return: list of tuples
21 """
22 if context is None:
23 context = {}
24@@ -208,13 +214,7 @@
25 shop_obj = self.pool.get('sale.shop')
26
27 states = context.get('states',[])
28- what = context.get('what',())
29- if not ids:
30- ids = self.search(cr, uid, [])
31- res = {}.fromkeys(ids, 0.0)
32- if not ids:
33- return res
34-
35+
36 if context.get('shop', False):
37 warehouse_id = shop_obj.read(cr, uid, int(context['shop']), ['warehouse_id'])['warehouse_id'][0]
38 if warehouse_id:
39@@ -243,66 +243,97 @@
40 child_location_ids = location_obj.search(cr, uid, [('location_id', 'child_of', location_ids)])
41 location_ids = child_location_ids or location_ids
42
43- # this will be a dictionary of the product UoM by product id
44- product2uom = {}
45- uom_ids = []
46- for product in self.read(cr, uid, ids, ['uom_id'], context=context):
47- product2uom[product['id']] = product['uom_id'][0]
48- uom_ids.append(product['uom_id'][0])
49- # this will be a dictionary of the UoM resources we need for conversion purposes, by UoM id
50- uoms_o = {}
51- for uom in self.pool.get('product.uom').browse(cr, uid, uom_ids, context=context):
52- uoms_o[uom.id] = uom
53-
54- results = []
55- results2 = []
56-
57+ where = [tuple(location_ids),tuple(location_ids),tuple(ids),tuple(states)]
58+
59+ if context.get('date_filter') in ['between', 'from_date']:
60+ where.append(tuple([from_date]))
61+ if context.get('date_filter') in ['between', 'to_date']:
62+ where.append(tuple([to_date]))
63+
64+ return where
65+
66+ def _get_query(self, cr, uid, what, context=None):
67+ """Prepare the string query used in the sql request to get the
68+ availability of the products. This method may be overridden
69+ to implement custom calculation of the availability of the
70+ products (making sure to call super() to establish a clean
71+ extension chain).
72+
73+ :param str what: str to define type of location to search
74+ :param list(int) ids: list of ids of products to get availability
75+ :return: str
76+ """
77+ if context is None:
78+ context = {}
79+ date_query = {
80+ 'between': "date>=%s and date<=%s",
81+ 'from_date': "date>=%s",
82+ 'to_date': "date<=%s",
83+ }
84+
85+ prodlot_id = context.get('prodlot_id', False)
86+
87+ query = 'select sum(product_qty), product_id, product_uom '\
88+ 'from stock_move '\
89+ 'where location_id' + (what=='in' and ' NOT' or ' ') + ' IN %s '\
90+ 'and location_dest_id' + (what=='out' and ' NOT' or ' ') + ' IN %s '\
91+ 'and product_id IN %s '\
92+ '' + (prodlot_id and ('and prodlot_id = ' + str(prodlot_id)) or '') + ' '\
93+ 'and state IN %s ' + (date_query.get(context.get('date_filter')) or '') +' '\
94+ 'group by product_id,product_uom'
95+ return query
96+
97+ def get_product_available(self, cr, uid, ids, context=None):
98+ """ Finds whether product is available or not in particular warehouse.
99+ @return: Dictionary of values
100+ """
101+ if context is None:
102+ context = {}
103+
104+ what = context.get('what',())
105+ if not ids:
106+ ids = self.search(cr, uid, [])
107+ res = {}.fromkeys(ids, 0.0)
108+ if not ids:
109+ return res
110+
111 from_date = context.get('from_date',False)
112 to_date = context.get('to_date',False)
113- date_str = False
114- date_values = False
115- where = [tuple(location_ids),tuple(location_ids),tuple(ids),tuple(states)]
116+
117 if from_date and to_date:
118- date_str = "date>=%s and date<=%s"
119- where.append(tuple([from_date]))
120- where.append(tuple([to_date]))
121+ context['date_filter'] = 'between'
122 elif from_date:
123- date_str = "date>=%s"
124- date_values = [from_date]
125+ context['date_filter'] = 'from_date'
126 elif to_date:
127- date_str = "date<=%s"
128- date_values = [to_date]
129-
130- prodlot_id = context.get('prodlot_id', False)
131-
132- # TODO: perhaps merge in one query.
133- if date_values:
134- where.append(tuple(date_values))
135+ context['date_filter'] = 'to_date'
136+
137+ where = self._get_where_params(cr, uid, ids, context=context)
138+
139+ results = []
140+ results2 = []
141+ # TODO: perhaps merge in one query.
142 if 'in' in what:
143 # all moves from a location out of the set to a location in the set
144- cr.execute(
145- 'select sum(product_qty), product_id, product_uom '\
146- 'from stock_move '\
147- 'where location_id NOT IN %s '\
148- 'and location_dest_id IN %s '\
149- 'and product_id IN %s '\
150- '' + (prodlot_id and ('and prodlot_id = ' + str(prodlot_id)) or '') + ' '\
151- 'and state IN %s ' + (date_str and 'and '+date_str+' ' or '') +' '\
152- 'group by product_id,product_uom',tuple(where))
153+ query = self._get_query(cr, uid, 'in', context=context)
154+ cr.execute(query,tuple(where))
155 results = cr.fetchall()
156 if 'out' in what:
157 # all moves from a location in the set to a location out of the set
158- cr.execute(
159- 'select sum(product_qty), product_id, product_uom '\
160- 'from stock_move '\
161- 'where location_id IN %s '\
162- 'and location_dest_id NOT IN %s '\
163- 'and product_id IN %s '\
164- '' + (prodlot_id and ('and prodlot_id = ' + str(prodlot_id)) or '') + ' '\
165- 'and state in %s ' + (date_str and 'and '+date_str+' ' or '') + ' '\
166- 'group by product_id,product_uom',tuple(where))
167+ query = self._get_query(cr, uid, 'out', context=context)
168+ cr.execute(query,tuple(where))
169 results2 = cr.fetchall()
170-
171+
172+ # this will be a dictionary of the product UoM by product id
173+ product2uom = {}
174+ uom_ids = []
175+ for product in self.read(cr, uid, ids, ['uom_id'], context=context):
176+ product2uom[product['id']] = product['uom_id'][0]
177+ uom_ids.append(product['uom_id'][0])
178+ # this will be a dictionary of the UoM resources we need for conversion purposes, by UoM id
179+ uoms_o = {}
180+ for uom in self.pool.get('product.uom').browse(cr, uid, uom_ids, context=context):
181+ uoms_o[uom.id] = uom
182+
183 # Get the missing UoM resources
184 uom_obj = self.pool.get('product.uom')
185 uoms = map(lambda x: x[2], results) + map(lambda x: x[2], results2)

Subscribers

People subscribed via source and target branches

to all changes: