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 _
25 class stock_move_track(osv.osv_memory):
26 _name = "stock.move.track"
27 _description = "Track moves"
30 'tracking_prefix': fields.char('Tracking prefix', size=64),
31 'quantity': fields.float("Quantity per lot")
35 'quantity': lambda *x: 1
38 def track_lines(self, cr, uid, ids, context={}):
40 To track stock moves lines
42 @param self: The object pointer.
43 @param cr: A database cursor
44 @param uid: ID of the user currently logged in
45 @param ids: the ID or list of IDs if we want more than one
46 @param context: A standard dictionary
51 datas = self.read(cr, uid, ids)[0]
52 move_obj = self.pool.get('stock.move')
53 move_obj._track_lines(cr, uid, context['active_id'], datas, context=context)
58 class stock_move_consume(osv.osv_memory):
59 _name = "stock.move.consume"
60 _description = "Consume Products"
63 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
64 'product_qty': fields.float('Quantity', required=True),
65 'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
66 'location_id': fields.many2one('stock.location', 'Location', required=True)
69 def default_get(self, cr, uid, fields, context=None):
73 @param self: The object pointer.
74 @param cr: A database cursor
75 @param uid: ID of the user currently logged in
76 @param fields: List of fields for default value
77 @param context: A standard dictionary
79 @return: default values of fields
82 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
83 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
84 if 'product_id' in fields:
85 res.update({'product_id': move.product_id.id})
86 if 'product_uom' in fields:
87 res.update({'product_uom': move.product_uom.id})
88 if 'product_qty' in fields:
89 res.update({'product_qty': move.product_qty})
90 if 'location_id' in fields:
91 res.update({'location_id': move.location_id.id})
95 def do_move_consume(self, cr, uid, ids, context={}):
97 To move consumed products
99 @param self: The object pointer.
100 @param cr: A database cursor
101 @param uid: ID of the user currently logged in
102 @param ids: the ID or list of IDs if we want more than one
103 @param context: A standard dictionary
108 move_obj = self.pool.get('stock.move')
109 move_ids = context['active_ids']
110 for data in self.read(cr, uid, ids):
111 move_obj.action_consume(cr, uid, move_ids,
112 data['product_qty'], data['location_id'],
119 class stock_move_scrap(osv.osv_memory):
120 _name = "stock.move.scrap"
121 _description = "Scrap Products"
122 _inherit = "stock.move.consume"
125 'location_id': lambda *x: False
128 def default_get(self, cr, uid, fields, context=None):
132 @param self: The object pointer.
133 @param cr: A database cursor
134 @param uid: ID of the user currently logged in
135 @param fields: List of fields for default value
136 @param context: A standard dictionary
138 @return: default values of fields
141 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
142 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
143 location_obj = self.pool.get('stock.location')
144 scrpaed_location_ids = location_obj.search(cr, uid, [('scrap_location','=',True)])
146 if 'product_id' in fields:
147 res.update({'product_id': move.product_id.id})
148 if 'product_uom' in fields:
149 res.update({'product_uom': move.product_uom.id})
150 if 'product_qty' in fields:
151 res.update({'product_qty': move.product_qty})
152 if 'location_id' in fields:
153 if scrpaed_location_ids:
154 res.update({'location_id': scrpaed_location_ids[0]})
156 res.update({'location_id': False})
160 def move_scrap(self, cr, uid, ids, context={}):
162 To move scraped products
164 @param self: The object pointer.
165 @param cr: A database cursor
166 @param uid: ID of the user currently logged in
167 @param ids: the ID or list of IDs if we want more than one
168 @param context: A standard dictionary
173 move_obj = self.pool.get('stock.move')
174 move_ids = context['active_ids']
175 for data in self.read(cr, uid, ids):
176 move_obj.action_scrap(cr, uid, move_ids,
177 data['product_qty'], data['location_id'],
184 class split_in_production_lot(osv.osv_memory):
185 _name = "stock.move.split"
186 _description = "Split in Production lots"
188 def default_get(self, cr, uid, fields, context=None):
192 @param self: The object pointer.
193 @param cr: A database cursor
194 @param uid: ID of the user currently logged in
195 @param fields: List of fields for default value
196 @param context: A standard dictionary
198 @return: default values of fields
202 res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)
203 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
204 if 'product_id' in fields:
205 res.update({'product_id': move.product_id.id})
209 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
210 'line_ids': fields.one2many('stock.move.split.lines', 'lot_id', 'Lots Number')
212 def split_lot(self, cr, uid, ids, context=None):
216 @param self: The object pointer.
217 @param cr: A database cursor
218 @param uid: ID of the user currently logged in
219 @param ids: the ID or list of IDs if we want more than one
220 @param context: A standard dictionary
225 self.split(cr, uid, ids, context.get('active_ids'), context=context)
228 def split(self, cr, uid, ids, move_ids, context=None):
230 To split stock moves into production lot
232 @param self: The object pointer.
233 @param cr: A database cursor
234 @param uid: ID of the user currently logged in
235 @param ids: the ID or list of IDs if we want more than one
236 @param move_ids: the ID or list of IDs of stock move we want to split
237 @param context: A standard dictionary
242 prodlot_obj = self.pool.get('stock.production.lot')
243 ir_sequence_obj = self.pool.get('ir.sequence')
244 move_obj = self.pool.get('stock.move')
246 for data in self.browse(cr, uid, ids):
247 for move in move_obj.browse(cr, uid, move_ids):
248 move_qty = move.product_qty
249 quantity_rest = move.product_qty
250 uos_qty_rest = move.product_uos_qty
252 for line in data.line_ids:
253 quantity = line.quantity
256 if quantity <= 0 or move_qty == 0:
258 quantity_rest -= quantity
259 uos_qty = quantity / move_qty * move.product_uos_qty
260 uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
261 if quantity_rest <= 0:
262 quantity_rest = quantity
265 'product_qty': quantity,
266 'product_uos_qty': uos_qty,
269 current_move = move_obj.copy(cr, uid, move.id, default_val)
270 new_move.append(current_move)
272 if line.use_exist and line.name:
273 prodlot_id = prodlot_obj.search(cr, uid, [('prefix','=',line.name),('product_id','=',data.product_id.id)])
275 prodlot_id = prodlot_id[0]
277 sequence = ir_sequence_obj.get(cr, uid, 'stock.lot.serial')
278 prodlot_id = prodlot_obj.create(cr, uid, {'name': sequence, 'prefix' : line.name},
279 {'product_id': move.product_id.id})
280 move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id})
281 prodlot = prodlot_obj.browse(cr, uid, prodlot_id)
282 ref = '%d' % (move.id)
284 ref = '%s, %s' % (prodlot.ref, ref)
285 prodlot_obj.write(cr, uid, [prodlot_id], {'ref': ref})
288 if quantity_rest > 0:
289 update_val['product_qty'] = quantity_rest
290 update_val['product_uos_qty'] = uos_qty_rest
291 move_obj.write(cr, uid, [move.id], update_val)
293 split_in_production_lot()
295 class stock_move_split_lines(osv.osv_memory):
296 _name = "stock.move.split.lines"
297 _description = "Split lines"
300 'name': fields.char('Tracking serial', size=64),
301 'quantity': fields.integer('Quantity'),
302 'use_exist' : fields.boolean('Use Exist'),
303 'lot_id': fields.many2one('stock.move.split', 'Lot'),
304 'action': fields.selection([('split','Split'),('keepinone','Keep in one lot')],'Action'),
307 'quantity': lambda *x: 1,
308 'action' : lambda *x: 'split',
311 stock_move_split_lines()