[MERGE] Sync with trunk, until revision 8927
[odoo/odoo.git] / addons / stock / wizard / stock_inventory_line_split.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23
24 class stock_inventory_line_split(osv.osv_memory):
25     _inherit = "stock.move.split"
26     _name = "stock.inventory.line.split"
27     _description = "Split inventory lines"
28
29     _columns = {
30         'line_ids': fields.one2many('stock.inventory.line.split.lines', 'wizard_id', 'Serial Numbers'),
31         'line_exist_ids': fields.one2many('stock.inventory.line.split.lines', 'wizard_exist_id', 'Serial Numbers'),
32      }
33
34     def default_get(self, cr, uid, fields, context=None):
35         if context is None:
36             context = {}
37         record_id = context and context.get('active_id',False)
38         res = {}
39         line = self.pool.get('stock.inventory.line').browse(cr, uid, record_id, context=context)
40         if 'product_id' in fields:
41             res.update({'product_id':line.product_id.id})
42         if 'product_uom' in fields:
43             res.update({'product_uom': line.product_uom.id})
44         if 'qty' in fields:
45             res.update({'qty': line.product_qty})
46         return res
47
48     def split(self, cr, uid, ids, line_ids, context=None):
49         """ To split stock inventory lines according to serial numbers.
50
51         :param line_ids: the ID or list of IDs of inventory lines we want to split
52         """
53         if context is None:
54             context = {}
55         assert context.get('active_model') == 'stock.inventory.line',\
56              'Incorrect use of the inventory line split wizard.'
57         prodlot_obj = self.pool.get('stock.production.lot')
58         ir_sequence_obj = self.pool.get('ir.sequence')
59         line_obj = self.pool.get('stock.inventory.line')
60         new_line = []
61         for data in self.browse(cr, uid, ids, context=context):
62             for inv_line in line_obj.browse(cr, uid, line_ids, context=context):
63                 line_qty = inv_line.product_qty
64                 quantity_rest = inv_line.product_qty
65                 new_line = []
66                 if data.use_exist:
67                     lines = [l for l in data.line_exist_ids if l]
68                 else:
69                     lines = [l for l in data.line_ids if l]
70                 for line in lines:
71                     quantity = line.quantity
72                     if quantity <= 0 or line_qty == 0:
73                         continue
74                     quantity_rest -= quantity
75                     if quantity_rest < 0:
76                         quantity_rest = quantity
77                         break
78                     default_val = {
79                         'product_qty': quantity,
80                     }
81                     if quantity_rest > 0:
82                         current_line = line_obj.copy(cr, uid, inv_line.id, default_val)
83                         new_line.append(current_line)
84                     if quantity_rest == 0:
85                         current_line = inv_line.id
86                     prodlot_id = False
87                     if data.use_exist:
88                         prodlot_id = line.prodlot_id.id
89                     if not prodlot_id:
90                         prodlot_id = prodlot_obj.create(cr, uid, {
91                             'name': line.name,
92                             'product_id': inv_line.product_id.id},
93                         context=context)
94                     line_obj.write(cr, uid, [current_line], {'prod_lot_id': prodlot_id})
95                     prodlot = prodlot_obj.browse(cr, uid, prodlot_id)
96
97                     update_val = {}
98                     if quantity_rest > 0:
99                         update_val['product_qty'] = quantity_rest
100                         line_obj.write(cr, uid, [inv_line.id], update_val)
101
102         return new_line
103
104 class stock_inventory_split_lines(osv.osv_memory):
105     _inherit = "stock.move.split.lines"
106     _name = "stock.inventory.line.split.lines"
107     _description = "Inventory Split lines"
108     _columns = {
109         'wizard_id': fields.many2one('stock.inventory.line.split', 'Parent Wizard'),
110         'wizard_exist_id': fields.many2one('stock.inventory.line.split', 'Parent Wizard'),
111     }
112