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