1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
23 from tools.translate import _
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)
31 if (context.get('location', False)):
32 return _('Products: ')+self.pool.get('stock.location').browse(cr, user, context['location'], context).name
35 def get_product_available(self,cr,uid,ids,context=None):
38 states=context.get('states',[])
39 what=context.get('what',())
41 ids = self.search(cr, uid, [])
42 res = {}.fromkeys(ids, 0.0)
46 if context.get('shop', False):
47 cr.execute('select warehouse_id from sale_shop where id=%s', (int(context['shop']),))
50 context['warehouse'] = res2[0]
52 if context.get('warehouse', False):
53 cr.execute('select lot_stock_id from stock_warehouse where id=%s', (int(context['warehouse']),))
56 context['location'] = res2[0]
58 if context.get('location', False):
59 if type(context['location']) == type(1):
60 location_ids = [context['location']]
62 location_ids = context['location']
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)
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
74 location_ids= location_ids
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
85 from_date=context.get('from_date',False)
86 to_date=context.get('to_date',False)
88 if from_date and to_date:
89 date_str="date_planned>='%s' and date_planned<='%s'"%(from_date,to_date)
91 date_str="date_planned>='%s'"%(from_date)
93 date_str="date_planned<='%s'"%(to_date)
96 # all moves from a location out of the set to a location in the set
98 'select sum(product_qty), product_id, product_uom '\
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),)
106 results = cr.fetchall()
108 # all moves from a location in the set to a location out of the set
110 'select sum(product_qty), product_id, product_uom '\
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),)
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']]
124 uoms = filter(lambda x: x not in uoms_o.keys(), uoms)
126 uoms = uom_obj.browse(cr, uid, list(set(uoms)), context=context)
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
139 def _product_available(self, cr, uid, ids, field_names=None, arg=False, context={}):
144 res[id] = {}.fromkeys(field_names, 0.0)
145 for f in field_names:
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)
157 res[id][f] = stock.get(id, 0.0)
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')]),
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)
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',{})
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')
184 if location_info.usage == 'internal':
185 if fields.get('virtual_available'):
186 res['fields']['virtual_available']['string'] = _('Future Stock')
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')
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')
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')
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')
215 class product_template(osv.osv):
216 _name = 'product.template'
217 _inherit = 'product.template'
219 'property_stock_procurement': fields.property(
222 relation='stock.location',
223 string="Requisition Location",
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(
231 relation='stock.location',
232 string="Production Location",
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(
240 relation='stock.location',
241 string="Inventory Location",
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'),
259 class product_category(osv.osv):
260 _inherit = 'product.category'
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'),
278 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: