[IMP]stock:put attrs in tree view
[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 osv import fields, osv
23
24 from tools.translate import _
25 import time
26
27 class stock_inventory_line_split(osv.osv_memory):
28     _inherit = "stock.move.split"
29     _name = "stock.inventory.line.split"
30     _description = "Split inventory lines"
31
32
33     def default_get(self, cr, uid, fields, context=None):
34         """ To check the availability of production lot.
35         @param self: The object pointer.
36         @param cr: A database cursor
37         @param uid: ID of the user currently logged in
38         @param fields: List of fields for which we want default values
39         @param context: A standard dictionary
40         @return: A dictionary which of fields with values.
41         """
42         if context is None:
43             context = {}
44         record_id = context and context.get('active_id',False)
45         res = {}
46         line = self.pool.get('stock.inventory.line').browse(cr, uid, record_id, context=context)
47         if 'product_id' in fields:
48             res.update({'product_id':line.product_id.id})
49         if 'product_uom' in fields:
50             res.update({'product_uom': line.product_uom.id})
51         if 'qty' in fields:
52             res.update({'qty': line.product_qty})
53         return res
54
55     def split(self, cr, uid, ids, line_ids, context=None):
56         """ To split stock inventory lines according to production lot.
57         @param self: The object pointer.
58         @param cr: A database cursor
59         @param uid: ID of the user currently logged in
60         @param ids: the ID or list of IDs if we want more than one
61         @param line_ids: the ID or list of IDs of inventory lines we want to split
62         @param context: A standard dictionary
63         @return:
64         """
65         prodlot_obj = self.pool.get('stock.production.lot')
66         ir_sequence_obj = self.pool.get('ir.sequence')
67         line_obj = self.pool.get('stock.inventory.line')
68         new_line = []
69         for data in self.browse(cr, uid, ids, context=context):
70             for inv_line in line_obj.browse(cr, uid, line_ids, context=context):
71                 line_qty = inv_line.product_qty
72                 quantity_rest = inv_line.product_qty
73                 new_line = []
74                 if data.use_exist:
75                     lines = [l for l in data.line_exist_ids if l]
76                 else:
77                     lines = [l for l in data.line_ids if l]
78                 for line in lines:
79                     quantity = line.quantity
80                     if quantity <= 0 or line_qty == 0:
81                         continue
82                     quantity_rest -= quantity
83                     if quantity_rest < 0:
84                         quantity_rest = quantity
85                         break
86                     default_val = {
87                         'product_qty': quantity,
88                     }
89                     if quantity_rest > 0:
90                         current_line = line_obj.copy(cr, uid, inv_line.id, default_val)
91                         new_line.append(current_line)
92                     if quantity_rest == 0:
93                         current_line = inv_line.id
94                     prodlot_id = False
95                     if data.use_exist:
96                         prodlot_id = line.prodlot_id.id
97                     if not prodlot_id:
98                         prodlot_id = prodlot_obj.create(cr, uid, {
99                             'name': line.name,
100                             'product_id': inv_line.product_id.id},
101                         context=context)
102                     line_obj.write(cr, uid, [current_line], {'prod_lot_id': prodlot_id})
103                     prodlot = prodlot_obj.browse(cr, uid, prodlot_id)
104
105                     update_val = {}
106                     if quantity_rest > 0:
107                         update_val['product_qty'] = quantity_rest
108                         line_obj.write(cr, uid, [inv_line.id], update_val)
109
110         return new_line
111 stock_inventory_line_split()
112
113 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
114