[FIX] Stock-MRP : Split production wizard made individual to mrp if mrp is not installed
[odoo/odoo.git] / addons / stock / wizard / wizard_track_line.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import wizard
24 import netsvc
25 import pooler
26
27 import time
28 from osv import osv
29 from tools.translate import _
30
31 track_form = '''<?xml version="1.0"?>
32 <form string="Tracking a move">
33  <field name="tracking_prefix"/>
34  <newline/>
35  <field name="quantity"/>
36 </form>
37 '''
38 fields = {
39         'tracking_prefix': {
40             'string': 'Tracking prefix',
41             'type': 'char',
42             'size': 64,
43         },
44         'quantity': {
45             'string': 'Quantity per lot',
46             'type': 'float',
47             'default': 1,
48         }
49 }
50
51 def _track_lines(self, cr, uid, data, context):
52     move_id = data['id']
53
54     pool = pooler.get_pool(cr.dbname)
55     prodlot_obj = pool.get('stock.production.lot')
56     move_obj = pool.get('stock.move')
57 #    production_obj = pool.get('mrp.production')
58     ir_sequence_obj = pool.get('ir.sequence')
59
60     sequence = ir_sequence_obj.get(cr, uid, 'stock.lot.serial')
61     if not sequence:
62         raise wizard.except_wizard(_('Error!'), _('No production sequence defined'))
63     if data['form']['tracking_prefix']:
64         sequence=data['form']['tracking_prefix']+'/'+(sequence or '')
65
66     move = move_obj.browse(cr, uid, [move_id])[0]
67     quantity=data['form']['quantity']
68     if quantity <= 0 or move.product_qty == 0:
69         return {}
70     uos_qty=quantity/move.product_qty*move.product_uos_qty
71
72     quantity_rest = move.product_qty%quantity
73     uos_qty_rest = quantity_rest/move.product_qty*move.product_uos_qty
74
75     update_val = {
76         'product_qty': quantity,
77         'product_uos_qty': uos_qty,
78     }
79     new_move = []
80 #    production_ids = []
81     for idx in range(int(move.product_qty//quantity)):
82         if idx:
83 #            current_move = move_obj.copy(cr, uid, move.id, {'state': move.state, 'production_id': move.production_id.id})
84             current_move = move_obj.copy(cr, uid, move.id, {'state': move.state})
85             new_move.append(current_move)
86         else:
87             current_move = move.id
88         new_prodlot = prodlot_obj.create(cr, uid, {'name': sequence, 'ref': '%d'%idx}, {'product_id': move.product_id.id})
89         update_val['prodlot_id'] = new_prodlot
90         move_obj.write(cr, uid, [current_move], update_val)
91 #        production_ids = production_obj.search(cr, uid, [('move_lines', 'in', [move.id])])
92     
93     if quantity_rest > 0:
94         idx = int(move.product_qty//quantity)
95         update_val['product_qty']=quantity_rest
96         update_val['product_uos_qty']=uos_qty_rest
97         if idx:
98 #            current_move = move_obj.copy(cr, uid, move.id, {'state': move.state, 'production_id': move.production_id.id})
99             current_move = move_obj.copy(cr, uid, move.id, {'state': move.state})
100             new_move.append(current_move)
101         else:
102             current_move = move.id
103         new_prodlot = prodlot_obj.create(cr, uid, {'name': sequence, 'ref': '%d'%idx}, {'product_id': move.product_id.id})
104         update_val['prodlot_id'] = new_prodlot
105         move_obj.write(cr, uid, [current_move], update_val)
106
107 #    products = production_obj.read(cr, uid, production_ids, ['move_lines'])
108 #    for p in products:
109 #        for new in new_move:
110 #            if new not in p['move_lines']:
111 #                p['move_lines'].append(new)
112 #        production_obj.write(cr, uid, [p['id']], {'move_lines': [(6, 0, p['move_lines'])]})
113
114     return {}
115
116 class wizard_track_move(wizard.interface):
117     states = {
118         'init': {
119             'actions': [],
120             'result': {'type': 'form', 'arch': track_form, 'fields': fields, 'state': [('end', 'Cancel', 'gtk-cancel'), ('track', 'Ok', 'gtk-ok')]},
121             },
122         'track': {
123             'actions': [_track_lines],
124             'result': {'type':'state', 'state':'end'}
125         }
126     }
127
128 wizard_track_move('stock.move.track')
129
130
131 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
132