Back ORder integrated in stock
[odoo/odoo.git] / addons / stock / wizard / wizard_partial_picking.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 import time
32 import netsvc
33 from tools.misc import UpdateableStr, UpdateableDict
34 import pooler
35
36 import wizard
37 from osv import osv
38
39 _moves_arch = UpdateableStr()
40 _moves_fields = UpdateableDict()
41
42 _moves_arch_end = '''<?xml version="1.0"?>
43 <form string="Packing result">
44     <label string="The packing has been successfully made !" colspan="4"/>
45 </form>'''
46 _moves_fields_end = {}
47
48 def make_default(val):
49     def fct(uid, data, state):
50         return val
51     return fct
52
53 def _to_xml(s):
54     return (s or '').replace('&','&amp;').replace('<','&lt;').replace('>','&gt;')
55
56 def _get_moves(self, cr, uid, data, context):
57     pick_obj = pooler.get_pool(cr.dbname).get('stock.picking')
58     pick = pick_obj.browse(cr, uid, [data['id']])[0]
59     res = {}
60
61     _moves_fields.clear()
62     _moves_arch_lst = ['<?xml version="1.0"?>', '<form string="Make packing">']
63
64     for m in pick.move_lines:
65         quantity = m.product_qty
66         if m.state<>'assigned':
67             quantity = 0
68
69         _moves_arch_lst.append('<field name="move%s" />' % (m.id,))
70         _moves_fields['move%s' % m.id] = {
71                 'string': '%s - %s' % (_to_xml(m.product_id.code or '/'), _to_xml(m.product_id.name)),
72                 'type' : 'float', 'required' : True, 'default' : make_default(quantity)}
73
74         if (pick.type == 'in') and (m.product_id.cost_method == 'average'):
75             price=0
76             if hasattr(m, 'purchase_line_id') and m.purchase_line_id:
77                 price=m.purchase_line_id.price_unit
78
79             currency=0
80             if hasattr(pick, 'purchase_id') and pick.purchase_id:
81                 currency=pick.purchase_id.pricelist_id.currency_id.id
82
83             _moves_arch_lst.append('<group col="6"><field name="uom%s" nolabel="1"/>\
84                     <field name="price%s"/>' % (m.id,m.id,))
85
86             _moves_fields['price%s' % m.id] = {'string': 'Unit Price',
87                     'type': 'float', 'required': True, 'default': make_default(price)}
88
89             _moves_fields['uom%s' % m.id] = {'string': 'UOM', 'type': 'many2one',
90                     'relation': 'product.uom', 'required': True,
91                     'default': make_default(m.product_uom.id)}
92
93             _moves_arch_lst.append('<field name="currency%d" nolabel="1"/></group>' % (m.id,))
94             _moves_fields['currency%s' % m.id] = {'string': 'Currency',
95                     'type': 'many2one', 'relation': 'res.currency',
96                     'required': True, 'default': make_default(currency)}
97
98         _moves_arch_lst.append('<newline/>')
99         res.setdefault('moves', []).append(m.id)
100
101     _moves_arch_lst.append('</form>')
102     _moves_arch.string = '\n'.join(_moves_arch_lst)
103     return res
104
105 def _do_split(self, cr, uid, data, context):
106     move_obj = pooler.get_pool(cr.dbname).get('stock.move')
107     pick_obj = pooler.get_pool(cr.dbname).get('stock.picking')
108     pick = pick_obj.browse(cr, uid, [data['id']])[0]
109     new_picking = None
110     new_moves = []
111
112     complete, too_many, too_few = [], [], []
113     for move in move_obj.browse(cr, uid, data['form'].get('moves',[])):
114         if move.product_qty == data['form']['move%s' % move.id]:
115             complete.append(move)
116         elif move.product_qty > data['form']['move%s' % move.id]:
117             too_few.append(move)
118         else:
119             too_many.append(move)
120
121         # Average price computation
122         if (pick.type == 'in') and (move.product_id.cost_method == 'average'):
123             product_obj = pooler.get_pool(cr.dbname).get('product.product')
124             currency_obj = pooler.get_pool(cr.dbname).get('res.currency')
125             users_obj = pooler.get_pool(cr.dbname).get('res.users')
126             uom_obj = pooler.get_pool(cr.dbname).get('product.uom')
127
128             product = product_obj.browse(cr, uid, [move.product_id.id])[0]
129             user = users_obj.browse(cr, uid, [uid])[0]
130
131             qty = data['form']['move%s' % move.id]
132             uom = data['form']['uom%s' % move.id]
133             price = data['form']['price%s' % move.id]
134             currency = data['form']['currency%s' % move.id]
135
136             qty = uom_obj._compute_qty(cr, uid, uom, qty, product.uom_id.id)
137
138             if qty > 0:
139                 new_price = currency_obj.compute(cr, uid, currency,
140                         user.company_id.currency_id.id, price)
141                 new_price = uom_obj._compute_price(cr, uid, uom, new_price,
142                         product.uom_id.id)
143                 new_std_price = ((product.standard_price * product.qty_available)\
144                         + (new_price * qty))/(product.qty_available + qty)
145
146                 product_obj.write(cr, uid, [product.id],
147                         {'standard_price': new_std_price})
148                 move_obj.write(cr, uid, [move.id], {'price_unit': new_price})
149
150     for move in too_few:
151         if not new_picking:
152             new_picking = pick_obj.copy(cr, uid, pick.id,
153                     {
154                         'name' : '%s (splitted)' % pick.name,
155                         'move_lines' : [],
156                         'state':'draft',
157                         'back_order_id': pick.id
158                     })
159         new_obj = move_obj.copy(cr, uid, move.id,
160                 {
161                     'product_qty' : data['form']['move%s' % move.id],
162                     'product_uos_qty':data['form']['move%s' % move.id],
163                     'picking_id' : new_picking,
164                     'state': 'assigned',
165                     'move_dest_id': False,
166                     'price_unit': move.price_unit,
167                 })
168         move_obj.write(cr, uid, [move.id],
169                 {
170                     'product_qty' : move.product_qty - data['form']['move%s' % move.id],
171                     'product_uos_qty':move.product_qty - data['form']['move%s' % move.id],
172                 })
173
174     if new_picking:
175         move_obj.write(cr, uid, [c.id for c in complete], {'picking_id': new_picking})
176         for move in too_many:
177             move_obj.write(cr, uid, [move.id],
178                     {
179                         'product_qty' : data['form']['move%s' % move.id],
180                         'product_uos_qty': data['form']['move%s' % move.id],
181                         'picking_id': new_picking,
182                     })
183     else:
184         for move in too_many:
185             move_obj.write(cr, uid, [move.id],
186                     {
187                         'product_qty': data['form']['move%s' % move.id],
188                         'product_uos_qty': data['form']['move%s' % move.id]
189                     })
190
191     # At first we confirm the new picking (if necessary)
192     wf_service = netsvc.LocalService("workflow")
193     if new_picking:
194         wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr)
195     # Then we finish the good picking
196     if new_picking:
197         pick_obj.action_move(cr, uid, [new_picking])
198         wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_done', cr)
199         wf_service.trg_write(uid, 'stock.picking', pick.id, cr)
200     else:
201         pick_obj.action_move(cr, uid, [pick.id])
202         wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_done', cr)
203     return {'new_picking':new_picking or False}
204
205
206 class partial_picking(wizard.interface):
207
208     states = {
209         'init': {
210             'actions': [ _get_moves ],
211             'result': {'type': 'form', 'arch': _moves_arch, 'fields': _moves_fields,
212                 'state' : (
213                     ('end', 'Cancel'),
214                     ('split', 'Make Picking')
215                 )
216             },
217         },
218         'split': {
219             'actions': [ _do_split ],
220             'result': {'type': 'state', 'state': 'end'},
221         },
222         'end2': {
223             'actions': [ ],
224             'result': {'type': 'form', 'arch': _moves_arch_end,
225                 'fields': _moves_fields_end,
226                 'state': (
227                     ('end', 'Close'),
228                 )
229             },
230         },
231     }
232
233 partial_picking('stock.partial_picking')
234
235
236 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
237