[REF] purchase: search view of purchase order and form view of merge order wizard
[odoo/odoo.git] / addons / stock / product.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 fields, osv
23 from tools.translate import _
24
25
26 class product_product(osv.osv):
27     _inherit = "product.product"
28     def view_header_get(self, cr, user, view_id, view_type, context):
29         res = super(product_product, self).view_header_get(cr, user, view_id, view_type, context)
30         if res: return res
31         if (context.get('location', False)):
32             return _('Products: ')+self.pool.get('stock.location').browse(cr, user, context['location'], context).name
33         return res
34
35     def get_product_available(self,cr,uid,ids,context=None):
36         if not context:
37             context = {}
38         states=context.get('states',[])
39         what=context.get('what',())
40         if not ids:
41             ids = self.search(cr, uid, [])
42         res = {}.fromkeys(ids, 0.0)
43         if not ids:
44             return res
45
46         if context.get('shop', False):
47             cr.execute('select warehouse_id from sale_shop where id=%s', (int(context['shop']),))
48             res2 = cr.fetchone()
49             if res2:
50                 context['warehouse'] = res2[0]
51
52         if context.get('warehouse', False):
53             cr.execute('select lot_stock_id from stock_warehouse where id=%s', (int(context['warehouse']),))
54             res2 = cr.fetchone()
55             if res2:
56                 context['location'] = res2[0]
57
58         if context.get('location', False):
59             if type(context['location']) == type(1):
60                 location_ids = [context['location']]
61             else:
62                 location_ids = context['location']
63         else:
64             location_ids = []
65             wids = self.pool.get('stock.warehouse').search(cr, uid, [], context=context)
66             for w in self.pool.get('stock.warehouse').browse(cr, uid, wids, context=context):
67                 location_ids.append(w.lot_stock_id.id)
68
69         # build the list of ids of children of the location given by id
70         if context.get('compute_child',True):
71             child_location_ids = self.pool.get('stock.location').search(cr, uid, [('location_id', 'child_of', location_ids)])
72             location_ids= len(child_location_ids) and child_location_ids or location_ids
73         else:
74             location_ids= location_ids
75
76         uoms_o = {}
77         product2uom = {}
78         for product in self.browse(cr, uid, ids, context=context):
79             product2uom[product.id] = product.uom_id.id
80             uoms_o[product.uom_id.id] = product.uom_id
81
82         results = []
83         results2 = []
84
85         from_date=context.get('from_date',False)
86         to_date=context.get('to_date',False)
87         date_str=False
88         if from_date and to_date:
89             date_str="date_planned>='%s' and date_planned<='%s'"%(from_date,to_date)
90         elif from_date:
91             date_str="date_planned>='%s'"%(from_date)
92         elif to_date:
93             date_str="date_planned<='%s'"%(to_date)
94
95         if 'in' in what:
96             # all moves from a location out of the set to a location in the set
97             cr.execute(
98                 'select sum(product_qty), product_id, product_uom '\
99                 'from stock_move '\
100                 'where location_id <> ANY(%s)'\
101                 'and location_dest_id =ANY(%s)'\
102                 'and product_id =ANY(%s)'\
103                 'and state in %s' + (date_str and 'and '+date_str+' ' or '') +''\
104                 'group by product_id,product_uom',(location_ids,location_ids,ids,tuple(states),)
105             )
106             results = cr.fetchall()
107         if 'out' in what:
108             # all moves from a location in the set to a location out of the set
109             cr.execute(
110                 'select sum(product_qty), product_id, product_uom '\
111                 'from stock_move '\
112                 'where location_id = ANY(%s)'\
113                 'and location_dest_id <> ANY(%s) '\
114                 'and product_id =ANY(%s)'\
115                 'and state in %s' + (date_str and 'and '+date_str+' ' or '') + ''\
116                 'group by product_id,product_uom',(location_ids,location_ids,ids,tuple(states),)
117             )
118             results2 = cr.fetchall()
119         uom_obj = self.pool.get('product.uom')
120         uoms = map(lambda x: x[2], results) + map(lambda x: x[2], results2)
121         if context.get('uom', False):
122             uoms += [context['uom']]
123
124         uoms = filter(lambda x: x not in uoms_o.keys(), uoms)
125         if uoms:
126             uoms = uom_obj.browse(cr, uid, list(set(uoms)), context=context)
127         for o in uoms:
128             uoms_o[o.id] = o
129         for amount, prod_id, prod_uom in results:
130             amount = uom_obj._compute_qty_obj(cr, uid, uoms_o[prod_uom], amount,
131                     uoms_o[context.get('uom', False) or product2uom[prod_id]])
132             res[prod_id] += amount
133         for amount, prod_id, prod_uom in results2:
134             amount = uom_obj._compute_qty_obj(cr, uid, uoms_o[prod_uom], amount,
135                     uoms_o[context.get('uom', False) or product2uom[prod_id]])
136             res[prod_id] -= amount
137         return res
138
139     def _product_available(self, cr, uid, ids, field_names=None, arg=False, context={}):
140         if not field_names:
141             field_names=[]
142         res = {}
143         for id in ids:
144             res[id] = {}.fromkeys(field_names, 0.0)
145         for f in field_names:
146             c = context.copy()
147             if f=='qty_available':
148                 c.update({ 'states':('done',), 'what':('in', 'out') })
149             if f=='virtual_available':
150                 c.update({ 'states':('confirmed','waiting','assigned','done'), 'what':('in', 'out') })
151             if f=='incoming_qty':
152                 c.update({ 'states':('confirmed','waiting','assigned'), 'what':('in',) })
153             if f=='outgoing_qty':
154                 c.update({ 'states':('confirmed','waiting','assigned'), 'what':('out',) })
155             stock=self.get_product_available(cr,uid,ids,context=c)
156             for id in ids:
157                 res[id][f] = stock.get(id, 0.0)
158         return res
159
160     _columns = {
161         'qty_available': fields.function(_product_available, method=True, type='float', string='Real Stock', help="Current quantities of products in selected locations or all internal if none have been selected.", multi='qty_available'),
162         'virtual_available': fields.function(_product_available, method=True, type='float', string='Virtual Stock', help="Future stock for this product according to the selected locations or all internal if none have been selected. Computed as: Real Stock - Outgoing + Incoming.", multi='qty_available'),
163         'incoming_qty': fields.function(_product_available, method=True, type='float', string='Incoming', help="Quantities of products that are planned to arrive in selected locations or all internal if none have been selected.", multi='qty_available'),
164         'outgoing_qty': fields.function(_product_available, method=True, type='float', string='Outgoing', help="Quantities of products that are planned to leave in selected locations or all internal if none have been selected.", multi='qty_available'),
165         'track_production': fields.boolean('Track Production Lots' , help="Forces to use a Production Lot during production order"),
166         'track_incoming': fields.boolean('Track Incoming Lots', help="Forces to use a tracking lot during receptions"),
167         'track_outgoing': fields.boolean('Track Outgoing Lots', help="Forces to use a tracking lot during deliveries"),
168         'location_id': fields.dummy(string='Location', relation='stock.location', type='many2one', domain=[('usage','=','internal')]),
169     }
170     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
171         res = super(product_product,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
172         if context == None:
173             context = {}
174         if ('location' in context) and context['location']:
175             location_info = self.pool.get('stock.location').browse(cr, uid, context['location'])
176             fields=res.get('fields',{})
177             if fields:
178                 if location_info.usage == 'supplier':
179                     if fields.get('virtual_available'):
180                         res['fields']['virtual_available']['string'] = _('Future Receptions')
181                     if fields.get('qty_available'):
182                         res['fields']['qty_available']['string'] = _('Received Qty')
183
184                 if location_info.usage == 'internal':
185                     if fields.get('virtual_available'):
186                         res['fields']['virtual_available']['string'] = _('Future Stock')
187
188                 if location_info.usage == 'customer':
189                     if fields.get('virtual_available'):
190                         res['fields']['virtual_available']['string'] = _('Future Deliveries')
191                     if fields.get('qty_available'):
192                         res['fields']['qty_available']['string'] = _('Delivered Qty')
193
194                 if location_info.usage == 'inventory':
195                     if fields.get('virtual_available'):
196                         res['fields']['virtual_available']['string'] = _('Future P&L')
197                     res['fields']['qty_available']['string'] = _('P&L Qty')
198
199                 if location_info.usage == 'procurement':
200                     if fields.get('virtual_available'):
201                         res['fields']['virtual_available']['string'] = _('Future Qty')
202                     if fields.get('qty_available'):
203                         res['fields']['qty_available']['string'] = _('Unplanned Qty')
204
205                 if location_info.usage == 'production':
206                     if fields.get('virtual_available'):
207                         res['fields']['virtual_available']['string'] = _('Future Productions')
208                     if fields.get('qty_available'):
209                         res['fields']['qty_available']['string'] = _('Produced Qty')
210
211         return res
212 product_product()
213
214
215 class product_template(osv.osv):
216     _name = 'product.template'
217     _inherit = 'product.template'
218     _columns = {
219         'property_stock_procurement': fields.property(
220             'stock.location',
221             type='many2one',
222             relation='stock.location',
223             string="Requisition Location",
224             method=True,
225             view_load=True,
226             domain=[('usage','like','procurement')],
227             help="For the current product, this stock location will be used, instead of the default one, as the source location for stock moves generated by procurements"),
228         'property_stock_production': fields.property(
229             'stock.location',
230             type='many2one',
231             relation='stock.location',
232             string="Production Location",
233             method=True,
234             view_load=True,
235             domain=[('usage','like','production')],
236             help="For the current product, this stock location will be used, instead of the default one, as the source location for stock moves generated by production orders"),
237         'property_stock_inventory': fields.property(
238             'stock.location',
239             type='many2one',
240             relation='stock.location',
241             string="Inventory Location",
242             method=True,
243             view_load=True,
244             domain=[('usage','like','inventory')],
245             help="For the current product, this stock location will be used, instead of the default one, as the source location for stock moves generated when you do an inventory"),
246         'property_stock_account_input': fields.property('account.account',
247             type='many2one', relation='account.account',
248             string='Stock Input Account', method=True, view_load=True,
249             help='This account will be used, instead of the default one, to value input stock'),
250         'property_stock_account_output': fields.property('account.account',
251             type='many2one', relation='account.account',
252             string='Stock Output Account', method=True, view_load=True,
253             help='This account will be used, instead of the default one, to value output stock'),
254     }
255
256 product_template()
257
258
259 class product_category(osv.osv):
260     _inherit = 'product.category'
261     _columns = {
262         'property_stock_journal': fields.property('account.journal',
263             relation='account.journal', type='many2one',
264             string='Stock journal', method=True, view_load=True,
265             help="This journal will be used for the accounting move generated by stock move"),
266         'property_stock_account_input_categ': fields.property('account.account',
267             type='many2one', relation='account.account',
268             string='Stock Input Account', method=True, view_load=True,
269             help='This account will be used to value the input stock'),
270         'property_stock_account_output_categ': fields.property('account.account',
271             type='many2one', relation='account.account',
272             string='Stock Output Account', method=True, view_load=True,
273             help='This account will be used to value the output stock'),
274     }
275
276 product_category()
277
278 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
279