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={}):
39 """ To track stock moves lines
40 @param self: The object pointer.
41 @param cr: A database cursor
42 @param uid: ID of the user currently logged in
43 @param ids: An ID or list of IDs if we want more than one
44 @param context: A standard dictionary
47 datas = self.read(cr, uid, ids)[0]
48 move_obj = self.pool.get('stock.move')
49 move_obj._track_lines(cr, uid, context['active_id'], datas, context=context)
54 class stock_move_consume(osv.osv_memory):
55 _name = "stock.move.consume"
56 _description = "Consume Products"
59 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
60 'product_qty': fields.float('Quantity', required=True),
61 'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
62 'location_id': fields.many2one('stock.location', 'Location', required=True)
65 def default_get(self, cr, uid, fields, context=None):
66 """ Get default values
67 @param self: The object pointer.
68 @param cr: A database cursor
69 @param uid: ID of the user currently logged in
70 @param fields: List of fields for default value
71 @param context: A standard dictionary
72 @return: default values of fields
74 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
75 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
76 if 'product_id' in fields:
77 res.update({'product_id': move.product_id.id})
78 if 'product_uom' in fields:
79 res.update({'product_uom': move.product_uom.id})
80 if 'product_qty' in fields:
81 res.update({'product_qty': move.product_qty})
82 if 'location_id' in fields:
83 res.update({'location_id': move.location_id.id})
87 def do_move_consume(self, cr, uid, ids, context={}):
88 """ To move consumed products
89 @param self: The object pointer.
90 @param cr: A database cursor
91 @param uid: ID of the user currently logged in
92 @param ids: the ID or list of IDs if we want more than one
93 @param context: A standard dictionary
96 move_obj = self.pool.get('stock.move')
97 move_ids = context['active_ids']
98 for data in self.read(cr, uid, ids):
99 move_obj.action_consume(cr, uid, move_ids,
100 data['product_qty'], data['location_id'],
107 class stock_move_scrap(osv.osv_memory):
108 _name = "stock.move.scrap"
109 _description = "Scrap Products"
110 _inherit = "stock.move.consume"
113 'location_id': lambda *x: False
116 def default_get(self, cr, uid, fields, context=None):
117 """ Get default values
118 @param self: The object pointer.
119 @param cr: A database cursor
120 @param uid: ID of the user currently logged in
121 @param fields: List of fields for default value
122 @param context: A standard dictionary
123 @return: default values of fields
125 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
126 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
127 location_obj = self.pool.get('stock.location')
128 scrpaed_location_ids = location_obj.search(cr, uid, [('scrap_location','=',True)])
130 if 'product_id' in fields:
131 res.update({'product_id': move.product_id.id})
132 if 'product_uom' in fields:
133 res.update({'product_uom': move.product_uom.id})
134 if 'product_qty' in fields:
135 res.update({'product_qty': move.product_qty})
136 if 'location_id' in fields:
137 if scrpaed_location_ids:
138 res.update({'location_id': scrpaed_location_ids[0]})
140 res.update({'location_id': False})
144 def move_scrap(self, cr, uid, ids, context={}):
145 """ To move scraped products
146 @param self: The object pointer.
147 @param cr: A database cursor
148 @param uid: ID of the user currently logged in
149 @param ids: the ID or list of IDs if we want more than one
150 @param context: A standard dictionary
153 move_obj = self.pool.get('stock.move')
154 move_ids = context['active_ids']
155 for data in self.read(cr, uid, ids):
156 move_obj.action_scrap(cr, uid, move_ids,
157 data['product_qty'], data['location_id'],
164 class split_in_production_lot(osv.osv_memory):
165 _name = "stock.move.split"
166 _description = "Split in Production lots"
168 def default_get(self, cr, uid, fields, context=None):
169 """ Get default values
170 @param self: The object pointer.
171 @param cr: A database cursor
172 @param uid: ID of the user currently logged in
173 @param fields: List of fields for default value
174 @param context: A standard dictionary
175 @return: Default values of fields
178 res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)
179 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
180 if 'product_id' in fields:
181 res.update({'product_id': move.product_id.id})
182 if 'product_uom' in fields:
183 res.update({'product_uom': move.product_uom.id})
185 res.update({'qty': move.product_qty})
186 if 'use_exist' in fields:
187 res.update({'use_exist': (move.picking_id and move.picking_id.type=='out' and True) or False})
191 'qty': fields.integer('Quantity'),
192 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
193 'product_uom': fields.many2one('product.uom', 'Product UOM'),
194 'line_ids': fields.one2many('stock.move.split.lines', 'lot_id', 'Lots Number'),
195 'line_exist_ids': fields.one2many('stock.move.split.lines.exist', 'lot_id', 'Lots Existing Numbers'),
196 'use_exist' : fields.boolean('Existing Lot'),
199 def split_lot(self, cr, uid, ids, context=None):
201 @param self: The object pointer.
202 @param cr: A database cursor
203 @param uid: ID of the user currently logged in
204 @param ids: An ID or list of IDs if we want more than one
205 @param context: A standard dictionary
208 self.split(cr, uid, ids, context.get('active_ids'), context=context)
211 def split(self, cr, uid, ids, move_ids, context=None):
212 """ To split stock moves into production lot
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: the ID or list of IDs if we want more than one
217 @param move_ids: the ID or list of IDs of stock move we want to split
218 @param context: A standard dictionary
221 prodlot_obj = self.pool.get('stock.production.lot')
222 ir_sequence_obj = self.pool.get('ir.sequence')
223 move_obj = self.pool.get('stock.move')
225 for data in self.browse(cr, uid, ids):
226 for move in move_obj.browse(cr, uid, move_ids):
227 move_qty = move.product_qty
228 quantity_rest = move.product_qty
229 uos_qty_rest = move.product_uos_qty
232 lines = [l for l in data.line_exist_ids if l]
234 lines = [l for l in data.line_ids if l]
236 quantity = line.quantity
237 if quantity <= 0 or move_qty == 0:
239 quantity_rest -= quantity
240 uos_qty = quantity / move_qty * move.product_uos_qty
241 uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
242 if quantity_rest < 0:
243 quantity_rest = quantity
246 'product_qty': quantity,
247 'product_uos_qty': uos_qty,
250 if quantity_rest > 0:
251 current_move = move_obj.copy(cr, uid, move.id, default_val)
252 new_move.append(current_move)
253 if quantity_rest == 0:
254 current_move = move.id
257 prodlot_id = line.prodlot_id.id
259 prodlot_id = prodlot_obj.create(cr, uid, {
261 'product_id': move.product_id.id},
263 print 'write', current_move, {'prodlot_id': prodlot_id, 'state':move.state}
264 move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id, 'state':move.state})
267 if quantity_rest > 0:
268 update_val['product_qty'] = quantity_rest
269 update_val['product_uos_qty'] = uos_qty_rest
270 update_val['state'] = move.state
271 move_obj.write(cr, uid, [move.id], update_val)
273 split_in_production_lot()
275 class stock_move_split_lines_exist(osv.osv_memory):
276 _name = "stock.move.split.lines.exist"
277 _description = "Exist Split lines"
279 'name': fields.char('Tracking serial', size=64),
280 'quantity': fields.integer('Quantity'),
281 'lot_id': fields.many2one('stock.move.split', 'Lot'),
282 'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot'),
285 'quantity': lambda *x: 1,
288 stock_move_split_lines_exist()
290 class stock_move_split_lines(osv.osv_memory):
291 _name = "stock.move.split.lines"
292 _description = "Split lines"
294 'name': fields.char('Tracking serial', size=64),
295 'quantity': fields.integer('Quantity'),
296 'use_exist' : fields.boolean('Existing Lot'),
297 'lot_id': fields.many2one('stock.move.split', 'Lot'),
298 'action': fields.selection([('split','Split'),('keepinone','Keep in one lot')],'Action'),
301 'quantity': lambda *x: 1,
302 'action' : lambda *x: 'split',
304 stock_move_split_lines()