1 ##############################################################################
3 # Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
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
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.
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.
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.
28 ##############################################################################
32 from tools.misc import UpdateableStr
38 _moves_arch = UpdateableStr()
41 _moves_arch_end = '''<?xml version="1.0"?>
42 <form string="Packing result">
43 <label string="The packing has been successfully made !" colspan="4"/>
45 _moves_fields_end = {}
47 def make_default(val):
48 def fct(uid, data, state):
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]
58 _moves_arch_lst = ['<?xml version="1.0"?>', '<form string="Make packing">']
60 for m in pick.move_lines:
61 quantity = m.product_qty
62 if m.state<>'assigned':
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)}
70 if (pick.type == 'in') and (m.product_id.cost_method == 'average'):
72 if hasattr(m, 'purchase_line_id') and m.purchase_line_id:
73 price=m.purchase_line_id.price_unit
76 if hasattr(pick, 'purchase_id') and pick.purchase_id:
77 currency=pick.purchase_id.pricelist_id.currency_id.id
79 _moves_arch_lst.append('<group col="6"><field name="uom%s"/>\
80 <field name="price%s"/>' % (m.id,m.id,))
82 _moves_fields['price%s' % m.id] = {'string': 'Unit Price',
83 'type': 'float', 'required': True, 'default': make_default(price)}
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)}
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)}
94 _moves_arch_lst.append('<newline/>')
95 res.setdefault('moves', []).append(m.id)
97 _moves_arch_lst.append('</form>')
98 _moves_arch.string = '\n'.join(_moves_arch_lst)
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]
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]:
115 too_many.append(move)
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')
124 product = product_obj.browse(cr, uid, [move.product_id.id])[0]
125 user = users_obj.browse(cr, uid, [uid])[0]
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]
132 qty = uom_obj._compute_qty(cr, uid, uom, qty, product.uom_id.id)
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)
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})
146 new_picking = pick_obj.copy(cr, uid, pick.id,
148 'name' : '%s (splitted)' % pick.name,
152 new_obj = move_obj.copy(cr, uid, move.id,
154 'product_qty' : data['form']['move%s' % move.id],
155 'product_uos_qty':data['form']['move%s' % move.id],
156 'picking_id' : new_picking,
158 'move_dest_id': False,
161 move_obj.write(cr, uid, [move.id],
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],
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],
172 'product_qty' : data['form']['move%s' % move.id],
173 'product_uos_qty': data['form']['move%s' % move.id],
174 'picking_id': new_picking,
177 for move in too_many:
178 move_obj.write(cr, uid, [move.id],
180 'product_qty': data['form']['move%s' % move.id],
181 'product_uos_qty': data['form']['move%s' % move.id]
184 # At first we confirm the new picking (if necessary)
185 wf_service = netsvc.LocalService("workflow")
187 wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr)
188 # Then we finish the good 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)
194 pick_obj.action_move(cr, uid, [pick.id])
195 wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_done', cr)
199 class partial_picking(wizard.interface):
203 'actions': [ _get_moves ],
204 'result': {'type': 'form', 'arch': _moves_arch, 'fields': _moves_fields,
207 ('split', 'Make Picking')
212 'actions': [ _do_split ],
213 'result': {'type': 'state', 'state': 'end'},
217 'result': {'type': 'form', 'arch': _moves_arch_end,
218 'fields': _moves_fields_end,
226 partial_picking('stock.partial_picking')