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
24 import decimal_precision as dp
26 class stock_move_track(osv.osv_memory):
27 _name = "stock.move.track"
28 _description = "Track moves"
31 'tracking_prefix': fields.char('Tracking prefix', size=64),
32 'quantity': fields.float("Quantity per lot")
36 'quantity': lambda *x: 1
39 def track_lines(self, cr, uid, ids, context=None):
40 """ To track stock moves lines
41 @param self: The object pointer.
42 @param cr: A database cursor
43 @param uid: ID of the user currently logged in
44 @param ids: An ID or list of IDs if we want more than one
45 @param context: A standard dictionary
48 datas = self.read(cr, uid, ids)[0]
49 move_obj = self.pool.get('stock.move')
50 move_obj._track_lines(cr, uid, context['active_id'], datas, context=context)
51 return {'type': 'ir.actions.act_window_close'}
55 class stock_move_consume(osv.osv_memory):
56 _name = "stock.move.consume"
57 _description = "Consume Products"
60 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
61 'product_qty': fields.float('Quantity', required=True),
62 'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
63 'location_id': fields.many2one('stock.location', 'Location', required=True)
66 def default_get(self, cr, uid, fields, context=None):
67 """ Get default values
68 @param self: The object pointer.
69 @param cr: A database cursor
70 @param uid: ID of the user currently logged in
71 @param fields: List of fields for default value
72 @param context: A standard dictionary
73 @return: default values of fields
77 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
78 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
79 if 'product_id' in fields:
80 res.update({'product_id': move.product_id.id})
81 if 'product_uom' in fields:
82 res.update({'product_uom': move.product_uom.id})
83 if 'product_qty' in fields:
84 res.update({'product_qty': move.product_qty})
85 if 'location_id' in fields:
86 res.update({'location_id': move.location_id.id})
90 def do_move_consume(self, cr, uid, ids, context=None):
91 """ To move consumed products
92 @param self: The object pointer.
93 @param cr: A database cursor
94 @param uid: ID of the user currently logged in
95 @param ids: the ID or list of IDs if we want more than one
96 @param context: A standard dictionary
101 move_obj = self.pool.get('stock.move')
102 move_ids = context['active_ids']
103 for data in self.browse(cr, uid, ids, context=context):
104 move_obj.action_consume(cr, uid, move_ids,
105 data.product_qty, data.location_id.id,
107 return {'type': 'ir.actions.act_window_close'}
112 class stock_move_scrap(osv.osv_memory):
113 _name = "stock.move.scrap"
114 _description = "Scrap Products"
115 _inherit = "stock.move.consume"
118 'location_id': lambda *x: False
121 def default_get(self, cr, uid, fields, context=None):
122 """ Get default values
123 @param self: The object pointer.
124 @param cr: A database cursor
125 @param uid: ID of the user currently logged in
126 @param fields: List of fields for default value
127 @param context: A standard dictionary
128 @return: default values of fields
132 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
133 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
134 location_obj = self.pool.get('stock.location')
135 scrpaed_location_ids = location_obj.search(cr, uid, [('scrap_location','=',True)])
137 if 'product_id' in fields:
138 res.update({'product_id': move.product_id.id})
139 if 'product_uom' in fields:
140 res.update({'product_uom': move.product_uom.id})
141 if 'product_qty' in fields:
142 res.update({'product_qty': move.product_qty})
143 if 'location_id' in fields:
144 if scrpaed_location_ids:
145 res.update({'location_id': scrpaed_location_ids[0]})
147 res.update({'location_id': False})
151 def move_scrap(self, cr, uid, ids, context=None):
152 """ To move scrapped products
153 @param self: The object pointer.
154 @param cr: A database cursor
155 @param uid: ID of the user currently logged in
156 @param ids: the ID or list of IDs if we want more than one
157 @param context: A standard dictionary
162 move_obj = self.pool.get('stock.move')
163 move_ids = context['active_ids']
164 for data in self.browse(cr, uid, ids):
165 move_obj.action_scrap(cr, uid, move_ids,
166 data.product_qty, data.location_id.id,
168 return {'type': 'ir.actions.act_window_close'}
173 class split_in_production_lot(osv.osv_memory):
174 _name = "stock.move.split"
175 _description = "Split in Production lots"
177 def default_get(self, cr, uid, fields, context=None):
178 """ Get default values
179 @param self: The object pointer.
180 @param cr: A database cursor
181 @param uid: ID of the user currently logged in
182 @param fields: List of fields for default value
183 @param context: A standard dictionary
184 @return: Default values of fields
189 res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)
190 if context.get('active_id'):
191 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
192 if 'product_id' in fields:
193 res.update({'product_id': move.product_id.id})
194 if 'product_uom' in fields:
195 res.update({'product_uom': move.product_uom.id})
197 res.update({'qty': move.product_qty})
198 if 'use_exist' in fields:
199 res.update({'use_exist': (move.picking_id and move.picking_id.type=='out' and True) or False})
203 'qty': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
204 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
205 'product_uom': fields.many2one('product.uom', 'UoM'),
206 'line_ids': fields.one2many('stock.move.split.lines', 'lot_id', 'Production Lots'),
207 'line_exist_ids': fields.one2many('stock.move.split.lines.exist', 'lot_id', 'Production Lots'),
208 '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."),
211 def split_lot(self, cr, uid, ids, context=None):
213 @param self: The object pointer.
214 @param cr: A database cursor
215 @param uid: ID of the user currently logged in
216 @param ids: An ID or list of IDs if we want more than one
217 @param context: A standard dictionary
222 self.split(cr, uid, ids, context.get('active_ids'), context=context)
223 return {'type': 'ir.actions.act_window_close'}
225 def split(self, cr, uid, ids, move_ids, context=None):
226 """ To split stock moves into production lot
227 @param self: The object pointer.
228 @param cr: A database cursor
229 @param uid: ID of the user currently logged in
230 @param ids: the ID or list of IDs if we want more than one
231 @param move_ids: the ID or list of IDs of stock move we want to split
232 @param context: A standard dictionary
237 inventory_id = context.get('inventory_id', False)
238 prodlot_obj = self.pool.get('stock.production.lot')
239 inventory_obj = self.pool.get('stock.inventory')
240 move_obj = self.pool.get('stock.move')
242 for data in self.browse(cr, uid, ids, context=context):
243 for move in move_obj.browse(cr, uid, move_ids, context=context):
244 move_qty = move.product_qty
245 quantity_rest = move.product_qty
246 uos_qty_rest = move.product_uos_qty
249 lines = [l for l in data.line_exist_ids if l]
251 lines = [l for l in data.line_ids if l]
253 quantity = line.quantity
254 if quantity <= 0 or move_qty == 0:
256 quantity_rest -= quantity
257 uos_qty = quantity / move_qty * move.product_uos_qty
258 uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
259 if quantity_rest < 0:
260 quantity_rest = quantity
263 'product_qty': quantity,
264 'product_uos_qty': uos_qty,
267 if quantity_rest > 0:
268 current_move = move_obj.copy(cr, uid, move.id, default_val, context=context)
269 if inventory_id and current_move:
270 inventory_obj.write(cr, uid, inventory_id, {'move_ids': [(4, current_move)]}, context=context)
271 new_move.append(current_move)
273 if quantity_rest == 0:
274 current_move = move.id
277 prodlot_id = line.prodlot_id.id
279 prodlot_id = prodlot_obj.create(cr, uid, {
281 'product_id': move.product_id.id},
284 move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id, 'state':move.state})
287 if quantity_rest > 0:
288 update_val['product_qty'] = quantity_rest
289 update_val['product_uos_qty'] = uos_qty_rest
290 update_val['state'] = move.state
291 move_obj.write(cr, uid, [move.id], update_val)
295 split_in_production_lot()
297 class stock_move_split_lines_exist(osv.osv_memory):
298 _name = "stock.move.split.lines.exist"
299 _description = "Exist Split lines"
301 'name': fields.char('Tracking serial', size=64),
302 'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
303 'lot_id': fields.many2one('stock.move.split', 'Lot'),
304 'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot'),
310 stock_move_split_lines_exist()
312 class stock_move_split_lines(osv.osv_memory):
313 _name = "stock.move.split.lines"
314 _description = "Split lines"
316 'name': fields.char('Tracking serial', size=64),
317 'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
318 'use_exist' : fields.boolean('Existing Lot'),
319 'lot_id': fields.many2one('stock.move.split', 'Lot'),
320 'action': fields.selection([('split','Split'),('keepinone','Keep in one lot')],'Action'),
326 stock_move_split_lines()