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 #TOFIX: product_uom should not have differemt category of default UOM of product. Qty should be convert into UOM of original move line before going in consume and scrap
38 def default_get(self, cr, uid, fields, context=None):
39 """ Get default values
40 @param self: The object pointer.
41 @param cr: A database cursor
42 @param uid: ID of the user currently logged in
43 @param fields: List of fields for default value
44 @param context: A standard dictionary
45 @return: default values of fields
49 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
50 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
51 if 'product_id' in fields:
52 res.update({'product_id': move.product_id.id})
53 if 'product_uom' in fields:
54 res.update({'product_uom': move.product_uom.id})
55 if 'product_qty' in fields:
56 res.update({'product_qty': move.product_qty})
57 if 'location_id' in fields:
58 res.update({'location_id': move.location_id.id})
62 def do_move_consume(self, cr, uid, ids, context=None):
63 """ To move consumed products
64 @param self: The object pointer.
65 @param cr: A database cursor
66 @param uid: ID of the user currently logged in
67 @param ids: the ID or list of IDs if we want more than one
68 @param context: A standard dictionary
73 move_obj = self.pool.get('stock.move')
74 move_ids = context['active_ids']
75 for data in self.browse(cr, uid, ids, context=context):
76 move_obj.action_consume(cr, uid, move_ids,
77 data.product_qty, data.location_id.id,
79 return {'type': 'ir.actions.act_window_close'}
84 class stock_move_scrap(osv.osv_memory):
85 _name = "stock.move.scrap"
86 _description = "Scrap Products"
87 _inherit = "stock.move.consume"
90 'location_id': lambda *x: False
93 def default_get(self, cr, uid, fields, context=None):
94 """ Get default values
95 @param self: The object pointer.
96 @param cr: A database cursor
97 @param uid: ID of the user currently logged in
98 @param fields: List of fields for default value
99 @param context: A standard dictionary
100 @return: default values of fields
104 res = super(stock_move_consume, self).default_get(cr, uid, fields, context=context)
105 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
106 location_obj = self.pool.get('stock.location')
107 scrpaed_location_ids = location_obj.search(cr, uid, [('scrap_location','=',True)])
109 if 'product_id' in fields:
110 res.update({'product_id': move.product_id.id})
111 if 'product_uom' in fields:
112 res.update({'product_uom': move.product_uom.id})
113 if 'product_qty' in fields:
114 res.update({'product_qty': move.product_qty})
115 if 'location_id' in fields:
116 if scrpaed_location_ids:
117 res.update({'location_id': scrpaed_location_ids[0]})
119 res.update({'location_id': False})
123 def move_scrap(self, cr, uid, ids, context=None):
124 """ To move scrapped products
125 @param self: The object pointer.
126 @param cr: A database cursor
127 @param uid: ID of the user currently logged in
128 @param ids: the ID or list of IDs if we want more than one
129 @param context: A standard dictionary
134 move_obj = self.pool.get('stock.move')
135 move_ids = context['active_ids']
136 for data in self.browse(cr, uid, ids):
137 move_obj.action_scrap(cr, uid, move_ids,
138 data.product_qty, data.location_id.id,
140 return {'type': 'ir.actions.act_window_close'}
145 class split_in_production_lot(osv.osv_memory):
146 _name = "stock.move.split"
147 _description = "Split in Production lots"
149 def default_get(self, cr, uid, fields, context=None):
152 res = super(split_in_production_lot, self).default_get(cr, uid, fields, context=context)
153 if context.get('active_id'):
154 move = self.pool.get('stock.move').browse(cr, uid, context['active_id'], context=context)
155 if 'product_id' in fields:
156 res.update({'product_id': move.product_id.id})
157 if 'product_uom' in fields:
158 res.update({'product_uom': move.product_uom.id})
160 res.update({'qty': move.product_qty})
161 if 'use_exist' in fields:
162 res.update({'use_exist': (move.picking_id and move.picking_id.type=='out' and True) or False})
163 if 'location_id' in fields:
164 res.update({'location_id': move.location_id.id})
168 'qty': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
169 'product_id': fields.many2one('product.product', 'Product', required=True, select=True),
170 'product_uom': fields.many2one('product.uom', 'UoM'),
171 'line_ids': fields.one2many('stock.move.split.lines', 'wizard_id', 'Production Lots'),
172 'line_exist_ids': fields.one2many('stock.move.split.lines', 'wizard_exist_id', 'Production Lots'),
173 '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."),
174 'location_id': fields.many2one('stock.location', 'Source Location')
177 def split_lot(self, cr, uid, ids, context=None):
178 """ To split a lot"""
181 res = self.split(cr, uid, ids, context.get('active_ids'), context=context)
182 return {'type': 'ir.actions.act_window_close'}
184 def split(self, cr, uid, ids, move_ids, context=None):
185 """ To split stock moves into production lot
187 :param move_ids: the ID or list of IDs of stock move we want to split
191 assert context.get('active_model') == 'stock.move',\
192 'Incorrect use of the stock move split wizard'
193 inventory_id = context.get('inventory_id', False)
194 prodlot_obj = self.pool.get('stock.production.lot')
195 inventory_obj = self.pool.get('stock.inventory')
196 move_obj = self.pool.get('stock.move')
198 for data in self.browse(cr, uid, ids, context=context):
199 for move in move_obj.browse(cr, uid, move_ids, context=context):
200 move_qty = move.product_qty
201 quantity_rest = move.product_qty
202 uos_qty_rest = move.product_uos_qty
205 lines = [l for l in data.line_exist_ids if l]
207 lines = [l for l in data.line_ids if l]
210 quantity = line.quantity
211 total_move_qty += quantity
212 if total_move_qty > move_qty:
213 raise osv.except_osv(_('Processing Error'), _('Processing quantity %d for %s is larger than the available quantity %d!')\
214 %(total_move_qty, move.product_id.name, move_qty))
215 if quantity <= 0 or move_qty == 0:
217 quantity_rest -= quantity
218 uos_qty = quantity / move_qty * move.product_uos_qty
219 uos_qty_rest = quantity_rest / move_qty * move.product_uos_qty
220 if quantity_rest < 0:
221 quantity_rest = quantity
222 self.pool.get('stock.move').log(cr, uid, move.id, _('Unable to assign all lots to this move!'))
225 'product_qty': quantity,
226 'product_uos_qty': uos_qty,
229 if quantity_rest > 0:
230 current_move = move_obj.copy(cr, uid, move.id, default_val, context=context)
231 if inventory_id and current_move:
232 inventory_obj.write(cr, uid, inventory_id, {'move_ids': [(4, current_move)]}, context=context)
233 new_move.append(current_move)
235 if quantity_rest == 0:
236 current_move = move.id
239 prodlot_id = line.prodlot_id.id
241 prodlot_id = prodlot_obj.create(cr, uid, {
243 'product_id': move.product_id.id},
246 move_obj.write(cr, uid, [current_move], {'prodlot_id': prodlot_id, 'state':move.state})
249 if quantity_rest > 0:
250 update_val['product_qty'] = quantity_rest
251 update_val['product_uos_qty'] = uos_qty_rest
252 update_val['state'] = move.state
253 move_obj.write(cr, uid, [move.id], update_val)
257 split_in_production_lot()
259 class stock_move_split_lines_exist(osv.osv_memory):
260 _name = "stock.move.split.lines"
261 _description = "Stock move Split lines"
263 'name': fields.char('Production Lot', size=64),
264 'quantity': fields.float('Quantity', digits_compute=dp.get_precision('Product UoM')),
265 'wizard_id': fields.many2one('stock.move.split', 'Parent Wizard'),
266 'wizard_exist_id': fields.many2one('stock.move.split', 'Parent Wizard (for existing lines)'),
267 'prodlot_id': fields.many2one('stock.production.lot', 'Production Lot'),
273 def onchange_lot_id(self, cr, uid, ids, prodlot_id=False, product_qty=False,
274 loc_id=False, product_id=False, uom_id=False):
275 return self.pool.get('stock.move').onchange_lot_id(cr, uid, [], prodlot_id, product_qty,
276 loc_id, product_id, uom_id)
278 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: