1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
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
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.
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.
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.
29 ##############################################################################
33 from tools.misc import UpdateableStr, UpdateableDict
39 _moves_arch = UpdateableStr()
40 _moves_fields = UpdateableDict()
42 _moves_arch_end = '''<?xml version="1.0"?>
43 <form string="Packing result">
44 <label string="The packing has been successfully made !" colspan="4"/>
46 _moves_fields_end = {}
48 def make_default(val):
49 def fct(uid, data, state):
54 return (s or '').replace('&','&').replace('<','<').replace('>','>')
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]
62 _moves_arch_lst = ['<?xml version="1.0"?>', '<form string="Make packing">']
64 for m in pick.move_lines:
65 quantity = m.product_qty
66 if m.state<>'assigned':
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)}
74 if (pick.type == 'in') and (m.product_id.cost_method == 'average'):
76 if hasattr(m, 'purchase_line_id') and m.purchase_line_id:
77 price=m.purchase_line_id.price_unit
80 if hasattr(pick, 'purchase_id') and pick.purchase_id:
81 currency=pick.purchase_id.pricelist_id.currency_id.id
83 _moves_arch_lst.append('<group col="6"><field name="uom%s" nolabel="1"/>\
84 <field name="price%s"/>' % (m.id,m.id,))
86 _moves_fields['price%s' % m.id] = {'string': 'Unit Price',
87 'type': 'float', 'required': True, 'default': make_default(price)}
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)}
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)}
98 _moves_arch_lst.append('<newline/>')
99 res.setdefault('moves', []).append(m.id)
101 _moves_arch_lst.append('</form>')
102 _moves_arch.string = '\n'.join(_moves_arch_lst)
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]
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]:
119 too_many.append(move)
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')
128 product = product_obj.browse(cr, uid, [move.product_id.id])[0]
129 user = users_obj.browse(cr, uid, [uid])[0]
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]
136 qty = uom_obj._compute_qty(cr, uid, uom, qty, product.uom_id.id)
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,
143 new_std_price = ((product.standard_price * product.qty_available)\
144 + (new_price * qty))/(product.qty_available + qty)
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})
152 new_picking = pick_obj.copy(cr, uid, pick.id,
154 'name' : '%s (splitted)' % pick.name,
157 'back_order_id': pick.id
159 new_obj = move_obj.copy(cr, uid, move.id,
161 'product_qty' : data['form']['move%s' % move.id],
162 'product_uos_qty':data['form']['move%s' % move.id],
163 'picking_id' : new_picking,
165 'move_dest_id': False,
166 'price_unit': move.price_unit,
168 move_obj.write(cr, uid, [move.id],
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],
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],
179 'product_qty' : data['form']['move%s' % move.id],
180 'product_uos_qty': data['form']['move%s' % move.id],
181 'picking_id': new_picking,
184 for move in too_many:
185 move_obj.write(cr, uid, [move.id],
187 'product_qty': data['form']['move%s' % move.id],
188 'product_uos_qty': data['form']['move%s' % move.id]
191 # At first we confirm the new picking (if necessary)
192 wf_service = netsvc.LocalService("workflow")
194 wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr)
195 # Then we finish the good 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)
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}
206 class partial_picking(wizard.interface):
210 'actions': [ _get_moves ],
211 'result': {'type': 'form', 'arch': _moves_arch, 'fields': _moves_fields,
214 ('split', 'Make Picking')
219 'actions': [ _do_split ],
220 'result': {'type': 'state', 'state': 'end'},
224 'result': {'type': 'form', 'arch': _moves_arch_end,
225 'fields': _moves_fields_end,
233 partial_picking('stock.partial_picking')
236 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: