Merge lp:~openerp-dev/openobject-addons/6.0-bug_725908-ach into lp:openobject-addons/6.0

Proposed by Anup(SerpentCS)
Status: Merged
Merged at revision: 4534
Proposed branch: lp:~openerp-dev/openobject-addons/6.0-bug_725908-ach
Merge into: lp:openobject-addons/6.0
Diff against target: 139 lines (+59/-51)
1 file modified
stock/wizard/stock_fill_inventory.py (+59/-51)
To merge this branch: bzr merge lp:~openerp-dev/openobject-addons/6.0-bug_725908-ach
Reviewer Review Type Date Requested Status
Jay Vora (Serpent Consulting Services) (community) Approve
Review via email: mp+56554@code.launchpad.net

Description of the change

Hello,

   The problem with the Stock Fill Inventory has been fixed by this solution. Now all the stock moves will be imported to the Inventory with production lots.

Thanks.

To post a comment you must log in.
Revision history for this message
Rucha (Open ERP) (rpa-openerp) wrote :
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Anup,

Thanks to you and Kirti for the fix. It was really a good one.

Kindly go through these important points to improve here:

1. Variable naming convention.(Do not opt for i,j,k,v).
2. Wrong indentation on your proposal Line 21.
3. Just a note : Whenever we use child_of operator, go for the right operant with [] of ids. This doesn't create any problem as this has been covered well recently in expression.py, but its a good practice.(Refer to line 31)
4. Does the order='id' give you a different result when order='parent_left'? (Refer to Line 31)
5. Line 61 should be location_dest_id = location. Think over it.
6. Use a shorthand operator. (Refer to line 63)
7. Improve the notification, it could be notification or warning(Line 70).
8. You don't need line 73.
9. Why do we need len(line_ids)? (Line 90).
10. Regarding last 2 for loops, I am not 100% sure whether its an optimized solution or not.
11. Regarding Line 75, are you sure we always have one inventory to check? I doubt because, we can use this wizard for multiple inventories. And its always wise to check before writing list[0].

Correct me If I missed anything.

I am investigating more in order to get the best suited fix.

Thanks.

review: Needs Fixing
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Corrections:

4. Does the order='id' give you a different result than order='parent_left'? (Refer to Line 31)

Thanks.

Revision history for this message
Anup(SerpentCS) (anup-serpent) wrote :

Hello Jay,

   Thanks a lot for a detailed investigation. I have fixed many of the issues mentioned by you like indentations and naming conventions, child_of, shorthand operator,notifications etc.

4.Currently order = 'parent_left' is giving the same result as id. But IMHO it's better to keep id rather than parent_left.

5. You are right we don't need that line. It saves many read calls and we don't even need that variable.

11. Well The wizard should be used with a single inventory only. You can not use it with multiple inventories. I have kept a notification if more than one inventory is selected. So it solves the issue.

We even don't need the lines 38-42. When will it be the case when ids will not be there?

Please share your views.

Thanks.

Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Thanks for the corrections Anup.

However, I am investigating 3 changes here:
1. Return True would be good for view_init.
2. ids = ids[0] (Line 44)
3. order='id' (Line 56).

Thanks again.

review: Approve
Revision history for this message
Jay Vora (Serpent Consulting Services) (jayvora) wrote :

Anup,

Corrections have been made and the branch has been merged to stable 6.0.2.

Thanks for all your efforts.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'stock/wizard/stock_fill_inventory.py'
2--- stock/wizard/stock_fill_inventory.py 2011-01-14 00:11:01 +0000
3+++ stock/wizard/stock_fill_inventory.py 2011-04-18 07:33:37 +0000
4@@ -39,14 +39,18 @@
5 @param context: A standard dictionary
6 @return: New arch of view with new columns.
7 """
8- if context==None:
9- context={}
10- res = super(stock_fill_inventory, self).view_init(cr, uid, fields_list, context=context)
11+ if context is None:
12+ context = {}
13+ super(stock_fill_inventory, self).view_init(cr, uid, fields_list, context=context)
14+
15+ if len(context['active_ids']) > 1:
16+ raise osv.except_osv(_('Error!'), _('You can not perform this operation on more than one Stock Inventories'))
17+
18 if context.get('active_id', False):
19 stock = self.pool.get('stock.inventory').browse(cr, uid, context.get('active_id', False))
20+
21 if stock.state=='done':
22- raise osv.except_osv(_('Error!'), _('Stock Inventory is done'))
23- True
24+ raise osv.except_osv(_('Error!'), _('Stock Inventory is already Validated.'))
25
26 def fill_inventory(self, cr, uid, ids, context=None):
27 """ To Import stock inventory according to products available in the selected locations.
28@@ -58,61 +62,65 @@
29 @return:
30 """
31 if context is None:
32- context = {}
33+ context = {}
34+
35 inventory_line_obj = self.pool.get('stock.inventory.line')
36 location_obj = self.pool.get('stock.location')
37 product_obj = self.pool.get('product.product')
38- stock_location_obj = self.pool.get('stock.location')
39- if ids and len(ids):
40- ids = ids[0]
41- else:
42- return {'type': 'ir.actions.act_window_close'}
43+ move_obj = self.pool.get('stock.move')
44+ ids = ids[0]
45+
46 fill_inventory = self.browse(cr, uid, ids, context=context)
47 res = {}
48 res_location = {}
49+
50 if fill_inventory.recursive :
51 location_ids = location_obj.search(cr, uid, [('location_id',
52- 'child_of', fill_inventory.location_id.id)])
53- for location in location_ids :
54- res = location_obj._product_get(cr, uid, location)
55- res_location[location] = res
56+ 'child_of', [fill_inventory.location_id.id])], order="id")
57 else:
58- context.update({'compute_child': False})
59- res = location_obj._product_get(cr, uid,
60- fill_inventory.location_id.id, context=context)
61- res_location[fill_inventory.location_id.id] = res
62-
63- product_ids = []
64- for location in res_location.keys():
65- res = res_location[location]
66- for product_id in res.keys():
67- prod = product_obj.browse(cr, uid, product_id, context=context)
68- uom = prod.uom_id.id
69- context.update(uom=uom, compute_child=False)
70- amount = stock_location_obj._product_get(cr, uid,
71- location, [product_id], context=context)[product_id]
72- if(amount):
73- if fill_inventory.set_stock_zero:
74- amount = 0
75- line_ids=inventory_line_obj.search(cr, uid,
76- [('inventory_id', '=', context['active_ids']),
77- ('location_id', '=', location),
78- ('product_id', '=', product_id),
79- ('product_uom', '=', uom),
80- ('product_qty', '=', amount)])
81- if not len(line_ids):
82- inventory_line = {
83- 'inventory_id': context['active_ids'][0],
84- 'location_id': location,
85- 'product_id': product_id,
86- 'product_uom': uom,
87- 'product_qty': amount
88- }
89- inventory_line_obj.create(cr, uid, inventory_line)
90- product_ids.append(product_id)
91-
92- if(len(product_ids) == 0):
93- raise osv.except_osv(_('Message !'), _('No product in this location.'))
94+ location_ids = [fill_inventory.location_id.id]
95+
96+ res = {}
97+ flag = False
98+
99+ for location in location_ids:
100+ datas = {}
101+ res[location] = {}
102+ move_ids = move_obj.search(cr, uid, [('location_dest_id','=',location),('state','=','done')], context=context)
103+
104+ for move in move_obj.browse(cr, uid, move_ids, context=context):
105+ lot_id = move.prodlot_id.id
106+ prod_id = move.product_id.id
107+ qty = move.product_qty
108+
109+ if datas.get((prod_id, lot_id)):
110+ qty += datas[(prod_id, lot_id)]['product_qty']
111+
112+ datas[(prod_id, lot_id)] = {'product_id': prod_id, 'location_id': location, 'product_qty': qty, 'product_uom': move.product_id.uom_id.id, 'prod_lot_id': lot_id}
113+
114+ if datas:
115+ flag = True
116+ res[location] = datas
117+
118+ if not flag:
119+ raise osv.except_osv(_('Warning !'), _('No product in this location.'))
120+
121+ for stock_move in res.values():
122+ for stock_move_details in stock_move.values():
123+ stock_move_details.update({'inventory_id': context['active_ids'][0]})
124+ domain = []
125+
126+ if fill_inventory.set_stock_zero:
127+ stock_move_details.update({'product_qty': 0})
128+
129+ for field, value in stock_move_details.items():
130+ domain.append((field, '=', value))
131+
132+ line_ids = inventory_line_obj.search(cr, uid, domain, context=context)
133+
134+ if not line_ids:
135+ inventory_line_obj.create(cr, uid, stock_move_details, context=context)
136+
137 return {'type': 'ir.actions.act_window_close'}
138
139 stock_fill_inventory()