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 _
24 import decimal_precision as dp
26 class stock_move_consume(osv.osv_memory):
27 _name = "stock.move.consume"
28 _description = "Consume Products"
31 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
32 'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM'), required=True),
33 'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
34 'location_id': fields.many2one('stock.location', 'Location', required=True)
37 def default_get(self, cr, uid, fields, context=None):
38 """ Get default values
39 @param self: The object pointer.
40 @param cr: A database cursor
41 @param uid: ID of the user currently logged in
42 @param fields: List of fields for default value
43 @param context: A standard dictionary
44 @return: default values of fields
48 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
49 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
50 if 'product_id' in fields:
51 res.update({'product_id': move.product_id.id})
52 if 'product_uom' in fields:
53 res.update({'product_uom': move.product_uom.id})
54 if 'product_qty' in fields:
55 res.update({'product_qty': move.product_qty})
56 if 'location_id' in fields:
57 res.update({'location_id': move.location_id.id})
61 def do_move_consume(self, cr, uid, ids, context=None):
62 """ To move consumed products
63 @param self: The object pointer.
64 @param cr: A database cursor
65 @param uid: ID of the user currently logged in
66 @param ids: the ID or list of IDs if we want more than one
67 @param context: A standard dictionary
72 move_obj = self.pool.get('stock.move')
73 move_ids = context['active_ids']
74 for data in self.browse(cr, uid, ids, context=context):
75 move_obj.action_consume(cr, uid, move_ids,
76 data.product_qty, data.location_id.id,
78 return {'type': 'ir.actions.act_window_close'}
83 class stock_move_scrap(osv.osv_memory):
84 _name = "stock.move.scrap"
85 _description = "Scrap Products"
86 _inherit = "stock.move.consume"
89 'location_id': lambda *x: False
92 def default_get(self, cr, uid, fields, context=None):
93 """ Get default values
94 @param self: The object pointer.
95 @param cr: A database cursor
96 @param uid: ID of the user currently logged in
97 @param fields: List of fields for default value
98 @param context: A standard dictionary
99 @return: default values of fields
103 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
104 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
105 location_obj = self.pool.get('stock.location')
106 scrpaed_location_ids = location_obj.search(cr, uid, [('scrap_location','=',True)])
108 if 'product_id' in fields:
109 res.update({'product_id': move.product_id.id})
110 if 'product_uom' in fields:
111 res.update({'product_uom': move.product_uom.id})
112 if 'product_qty' in fields:
113 res.update({'product_qty': move.product_qty})
114 if 'location_id' in fields:
115 if scrpaed_location_ids:
116 res.update({'location_id': scrpaed_location_ids[0]})
118 res.update({'location_id': False})
122 def move_scrap(self, cr, uid, ids, context=None):
123 """ To move scrapped products
124 @param self: The object pointer.
125 @param cr: A database cursor
126 @param uid: ID of the user currently logged in
127 @param ids: the ID or list of IDs if we want more than one
128 @param context: A standard dictionary
133 move_obj = self.pool.get('stock.move')
134 move_ids = context['active_ids']
135 for data in self.browse(cr, uid, ids):
136 move_obj.action_scrap(cr, uid, move_ids,
137 data.product_qty, data.location_id.id,
139 return {'type': 'ir.actions.act_window_close'}
144 class split_in_production_lot(osv.osv_memory):
145 _name = "stock.move.split"
146 _description = "Split in Production lots"
148 def default_get(self, cr, uid, fields, context=None):
151 res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)
152 if context.get('active_id'):
153 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
154 if 'product_id' in fields:
155 res.update({'product_id': move.product_id.id})
156 if 'product_uom' in fields:
157 res.update({'product_uom': move.product_uom.id})
159 res.update({'qty': move.product_qty})
160 if 'use_exist' in fields:
161 res.update({'use_exist': (move.picking_id and move.picking_id.type=='out' and True) or False})
162 if 'location_id' in fields:
163 res.update({'location_id': move.location_id.id})
167 'qty': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
168 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
169 'product_uom': fields.many2one('product.uom', 'UoM'),
170 'line_ids': fields.one2many('stock.move.split.lines', 'wizard_id', 'Production Lots'),
171 'line_exist_ids': fields.one2many('stock.move.split.lines', 'wizard_exist_id', 'Production Lots'),
172 'use_exist' : fields.boolean('Existing Lots', help="Check this option to select existing lots in the list below, otherwise you should enter new ones line by line."),
173 'location_id': fields.many2one('stock.location', 'Source Location')
176 def split_lot(self, cr, uid, ids, context=None):
177 """ To split a lot"""
180 res = self.split(cr, uid, ids, context.get('active_ids'), context=context)
181 return {'type': 'ir.actions.act_window_close'}
183 def split(self, cr, uid, ids, move_ids, context=None):
184 """ To split stock moves into production lot
186 :param move_ids: the ID or list of IDs of stock move we want to split
190 assert context.get('active_model') == 'stock.move',\
191 'Incorrect use of the stock move split wizard'
192 inventory_id = context.get('inventory_id', False)
193 prodlot_obj = self.pool.get('stock.production.lot')
194 inventory_obj = self.pool.get('stock.inventory')
195 move_obj = self.pool.get('stock.move')
197 for data in self.browse(cr, uid, ids, context=context):
198 for move in move_obj.browse(cr, uid, move_ids, context=context):
199 move_qty = move.product_qty
200 quantity_rest = move.product_qty
201 uos_qty_rest = move.product_uos_qty
204 lines = [l for l in data.line_exist_ids if l]
206 lines = [l for l in data.line_ids if l]
208 quantity = line.quantity
209 if quantity <= 0 or move_qty == 0:
211 quantity_rest -= quantity
212 uos_qty = quantity / move_qty * move.product_uos_qty
213 uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
214 if quantity_rest < 0:
215 quantity_rest = quantity
216 self.pool.get('stock.move').log(cr, uid, move.id, _('Unable to assign all lots to this move!'))
219 'product_qty': quantity,
220 'product_uos_qty': uos_qty,
223 if quantity_rest > 0:
224 current_move = move_obj.copy(cr, uid, move.id, default_val, context=context)
225 if inventory_id and current_move:
226 inventory_obj.write(cr, uid, inventory_id, {'move_ids': [(4, current_move)]}, context=context)
227 new_move.append(current_move)
229 if quantity_rest == 0:
230 current_move = move.id
233 prodlot_id = line.prodlot_id.id
235 prodlot_id = prodlot_obj.create(cr, uid, {
237 'product_id': move.product_id.id},
240 move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id, 'state':move.state})
243 if quantity_rest > 0:
244 update_val['product_qty'] = quantity_rest
245 update_val['product_uos_qty'] = uos_qty_rest
246 update_val['state'] = move.state
247 move_obj.write(cr, uid, [move.id], update_val)
251 split_in_production_lot()
253 class stock_move_split_lines_exist(osv.osv_memory):
254 _name = "stock.move.split.lines"
255 _description = "Stock move Split lines"
257 'name': fields.char('Production Lot', size=64),
258 'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
259 'wizard_id': fields.many2one('stock.move.split', 'Parent Wizard'),
260 'wizard_exist_id': fields.many2one('stock.move.split', 'Parent Wizard (for existing lines)'),
261 'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot'),
267 def onchange_lot_id(self, cr, uid, ids, prodlot_id=False, product_qty=False,
268 loc_id=False, product_id=False, uom_id=False):
269 return self.pool.get('stock.move').onchange_lot_id(cr, uid, [], prodlot_id, product_qty,
270 loc_id, product_id, uom_id)